Tag Assignment - organization OTP

This commit is contained in:
2025-05-28 14:56:59 +03:30
parent 44d3960f94
commit c3c13ef21e
5 changed files with 149 additions and 3 deletions

View File

@@ -6,6 +6,7 @@ from apps.core.models import BaseModel
from crum import get_current_user
from django.db import models
from jdatetime import datetime
import random
class Tag(BaseModel):
@@ -85,6 +86,7 @@ class TagAssignment(BaseModel):
)[3] + str(
datetime.now().month
)
self.serial_random_part = random.randint(1000, 9999)
if not self.serial:
self.serial = f"" \
f"{self.serial_sender_part}" \

View File

@@ -12,6 +12,7 @@ from django.db import transaction
from rest_framework.exceptions import APIException
from apps.tag import permissions as tag_permissions
from apps.authorization import models as authorize_models
from apps.authentication.api.v1.api import GeneralOTPViewSet
from apps.tag.tools import tag_code_serial_scanning
from apps.tag import exceptions as tag_exceptions
from common.helpers import detect_file_extension
@@ -110,6 +111,7 @@ class TagViewSet(viewsets.ModelViewSet):
class TagAssignmentViewSet(viewsets.ModelViewSet):
""" assignment of tags """
queryset = tag_models.TagAssignment.objects.all()
user_relations_queryset = authorize_models.UserRelations.objects.all()
serializer_class = TagAssignmentSerializer
@transaction.atomic
@@ -120,6 +122,12 @@ class TagAssignmentViewSet(viewsets.ModelViewSet):
if serializer.is_valid(raise_exception=True):
tag_assignment = serializer.save()
# get assigner organization code
assigner_organization_code = self.user_relations_queryset.get(
user=request.user).organization.type.code # noqa
tag_assignment.serial_sender_part = assigner_organization_code # noqa
tag_assignment.save()
# get tags by species number like: 2 tags of species code 4
tags_to_allocate = request.data['allocated_tags']
for tag in tags_to_allocate:
@@ -140,7 +148,7 @@ class TagAssignmentViewSet(viewsets.ModelViewSet):
status='W',
species_code=tag['species_code']
).save() # noqa
tag_to_allocate.status = 'W' # change tag status from free to waiting
tag_to_allocate.status = ' ' # change tag status from free to waiting
tag_to_allocate.save()
return Response(serializer.data, status.HTTP_201_CREATED)
@@ -207,6 +215,29 @@ class TagAssignmentViewSet(viewsets.ModelViewSet):
allocated_tag.delete()
return Response(status.HTTP_200_OK)
@action(
methods=['post'],
detail=False,
url_path='otp_verification',
url_name='otp_verification',
name='otp_verification'
)
def otp_verification(self, request):
""" OTP verification for organization """
if request.data['type'] == 'send':
otp_response = GeneralOTPViewSet().send_otp(request)
if otp_response.status_code == 200:
return Response(otp_response.status_code, status=status.HTTP_200_OK)
else:
return Response(otp_response.status_code, status=status.HTTP_403_FORBIDDEN)
if request.data['type'] == 'check':
check_response = GeneralOTPViewSet().check_otp(request)
if check_response.status_code == 200:
return Response(check_response.status_code, status=status.HTTP_200_OK)
else:
return Response(check_response.status_code, status=status.HTTP_403_FORBIDDEN)
@action(
methods=['put'],
detail=True,