add product count
This commit is contained in:
@@ -4,10 +4,15 @@ from core.apps.products.models.folder import Folder, SubFolder
|
||||
|
||||
|
||||
class FolderSerializer(serializers.ModelSerializer):
|
||||
product_count = serializers.SerializerMethodField(method_name='get_product_count')
|
||||
|
||||
class Meta:
|
||||
model = Folder
|
||||
fields = ['id', 'name']
|
||||
extra_kwargs = {'id': {'read_only': True}}
|
||||
fields = ['id', 'name', 'product_count']
|
||||
extra_kwargs = {
|
||||
'id': {'read_only': True},
|
||||
'product_count': {'read_only': True}
|
||||
}
|
||||
|
||||
def create(self, validated_data):
|
||||
return Folder.objects.create(**validated_data)
|
||||
@@ -17,12 +22,21 @@ class FolderSerializer(serializers.ModelSerializer):
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
def get_product_count(self, obj):
|
||||
return obj.products.count()
|
||||
|
||||
|
||||
class SubFolderSerializer(serializers.ModelSerializer):
|
||||
product_count = serializers.SerializerMethodField(method_name='get_product_count')
|
||||
|
||||
class Meta:
|
||||
model = SubFolder
|
||||
fields = ['id', 'name', 'folder']
|
||||
extra_kwargs = {'id': {'read_only': True}, "folder": {"write_only": True}}
|
||||
extra_kwargs = {
|
||||
'id': {'read_only': True},
|
||||
"folder": {"write_only": True},
|
||||
'product_count': {'read_only': True},
|
||||
}
|
||||
|
||||
def create(self, validated_data):
|
||||
return SubFolder.objects.create(**validated_data)
|
||||
@@ -31,4 +45,7 @@ class SubFolderSerializer(serializers.ModelSerializer):
|
||||
instance.name = validated_data.get('name', instance.name)
|
||||
instance.folder = validated_data.get('folder', instance.folder)
|
||||
instance.save()
|
||||
return instance
|
||||
return instance
|
||||
|
||||
def get_product_count(self, obj):
|
||||
return obj.products.count()
|
||||
@@ -1,8 +1,11 @@
|
||||
from django.db import transaction
|
||||
|
||||
from rest_framework import serializers
|
||||
|
||||
from core.apps.wherehouse.models.wherehouse import WhereHouse
|
||||
from core.apps.wherehouse.serializers.inventory import WhereHouseInventoryListSerializer
|
||||
from core.apps.company.serializers.branch import BranchListSerializer
|
||||
from core.apps.company.models import Branch
|
||||
|
||||
|
||||
class WhereHouseListSerializer(serializers.ModelSerializer):
|
||||
@@ -17,11 +20,45 @@ class WhereHouseListSerializer(serializers.ModelSerializer):
|
||||
|
||||
class WhereHouseDetailSerializer(serializers.ModelSerializer):
|
||||
branch = BranchListSerializer()
|
||||
inventories = WhereHouseInventoryListSerializer(many=True)
|
||||
|
||||
class Meta:
|
||||
model = WhereHouse
|
||||
fields = [
|
||||
'id', 'name', 'address', 'branch', 'inventories'
|
||||
'id', 'name', 'address', 'branch',
|
||||
]
|
||||
|
||||
|
||||
class WhereHouseCreateSerializer(serializers.Serializer):
|
||||
name = serializers.CharField()
|
||||
address = serializers.CharField()
|
||||
branch_id = serializers.UUIDField()
|
||||
|
||||
def validate(self, data):
|
||||
branch = Branch.objects.filter(id=data.get('branch_id')).first()
|
||||
if not branch:
|
||||
raise serializers.ValidationError("Branch not found")
|
||||
data['branch'] = branch
|
||||
return data
|
||||
|
||||
def create(self, validated_data):
|
||||
with transaction.atomic():
|
||||
return WhereHouse.objects.create(
|
||||
name=validated_data.get('name'),
|
||||
address=validated_data.get('address'),
|
||||
branch=validated_data.get('branch'),
|
||||
)
|
||||
|
||||
|
||||
class WhereHouseUpdateSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = WhereHouse
|
||||
fields = [
|
||||
'name', 'address', 'branch'
|
||||
]
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
instance.name = validated_data.get('name', instance.name)
|
||||
instance.address = validated_data.get('address', instance.address)
|
||||
instance.branch = validated_data.get('branch', instance.branch)
|
||||
instance.save()
|
||||
return instance
|
||||
@@ -8,6 +8,9 @@ urlpatterns = [
|
||||
[
|
||||
path('list/', wherehouse_views.WhereHouseListApiView.as_view()),
|
||||
path('<uuid:id>/', wherehouse_views.WhereHouseDetailApiView.as_view()),
|
||||
path('create/', wherehouse_views.WhereHouseCreateApiView.as_view()),
|
||||
path('<uuid:id>/delete/', wherehouse_views.WhereHouseDeleteApiView.as_view()),
|
||||
path('<uuid:id>/update/', wherehouse_views.WhereHouseUpdateApiView.as_view()),
|
||||
]
|
||||
))
|
||||
)),
|
||||
]
|
||||
@@ -1,4 +1,6 @@
|
||||
from rest_framework import generics, status
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from rest_framework import generics, views
|
||||
from rest_framework.response import Response
|
||||
|
||||
from core.apps.wherehouse.models import WhereHouse, Inventory
|
||||
@@ -15,7 +17,35 @@ class WhereHouseListApiView(generics.ListAPIView):
|
||||
|
||||
class WhereHouseDetailApiView(generics.RetrieveAPIView):
|
||||
serializer_class = serializers.WhereHouseDetailSerializer
|
||||
queryset = WhereHouse.objects.select_related('branch').prefetch_related('inventories')
|
||||
queryset = WhereHouse.objects.select_related('branch')
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
lookup_field = 'id'
|
||||
|
||||
|
||||
class WhereHouseCreateApiView(generics.CreateAPIView):
|
||||
serializer_class = serializers.WhereHouseCreateSerializer
|
||||
queryset = WhereHouse.objects.all()
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
|
||||
|
||||
class WhereHouseDeleteApiView(views.APIView):
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
|
||||
def delete(self, request, id):
|
||||
wherehouse = get_object_or_404(WhereHouse, id=id)
|
||||
wherehouse.delete()
|
||||
return Response(
|
||||
{'success': True, 'message': 'Deleted!'},
|
||||
status=204
|
||||
)
|
||||
|
||||
|
||||
class WhereHouseUpdateApiView(generics.UpdateAPIView):
|
||||
serializer_class = serializers.WhereHouseUpdateSerializer
|
||||
queryset = WhereHouse.objects.all()
|
||||
lookup_field = 'id'
|
||||
permission_classes = [HasRolePermission]
|
||||
required_permissions = []
|
||||
Reference in New Issue
Block a user