diff --git a/apps/pos_device/migrations/0006_alter_posclientattribute_choices.py b/apps/pos_device/migrations/0006_alter_posclientattribute_choices.py new file mode 100644 index 0000000..b6ab7b5 --- /dev/null +++ b/apps/pos_device/migrations/0006_alter_posclientattribute_choices.py @@ -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), + ), + ] diff --git a/apps/pos_device/models.py b/apps/pos_device/models.py index 6d2d36b..607ae26 100644 --- a/apps/pos_device/models.py +++ b/apps/pos_device/models.py @@ -126,7 +126,7 @@ class POSClientAttribute(BaseModel): ('choice', 'چند کزینه ای'), # noqa ], null=True) 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): return f'attribute: {self.key}-{self.field_type}' diff --git a/apps/pos_device/web/api/v1/urls.py b/apps/pos_device/web/api/v1/urls.py index 682a1b7..2dea624 100644 --- a/apps/pos_device/web/api/v1/urls.py +++ b/apps/pos_device/web/api/v1/urls.py @@ -1,7 +1,12 @@ from django.urls import path, include from rest_framework.routers import DefaultRouter +from .viewsets import client as client_views +from .viewsets import device as device_views 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 = [ path('v1/pos/', include(router.urls)) diff --git a/apps/pos_device/web/api/v1/viewsets/client.py b/apps/pos_device/web/api/v1/viewsets/client.py new file mode 100644 index 0000000..37a2103 --- /dev/null +++ b/apps/pos_device/web/api/v1/viewsets/client.py @@ -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 diff --git a/apps/pos_device/web/api/v1/viewsets/api.py b/apps/pos_device/web/api/v1/viewsets/device.py similarity index 60% rename from apps/pos_device/web/api/v1/viewsets/api.py rename to apps/pos_device/web/api/v1/viewsets/device.py index 817208c..e5dee6e 100644 --- a/apps/pos_device/web/api/v1/viewsets/api.py +++ b/apps/pos_device/web/api/v1/viewsets/device.py @@ -1,14 +1,30 @@ 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 rest_framework import viewsets from rest_framework.response import Response +from common.tools import CustomOperations +from rest_framework import viewsets from rest_framework import status - class ProviderCompanyViewSet(viewsets.ModelViewSet): # noqa queryset = pos_models.ProviderCompany.objects.all() 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): queryset = pos_models.Device.objects.all() @@ -23,18 +39,3 @@ class DeviceVersionViewSet(viewsets.ModelViewSet): class SessionViewSet(viewsets.ModelViewSet): # noqa queryset = pos_models.Sessions.objects.all() 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 diff --git a/apps/product/web/api/v1/serializers/quota_serializers.py b/apps/product/web/api/v1/serializers/quota_serializers.py index 4f4303f..92b8922 100644 --- a/apps/product/web/api/v1/serializers/quota_serializers.py +++ b/apps/product/web/api/v1/serializers/quota_serializers.py @@ -82,7 +82,7 @@ class QuotaSerializer(serializers.ModelSerializer): ) # 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.add( *(validated_data.get('limit_by_organizations', instance.limit_by_organizations))