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,
|
||||
)
|
||||
from rest_framework_simplejwt.views import TokenObtainPairView
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from apps.core.pagination import CustomPageNumberPagination
|
||||
from apps.authorization.api.v1 import api as authorize_view
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
@@ -193,10 +194,20 @@ class OrganizationTypeViewSet(ModelViewSet):
|
||||
serializer_class = OrganizationTypeSerializer
|
||||
|
||||
|
||||
class OrganizationViewSet(ModelViewSet):
|
||||
class OrganizationViewSet(ModelViewSet, DynamicSearchMixin):
|
||||
""" Crud operations for organization model """ #
|
||||
queryset = Organization.objects.all()
|
||||
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):
|
||||
descendants = []
|
||||
@@ -295,7 +306,10 @@ class OrganizationViewSet(ModelViewSet):
|
||||
organization = get_organization_by_user(request.user)
|
||||
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:
|
||||
serializer = self.serializer_class(page, many=True)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from apps.herd.web.api.v1.serializers import HerdSerializer, RancherSerializer
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
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.response import Response
|
||||
from rest_framework.decorators import action
|
||||
@@ -19,6 +20,8 @@ class HerdViewSet(viewsets.ModelViewSet):
|
||||
@transaction.atomic
|
||||
def create(self, request, *args, **kwargs):
|
||||
""" create herd with user """
|
||||
organization = get_organization_by_user(request.user)
|
||||
|
||||
if 'user' in request.data.keys():
|
||||
# create user if owner of herd is not exist
|
||||
user = CustomOperations().custom_create(
|
||||
@@ -29,6 +32,10 @@ class HerdViewSet(viewsets.ModelViewSet):
|
||||
owner = user['id']
|
||||
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)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
|
||||
@@ -18,11 +18,13 @@ class HerdSerializer(serializers.ModelSerializer):
|
||||
""" Customize serializer output """
|
||||
representation = super().to_representation(instance)
|
||||
if isinstance(instance, Herd):
|
||||
representation['owner'] = instance.owner.id
|
||||
if instance.owner:
|
||||
representation['owner'] = instance.owner.id
|
||||
representation['cooperative'] = OrganizationSerializer(instance.cooperative).data
|
||||
representation['province'] = ProvinceSerializer(instance.province).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
|
||||
|
||||
return representation
|
||||
|
||||
Reference in New Issue
Block a user