Add ability to filter by 'starred' status

This commit is contained in:
Oliver Walters 2020-04-13 21:24:36 +10:00
parent c76c93237e
commit b850beb687
3 changed files with 46 additions and 26 deletions

View File

@ -87,7 +87,7 @@ function loadPartTable(table, url, options={}) {
* buttons: If provided, link buttons to selection status of this table * buttons: If provided, link buttons to selection status of this table
*/ */
var params = options.parms || {}; var params = options.params || {};
var filters = loadTableFilters("parts"); var filters = loadTableFilters("parts");

View File

@ -153,6 +153,7 @@ class PartList(generics.ListCreateAPIView):
The Part object list can be filtered by: The Part object list can be filtered by:
- category: Filter by PartCategory reference - category: Filter by PartCategory reference
- cascade: If true, include parts from sub-categories - cascade: If true, include parts from sub-categories
- starred: Is the part "starred" by the current user?
- is_template: Is the part a template part? - is_template: Is the part a template part?
- variant_of: Filter by variant_of Part reference - variant_of: Filter by variant_of Part reference
- assembly: Filter by assembly field - assembly: Filter by assembly field
@ -295,31 +296,50 @@ class PartList(generics.ListCreateAPIView):
def get_queryset(self): def get_queryset(self):
# Does the user wish to filter by category?
cat_id = self.request.query_params.get('category', None)
# Start with all objects # Start with all objects
parts_list = Part.objects.all() parts_list = Part.objects.all()
cascade = str2bool(self.request.query_params.get('cascade', False)) # Filter by 'starred' parts?
starred = str2bool(self.request.query_params.get('starred', None))
if starred is not None:
starred_parts = [star.part.pk for star in self.request.user.starred_parts.all()]
if starred:
parts_list = parts_list.filter(pk__in=starred_parts)
else:
parts_list = parts_list.exclude(pk__in=starred_parts)
cascade = str2bool(self.request.query_params.get('cascade', None))
# Does the user wish to filter by category?
cat_id = self.request.query_params.get('category', None)
if cat_id is None: if cat_id is None:
# Top-level parts # No category filtering if category is not specified
if not cascade: pass
parts_list = parts_list.filter(category=None)
else: else:
try: # Category has been specified!
category = PartCategory.objects.get(pk=cat_id) if isNull(cat_id):
# A 'null' category is the top-level category
if cascade is False:
# Do not cascade, only list parts in the top-level category
parts_list = parts_list.filter(category=None)
# If '?cascade=true' then include parts which exist in sub-categories else:
if cascade: try:
parts_list = parts_list.filter(category__in=category.getUniqueChildren()) category = PartCategory.objects.get(pk=cat_id)
# Just return parts directly in the requested category
else: # If '?cascade=true' then include parts which exist in sub-categories
parts_list = parts_list.filter(category=cat_id) if cascade:
except (ValueError, PartCategory.DoesNotExist): parts_list = parts_list.filter(category__in=category.getUniqueChildren())
pass # Just return parts directly in the requested category
else:
parts_list = parts_list.filter(category=cat_id)
except (ValueError, PartCategory.DoesNotExist):
pass
# Ensure that related models are pre-loaded to reduce DB trips # Ensure that related models are pre-loaded to reduce DB trips
parts_list = self.get_serializer_class().setup_eager_loading(parts_list) parts_list = self.get_serializer_class().setup_eager_loading(parts_list)

View File

@ -200,11 +200,11 @@
{% if category %} {% if category %}
$("#cat-edit").click(function () { $("#cat-edit").click(function () {
launchModalForm( launchModalForm(
"{% url 'category-edit' category.id %}", "{% url 'category-edit' category.id %}",
{ {
reload: true reload: true
}, },
); );
return false; return false;
}); });
@ -227,9 +227,9 @@
"#part-table", "#part-table",
"{% url 'api-part-list' %}", "{% url 'api-part-list' %}",
{ {
query: { params: {
{% if category %} {% if category %}category: {{ category.id }},
category: {{ category.id }}, {% else %}category: "null",
{% endif %} {% endif %}
}, },
buttons: ['#part-options'], buttons: ['#part-options'],