diff --git a/apps/authentication/api/v1/api.py b/apps/authentication/api/v1/api.py index 060c6e6..5962f18 100644 --- a/apps/authentication/api/v1/api.py +++ b/apps/authentication/api/v1/api.py @@ -141,6 +141,11 @@ class UserViewSet(ModelViewSet): else: return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN) + def destroy(self, request, *args, **kwargs): + instance = self.get_object() + self.perform_destroy(instance) + return Response(status=status.HTTP_204_NO_CONTENT) + @action( methods=['get'], detail=False, diff --git a/apps/authentication/api/v1/serializers/jwt.py b/apps/authentication/api/v1/serializers/jwt.py index 5a7ad51..cc55289 100644 --- a/apps/authentication/api/v1/serializers/jwt.py +++ b/apps/authentication/api/v1/serializers/jwt.py @@ -5,6 +5,7 @@ from django.contrib.auth.models import update_last_login from apps.authentication.models import User from rest_framework import exceptions from django.core.cache import cache +from rest_framework import status from typing import Any @@ -34,9 +35,9 @@ class CustomizedTokenObtainPairSerializer(TokenObtainPairSerializer): # noqa data["otp_status"] = self.user.otp_status if not self.user.is_active: - raise exceptions.AuthenticationFailed( - self.error_messages["no_active_account"], - "no_active_account", + raise exceptions.APIException( + "user is not active", + status.HTTP_403_FORBIDDEN ) if api_settings.UPDATE_LAST_LOGIN: diff --git a/apps/authentication/api/v1/serializers/serializer.py b/apps/authentication/api/v1/serializers/serializer.py index 824d8da..4091e24 100644 --- a/apps/authentication/api/v1/serializers/serializer.py +++ b/apps/authentication/api/v1/serializers/serializer.py @@ -191,7 +191,8 @@ class OrganizationSerializer(serializers.ModelSerializer): 'province', 'city', 'parent_organization', - 'national_unique_id' + 'national_unique_id', + 'company_code' ] extra_kwargs = {} diff --git a/apps/core/middlewares.py b/apps/core/middlewares.py index 38165a0..9c4b56c 100644 --- a/apps/core/middlewares.py +++ b/apps/core/middlewares.py @@ -1,3 +1,5 @@ +from rest_framework.exceptions import APIException +from rest_framework import status from django.db import connection from django.conf import settings import os @@ -34,7 +36,6 @@ class SqlPrintingMiddleware(object): """ def __init__(self, get_response): - # print("heloo") self.get_response = get_response def __call__(self, request): diff --git a/apps/product/models.py b/apps/product/models.py index 070571f..bdcfd71 100644 --- a/apps/product/models.py +++ b/apps/product/models.py @@ -294,6 +294,12 @@ class Quota(BaseModel): return factor_total + broker_total + coop + factory + @property + def remaining_quota_weight(self): + """ calculate remaining quota weight after distribution """ + distributed_weight = self.distributions_assigned.aggregate(total=models.Sum("weight"))["total"] or 0 + return self.quota_weight - distributed_weight + def save(self, calculate_final_price=None, *args, **kwargs): if not self.quota_id: self.quota_id = self.generate_quota_id() @@ -424,4 +430,3 @@ class QuotaDistribution(BaseModel): def save(self, *args, **kwargs): return super(QuotaDistribution, self).save(*args, **kwargs) - diff --git a/apps/product/web/api/v1/product_api.py b/apps/product/web/api/v1/product_api.py index 2c2dfe4..33d95ff 100644 --- a/apps/product/web/api/v1/product_api.py +++ b/apps/product/web/api/v1/product_api.py @@ -424,6 +424,10 @@ class QuotaViewSet(viewsets.ModelViewSet): # noqa quota.save(calculate_final_price=True) return Response(data, status=status.HTTP_201_CREATED) + @transaction.atomic + def update(self, request, *args, **kwargs): + pass + @action( methods=['get'], detail=False, @@ -489,7 +493,7 @@ class QuotaViewSet(viewsets.ModelViewSet): # noqa return Response(e, status=status.HTTP_204_NO_CONTENT) -class QuotaIncentiveAssignmentViewSet(viewsets.ModelViewSet): +class QuotaIncentiveAssignmentViewSet(viewsets.ModelViewSet): # noqa """ apis for incentive assignment """ queryset = product_models.QuotaIncentiveAssignment.objects.all() diff --git a/apps/product/web/api/v1/quota_distribution_api.py b/apps/product/web/api/v1/quota_distribution_api.py index a22e307..2d1fc4c 100644 --- a/apps/product/web/api/v1/quota_distribution_api.py +++ b/apps/product/web/api/v1/quota_distribution_api.py @@ -50,6 +50,23 @@ class QuotaDistributionViewSet(viewsets.ModelViewSet): return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_403_FORBIDDEN) + @transaction.atomic + def update(self, request, *args, **kwargs): + """ Custom update of quota distribution """ + + partial = kwargs.pop('partial', False) + instance = self.get_object() + serializer = self.get_serializer(instance, data=request.data, partial=partial) + serializer.is_valid(raise_exception=True) + self.perform_update(serializer) + + if getattr(instance, '_prefetched_objects_cache', None): + # If 'prefetch_related' has been applied to a queryset, we need to + # forcibly invalidate the prefetch cache on the instance. + instance._prefetched_objects_cache = {} + + return Response(serializer.data) + @action( methods=['put'], detail=True,