def import_seminations_from_farm(self, request): serializer = serializers.ImportSeminationsFile(data=request.data) if serializer.is_valid(): wb = import_farm.init_wb(serializer.validated_data['file']) rows = import_farm.get_semenation_rows(wb) import_farm.is_there_single_tour_in_file(rows=rows) seminated_list, already_seminated_in_tour, sows_in_another_tour, proholost_list = \ import_farm.create_semination_lists(rows, request.user) return Response( { "seminated_list_count": len(seminated_list), "seminated_list_farm_ids": [sow.farm_id for sow in seminated_list], "already_seminated_in_tour_count": len(already_seminated_in_tour), "already_seminated_in_tour_farm_ids": [sow.farm_id for sow in already_seminated_in_tour], "proholost_list": len(proholost_list), # "sows_in_another_tour_farm_ids": [sow.farm_id for sow in sows_in_another_tour], "sows_in_another_tour": sows_serializers.SowSerializer(sows_in_another_tour, \ many=True).data, "sows_in_another_tour_count": len(sows_in_another_tour), "message": "Файл загружен и обработан." }, status=status.HTTP_200_OK) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def sow_farrow(self, request, pk=None): serializer = sows_events_serializers.CreateSowFarrowSerializer( data=request.data) if serializer.is_valid(): sow = self.get_object() farrow = sows_events_models.SowFarrow.objects.create_sow_farrow( sow=sow, alive_quantity=serializer.validated_data['alive_quantity'], dead_quantity=serializer.validated_data['dead_quantity'], mummy_quantity=serializer.validated_data['mummy_quantity'], initiator=request.user, date=serializer.validated_data['date']) return Response( { "sow": sows_serializers.SowSerializer(sow).data, "message": 'Свинья успешно опоросилась.', "farrow": sows_events_serializers.SowFarrowSerializer(farrow).data }, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def abortion(self, request, pk=None): sow = self.get_object() abortion = sows_events_models.AbortionSow.objects.create_abortion( sow=sow, initiator=request.user, date=timezone.now()) return Response( { "abortion": sows_events_serializers.AbortionSowSerializer(abortion).data, "sow": sows_serializers.SowSerializer(sow).data, "message": f'Аборт у свиньи №{sow.farm_id}.' }, status=status.HTTP_200_OK)
def assing_farm_id(self, request, pk=None): sow = self.get_object() serializer = serializers.CreateFarmIdSerializer(data=request.data) # initiator = request.user.workshopemployee if serializer.is_valid(): sow.assing_farm_id(serializer.validated_data['farm_id']) return Response( { "sow": sows_serializers.SowSerializer(sow).data, "message": f'Свиноматка {sow.farm_id} помечена.', }, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def move_to(self, request, pk=None): sow = self.get_object() serializer = locations_serializers.LocationPKSerializer(data=request.data) if serializer.is_valid(): transaction = transactions_models.SowTransaction.objects.create_transaction( sow, serializer.validated_data['location'], request.user) return Response( { "transaction": transactions_serializers.SowTransactionSerializer(transaction).data, "sow": sows_serializers.SowSerializer(sow).data, "message": f'Свиноматка {sow.farm_id} переведена' }, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def mark_as_nurse(self, request, pk=None): serializer = serializers.MarkSowAsNurseSerializer(data=request.data) if serializer.is_valid(): sow = self.get_object() sow.markasnurse_set.create_nurse_event(sow=sow, initiator=request.user, date=timezone.now()) sow.refresh_from_db() return Response( { "sow": sows_serializers.SowSerializer(sow).data, "message": 'Свинья помечена как кормилица.', }, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def culling(self, request, pk=None): sow = self.get_object() serializer = sows_events_serializers.CreateCullingSowPkSerializer(data=request.data) if serializer.is_valid(): culling = sows_events_models.CullingSow.objects.create_culling( sow=sow, culling_type=serializer.validated_data['culling_type'], reason=serializer.validated_data['reason'], weight=serializer.validated_data['weight'], initiator=request.user, date=timezone.now()) return Response( { "culling": sows_events_serializers.CullingSowSerializer(culling).data, "sow": sows_serializers.SowSerializer(sow).data, "message": f'Свиноматка {sow.farm_id} выбракована' }, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def double_semination(self, request): serializer = sows_events_serializers.CreateDoubleSeminationSerializer( data=request.data) if serializer.is_valid(): sow = sows_models.Sow.objects.get_queryset_with_not_alive() \ .filter(farm_id=serializer.validated_data['farm_id']).first() sow.prepare_for_double_semenation() semination1 = sows_events_models.Semination.objects.create_semination( sow=sow, week=serializer.validated_data['week'], initiator=request.user, semination_employee=serializer.validated_data['seminator1'], boar=serializer.validated_data['boar1']) semination2 = sows_events_models.Semination.objects.create_semination( sow=sow, week=serializer.validated_data['week'], initiator=request.user, semination_employee=serializer.validated_data['seminator2'], boar=serializer.validated_data['boar2']) return Response( { "semination1": sows_events_serializers.SeminationSerializer( semination1).data, "semination2": sows_events_serializers.SeminationSerializer( semination2).data, "sow": sows_serializers.SowSerializer(sow).data, "message": "Свиноматка {} осеменена 2 раза".format(sow.farm_id) }, status=status.HTTP_200_OK) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)