add new api
This commit is contained in:
@@ -3,9 +3,9 @@ from rest_framework import serializers
|
||||
from core.apps.wherehouse.models.inventory import Inventory
|
||||
|
||||
|
||||
class WhereHouseInventoryListSerializer(serializers.ModelSerializer):
|
||||
class InventoryListSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Inventory
|
||||
fields = [
|
||||
'id', 'quantity', 'product'
|
||||
]
|
||||
'id', 'quantity', 'product', 'price', 'unity'
|
||||
]
|
||||
|
||||
@@ -3,7 +3,7 @@ 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.wherehouse.serializers.inventory import InventoryListSerializer
|
||||
from core.apps.company.serializers.branch import BranchListSerializer
|
||||
from core.apps.company.models import Branch
|
||||
|
||||
@@ -39,7 +39,7 @@ class WhereHouseCreateSerializer(serializers.Serializer):
|
||||
raise serializers.ValidationError("Branch not found")
|
||||
data['branch'] = branch
|
||||
return data
|
||||
|
||||
|
||||
def create(self, validated_data):
|
||||
with transaction.atomic():
|
||||
return WhereHouse.objects.create(
|
||||
@@ -55,10 +55,10 @@ class WhereHouseUpdateSerializer(serializers.ModelSerializer):
|
||||
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
|
||||
return instance
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from django.urls import path, include
|
||||
|
||||
from core.apps.wherehouse.views import wherehouse as wherehouse_views
|
||||
from core.apps.wherehouse.views import inventory as inventory_views
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
@@ -13,4 +14,9 @@ urlpatterns = [
|
||||
path('<uuid:id>/update/', wherehouse_views.WhereHouseUpdateApiView.as_view()),
|
||||
]
|
||||
)),
|
||||
]
|
||||
path('inventory/', include(
|
||||
[
|
||||
path('<uuid:wherehouse_id>/list/', inventory_views.InventoryListApiView.as_view()),
|
||||
]
|
||||
)),
|
||||
]
|
||||
|
||||
25
core/apps/wherehouse/views/inventory.py
Normal file
25
core/apps/wherehouse/views/inventory.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from rest_framework import generics, views
|
||||
from rest_framework.response import Response
|
||||
|
||||
from core.apps.wherehouse.serializers import inventory as serializers
|
||||
from core.apps.wherehouse.models import WhereHouse, Inventory
|
||||
from core.apps.accounts.permissions.permissions import HasRolePermission
|
||||
|
||||
|
||||
class InventoryListApiView(generics.GenericAPIView):
|
||||
serializer_class = serializers.InventoryListSerializer
|
||||
queryset = Inventory.objects.all()
|
||||
permissions_class = [HasRolePermission]
|
||||
required_permissions = ['wherehouse']
|
||||
|
||||
def get(self, request, wherehouse_id):
|
||||
wherehouse = get_object_or_404(WhereHouse, id=wherehouse_id)
|
||||
inventories = Inventory.objects.filter(wherehouse=wherehouse)
|
||||
page = self.paginate_queryset(inventories)
|
||||
if page is not None:
|
||||
serializer = self.serializer_class(page, many=True)
|
||||
return self.get_paginated_response(serializer.data)
|
||||
serializer = self.serializer_class(inventories, many=True)
|
||||
return Response(serializer.data, status=200)
|
||||
Reference in New Issue
Block a user