mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-12 01:55:39 +00:00
Ability to toggle part category "star" status via the API
This commit is contained in:
@ -247,7 +247,9 @@
|
|||||||
<span class='fas fa-tools'></span> <span class='caret'></span>
|
<span class='fas fa-tools'></span> <span class='caret'></span>
|
||||||
</button>
|
</button>
|
||||||
<ul class='dropdown-menu'>
|
<ul class='dropdown-menu'>
|
||||||
<li><a class='dropdown-item' href='#' id='multi-output-complete' title='{% trans "Complete selected items" %}'><span class='fas fa-check-circle icon-green'></span> {% trans "Complete outputs" %}</a></li>
|
<li><a class='dropdown-item' href='#' id='multi-output-complete' title='{% trans "Complete selected items" %}'>
|
||||||
|
<span class='fas fa-check-circle icon-green'></span> {% trans "Complete outputs" %}
|
||||||
|
</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -177,6 +177,17 @@ class CategoryDetail(generics.RetrieveUpdateDestroyAPIView):
|
|||||||
|
|
||||||
return ctx
|
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):
|
class CategoryParameterList(generics.ListAPIView):
|
||||||
""" API endpoint for accessing a list of PartCategoryParameterTemplate objects.
|
""" API endpoint for accessing a list of PartCategoryParameterTemplate objects.
|
||||||
@ -446,7 +457,7 @@ class PartDetail(generics.RetrieveUpdateDestroyAPIView):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if 'starred' in request.data:
|
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)
|
self.get_object().set_starred(request.user, starred)
|
||||||
|
|
||||||
|
@ -35,8 +35,6 @@ class CategorySerializer(InvenTreeModelSerializer):
|
|||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
|
||||||
self.starred_categories = kwargs.pop('starred_categories', [])
|
|
||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def get_starred(self, category):
|
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 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)
|
url = serializers.CharField(source='get_absolute_url', read_only=True)
|
||||||
|
|
||||||
|
@ -20,15 +20,37 @@
|
|||||||
{% include "admin_button.html" with url=url %}
|
{% include "admin_button.html" with url=url %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if category %}
|
{% if category %}
|
||||||
{% if roles.part_category.change %}
|
{% if starred_directly %}
|
||||||
<button class='btn btn-outline-secondary' id='cat-edit' title='{% trans "Edit part category" %}'>
|
<button type='button' class='btn btn-outline-secondary' id='toggle-starred' title='{% trans "You are subscribed to notifications for this category" %}'>
|
||||||
<span class='fas fa-edit'/>
|
<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>
|
</button>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if roles.part_category.delete %}
|
{% if roles.part_category.change or roles.part_category.delete %}
|
||||||
<button class='btn btn-outline-secondary' id='cat-delete' title='{% trans "Delete part category" %}'>
|
<div class='btn-group' role='group'>
|
||||||
<span class='fas fa-trash-alt icon-red'/>
|
<button id='category-options' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown' title='{% trans "Category Actions" %}'>
|
||||||
</button>
|
<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 %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if roles.part_category.add %}
|
{% if roles.part_category.add %}
|
||||||
@ -198,6 +220,14 @@
|
|||||||
data: {{ parameters|safe }},
|
data: {{ parameters|safe }},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$("#toggle-starred").click(function() {
|
||||||
|
toggleStar({
|
||||||
|
url: '{% url "api-part-category-detail" category.pk %}',
|
||||||
|
button: '#category-star-icon'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
enableSidebar('category');
|
enableSidebar('category');
|
||||||
|
@ -320,7 +320,7 @@
|
|||||||
|
|
||||||
$("#toggle-starred").click(function() {
|
$("#toggle-starred").click(function() {
|
||||||
toggleStar({
|
toggleStar({
|
||||||
part: {{ part.id }},
|
url: '{% url "api-part-detail" part.pk %}',
|
||||||
button: '#part-star-icon',
|
button: '#part-star-icon',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1470,18 +1470,29 @@ class CategoryDetail(InvenTreeRoleMixin, DetailView):
|
|||||||
|
|
||||||
if category:
|
if category:
|
||||||
cascade = kwargs.get('cascade', True)
|
cascade = kwargs.get('cascade', True)
|
||||||
|
|
||||||
# Prefetch parts parameters
|
# Prefetch parts parameters
|
||||||
parts_parameters = category.prefetch_parts_parameters(cascade=cascade)
|
parts_parameters = category.prefetch_parts_parameters(cascade=cascade)
|
||||||
|
|
||||||
# Get table headers (unique parameters names)
|
# Get table headers (unique parameters names)
|
||||||
context['headers'] = category.get_unique_parameters(cascade=cascade,
|
context['headers'] = category.get_unique_parameters(cascade=cascade,
|
||||||
prefetch=parts_parameters)
|
prefetch=parts_parameters)
|
||||||
|
|
||||||
# Insert part information
|
# Insert part information
|
||||||
context['headers'].insert(0, 'description')
|
context['headers'].insert(0, 'description')
|
||||||
context['headers'].insert(0, 'part')
|
context['headers'].insert(0, 'part')
|
||||||
|
|
||||||
# Get parameters data
|
# Get parameters data
|
||||||
context['parameters'] = category.get_parts_parameters(cascade=cascade,
|
context['parameters'] = category.get_parts_parameters(cascade=cascade,
|
||||||
prefetch=parts_parameters)
|
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
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
@ -378,19 +378,18 @@ function duplicatePart(pk, options={}) {
|
|||||||
*
|
*
|
||||||
* options:
|
* options:
|
||||||
* - button: ID of the button (default = '#part-star-icon')
|
* - button: ID of the button (default = '#part-star-icon')
|
||||||
* - part: pk of the part object
|
* - URL: API url of the object
|
||||||
* - user: pk of the user
|
* - user: pk of the user
|
||||||
*/
|
*/
|
||||||
function toggleStar(options) {
|
function toggleStar(options) {
|
||||||
|
|
||||||
var url = `/api/part/${options.part}/`;
|
inventreeGet(options.url, {}, {
|
||||||
|
|
||||||
inventreeGet(url, {}, {
|
|
||||||
success: function(response) {
|
success: function(response) {
|
||||||
|
|
||||||
var starred = response.starred;
|
var starred = response.starred;
|
||||||
|
|
||||||
inventreePut(
|
inventreePut(
|
||||||
url,
|
options.url,
|
||||||
{
|
{
|
||||||
starred: !starred,
|
starred: !starred,
|
||||||
},
|
},
|
||||||
@ -399,16 +398,16 @@ function toggleStar(options) {
|
|||||||
success: function(response) {
|
success: function(response) {
|
||||||
if (response.starred) {
|
if (response.starred) {
|
||||||
$(options.button).removeClass('fa fa-bell-slash').addClass('fas fa-bell icon-green');
|
$(options.button).removeClass('fa fa-bell-slash').addClass('fas fa-bell icon-green');
|
||||||
$(options.button).attr('title', '{% trans "You are subscribed to notifications for this part" %}');
|
$(options.button).attr('title', '{% trans "You are subscribed to notifications for this item" %}');
|
||||||
|
|
||||||
showMessage('{% trans "You have subscribed to notifications for this part" %}', {
|
showMessage('{% trans "You have subscribed to notifications for this item" %}', {
|
||||||
style: 'success',
|
style: 'success',
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
$(options.button).removeClass('fas fa-bell icon-green').addClass('fa fa-bell-slash');
|
$(options.button).removeClass('fas fa-bell icon-green').addClass('fa fa-bell-slash');
|
||||||
$(options.button).attr('title', '{% trans "Subscribe to notifications for this part" %}');
|
$(options.button).attr('title', '{% trans "Subscribe to notifications for this item" %}');
|
||||||
|
|
||||||
showMessage('{% trans "You have unsubscribed to notifications for this part" %}', {
|
showMessage('{% trans "You have unsubscribed to notifications for this item" %}', {
|
||||||
style: 'warning',
|
style: 'warning',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -453,7 +452,7 @@ function makePartIcons(part) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (part.starred) {
|
if (part.starred) {
|
||||||
html += makeIconBadge('fa-star', '{% trans "Starred part" %}');
|
html += makeIconBadge('fa-bell icon-green', '{% trans "Subscribed part" %}');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (part.salable) {
|
if (part.salable) {
|
||||||
@ -461,7 +460,7 @@ function makePartIcons(part) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!part.active) {
|
if (!part.active) {
|
||||||
html += `<span class='badge badge-right rounded-pill bg-warning'>{% trans "Inactive" %}</span>`;
|
html += `<span class='badge badge-right rounded-pill bg-warning'>{% trans "Inactive" %}</span> `;
|
||||||
}
|
}
|
||||||
|
|
||||||
return html;
|
return html;
|
||||||
@ -1268,10 +1267,17 @@ function loadPartCategoryTable(table, options) {
|
|||||||
switchable: true,
|
switchable: true,
|
||||||
sortable: true,
|
sortable: true,
|
||||||
formatter: function(value, row) {
|
formatter: function(value, row) {
|
||||||
return renderLink(
|
|
||||||
|
var html = renderLink(
|
||||||
value,
|
value,
|
||||||
`/part/category/${row.pk}/`
|
`/part/category/${row.pk}/`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (row.starred) {
|
||||||
|
html += makeIconBadge('fa-bell icon-green', '{% trans "Subscribed category" %}');
|
||||||
|
}
|
||||||
|
|
||||||
|
return html;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -103,6 +103,10 @@ function getAvailableTableFilters(tableKey) {
|
|||||||
title: '{% trans "Include subcategories" %}',
|
title: '{% trans "Include subcategories" %}',
|
||||||
description: '{% trans "Include subcategories" %}',
|
description: '{% trans "Include subcategories" %}',
|
||||||
},
|
},
|
||||||
|
starred: {
|
||||||
|
type: 'bool',
|
||||||
|
title: '{% trans "Subscribed" %}',
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -368,7 +372,7 @@ function getAvailableTableFilters(tableKey) {
|
|||||||
},
|
},
|
||||||
starred: {
|
starred: {
|
||||||
type: 'bool',
|
type: 'bool',
|
||||||
title: '{% trans "Starred" %}',
|
title: '{% trans "Subscribed" %}',
|
||||||
},
|
},
|
||||||
salable: {
|
salable: {
|
||||||
type: 'bool',
|
type: 'bool',
|
||||||
|
Reference in New Issue
Block a user