Merge pull request #2420 from SchrodingersGat/assigned-to-me

Assigned to me
This commit is contained in:
Oliver 2021-12-04 23:08:08 +11:00 committed by GitHub
commit 120870712a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 97 additions and 6 deletions

View File

@ -20,6 +20,7 @@ from InvenTree.status_codes import BuildStatus
from .models import Build, BuildItem, BuildOrderAttachment from .models import Build, BuildItem, BuildOrderAttachment
from .serializers import BuildAttachmentSerializer, BuildCompleteSerializer, BuildSerializer, BuildItemSerializer from .serializers import BuildAttachmentSerializer, BuildCompleteSerializer, BuildSerializer, BuildItemSerializer
from .serializers import BuildAllocationSerializer, BuildUnallocationSerializer from .serializers import BuildAllocationSerializer, BuildUnallocationSerializer
from users.models import Owner
class BuildFilter(rest_filters.FilterSet): class BuildFilter(rest_filters.FilterSet):
@ -51,6 +52,25 @@ class BuildFilter(rest_filters.FilterSet):
return queryset return queryset
assigned_to_me = rest_filters.BooleanFilter(label='assigned_to_me', method='filter_assigned_to_me')
def filter_assigned_to_me(self, queryset, name, value):
"""
Filter by orders which are assigned to the current user
"""
value = str2bool(value)
# Work out who "me" is!
owners = Owner.get_owners_matching_user(self.request.user)
if value:
queryset = queryset.filter(responsible__in=owners)
else:
queryset = queryset.exclude(responsible__in=owners)
return queryset
class BuildList(generics.ListCreateAPIView): class BuildList(generics.ListCreateAPIView):
""" API endpoint for accessing a list of Build objects. """ API endpoint for accessing a list of Build objects.

View File

@ -22,8 +22,39 @@ from InvenTree.status_codes import PurchaseOrderStatus, SalesOrderStatus
import order.models as models import order.models as models
import order.serializers as serializers import order.serializers as serializers
from part.models import Part from part.models import Part
from users.models import Owner
class POFilter(rest_filters.FilterSet):
"""
Custom API filters for the POList endpoint
"""
assigned_to_me = rest_filters.BooleanFilter(label='assigned_to_me', method='filter_assigned_to_me')
def filter_assigned_to_me(self, queryset, name, value):
"""
Filter by orders which are assigned to the current user
"""
value = str2bool(value)
# Work out who "me" is!
owners = Owner.get_owners_matching_user(self.request.user)
if value:
queryset = queryset.filter(responsible__in=owners)
else:
queryset = queryset.exclude(responsible__in=owners)
return queryset
class Meta:
model = models.PurchaseOrder
fields = [
'supplier',
]
class POList(generics.ListCreateAPIView): class POList(generics.ListCreateAPIView):
@ -35,6 +66,7 @@ class POList(generics.ListCreateAPIView):
queryset = models.PurchaseOrder.objects.all() queryset = models.PurchaseOrder.objects.all()
serializer_class = serializers.POSerializer serializer_class = serializers.POSerializer
filterset_class = POFilter
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
""" """
@ -150,10 +182,6 @@ class POList(generics.ListCreateAPIView):
'reference': ['reference_int', 'reference'], 'reference': ['reference_int', 'reference'],
} }
filter_fields = [
'supplier',
]
search_fields = [ search_fields = [
'reference', 'reference',
'supplier__name', 'supplier__name',

View File

@ -583,6 +583,8 @@ class PartFilter(rest_filters.FilterSet):
active = rest_filters.BooleanFilter() active = rest_filters.BooleanFilter()
virtual = rest_filters.BooleanFilter()
class PartList(generics.ListCreateAPIView): class PartList(generics.ListCreateAPIView):
""" """

View File

@ -34,7 +34,12 @@ function buildFormFields() {
reference: { reference: {
prefix: global_settings.BUILDORDER_REFERENCE_PREFIX, prefix: global_settings.BUILDORDER_REFERENCE_PREFIX,
}, },
part: {}, part: {
filters: {
assembly: true,
virtual: false,
}
},
title: {}, title: {},
quantity: {}, quantity: {},
parent: { parent: {

View File

@ -298,6 +298,10 @@ function getAvailableTableFilters(tableKey) {
type: 'bool', type: 'bool',
title: '{% trans "Overdue" %}', title: '{% trans "Overdue" %}',
}, },
assigned_to_me: {
type: 'bool',
title: '{% trans "Assigned to me" %}',
},
}; };
} }
@ -327,6 +331,10 @@ function getAvailableTableFilters(tableKey) {
type: 'bool', type: 'bool',
title: '{% trans "Overdue" %}', title: '{% trans "Overdue" %}',
}, },
assigned_to_me: {
type: 'bool',
title: '{% trans "Assigned to me" %}',
},
}; };
} }

View File

@ -477,6 +477,34 @@ class Owner(models.Model):
owner: Returns the Group or User instance combining the owner_type and owner_id fields owner: Returns the Group or User instance combining the owner_type and owner_id fields
""" """
@classmethod
def get_owners_matching_user(cls, user):
"""
Return all "owner" objects matching the provided user:
A) An exact match for the user
B) Any groups that the user is a part of
"""
user_type = ContentType.objects.get(app_label='auth', model='user')
group_type = ContentType.objects.get(app_label='auth', model='group')
owners = []
try:
owners.append(cls.objects.get(owner_id=user.pk, owner_type=user_type))
except:
pass
for group in user.groups.all():
try:
owner = cls.objects.get(owner_id=group.pk, owner_type=group_type)
owners.append(owner)
except:
pass
return owners
@staticmethod @staticmethod
def get_api_url(): def get_api_url():
return reverse('api-owner-list') return reverse('api-owner-list')