device assignment bug-assignment information-bug of organizations list for assignment
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
from rest_framework.serializers import ModelSerializer
|
||||
from apps.pos_device import models as pos_models
|
||||
from rest_framework.exceptions import APIException
|
||||
from rest_framework import status
|
||||
|
||||
|
||||
class POSClientSerializer(ModelSerializer):
|
||||
@@ -7,6 +9,26 @@ class POSClientSerializer(ModelSerializer):
|
||||
model = pos_models.POSClient
|
||||
fields = '__all__'
|
||||
|
||||
def validate(self, attrs):
|
||||
org = attrs['organization']
|
||||
|
||||
# check for duplicate organization client
|
||||
if org:
|
||||
if self.Meta.model.objects.filter(organization=org).exists():
|
||||
raise APIException("قبلا کلاینت با این سازمان ثبت شده است", code=status.HTTP_403_FORBIDDEN) # noqa
|
||||
|
||||
return attrs
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
|
||||
representation['organization'] = {
|
||||
'name': instance.organization.name,
|
||||
'id': instance.organization.id
|
||||
}
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class POSClientAttributeSerializer(ModelSerializer):
|
||||
class Meta:
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
from apps.pos_device.web.api.v1.serilaizers import client as client_serializer
|
||||
from rest_framework.serializers import ModelSerializer
|
||||
from apps.pos_device import models as pos_models
|
||||
from rest_framework.exceptions import APIException
|
||||
from apps.pos_device import exceptions as pos_exceptions
|
||||
from rest_framework import status
|
||||
|
||||
|
||||
class ProviderCompanySerializer(ModelSerializer):
|
||||
@@ -23,6 +27,10 @@ class DeviceSerializer(ModelSerializer):
|
||||
'province': instance.organization.province.id
|
||||
}
|
||||
|
||||
representation['assignment'] = DeviceAssignmentSerializer(
|
||||
instance.assignment.all().first()
|
||||
).data
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
@@ -43,6 +51,23 @@ class DeviceAssignmentSerializer(ModelSerializer):
|
||||
model = pos_models.DeviceAssignment
|
||||
fields = '__all__'
|
||||
|
||||
def validate(self, attrs):
|
||||
|
||||
device = attrs['device']
|
||||
client = attrs['client']
|
||||
|
||||
if self.Meta.model.objects.filter(device=device, client=client).exists():
|
||||
raise pos_exceptions.DeviceAlreadyAssigned()
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
|
||||
representation['client'] = client_serializer.POSClientSerializer(
|
||||
instance.client
|
||||
).data
|
||||
|
||||
return representation
|
||||
|
||||
|
||||
class StakeHoldersSerializer(ModelSerializer):
|
||||
class Meta:
|
||||
|
||||
@@ -164,15 +164,8 @@ class DeviceAssignmentViewSet(viewsets.ModelViewSet):
|
||||
queryset = pos_models.DeviceAssignment.objects.all()
|
||||
serializer_class = device_serializer.DeviceAssignmentSerializer
|
||||
|
||||
@action(
|
||||
methods=['post'],
|
||||
detail=False,
|
||||
url_path='assignment',
|
||||
url_name='assignment',
|
||||
name='assignment',
|
||||
)
|
||||
@transaction.atomic
|
||||
def device_assignment(self, request):
|
||||
def create(self, request, *args, **kwargs):
|
||||
""" assign pos device to client by company """
|
||||
|
||||
if 'organization' not in request.data.keys():
|
||||
@@ -180,12 +173,22 @@ class DeviceAssignmentViewSet(viewsets.ModelViewSet):
|
||||
request.data.update({'organization': organization.id})
|
||||
|
||||
if 'client_data' in request.data.keys():
|
||||
client = CustomOperations().custom_create(
|
||||
request=request,
|
||||
view=POSClientViewSet(),
|
||||
data=request.data['client_data']
|
||||
)
|
||||
request.data.update({'client': client['id']})
|
||||
|
||||
# if client will be an organization
|
||||
if request.data['client_data']['is_organization']:
|
||||
client = pos_models.POSClient.objects.filter(
|
||||
organization_id=request.data['client_data']['organization']
|
||||
)
|
||||
|
||||
if client.exists():
|
||||
request.data.update({'client': client.first().id})
|
||||
else:
|
||||
client = CustomOperations().custom_create(
|
||||
request=request,
|
||||
view=POSClientViewSet(),
|
||||
data=request.data['client_data']
|
||||
)
|
||||
request.data.update({'client': client['id']})
|
||||
|
||||
# create assignment
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
@@ -193,8 +196,9 @@ class DeviceAssignmentViewSet(viewsets.ModelViewSet):
|
||||
assignment = serializer.save()
|
||||
|
||||
# set organization having pos status
|
||||
assignment.organization.has_pos = True
|
||||
assignment.organization.save()
|
||||
if assignment.client.organization:
|
||||
assignment.client.organization.has_pos = True
|
||||
assignment.client.organization.save()
|
||||
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
Reference in New Issue
Block a user