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,166 @@
from rest_framework import viewsets
from apps.livestock import models as livestock_models
from . import serializers as livestock_serializers
from rest_framework.exceptions import APIException
from rest_framework.decorators import action
from rest_framework.response import Response
from django.db import transaction
from rest_framework import status
def trash(queryset, pk):
""" sent object to trash """
obj = queryset.get(id=pk)
obj.trash = True
obj.save()
def delete(queryset, pk):
""" full delete object """
obj = queryset.get(id=pk)
obj.delete()
class LiveStockViewSet(viewsets.ModelViewSet):
queryset = livestock_models.LiveStock.objects.all()
serializer_class = livestock_serializers.LiveStockSerializer
@action(
methods=['put'],
detail=True,
url_path='trash',
url_name='trash',
name='trash',
)
@transaction.atomic
def trash(self, request, pk=None):
""" Sent livestock to trash """
try:
trash(self.queryset, pk)
except APIException as e:
return Response(e, status.HTTP_204_NO_CONTENT)
@action(
methods=['post'],
detail=True,
url_name='delete',
url_path='delete',
name='delete'
)
@transaction.atomic
def delete(self, request, pk=None):
""" Full delete of livestock object """
try:
delete(self.queryset, pk)
return Response(status=status.HTTP_200_OK)
except APIException as e:
return Response(e, status=status.HTTP_204_NO_CONTENT)
class LiveStockTypeViewSet(viewsets.ModelViewSet):
queryset = livestock_models.LiveStockType.objects.all()
serializer_class = livestock_serializers.LiveStockTypeSerializer
@action(
methods=['put'],
detail=True,
url_path='trash',
url_name='trash',
name='trash',
)
@transaction.atomic
def trash(self, request, pk=None):
""" Sent livestock type to trash """
try:
trash(self.queryset, pk)
except APIException as e:
return Response(e, status.HTTP_204_NO_CONTENT)
@action(
methods=['post'],
detail=True,
url_name='delete',
url_path='delete',
name='delete'
)
@transaction.atomic
def delete(self, request, pk=None):
""" Full delete of livestock type object """
try:
delete(self.queryset, pk)
return Response(status=status.HTTP_200_OK)
except APIException as e:
return Response(e, status=status.HTTP_204_NO_CONTENT)
class LiveStockUseTypeViewSet(viewsets.ModelViewSet):
queryset = livestock_models.LiveStockUseType.objects.all()
serializer_class = livestock_serializers.LiveStockUseTypeSerializer
@action(
methods=['put'],
detail=True,
url_path='trash',
url_name='trash',
name='trash',
)
@transaction.atomic
def trash(self, request, pk=None):
""" Sent livestock use type to trash """
try:
trash(self.queryset, pk)
except APIException as e:
return Response(e, status.HTTP_204_NO_CONTENT)
@action(
methods=['post'],
detail=True,
url_name='delete',
url_path='delete',
name='delete'
)
@transaction.atomic
def delete(self, request, pk=None):
""" Full delete of livestock use type object """
try:
delete(self.queryset, pk)
return Response(status=status.HTTP_200_OK)
except APIException as e:
return Response(e, status=status.HTTP_204_NO_CONTENT)
class LiveStockSpeciesViewSet(viewsets.ModelViewSet):
queryset = livestock_models.LiveStockSpecies.objects.all()
serializer_class = livestock_serializers.LiveStockSpeciesSerializer
@action(
methods=['put'],
detail=True,
url_path='trash',
url_name='trash',
name='trash',
)
@transaction.atomic
def trash(self, request, pk=None):
""" Sent species to trash """
try:
trash(self.queryset, pk)
return Response(status=status.HTTP_200_OK)
except APIException as e:
return Response
@action(
methods=['post'],
detail=True,
url_name='delete',
url_path='delete',
name='delete'
)
@transaction.atomic
def delete(self, request, pk=None):
""" Full delete of species object """
try:
delete(self.queryset, pk)
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,62 @@
from rest_framework import serializers
from apps.livestock import models as livestock_models
from apps.herd.web.api.v1.serializers import HerdSerializer
from apps.tag.web.api.v1.serializers import TagSerializer
class LiveStockTypeSerializer(serializers.ModelSerializer):
class Meta:
model = livestock_models.LiveStockType
fields = [
'id',
'name'
]
class LiveStockUseTypeSerializer(serializers.ModelSerializer):
class Meta:
model = livestock_models.LiveStockUseType
fields = [
'id',
'name'
]
class LiveStockSpeciesSerializer(serializers.ModelSerializer):
class Meta:
model = livestock_models.LiveStockSpecies
fields = [
'id',
'name'
]
class LiveStockSerializer(serializers.ModelSerializer):
""" livestock serializer """
class Meta:
model = livestock_models.LiveStock
fields = [
'id',
'herd',
'tag',
'type',
'use_type',
'weight_type',
'species',
'birthdate',
'gender',
]
depth = 1
def to_representation(self, instance):
""" Customize output of serializer """
representation = super().to_representation(instance)
if isinstance(instance, livestock_models.LiveStock):
representation['herd'] = HerdSerializer(instance.herd).data
representation['tag'] = TagSerializer(instance.tag).data
representation['type'] = LiveStockTypeSerializer(instance.type).data
representation['use_type'] = LiveStockUseTypeSerializer(instance.use_type).data
representation['species'] = LiveStockSpeciesSerializer(instance.species).data
return representation

View File

@@ -0,0 +1,18 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .api import (
LiveStockViewSet,
LiveStockTypeViewSet,
LiveStockSpeciesViewSet,
LiveStockUseTypeViewSet
)
router = DefaultRouter()
router.register(r'livestock', LiveStockViewSet, basename='livestock')
router.register(r'livestock_type', LiveStockTypeViewSet, basename='livestock_type')
router.register(r'livestock_use_type', LiveStockUseTypeViewSet, basename='livestock_use_type')
router.register(r'livestock_species', LiveStockSpeciesViewSet, basename='livestock_species')
urlpatterns = [
path('v1/', include(router.urls))
]