first base of project-changed apps: Herd-livestock-tag-log-elasticsearch-

This commit is contained in:
2025-05-24 15:01:55 +03:30
parent eab40af15d
commit 90a46e493c
129 changed files with 3844 additions and 187 deletions

View File

@@ -0,0 +1,87 @@
from apps.herd.web.api.v1.serializers import HerdSerializer
from apps.authentication.api.v1.api import UserViewSet
from rest_framework.exceptions import APIException
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
from django.db import transaction
from rest_framework import status
class HerdViewSet(viewsets.ModelViewSet):
""" Herd ViewSet """
queryset = Herd.objects.all()
serializer_class = HerdSerializer
@transaction.atomic
def create(self, request, *args, **kwargs):
""" create herd with user """
if 'user' in request.data.keys():
# create user if owner of herd is not exist
user = CustomOperations().custom_create(
request=request,
view=UserViewSet(),
data_key='user'
)
owner = user['id']
request.data.update({'owner': owner})
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
@action(
methods=['get'],
detail=False,
url_name='my_herds',
url_path='my_herds',
name='my_herds'
)
@transaction.atomic
def my_herds(self, request):
""" get current user herds """
serializer = self.serializer_class(self.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)

View File

@@ -0,0 +1,27 @@
from apps.authentication.api.v1.serializers.serializer import (
UserSerializer,
OrganizationSerializer,
ProvinceSerializer,
CitySerializer
)
from rest_framework import serializers
from apps.herd.models import Herd
class HerdSerializer(serializers.ModelSerializer):
""" Herd Serializer """
class Meta:
model = Herd
fields = '__all__'
def to_representation(self, instance):
""" Customize serializer output """
representation = super().to_representation(instance)
if isinstance(instance, Herd):
representation['owner'] = UserSerializer(instance.owner).data
representation['cooperative'] = OrganizationSerializer(instance.cooperative).data
representation['province'] = ProvinceSerializer(instance.province).data
representation['city'] = CitySerializer(instance.city).data
representation['contractor'] = OrganizationSerializer(instance.contractor).data
return representation

View File

@@ -0,0 +1,10 @@
from django.urls import path, include
from rest_framework import routers
from .api import HerdViewSet
router = routers.DefaultRouter()
router.register('herd', HerdViewSet, basename='herd')
urlpatterns = [
path('api/v1/', include(router.urls))
]