pos client & provider - fix org limit bug in quota
This commit is contained in:
@@ -0,0 +1,19 @@
|
|||||||
|
# Generated by Django 5.0 on 2025-07-23 11:50
|
||||||
|
|
||||||
|
import django.contrib.postgres.fields
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('pos_device', '0005_posclientattribute_choices_and_more'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='posclientattribute',
|
||||||
|
name='choices',
|
||||||
|
field=django.contrib.postgres.fields.ArrayField(base_field=models.CharField(max_length=150), blank=True, null=True, size=None),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -126,7 +126,7 @@ class POSClientAttribute(BaseModel):
|
|||||||
('choice', 'چند کزینه ای'), # noqa
|
('choice', 'چند کزینه ای'), # noqa
|
||||||
], null=True)
|
], null=True)
|
||||||
required = models.BooleanField(default=False)
|
required = models.BooleanField(default=False)
|
||||||
choices = ArrayField(base_field=models.CharField(max_length=150), null=True)
|
choices = ArrayField(base_field=models.CharField(max_length=150), null=True, blank=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'attribute: {self.key}-{self.field_type}'
|
return f'attribute: {self.key}-{self.field_type}'
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from rest_framework.routers import DefaultRouter
|
from rest_framework.routers import DefaultRouter
|
||||||
|
from .viewsets import client as client_views
|
||||||
|
from .viewsets import device as device_views
|
||||||
|
|
||||||
router = DefaultRouter()
|
router = DefaultRouter()
|
||||||
|
router.register(r'client', client_views.POSClientViewSet, basename='client')
|
||||||
|
router.register(r'attributes', client_views.POSClientAttributeViewSet, basename='attributes')
|
||||||
|
router.register(r'provider', device_views.ProviderCompanyViewSet, basename='provider')
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('v1/pos/', include(router.urls))
|
path('v1/pos/', include(router.urls))
|
||||||
|
|||||||
69
apps/pos_device/web/api/v1/viewsets/client.py
Normal file
69
apps/pos_device/web/api/v1/viewsets/client.py
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
from apps.pos_device.web.api.v1.serilaizers import serializers as pos_serializer
|
||||||
|
from apps.pos_device import models as pos_models
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from common.tools import CustomOperations
|
||||||
|
from rest_framework import viewsets
|
||||||
|
from rest_framework import status
|
||||||
|
|
||||||
|
|
||||||
|
class POSClientViewSet(viewsets.ModelViewSet):
|
||||||
|
queryset = pos_models.POSClient.objects.all()
|
||||||
|
serializer_class = pos_serializer.POSClientSerializer
|
||||||
|
|
||||||
|
def create(self, request, *args, **kwargs):
|
||||||
|
""" Custom create of pos client """
|
||||||
|
|
||||||
|
serializer = self.serializer_class(data=request.data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
client = serializer.save()
|
||||||
|
|
||||||
|
# create attributes with value for client
|
||||||
|
attr_values_list = []
|
||||||
|
if 'attribute_values' in request.data.keys():
|
||||||
|
for attr in request.data['attribute_values']:
|
||||||
|
attr.update({'client': client.id})
|
||||||
|
attr_value = CustomOperations().custom_create(
|
||||||
|
request=request,
|
||||||
|
view=POSClientAttributeValueViewSet(),
|
||||||
|
data=attr
|
||||||
|
)
|
||||||
|
attr_values_list.append(attr_value)
|
||||||
|
|
||||||
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
|
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
|
def update(self, request, pk=None, *args, **kwargs):
|
||||||
|
""" Custom update of pos client """
|
||||||
|
|
||||||
|
serializer = self.serializer_class(instance=self.get_object(), data=request.data)
|
||||||
|
if serializer.is_valid():
|
||||||
|
client = serializer.save()
|
||||||
|
|
||||||
|
if 'attribute_values' in request.data.keys():
|
||||||
|
|
||||||
|
# remove attribute relations from client
|
||||||
|
client.attribute_values.all().delete()
|
||||||
|
|
||||||
|
# create new relations
|
||||||
|
attr_values_list = []
|
||||||
|
for attr in request.data['attribute_values']:
|
||||||
|
attr.update({'client': client.id})
|
||||||
|
attr_value = CustomOperations().custom_create(
|
||||||
|
request=request,
|
||||||
|
view=POSClientAttributeValueViewSet,
|
||||||
|
data=attr
|
||||||
|
)
|
||||||
|
attr_values_list.append(attr_value)
|
||||||
|
|
||||||
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||||
|
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
|
|
||||||
|
class POSClientAttributeViewSet(viewsets.ModelViewSet):
|
||||||
|
queryset = pos_models.POSClientAttribute.objects.all()
|
||||||
|
serializer_class = pos_serializer.POSClientAttributeSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class POSClientAttributeValueViewSet(viewsets.ModelViewSet):
|
||||||
|
queryset = pos_models.POSClientAttributeValue.objects.all()
|
||||||
|
serializer_class = pos_serializer.POSClientAttributeValueSerializer
|
||||||
@@ -1,14 +1,30 @@
|
|||||||
from apps.pos_device.web.api.v1.serilaizers import serializers as pos_serializer
|
from apps.pos_device.web.api.v1.serilaizers import serializers as pos_serializer
|
||||||
|
from apps.authentication.api.v1.api import UserViewSet
|
||||||
|
from rest_framework.exceptions import APIException
|
||||||
from apps.pos_device import models as pos_models
|
from apps.pos_device import models as pos_models
|
||||||
from rest_framework import viewsets
|
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
from common.tools import CustomOperations
|
||||||
|
from rest_framework import viewsets
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
|
||||||
|
|
||||||
class ProviderCompanyViewSet(viewsets.ModelViewSet): # noqa
|
class ProviderCompanyViewSet(viewsets.ModelViewSet): # noqa
|
||||||
queryset = pos_models.ProviderCompany.objects.all()
|
queryset = pos_models.ProviderCompany.objects.all()
|
||||||
serializer_class = pos_serializer.ProviderCompanySerializer
|
serializer_class = pos_serializer.ProviderCompanySerializer
|
||||||
|
|
||||||
|
def create(self, request, *args, **kwargs):
|
||||||
|
""" custom create of provider client """
|
||||||
|
|
||||||
|
try:
|
||||||
|
client = CustomOperations().custom_create(
|
||||||
|
request=request,
|
||||||
|
view=UserViewSet(),
|
||||||
|
data=request.data
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
raise APIException(detail="data is invalid", code=403)
|
||||||
|
|
||||||
|
return Response(client, status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
|
||||||
class DeviceViewSet(viewsets.ModelViewSet):
|
class DeviceViewSet(viewsets.ModelViewSet):
|
||||||
queryset = pos_models.Device.objects.all()
|
queryset = pos_models.Device.objects.all()
|
||||||
@@ -23,18 +39,3 @@ class DeviceVersionViewSet(viewsets.ModelViewSet):
|
|||||||
class SessionViewSet(viewsets.ModelViewSet): # noqa
|
class SessionViewSet(viewsets.ModelViewSet): # noqa
|
||||||
queryset = pos_models.Sessions.objects.all()
|
queryset = pos_models.Sessions.objects.all()
|
||||||
serializer_class = pos_serializer.SessionSerializer
|
serializer_class = pos_serializer.SessionSerializer
|
||||||
|
|
||||||
|
|
||||||
class POSClientViewSet(viewsets.ModelViewSet):
|
|
||||||
queryset = pos_models.POSClient.objects.all()
|
|
||||||
serializer_class = pos_serializer.POSClientSerializer
|
|
||||||
|
|
||||||
|
|
||||||
class POSClientAttributeViewSet(viewsets.ModelViewSet):
|
|
||||||
queryset = pos_models.POSClientAttribute.objects.all()
|
|
||||||
serializer_class = pos_serializer.POSClientAttributeSerializer
|
|
||||||
|
|
||||||
|
|
||||||
class POSClientAttributeValueViewSet(viewsets.ModelViewSet):
|
|
||||||
queryset = pos_models.POSClientAttributeValue.objects.all()
|
|
||||||
serializer_class = pos_serializer.POSClientAttributeValueSerializer
|
|
||||||
@@ -82,7 +82,7 @@ class QuotaSerializer(serializers.ModelSerializer):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# update organization limit many to many
|
# update organization limit many to many
|
||||||
if 'assigned_organizations' in validated_data.keys():
|
if 'limit_by_organizations' in validated_data.keys():
|
||||||
instance.limit_by_organizations.clear()
|
instance.limit_by_organizations.clear()
|
||||||
instance.limit_by_organizations.add(
|
instance.limit_by_organizations.add(
|
||||||
*(validated_data.get('limit_by_organizations', instance.limit_by_organizations))
|
*(validated_data.get('limit_by_organizations', instance.limit_by_organizations))
|
||||||
|
|||||||
Reference in New Issue
Block a user