mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 12:36:45 +00:00
Adds filter to purchase order for "assigned to me"
This commit is contained in:
parent
ab80980b5a
commit
087ac0bd34
@ -8,6 +8,9 @@ from __future__ import unicode_literals
|
|||||||
from django.conf.urls import url, include
|
from django.conf.urls import url, include
|
||||||
from django.db.models import Q, F
|
from django.db.models import Q, F
|
||||||
|
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.contrib.auth.models import Group
|
||||||
|
|
||||||
from django_filters import rest_framework as rest_filters
|
from django_filters import rest_framework as rest_filters
|
||||||
from rest_framework import generics
|
from rest_framework import generics
|
||||||
from rest_framework import filters, status
|
from rest_framework import filters, status
|
||||||
@ -22,8 +25,38 @@ 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:
|
||||||
|
fields = [
|
||||||
|
'supplier',
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class POList(generics.ListCreateAPIView):
|
class POList(generics.ListCreateAPIView):
|
||||||
@ -35,6 +68,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 +184,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',
|
||||||
|
@ -327,6 +327,10 @@ function getAvailableTableFilters(tableKey) {
|
|||||||
type: 'bool',
|
type: 'bool',
|
||||||
title: '{% trans "Overdue" %}',
|
title: '{% trans "Overdue" %}',
|
||||||
},
|
},
|
||||||
|
assigned_to_me: {
|
||||||
|
type: 'bool',
|
||||||
|
title: '{% trans "Assigned to me" %}',
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user