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
This commit is contained in:
simonkuehling 2023-02-25 03:52:12 +01:00 committed by GitHub
parent c46f153449
commit f523fb44f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 4 deletions

View File

@ -2,11 +2,14 @@
# InvenTree API version # 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 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 v97 -> 2023-02-20 : https://github.com/inventree/InvenTree/pull/4377
- Adds "external" attribute to StockLocation model - Adds "external" attribute to StockLocation model

View File

@ -2,6 +2,7 @@
from django.urls import include, re_path from django.urls import include, re_path
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import User
from rest_framework import filters from rest_framework import filters
from rest_framework.exceptions import ValidationError from rest_framework.exceptions import ValidationError
@ -65,6 +66,20 @@ class BuildFilter(rest_filters.FilterSet):
return queryset 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 # Exact match for reference
reference = rest_filters.CharFilter( reference = rest_filters.CharFilter(
label='Filter by exact reference', label='Filter by exact reference',

View File

@ -2695,11 +2695,19 @@ function loadBuildTable(table, options) {
title: '{% trans "Responsible" %}', title: '{% trans "Responsible" %}',
sortable: true, sortable: true,
formatter: function(value, row) { formatter: function(value, row) {
if (value) { if (!row.responsible_detail) {
return row.responsible_detail.name;
} else {
return '-'; return '-';
} }
var html = row.responsible_detail.name;
if (row.responsible_detail.label == 'group') {
html += `<span class='float-right fas fa-users'></span>`;
} else {
html += `<span class='float-right fas fa-user'></span>`;
}
return html;
} }
}, },
{ {

View File

@ -241,6 +241,11 @@ function generateFilterInput(tableKey, filterKey) {
// Return a 'select' input with the available values // Return a 'select' input with the available values
html = `<select class='form-control filter-input' id='${id}' name='value'>`; html = `<select class='form-control filter-input' id='${id}' name='value'>`;
// options can be an object or a function, in which case we need to run
// this callback first
if (options instanceof Function) {
options = options();
}
for (var key in options) { for (var key in options) {
var option = options[key]; var option = options[key];
html += `<option value='${key}'>${option.value}</option>`; html += `<option value='${key}'>${option.value}</option>`;

View File

@ -359,6 +359,25 @@ function getAvailableTableFilters(tableKey) {
type: 'bool', type: 'bool',
title: '{% trans "Assigned to me" %}', title: '{% trans "Assigned to me" %}',
}, },
assigned_to: {
title: '{% trans "Responsible" %}',
options: function() {
var ownersList = {};
inventreeGet(`/api/user/owner/`, {}, {
async: false,
success: function(response) {
for (key in response) {
var owner = response[key];
ownersList[owner.pk] = {
key: owner.pk,
value: `${owner.name} (${owner.label})`,
};
}
}
});
return ownersList;
},
},
}; };
} }