Merge branch 'uses-fix'

This commit is contained in:
Oliver Walters 2021-02-26 11:08:58 +11:00
commit f23fe07052
5 changed files with 59 additions and 44 deletions

View File

@ -328,6 +328,22 @@ class PartDetail(generics.RetrieveUpdateDestroyAPIView):
message = f'Part \'{part.name}\' (pk = {part.pk}) is active: cannot delete' message = f'Part \'{part.name}\' (pk = {part.pk}) is active: cannot delete'
return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED, data=message) return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED, data=message)
def update(self, request, *args, **kwargs):
"""
Custom update functionality for Part instance.
- If the 'starred' field is provided, update the 'starred' status against current user
"""
if 'starred' in request.data:
starred = str2bool(request.data.get('starred', None))
self.get_object().setStarred(request.user, starred)
response = super().update(request, *args, **kwargs)
return response
class PartList(generics.ListCreateAPIView): class PartList(generics.ListCreateAPIView):
""" API endpoint for accessing a list of Part objects """ API endpoint for accessing a list of Part objects

View File

@ -1056,6 +1056,23 @@ class Part(MPTTModel):
except PartStar.DoesNotExist: except PartStar.DoesNotExist:
return False return False
def setStarred(self, user, starred):
"""
Set the "starred" status of this Part for the given user
"""
if not user:
return
# Do not duplicate efforts
if self.isStarredBy(user) == starred:
return
if starred:
PartStar.objects.create(part=self, user=user)
else:
PartStar.objects.filter(part=self, user=user).delete()
def need_to_restock(self): def need_to_restock(self):
""" Return True if this part needs to be restocked """ Return True if this part needs to be restocked
(either by purchasing or building). (either by purchasing or building).

View File

@ -248,8 +248,7 @@
$("#toggle-starred").click(function() { $("#toggle-starred").click(function() {
toggleStar({ toggleStar({
part: {{ part.id }}, part: {{ part.id }},
user: {{ user.id }}, button: '#part-star-icon',
button: '#part-star-icon'
}); });
}); });

View File

@ -50,8 +50,11 @@
<link rel="stylesheet" href="{% get_color_theme_css user.get_username %}"> <link rel="stylesheet" href="{% get_color_theme_css user.get_username %}">
{% block css %} <style>
{% endblock %} {% block css %}
<!-- Custom CSS style goes here -->
{% endblock %}
</style>
{% block head %} {% block head %}
{% endblock %} {% endblock %}

View File

@ -14,50 +14,30 @@ function toggleStar(options) {
* - user: pk of the user * - user: pk of the user
*/ */
var url = '/api/part/star/'; var url = `/api/part/${options.part}/`;
inventreeGet( inventreeGet(url, {}, {
url, success: function(response) {
{ var starred = response.starred;
part: options.part,
user: options.user, inventreePut(
}, url,
{ {
success: function(response) { starred: !starred,
if (response.length == 0) { },
// Zero length response = star does not exist {
// So let's add one! method: 'PATCH',
inventreePut( success: function(response) {
url, if (response.starred) {
{ $(options.button).addClass('icon-yellow');
part: options.part, } else {
user: options.user, $(options.button).removeClass('icon-yellow');
},
{
method: 'POST',
success: function(response, status) {
$(options.button).addClass('icon-yellow');
},
} }
); }
} else {
var pk = response[0].pk;
// There IS a star (delete it!)
inventreePut(
url + pk + "/",
{
},
{
method: 'DELETE',
success: function(response, status) {
$(options.button).removeClass('icon-yellow');
},
}
);
} }
}, );
} }
); });
} }