Filter by category too

This commit is contained in:
Oliver Walters 2019-09-09 19:59:56 +10:00
parent 776fc8b1e5
commit ddb041fe44
3 changed files with 28 additions and 14 deletions

View File

@ -44,6 +44,7 @@
<div id='button-toolbar'>
<div class='button-toolbar container-fluid' style="float: right;">
<button class='btn btn-default' id='part-export' title='Export Part Data'>Export</button>
<button class='btn btn-success' id='part-create'>New Part</button>
<div class='dropdown' style='float: right;'>
<button id='part-options' class='btn btn-primary dropdown-toggle' type='button' data-toggle="dropdown">Options<span class='caret'></span></button>
@ -104,6 +105,13 @@
});
})
$("#part-export").click(function() {
var url = "{% url 'part-export' %}?category={{ category.id }}";
location.href = url;
});
$("#part-create").click(function() {
launchModalForm(
"{% url 'part-create' %}",

View File

@ -1166,12 +1166,29 @@ class PartExport(AjaxView):
""" Extract part list from the POST parameters.
Parts can be supplied as:
- List of part PK values
- Part category
- List of part PK values
"""
part_list = Part.objects.all()
# Filter by part category
cat_id = request.GET.get('category', None)
print('cat_id:', cat_id)
part_list = None
if cat_id is not None:
try:
category = PartCategory.objects.get(pk=cat_id)
part_list = category.get_parts()
except (ValueError, PartCategory.DoesNotExist):
pass
# Backup - All parts
if part_list is None:
part_list = Part.objects.all()
# Also optionally filter by explicit list of part IDs
part_ids = request.GET.get('parts', '')
parts = []
@ -1181,20 +1198,9 @@ class PartExport(AjaxView):
except ValueError:
pass
# Filter by list of Part IDs
if len(parts) > 0:
part_list = part_list.filter(pk__in=parts)
# Filter by part category
cat_id = request.GET.get('category', None)
if cat_id is not None:
try:
category = PartCategory.objects.get(cat_id)
part_list = part_list.filter(category=category)
except (ValueError, PartCategory.DoesNotExist):
pass
# Prefetch related fields to reduce DB hits
part_list = part_list.prefetch_related(
'category',

View File

@ -1,6 +1,6 @@
<div id='button-toolbar'>
<div class='button-toolbar container-fluid' style='float: right;'>
<button class='btn btn-success' id='stock-export' title='Export Stock Information'>Export</button>
<button class='btn btn-default' id='stock-export' title='Export Stock Information'>Export</button>
{% if not part or part.is_template == False %}
<button class="btn btn-success" id='item-create'>New Stock Item</button>
{% endif %}