fix - import request from thread in quota serialzier

This commit is contained in:
2025-12-03 10:10:13 +03:30
parent 9f9eae70f1
commit a5635c278f
2 changed files with 42 additions and 33 deletions

View File

@@ -7,11 +7,16 @@ def get_current_request_body():
return getattr(_local, "request_body", None)
def get_current_request():
return getattr(_local, "request", None)
class RequestMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
_local.request_body = request.body
_local.request = request
response = self.get_response(request)
return response

View File

@@ -1,9 +1,11 @@
from rest_framework import serializers, status
from apps.core.custom_middlewares.request_middleware import get_current_request
from apps.livestock.web.api.v1.serializers import LiveStockTypeSerializer
from apps.product import models as product_models
from apps.product.exceptions import QuotaException
from apps.product.web.api.v1.serializers import product_serializers
from common.helpers import get_organization_by_user
class QuotaSerializer(serializers.ModelSerializer):
@@ -26,9 +28,13 @@ class QuotaSerializer(serializers.ModelSerializer):
assigned_orgs = instance.assigned_organizations.all()
# get request from thread
request = get_current_request()
# get organization
org = get_organization_by_user(request.user)
# change quota weight by organization received weight
if 'org' in self.context.keys():
org = self.context['org']
quota_weight_by_org = instance.quota_amount_by_org(org)
if quota_weight_by_org:
# organization quota stat record
@@ -61,8 +67,6 @@ class QuotaSerializer(serializers.ModelSerializer):
representation['assigned_organizations'] = [{
"name": org.name, "id": org.id
} for org in assigned_orgs]
else:
org = None
if instance.sale_unit:
representation['sale_unit'] = product_serializers.SaleUnitSerializer(