filter all apis of organization city & province - cant remove own user or organoization
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
from django.db import transaction
|
||||
from rest_framework import status
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.response import Response
|
||||
|
||||
from apps.authentication.api.v1.api import UserViewSet
|
||||
from apps.core.api import BaseViewSet
|
||||
from apps.core.mixins.search_mixin import DynamicSearchMixin
|
||||
from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
|
||||
from apps.herd.models import Herd, Rancher
|
||||
from apps.herd.web.api.v1.serializers import HerdSerializer, RancherSerializer
|
||||
from apps.livestock.web.api.v1.serializers import LiveStockSerializer
|
||||
from apps.product.web.api.v1.serializers import product_serializers
|
||||
from apps.core.mixins.soft_delete_mixin import SoftDeleteMixin
|
||||
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 apps.product import models as product_models
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import action
|
||||
from common.tools import CustomOperations
|
||||
from rest_framework import viewsets
|
||||
from apps.herd.models import Herd, Rancher
|
||||
from django.db import transaction
|
||||
from rest_framework import status
|
||||
|
||||
|
||||
class HerdViewSet(SoftDeleteMixin, viewsets.ModelViewSet):
|
||||
class HerdViewSet(BaseViewSet, SoftDeleteMixin, viewsets.ModelViewSet):
|
||||
""" Herd ViewSet """
|
||||
queryset = Herd.objects.all()
|
||||
serializer_class = HerdSerializer
|
||||
@@ -57,47 +57,12 @@ class HerdViewSet(SoftDeleteMixin, viewsets.ModelViewSet):
|
||||
@transaction.atomic
|
||||
def my_herds(self, request):
|
||||
""" get current user herds """
|
||||
serializer = self.serializer_class(self.queryset.filter(owner=request.user.id), many=True)
|
||||
serializer = self.serializer_class(self.get_queryset().filter(owner=request.user.id), many=True)
|
||||
if serializer.data:
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
else:
|
||||
return Response(status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_path='trash',
|
||||
url_name='trash',
|
||||
name='trash'
|
||||
)
|
||||
@transaction.atomic
|
||||
def trash(self, request, pk=None):
|
||||
""" Sent herd to trash """
|
||||
try:
|
||||
herd = self.queryset.get(id=pk)
|
||||
herd.trash = True
|
||||
herd.save()
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=True,
|
||||
url_path='delete',
|
||||
url_name='delete',
|
||||
name='delete'
|
||||
)
|
||||
@transaction.atomic
|
||||
def delete(self, request, pk=None):
|
||||
""" full delete of herd """
|
||||
try:
|
||||
herd = self.queryset.get(id=pk)
|
||||
herd.delete()
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
except APIException as e:
|
||||
return Response(e, status=status.HTTP_204_NO_CONTENT)
|
||||
|
||||
@action(
|
||||
methods=['get'],
|
||||
detail=True,
|
||||
@@ -113,12 +78,12 @@ class HerdViewSet(SoftDeleteMixin, viewsets.ModelViewSet):
|
||||
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(queryset)
|
||||
if page is not None:
|
||||
if page is not None: # noqa
|
||||
serializer = LiveStockSerializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
|
||||
class RancherViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
class RancherViewSet(BaseViewSet, SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearchMixin):
|
||||
queryset = Rancher.objects.all()
|
||||
serializer_class = RancherSerializer
|
||||
search_fields = [
|
||||
@@ -137,9 +102,9 @@ class RancherViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearchMixin)
|
||||
def list(self, request, *args, **kwargs):
|
||||
""" list of ranchers """
|
||||
|
||||
search = self.filter_query(self.queryset.order_by('-modify_date')) # search & filter
|
||||
search = self.filter_query(self.get_queryset().order_by('-modify_date')) # search & filter
|
||||
page = self.paginate_queryset(search)
|
||||
if page is not None:
|
||||
if page is not None: # noqa
|
||||
serializer = self.get_serializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
@@ -158,7 +123,7 @@ class RancherViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearchMixin)
|
||||
|
||||
# paginate queryset
|
||||
page = self.paginate_queryset(queryset)
|
||||
if page is not None:
|
||||
if page is not None: # noqa
|
||||
serializer = HerdSerializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
@@ -175,6 +140,6 @@ class RancherViewSet(SoftDeleteMixin, viewsets.ModelViewSet, DynamicSearchMixin)
|
||||
rancher = self.get_object() # rancher object
|
||||
|
||||
page = self.paginate_queryset(rancher.plans.all())
|
||||
if page is not None:
|
||||
if page is not None: # noqa
|
||||
serializer = product_serializers.IncentivePlanRancherSerializer(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
|
||||
Reference in New Issue
Block a user