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'
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

View File

@ -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).

View File

@ -248,8 +248,7 @@
$("#toggle-starred").click(function() {
toggleStar({
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 %}">
{% block css %}
{% endblock %}
<style>
{% block css %}
<!-- Custom CSS style goes here -->
{% endblock %}
</style>
{% block head %}
{% endblock %}

View File

@ -14,50 +14,30 @@ function toggleStar(options) {
* - user: pk of the user
*/
var url = '/api/part/star/';
var url = `/api/part/${options.part}/`;
inventreeGet(
url,
{
part: options.part,
user: options.user,
},
{
success: function(response) {
if (response.length == 0) {
// Zero length response = star does not exist
// So let's add one!
inventreePut(
url,
{
part: options.part,
user: options.user,
},
{
method: 'POST',
success: function(response, status) {
$(options.button).addClass('icon-yellow');
},
inventreeGet(url, {}, {
success: function(response) {
var starred = response.starred;
inventreePut(
url,
{
starred: !starred,
},
{
method: 'PATCH',
success: function(response) {
if (response.starred) {
$(options.button).addClass('icon-yellow');
} else {
$(options.button).removeClass('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');
},
}
);
}
}
},
);
}
);
});
}