mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-20 22:06:28 +00:00
Simplify process for marking a part as "starred"
This commit is contained in:
@ -328,6 +328,22 @@ class PartDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
message = f'Part \'{part.name}\' (pk = {part.pk}) is active: cannot delete'
|
||||
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):
|
||||
""" API endpoint for accessing a list of Part objects
|
||||
|
@ -1056,6 +1056,23 @@ class Part(MPTTModel):
|
||||
except PartStar.DoesNotExist:
|
||||
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):
|
||||
""" Return True if this part needs to be restocked
|
||||
(either by purchasing or building).
|
||||
|
@ -248,8 +248,7 @@
|
||||
$("#toggle-starred").click(function() {
|
||||
toggleStar({
|
||||
part: {{ part.id }},
|
||||
user: {{ user.id }},
|
||||
button: '#part-star-icon'
|
||||
button: '#part-star-icon',
|
||||
});
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user