diff --git a/apps/product/web/api/v1/viewsets/quota_distribution_api.py b/apps/product/web/api/v1/viewsets/quota_distribution_api.py index 6216736..4aa4fa6 100644 --- a/apps/product/web/api/v1/viewsets/quota_distribution_api.py +++ b/apps/product/web/api/v1/viewsets/quota_distribution_api.py @@ -86,8 +86,7 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet): # paginate queryset page = self.paginate_queryset( self.queryset.filter( - Q(assigned_organization=organization) | - Q(assigner_organization=organization) + Q(assigned_organization=organization) ).order_by('-modify_date') ) if page is not None: diff --git a/apps/warehouse/web/api/v1/api.py b/apps/warehouse/web/api/v1/api.py index a64f4fc..70a6abb 100644 --- a/apps/warehouse/web/api/v1/api.py +++ b/apps/warehouse/web/api/v1/api.py @@ -1,11 +1,14 @@ from apps.warehouse.web.api.v1 import serializers as warehouse_serializers from apps.warehouse import models as warehouse_models +from common.helpers import get_organization_by_user +from common.generics import base64_to_image_file from common.liara_tools import upload_to_liara from rest_framework.decorators import action from rest_framework.response import Response from rest_framework import viewsets, filters from django.db import transaction from rest_framework import status +import typing class InventoryEntryViewSet(viewsets.ModelViewSet): @@ -14,24 +17,78 @@ class InventoryEntryViewSet(viewsets.ModelViewSet): filter_backends = [filters.SearchFilter] search_fields = [''] - @action( - methods=['get'], - detail=True, - url_path='confirmation_document', - url_name='confirmation_document', - name='confirmation_document' - ) - def confirmation_document(self, request, pk=None): + def upload_confirmation_document(self, request, inventory: int) -> typing.Any: """ upload document for inventory entry confirmation """ - inventory = self.get_object() + inventory = self.queryset.get(id=inventory) + + # convert base64 to document file + document = base64_to_image_file( + request.data['document'], + filename=f'inventory_entry_document_{inventory.id}' + ) + file, file_format = document[0], document[1] # upload document to liara - document = request.FILES.get('document') document_url = upload_to_liara( - document, - f'inventory_entry_document_{inventory.di}' + file, + f'inventory_entry_document_{inventory.id}.{file_format}' ) + inventory.document = document_url + inventory.is_confirmed = True + inventory.save() + return Response(status=status.HTTP_200_OK) + + @transaction.atomic + def create(self, request, *args, **kwargs): + """ custom create of inventory entry """ + + # create inventory entry + request.data.update({ + 'organization': (get_organization_by_user(request.user)).id + }) + serializer = self.serializer_class(data=request.data) + if serializer.is_valid(): + inventory_entry = serializer.save() + + # upload document for confirmation entry + if 'document' in request.data.keys(): + self.upload_confirmation_document(request, inventory=inventory_entry.id) + + return Response(serializer.data, status=status.HTTP_201_CREATED) + return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN) + + @action( + methods=['post'], + detail=True, + url_path='confirm_entry', + url_name='confirm_entry', + name='confirm_entry' + ) + @transaction.atomic + def confirm_inventory_entry(self, request, pk=None): + """ confirm inventory entry """ + + self.upload_confirmation_document(request, inventory=pk) + return Response(status=status.HTTP_200_OK) + + @action( + methods=['get'], + detail=False, + url_path='my_entries', + url_name='my_entries', + name='my_entries' + ) + def my_inventory_entries(self, request): + """ list of my inventory entries """ + + entries = self.queryset.filter(organization=get_organization_by_user(request.user)) + + # paginate & response + page = self.paginate_queryset(entries) + if page is not None: + serializer = self.get_serializer(page, many=True) + return self.get_paginated_response(serializer.data) class InventoryQuotaSaleTransactionViewSet(viewsets.ModelViewSet): diff --git a/apps/warehouse/web/api/v1/serializers.py b/apps/warehouse/web/api/v1/serializers.py index 8f919a6..7154beb 100644 --- a/apps/warehouse/web/api/v1/serializers.py +++ b/apps/warehouse/web/api/v1/serializers.py @@ -12,7 +12,15 @@ from django.db import models class InventoryEntrySerializer(serializers.ModelSerializer): class Meta: model = warehouse_models.InventoryEntry - fields = '__all__' + fields = [ + "id", + "distribution", + "weight", + "lading_number", + "delivery_address", + "is_confirmed", + "notes", + ] def create(self, validated_data): """ Custom create & set organization """ @@ -47,6 +55,12 @@ class InventoryEntrySerializer(serializers.ModelSerializer): return attrs + def to_representation(self, instance): + representation = super().to_representation(instance) + representation['document'] = instance.document + + return representation + class InventoryQuotaSaleTransactionSerializer(serializers.ModelSerializer): class Meta: diff --git a/common/generics.py b/common/generics.py index e69de29..926e25d 100644 --- a/common/generics.py +++ b/common/generics.py @@ -0,0 +1,10 @@ +from django.core.files.base import ContentFile +import base64 + + +def base64_to_image_file(base64_string, filename="image.jpg"): + """ convert base64 to image, pdf,..... """ + + img_format, img_str = base64_string.split(';base64,') # split before & after of ';base64,' + ext = img_format.split('/')[-1] # split format of file + return ContentFile(base64.b64decode(img_str), name=f"{filename}.{ext}"), ext