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,43 +28,45 @@ 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():
|
quota_weight_by_org = instance.quota_amount_by_org(org)
|
||||||
org = self.context['org']
|
if quota_weight_by_org:
|
||||||
quota_weight_by_org = instance.quota_amount_by_org(org)
|
# organization quota stat record
|
||||||
if quota_weight_by_org:
|
representation['org_quota_stat'] = quota_weight_by_org['id']
|
||||||
# organization quota stat record
|
|
||||||
representation['org_quota_stat'] = quota_weight_by_org['id']
|
|
||||||
|
|
||||||
representation['quota_weight'] = quota_weight_by_org['quota_weight']
|
representation['quota_weight'] = quota_weight_by_org['quota_weight']
|
||||||
representation['quota_distributed'] = quota_weight_by_org['quota_distributed']
|
representation['quota_distributed'] = quota_weight_by_org['quota_distributed']
|
||||||
representation['remaining_weight'] = quota_weight_by_org['remaining_weight']
|
representation['remaining_weight'] = quota_weight_by_org['remaining_weight']
|
||||||
representation['been_sold'] = quota_weight_by_org['been_sold']
|
representation['been_sold'] = quota_weight_by_org['been_sold']
|
||||||
representation['inventory_received'] = quota_weight_by_org['inventory_received']
|
representation['inventory_received'] = quota_weight_by_org['inventory_received']
|
||||||
representation['inventory_entry_balance'] = quota_weight_by_org['inventory_entry_balance']
|
representation['inventory_entry_balance'] = quota_weight_by_org['inventory_entry_balance']
|
||||||
representation['distributions_number_by_me'] = instance.distributions_assigned.filter(
|
representation['distributions_number_by_me'] = instance.distributions_assigned.filter(
|
||||||
assigner_organization=org
|
assigner_organization=org
|
||||||
).count()
|
).count()
|
||||||
representation['distributions'] = [{
|
representation['distributions'] = [{
|
||||||
"id": dist.id,
|
"id": dist.id,
|
||||||
"distribution_id": dist.distribution_id,
|
"distribution_id": dist.distribution_id,
|
||||||
"create_date": dist.create_date,
|
"create_date": dist.create_date,
|
||||||
"modify_date": dist.modify_date,
|
"modify_date": dist.modify_date,
|
||||||
"assigner_organization": dist.assigner_organization.name,
|
"assigner_organization": dist.assigner_organization.name,
|
||||||
"assigner_organization_id": dist.assigner_organization.id,
|
"assigner_organization_id": dist.assigner_organization.id,
|
||||||
"weight": dist.weight,
|
"weight": dist.weight,
|
||||||
} for dist in instance.distributions_assigned.filter(assigned_organization=org)]
|
} for dist in instance.distributions_assigned.filter(assigned_organization=org)]
|
||||||
|
|
||||||
representation['assigned_to_me'] = True if (
|
representation['assigned_to_me'] = True if (
|
||||||
org in assigned_orgs or instance.registerer_organization == org
|
org in assigned_orgs or instance.registerer_organization == org
|
||||||
) else False
|
) else False
|
||||||
|
|
||||||
# list of assigned organizations that received this quota
|
# list of assigned organizations that received this quota
|
||||||
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