diff --git a/apps/core/custom_middlewares/request_middleware.py b/apps/core/custom_middlewares/request_middleware.py index ea7b037..28202b9 100644 --- a/apps/core/custom_middlewares/request_middleware.py +++ b/apps/core/custom_middlewares/request_middleware.py @@ -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 diff --git a/apps/product/web/api/v1/serializers/quota_serializers.py b/apps/product/web/api/v1/serializers/quota_serializers.py index 6c6c508..988537f 100644 --- a/apps/product/web/api/v1/serializers/quota_serializers.py +++ b/apps/product/web/api/v1/serializers/quota_serializers.py @@ -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,43 +28,45 @@ 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 - representation['org_quota_stat'] = quota_weight_by_org['id'] + quota_weight_by_org = instance.quota_amount_by_org(org) + if quota_weight_by_org: + # organization quota stat record + representation['org_quota_stat'] = quota_weight_by_org['id'] - representation['quota_weight'] = quota_weight_by_org['quota_weight'] - representation['quota_distributed'] = quota_weight_by_org['quota_distributed'] - representation['remaining_weight'] = quota_weight_by_org['remaining_weight'] - representation['been_sold'] = quota_weight_by_org['been_sold'] - representation['inventory_received'] = quota_weight_by_org['inventory_received'] - representation['inventory_entry_balance'] = quota_weight_by_org['inventory_entry_balance'] - representation['distributions_number_by_me'] = instance.distributions_assigned.filter( - assigner_organization=org - ).count() - representation['distributions'] = [{ - "id": dist.id, - "distribution_id": dist.distribution_id, - "create_date": dist.create_date, - "modify_date": dist.modify_date, - "assigner_organization": dist.assigner_organization.name, - "assigner_organization_id": dist.assigner_organization.id, - "weight": dist.weight, - } for dist in instance.distributions_assigned.filter(assigned_organization=org)] + representation['quota_weight'] = quota_weight_by_org['quota_weight'] + representation['quota_distributed'] = quota_weight_by_org['quota_distributed'] + representation['remaining_weight'] = quota_weight_by_org['remaining_weight'] + representation['been_sold'] = quota_weight_by_org['been_sold'] + representation['inventory_received'] = quota_weight_by_org['inventory_received'] + representation['inventory_entry_balance'] = quota_weight_by_org['inventory_entry_balance'] + representation['distributions_number_by_me'] = instance.distributions_assigned.filter( + assigner_organization=org + ).count() + representation['distributions'] = [{ + "id": dist.id, + "distribution_id": dist.distribution_id, + "create_date": dist.create_date, + "modify_date": dist.modify_date, + "assigner_organization": dist.assigner_organization.name, + "assigner_organization_id": dist.assigner_organization.id, + "weight": dist.weight, + } for dist in instance.distributions_assigned.filter(assigned_organization=org)] - representation['assigned_to_me'] = True if ( - org in assigned_orgs or instance.registerer_organization == org - ) else False + representation['assigned_to_me'] = True if ( + org in assigned_orgs or instance.registerer_organization == org + ) else False - # list of assigned organizations that received this quota - representation['assigned_organizations'] = [{ - "name": org.name, "id": org.id - } for org in assigned_orgs] - else: - org = None + # list of assigned organizations that received this quota + representation['assigned_organizations'] = [{ + "name": org.name, "id": org.id + } for org in assigned_orgs] if instance.sale_unit: representation['sale_unit'] = product_serializers.SaleUnitSerializer(