mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-19 21:45:39 +00:00
Ability to toggle part category "star" status via the API
This commit is contained in:
@ -177,6 +177,17 @@ class CategoryDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
|
||||
return ctx
|
||||
|
||||
def update(self, request, *args, **kwargs):
|
||||
|
||||
if 'starred' in request.data:
|
||||
starred = str2bool(request.data.get('starred', False))
|
||||
|
||||
self.get_object().set_starred(request.user, starred)
|
||||
|
||||
response = super().update(request, *args, **kwargs)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
class CategoryParameterList(generics.ListAPIView):
|
||||
""" API endpoint for accessing a list of PartCategoryParameterTemplate objects.
|
||||
@ -446,7 +457,7 @@ class PartDetail(generics.RetrieveUpdateDestroyAPIView):
|
||||
"""
|
||||
|
||||
if 'starred' in request.data:
|
||||
starred = str2bool(request.data.get('starred', None))
|
||||
starred = str2bool(request.data.get('starred', False))
|
||||
|
||||
self.get_object().set_starred(request.user, starred)
|
||||
|
||||
|
@ -35,8 +35,6 @@ class CategorySerializer(InvenTreeModelSerializer):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
||||
self.starred_categories = kwargs.pop('starred_categories', [])
|
||||
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def get_starred(self, category):
|
||||
@ -44,7 +42,7 @@ class CategorySerializer(InvenTreeModelSerializer):
|
||||
Return True if the category is directly "starred" by the current user
|
||||
"""
|
||||
|
||||
return category in self.starred_categories
|
||||
return category in self.context.get('starred_categories', [])
|
||||
|
||||
url = serializers.CharField(source='get_absolute_url', read_only=True)
|
||||
|
||||
|
@ -20,15 +20,37 @@
|
||||
{% include "admin_button.html" with url=url %}
|
||||
{% endif %}
|
||||
{% if category %}
|
||||
{% if roles.part_category.change %}
|
||||
<button class='btn btn-outline-secondary' id='cat-edit' title='{% trans "Edit part category" %}'>
|
||||
<span class='fas fa-edit'/>
|
||||
{% if starred_directly %}
|
||||
<button type='button' class='btn btn-outline-secondary' id='toggle-starred' title='{% trans "You are subscribed to notifications for this category" %}'>
|
||||
<span id='category-star-icon' class='fas fa-bell icon-green'></span>
|
||||
</button>
|
||||
{% elif starred %}
|
||||
<button type='button' class='btn btn-outline-secondary' title='{% trans "You are subscribed to notifications for this category" %}' disabled='true'>
|
||||
<span class='fas fa-bell icon-green'></span>
|
||||
</button>
|
||||
{% else %}
|
||||
<button type='button' class='btn btn-outline-secondary' id='toggle-starred' title='{% trans "Subscribe to nofications for this category" %}'>
|
||||
<span id='category-star-icon' class='fa fa-bell-slash'/>
|
||||
</button>
|
||||
{% endif %}
|
||||
{% if roles.part_category.delete %}
|
||||
<button class='btn btn-outline-secondary' id='cat-delete' title='{% trans "Delete part category" %}'>
|
||||
<span class='fas fa-trash-alt icon-red'/>
|
||||
</button>
|
||||
{% if roles.part_category.change or roles.part_category.delete %}
|
||||
<div class='btn-group' role='group'>
|
||||
<button id='category-options' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown' title='{% trans "Category Actions" %}'>
|
||||
<span class='fas fa-tools'></span> <span class='caret'></span>
|
||||
</button>
|
||||
<ul class='dropdown-menu'>
|
||||
{% if roles.part_category.change %}
|
||||
<li><a class='dropdown-item' href='#' id='cat-edit' title='{% trans "Edit category" %}'>
|
||||
<span class='fas fa-edit icon-green'></span> {% trans "Edit Category" %}
|
||||
</a></li>
|
||||
{% endif %}
|
||||
{% if roles.part_category.delete %}
|
||||
<li><a class='dropdown-item' href='#' id='cat-delete' title='{% trans "Delete category" %}'>
|
||||
<span class='fas fa-trash-alt icon-red'></span> {% trans "Delete Category" %}
|
||||
</a></li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{% if roles.part_category.add %}
|
||||
@ -198,6 +220,14 @@
|
||||
data: {{ parameters|safe }},
|
||||
}
|
||||
);
|
||||
|
||||
$("#toggle-starred").click(function() {
|
||||
toggleStar({
|
||||
url: '{% url "api-part-category-detail" category.pk %}',
|
||||
button: '#category-star-icon'
|
||||
});
|
||||
});
|
||||
|
||||
{% endif %}
|
||||
|
||||
enableSidebar('category');
|
||||
|
@ -320,7 +320,7 @@
|
||||
|
||||
$("#toggle-starred").click(function() {
|
||||
toggleStar({
|
||||
part: {{ part.id }},
|
||||
url: '{% url "api-part-detail" part.pk %}',
|
||||
button: '#part-star-icon',
|
||||
});
|
||||
});
|
||||
|
@ -1470,18 +1470,29 @@ class CategoryDetail(InvenTreeRoleMixin, DetailView):
|
||||
|
||||
if category:
|
||||
cascade = kwargs.get('cascade', True)
|
||||
|
||||
# Prefetch parts parameters
|
||||
parts_parameters = category.prefetch_parts_parameters(cascade=cascade)
|
||||
|
||||
# Get table headers (unique parameters names)
|
||||
context['headers'] = category.get_unique_parameters(cascade=cascade,
|
||||
prefetch=parts_parameters)
|
||||
|
||||
# Insert part information
|
||||
context['headers'].insert(0, 'description')
|
||||
context['headers'].insert(0, 'part')
|
||||
|
||||
# Get parameters data
|
||||
context['parameters'] = category.get_parts_parameters(cascade=cascade,
|
||||
prefetch=parts_parameters)
|
||||
|
||||
# Insert "starred" information
|
||||
context['starred'] = category.is_starred_by(self.request.user)
|
||||
context['starred_directly'] = context['starred'] and category.is_starred_by(
|
||||
self.request.user,
|
||||
include_parents=False,
|
||||
)
|
||||
|
||||
return context
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user