add new fields to rancher

This commit is contained in:
2025-08-19 09:03:41 +03:30
parent b64c28b6d1
commit 14cd349a7d
15 changed files with 459 additions and 82 deletions

View File

@@ -1,10 +1,27 @@
from rest_framework import serializers
from apps.product import models as product_models
from apps.pos_device.models import POSFreeProducts
from apps.authorization.api.v1 import serializers as authorize_serializers
from apps.authentication.api.v1.serializers.serializer import OrganizationSerializer, OrganizationTypeSerializer
class ProductCategorySerializer(serializers.ModelSerializer):
class POSFreeProductSerializer(serializers.ModelSerializer):
class Meta:
model = POSFreeProducts
fields = '__all__'
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['product'] = {
'name': instance.product.name,
'id': instance.product.id
}
return representation
class ProductCategorySerializer(serializers.ModelSerializer): # noqa
class Meta:
model = product_models.ProductCategory
fields = '__all__'

View File

@@ -1,8 +1,11 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .viewsets import product_api
router = DefaultRouter()
router.register(r'product', product_api.ProductViewSet, basename='product')
router.register(r'pos_free_products', product_api.POSFreeProductsViewSet, basename='pos_free_products')
urlpatterns = [
path('v1/', include(router.urls))
]
]

View File

@@ -1,16 +1,15 @@
import datetime
from apps.product.pos.api.v1.serializers import product_serializers as product_serializers
from apps.product.pos.api.v1.serializers import quota_serializers
from common.helpers import get_organization_by_user
from apps.pos_device.mixins.pos_device_mixin import POSDeviceMixin
from apps.core.mixins.search_mixin import DynamicSearchMixin
from rest_framework.exceptions import APIException
from apps.product import models as product_models
from apps.pos_device import models as pos_models
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework import viewsets, filters
from rest_framework import viewsets
from rest_framework import status
from django.db import transaction
from django.db.models import Q
from datetime import datetime
def trash(queryset, pk): # noqa
@@ -26,54 +25,15 @@ def delete(queryset, pk):
obj.delete()
class ProductCategoryViewSet(viewsets.ModelViewSet):
queryset = product_models.ProductCategory.objects.all()
serializer_class = product_serializers.ProductCategorySerializer
filter_backends = [filters.SearchFilter]
search_fields = ['type', 'name']
@action(
methods=['put'],
detail=True,
url_path='trash',
url_name='trash',
name='trash',
)
@transaction.atomic
def trash(self, request, pk=None):
""" Sent product to trash """
try:
trash(self.queryset, pk)
except APIException as e:
return Response(e, status.HTTP_204_NO_CONTENT)
@action(
methods=['post'],
detail=True,
url_name='delete',
url_path='delete',
name='delete'
)
@transaction.atomic
def delete(self, request, pk=None):
""" Full delete of product object """
try:
delete(self.queryset, pk)
return Response(status=status.HTTP_200_OK)
except APIException as e:
return Response(e, status=status.HTTP_204_NO_CONTENT)
class ProductViewSet(viewsets.ModelViewSet):
queryset = product_models.Product.objects.all()
class ProductViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDeviceMixin):
queryset = product_models.Product.objects.all() # noqa
serializer_class = product_serializers.ProductSerializer
filter_backends = [filters.SearchFilter]
permission_classes = [AllowAny]
search_fields = ['type', 'name']
def list(self, request, *args, **kwargs):
""" custom list view """ #
queryset = self.filter_queryset(self.get_queryset().order_by('-create_date')) # noqa
queryset = self.filter_query(self.get_queryset().order_by('-create_date')) # noqa
page = self.paginate_queryset(queryset)
if page is not None:
@@ -83,34 +43,6 @@ class ProductViewSet(viewsets.ModelViewSet):
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
@action(
methods=['get'],
detail=True,
url_path='related_quotas',
url_name='related_quotas',
name='related_quotas'
)
@transaction.atomic()
def my_related_quotas_by_product(self, request, pk=None):
""" quotas that related to my organization and product """
organization = get_organization_by_user(request.user)
quota = product_models.Quota.objects.filter(
Q(
distributions_assigned__in=product_models.QuotaDistribution.objects.filter(
Q(assigned_organization=organization) |
Q(assigner_organization=organization)
)
) |
Q(registerer_organization=organization),
product=self.get_object()
).distinct()
page = self.paginate_queryset(quota)
if page is not None:
serializer = quota_serializers.QuotaSerializer(page, many=True)
return self.get_paginated_response(serializer.data)
@action(
methods=['put'],
detail=True,
@@ -124,7 +56,7 @@ class ProductViewSet(viewsets.ModelViewSet):
try:
trash(self.queryset, pk)
except APIException as e:
return Response(e, status.HTTP_204_NO_CONTENT)
return Response(e, status.HTTP_204_NO_CONTENT)
@action(
methods=['post'],
@@ -142,3 +74,38 @@ class ProductViewSet(viewsets.ModelViewSet):
except APIException as e:
return Response(e, status=status.HTTP_204_NO_CONTENT)
class POSFreeProductsViewSet(viewsets.ModelViewSet, DynamicSearchMixin, POSDeviceMixin):
queryset = pos_models.POSFreeProducts.objects.all()
serializer_class = product_serializers.POSFreeProductSerializer
permission_classes = [AllowAny]
search_fields = ['product__name', 'organization__name', 'device__serial']
def list(self, request, *args, **kwargs):
device = self.get_pos_device()
queryset = self.filter_query(
self.get_queryset().filter(device=device).order_by('-create_date')
) # noqa
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
@transaction.atomic
def create(self, request, *args, **kwargs):
""" create my free product from pos & set details """
organization = self.get_device_organization()
device = self.get_pos_device()
request.data.update({'organization': organization.id, 'device': device.id})
serializer = product_serializers.POSFreeProductSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN)

View File

@@ -2,5 +2,6 @@ from django.urls import path, include
urlpatterns = [
path('web/api/', include('apps.product.web.api.v1.urls')),
path('pos/api/', include('apps.product.pos.api.v1.urls')),
path('excel/', include('apps.product.services.excel.urls')),
]