Owner list API updates (#5906)

* Implement better search matching for owner list API

* Update UserSerializer

-Add filterset fields to various API endpoints

* Allow owner list to be filtered by "is_active" status

- Only applies to users (not groups)

* Add ability to filter "owner" list by "is_active" status

- Only applies to users

* Use "is_active" filter for "responsible" form field
This commit is contained in:
Oliver 2023-11-13 15:06:45 +11:00 committed by GitHub
parent 8972dcb506
commit ac26f61ecd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 6 deletions

View File

@ -113,6 +113,9 @@ function buildFormFields() {
},
responsible: {
icon: 'fa-users',
filters: {
is_active: true,
}
},
};

View File

@ -172,6 +172,9 @@ function partFields(options={}) {
},
responsible: {
icon: 'fa-user',
filters: {
is_active: true,
}
},
component: {
default: global_settings.PART_COMPONENT,

View File

@ -139,6 +139,9 @@ function purchaseOrderFields(options={}) {
},
responsible: {
icon: 'fa-user',
filters: {
is_active: true,
}
},
};

View File

@ -104,6 +104,9 @@ function returnOrderFields(options={}) {
},
responsible: {
icon: 'fa-user',
filters: {
is_active: true,
}
}
};

View File

@ -130,6 +130,9 @@ function salesOrderFields(options={}) {
},
responsible: {
icon: 'fa-user',
filters: {
is_active: true,
}
}
};

View File

@ -10,6 +10,7 @@ from rest_framework import exceptions, permissions
from rest_framework.response import Response
from rest_framework.views import APIView
import InvenTree.helpers
from InvenTree.filters import SEARCH_ORDER_FILTER
from InvenTree.mixins import (ListAPI, ListCreateAPI, RetrieveAPI,
RetrieveUpdateAPI, RetrieveUpdateDestroyAPI)
@ -42,18 +43,38 @@ class OwnerList(ListAPI):
but until we determine a better way, this is what we have...
"""
search_term = str(self.request.query_params.get('search', '')).lower()
is_active = self.request.query_params.get('is_active', None)
queryset = super().filter_queryset(queryset)
if not search_term:
return queryset
results = []
# Extract search term f
# Get a list of all matching users, depending on the *is_active* flag
if is_active is not None:
is_active = InvenTree.helpers.str2bool(is_active)
matching_user_ids = User.objects.filter(is_active=is_active).values_list('pk', flat=True)
for result in queryset.all():
if search_term in result.name().lower():
name = str(result.name()).lower().strip()
search_match = True
# Extract search term f
if search_term:
for entry in search_term.strip().split(' '):
if entry not in name:
search_match = False
break
if not search_match:
continue
if is_active is not None:
# Skip any users which do not match the required *is_active* value
if result.owner_type.name == 'user' and result.owner_id not in matching_user_ids:
continue
# If we get here, there is no reason *not* to include this result
results.append(result)
return results
@ -156,6 +177,12 @@ class UserList(ListCreateAPI):
'is_active',
]
filterset_fields = [
'is_staff',
'is_active',
'is_superuser',
]
class GroupDetail(RetrieveUpdateDestroyAPI):
"""Detail endpoint for a particular auth group"""