set search & filter on organization - change on herd in cooperative
This commit is contained in:
@@ -10,6 +10,7 @@ from apps.authentication.api.v1.serializers.serializer import (
|
|||||||
BankAccountSerializer,
|
BankAccountSerializer,
|
||||||
)
|
)
|
||||||
from rest_framework_simplejwt.views import TokenObtainPairView
|
from rest_framework_simplejwt.views import TokenObtainPairView
|
||||||
|
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||||
from apps.core.pagination import CustomPageNumberPagination
|
from apps.core.pagination import CustomPageNumberPagination
|
||||||
from apps.authorization.api.v1 import api as authorize_view
|
from apps.authorization.api.v1 import api as authorize_view
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
@@ -193,10 +194,20 @@ class OrganizationTypeViewSet(ModelViewSet):
|
|||||||
serializer_class = OrganizationTypeSerializer
|
serializer_class = OrganizationTypeSerializer
|
||||||
|
|
||||||
|
|
||||||
class OrganizationViewSet(ModelViewSet):
|
class OrganizationViewSet(ModelViewSet, DynamicSearchMixin):
|
||||||
""" Crud operations for organization model """ #
|
""" Crud operations for organization model """ #
|
||||||
queryset = Organization.objects.all()
|
queryset = Organization.objects.all()
|
||||||
serializer_class = OrganizationSerializer
|
serializer_class = OrganizationSerializer
|
||||||
|
search_fields = [
|
||||||
|
"name",
|
||||||
|
"type__name",
|
||||||
|
"type__key",
|
||||||
|
"national_unique_id",
|
||||||
|
"field_of_activity",
|
||||||
|
"company_code",
|
||||||
|
"province__name",
|
||||||
|
"city__name",
|
||||||
|
]
|
||||||
|
|
||||||
def get_all_org_child(self, org):
|
def get_all_org_child(self, org):
|
||||||
descendants = []
|
descendants = []
|
||||||
@@ -295,7 +306,10 @@ class OrganizationViewSet(ModelViewSet):
|
|||||||
organization = get_organization_by_user(request.user)
|
organization = get_organization_by_user(request.user)
|
||||||
child_organizations = self.get_all_org_child(organization)
|
child_organizations = self.get_all_org_child(organization)
|
||||||
|
|
||||||
page = self.paginate_queryset(child_organizations) # paginate queryset
|
# search & filter
|
||||||
|
queryset = self.filter_query(self.queryset.filter(id__in={instance.id for instance in child_organizations}))
|
||||||
|
|
||||||
|
page = self.paginate_queryset(queryset) # paginate queryset
|
||||||
|
|
||||||
if page is not None:
|
if page is not None:
|
||||||
serializer = self.serializer_class(page, many=True)
|
serializer = self.serializer_class(page, many=True)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from apps.herd.web.api.v1.serializers import HerdSerializer, RancherSerializer
|
from apps.herd.web.api.v1.serializers import HerdSerializer, RancherSerializer
|
||||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||||
from apps.authentication.api.v1.api import UserViewSet
|
from apps.authentication.api.v1.api import UserViewSet
|
||||||
|
from common.helpers import get_organization_by_user
|
||||||
from rest_framework.exceptions import APIException
|
from rest_framework.exceptions import APIException
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
@@ -19,6 +20,8 @@ class HerdViewSet(viewsets.ModelViewSet):
|
|||||||
@transaction.atomic
|
@transaction.atomic
|
||||||
def create(self, request, *args, **kwargs):
|
def create(self, request, *args, **kwargs):
|
||||||
""" create herd with user """
|
""" create herd with user """
|
||||||
|
organization = get_organization_by_user(request.user)
|
||||||
|
|
||||||
if 'user' in request.data.keys():
|
if 'user' in request.data.keys():
|
||||||
# create user if owner of herd is not exist
|
# create user if owner of herd is not exist
|
||||||
user = CustomOperations().custom_create(
|
user = CustomOperations().custom_create(
|
||||||
@@ -29,6 +32,10 @@ class HerdViewSet(viewsets.ModelViewSet):
|
|||||||
owner = user['id']
|
owner = user['id']
|
||||||
request.data.update({'owner': owner})
|
request.data.update({'owner': owner})
|
||||||
|
|
||||||
|
# if cooperative do not send in request, set my organization in it
|
||||||
|
if 'cooperative' not in request.data.keys():
|
||||||
|
request.data.update({'cooperative': organization.id})
|
||||||
|
|
||||||
serializer = self.serializer_class(data=request.data)
|
serializer = self.serializer_class(data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
serializer.save()
|
serializer.save()
|
||||||
|
|||||||
@@ -18,11 +18,13 @@ class HerdSerializer(serializers.ModelSerializer):
|
|||||||
""" Customize serializer output """
|
""" Customize serializer output """
|
||||||
representation = super().to_representation(instance)
|
representation = super().to_representation(instance)
|
||||||
if isinstance(instance, Herd):
|
if isinstance(instance, Herd):
|
||||||
representation['owner'] = instance.owner.id
|
if instance.owner:
|
||||||
|
representation['owner'] = instance.owner.id
|
||||||
representation['cooperative'] = OrganizationSerializer(instance.cooperative).data
|
representation['cooperative'] = OrganizationSerializer(instance.cooperative).data
|
||||||
representation['province'] = ProvinceSerializer(instance.province).data
|
representation['province'] = ProvinceSerializer(instance.province).data
|
||||||
representation['city'] = CitySerializer(instance.city).data
|
representation['city'] = CitySerializer(instance.city).data
|
||||||
representation['contractor'] = OrganizationSerializer(instance.contractor).data
|
if instance.contractor:
|
||||||
|
representation['contractor'] = OrganizationSerializer(instance.contractor).data
|
||||||
representation['rancher'] = RancherSerializer(instance.rancher).data
|
representation['rancher'] = RancherSerializer(instance.rancher).data
|
||||||
|
|
||||||
return representation
|
return representation
|
||||||
|
|||||||
Reference in New Issue
Block a user