fix - import request from thread in quota serialzier
This commit is contained in:
@@ -7,11 +7,16 @@ def get_current_request_body():
|
|||||||
return getattr(_local, "request_body", None)
|
return getattr(_local, "request_body", None)
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_request():
|
||||||
|
return getattr(_local, "request", None)
|
||||||
|
|
||||||
|
|
||||||
class RequestMiddleware:
|
class RequestMiddleware:
|
||||||
def __init__(self, get_response):
|
def __init__(self, get_response):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
|
||||||
def __call__(self, request):
|
def __call__(self, request):
|
||||||
_local.request_body = request.body
|
_local.request_body = request.body
|
||||||
|
_local.request = request
|
||||||
response = self.get_response(request)
|
response = self.get_response(request)
|
||||||
return response
|
return response
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
from rest_framework import serializers, status
|
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.livestock.web.api.v1.serializers import LiveStockTypeSerializer
|
||||||
from apps.product import models as product_models
|
from apps.product import models as product_models
|
||||||
from apps.product.exceptions import QuotaException
|
from apps.product.exceptions import QuotaException
|
||||||
from apps.product.web.api.v1.serializers import product_serializers
|
from apps.product.web.api.v1.serializers import product_serializers
|
||||||
|
from common.helpers import get_organization_by_user
|
||||||
|
|
||||||
|
|
||||||
class QuotaSerializer(serializers.ModelSerializer):
|
class QuotaSerializer(serializers.ModelSerializer):
|
||||||
@@ -26,9 +28,13 @@ class QuotaSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
assigned_orgs = instance.assigned_organizations.all()
|
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
|
# 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)
|
quota_weight_by_org = instance.quota_amount_by_org(org)
|
||||||
if quota_weight_by_org:
|
if quota_weight_by_org:
|
||||||
# organization quota stat record
|
# organization quota stat record
|
||||||
@@ -61,8 +67,6 @@ class QuotaSerializer(serializers.ModelSerializer):
|
|||||||
representation['assigned_organizations'] = [{
|
representation['assigned_organizations'] = [{
|
||||||
"name": org.name, "id": org.id
|
"name": org.name, "id": org.id
|
||||||
} for org in assigned_orgs]
|
} for org in assigned_orgs]
|
||||||
else:
|
|
||||||
org = None
|
|
||||||
|
|
||||||
if instance.sale_unit:
|
if instance.sale_unit:
|
||||||
representation['sale_unit'] = product_serializers.SaleUnitSerializer(
|
representation['sale_unit'] = product_serializers.SaleUnitSerializer(
|
||||||
|
|||||||
Reference in New Issue
Block a user