From f523fb44f6f63e489b994e5327a60ee0d8f778f0 Mon Sep 17 00:00:00 2001 From: simonkuehling Date: Sat, 25 Feb 2023 03:52:12 +0100 Subject: [PATCH] Filter build orders by responsible owner (#4383) (#4408) * add assigned_to filter to Build API * extend API to filter build orders by assigned owner * rename API filter to 'responsible' * add 'Responsible' filter to build oders table * add user/group icon to owners in 'Responsible' column * remove unused python class import * bump API version number * fix handling of invalid IDs * refactor filter options as a callback function * fix JS styling --- InvenTree/InvenTree/api_version.py | 5 ++++- InvenTree/build/api.py | 15 +++++++++++++++ InvenTree/templates/js/translated/build.js | 14 +++++++++++--- InvenTree/templates/js/translated/filters.js | 5 +++++ .../templates/js/translated/table_filters.js | 19 +++++++++++++++++++ 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/InvenTree/InvenTree/api_version.py b/InvenTree/InvenTree/api_version.py index 203aa09713..29774af0d5 100644 --- a/InvenTree/InvenTree/api_version.py +++ b/InvenTree/InvenTree/api_version.py @@ -2,11 +2,14 @@ # InvenTree API version -INVENTREE_API_VERSION = 97 +INVENTREE_API_VERSION = 98 """ Increment this API version number whenever there is a significant change to the API that any clients need to know about +v98 -> 2023-02-24 : https://github.com/inventree/InvenTree/pull/4408 + - Adds "responsible" filter to Build API + v97 -> 2023-02-20 : https://github.com/inventree/InvenTree/pull/4377 - Adds "external" attribute to StockLocation model diff --git a/InvenTree/build/api.py b/InvenTree/build/api.py index 9fda2c3065..dd52145ad0 100644 --- a/InvenTree/build/api.py +++ b/InvenTree/build/api.py @@ -2,6 +2,7 @@ from django.urls import include, re_path from django.utils.translation import gettext_lazy as _ +from django.contrib.auth.models import User from rest_framework import filters from rest_framework.exceptions import ValidationError @@ -65,6 +66,20 @@ class BuildFilter(rest_filters.FilterSet): return queryset + assigned_to = rest_filters.NumberFilter(label='responsible', method='filter_responsible') + + def filter_responsible(self, queryset, name, value): + """Filter by orders which are assigned to the specified owner.""" + owners = list(Owner.objects.filter(pk=value)) + + # if we query by a user, also find all ownerships through group memberships + if len(owners) > 0 and owners[0].label() == 'user': + owners = Owner.get_owners_matching_user(User.objects.get(pk=owners[0].owner_id)) + + queryset = queryset.filter(responsible__in=owners) + + return queryset + # Exact match for reference reference = rest_filters.CharFilter( label='Filter by exact reference', diff --git a/InvenTree/templates/js/translated/build.js b/InvenTree/templates/js/translated/build.js index 4ccc44968c..fd9236994c 100644 --- a/InvenTree/templates/js/translated/build.js +++ b/InvenTree/templates/js/translated/build.js @@ -2695,11 +2695,19 @@ function loadBuildTable(table, options) { title: '{% trans "Responsible" %}', sortable: true, formatter: function(value, row) { - if (value) { - return row.responsible_detail.name; - } else { + if (!row.responsible_detail) { return '-'; } + + var html = row.responsible_detail.name; + + if (row.responsible_detail.label == 'group') { + html += ``; + } else { + html += ``; + } + + return html; } }, { diff --git a/InvenTree/templates/js/translated/filters.js b/InvenTree/templates/js/translated/filters.js index 78dd67043d..7a2d8687a2 100644 --- a/InvenTree/templates/js/translated/filters.js +++ b/InvenTree/templates/js/translated/filters.js @@ -241,6 +241,11 @@ function generateFilterInput(tableKey, filterKey) { // Return a 'select' input with the available values html = `