some parts of product - fix custom pagination - add id to pages list

This commit is contained in:
2025-06-07 09:18:27 +03:30
parent a6cd093665
commit 627acf05a1
41 changed files with 371 additions and 4 deletions

View File

View File

View File

@@ -0,0 +1,93 @@
from apps.product.web.api.v1 import serializers as product_serializers
from rest_framework.exceptions import APIException
from apps.product import models as product_models
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework import viewsets
from rest_framework import status
from django.db import transaction
def trash(queryset, pk):
""" sent object to trash """
obj = queryset.get(id=pk)
obj.trash = True
obj.save()
def delete(queryset, pk):
""" full delete object """
obj = queryset.get(id=pk)
obj.delete()
class ProductViewSet(viewsets.ModelViewSet):
queryset = product_models.Product.objects.all()
serializer_class = product_serializers.ProductSerializer
@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 ReferenceProductViewSet(viewsets.ModelViewSet):
queryset = product_models.ReferenceProduct.objects.all()
serializer_class = product_serializers.ReferenceProductSerializer
@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)

View File

@@ -0,0 +1,27 @@
from rest_framework import serializers
from apps.product import models as product_models
class ReferenceProductSerializer(serializers.ModelSerializer):
""" Serializer of reference product """
class Meta:
model = product_models.ReferenceProduct
fields = '__all__'
class ProductSerializer(serializers.ModelSerializer):
""" Serializer of product """
class Meta:
model = product_models.Product
fields = '__all__'
def to_representation(self, instance):
""" Custom output of product serializer """
representation = super().to_representation(instance)
if instance.reference:
representation['reference'] = ReferenceProductSerializer(instance.reference).data
return representation

View File

@@ -0,0 +1,11 @@
from apps.product.web.api.v1 import api as api_views
from rest_framework.routers import DefaultRouter
from django.urls import path, include
router = DefaultRouter()
router.register(r'product', api_views.ProductViewSet, basename='product')
router.register(r'reference', api_views.ReferenceProductViewSet, basename='reference')
urlpatterns = [
path('v1/', include(router.urls))
]

View File