add - full import pricing features on every distribution
This commit is contained in:
@@ -75,6 +75,7 @@ INSTALLED_APPS = [
|
|||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
# 'elasticapm.contrib.django.middleware.TracingMiddleware',
|
# 'elasticapm.contrib.django.middleware.TracingMiddleware',
|
||||||
|
'apps.core.custom_middlewares.request_middleware.RequestMiddleware',
|
||||||
'apps.core.middlewares.Json500Middleware',
|
'apps.core.middlewares.Json500Middleware',
|
||||||
'corsheaders.middleware.CorsMiddleware', # noqa
|
'corsheaders.middleware.CorsMiddleware', # noqa
|
||||||
'django.middleware.security.SecurityMiddleware',
|
'django.middleware.security.SecurityMiddleware',
|
||||||
|
|||||||
0
apps/core/custom_middlewares/__init__.py
Normal file
0
apps/core/custom_middlewares/__init__.py
Normal file
17
apps/core/custom_middlewares/request_middleware.py
Normal file
17
apps/core/custom_middlewares/request_middleware.py
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import threading
|
||||||
|
|
||||||
|
_local = threading.local()
|
||||||
|
|
||||||
|
|
||||||
|
def get_current_request_body():
|
||||||
|
return getattr(_local, "request_body", None)
|
||||||
|
|
||||||
|
|
||||||
|
class RequestMiddleware:
|
||||||
|
def __init__(self, get_response):
|
||||||
|
self.get_response = get_response
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
|
_local.request_body = request.body
|
||||||
|
response = self.get_response(request)
|
||||||
|
return response
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 5.0 on 2025-12-02 10:17
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('product', '0100_attributevalue_org_quota_stat_and_more'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='quota',
|
||||||
|
name='edited_pricing_features',
|
||||||
|
field=models.BooleanField(default=False, help_text='True when quota broker values & attribute values changed in distribution of quota'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -418,6 +418,10 @@ class Quota(BaseModel):
|
|||||||
pre_sale_balance = models.IntegerField(default=0)
|
pre_sale_balance = models.IntegerField(default=0)
|
||||||
free_sale = models.BooleanField(default=False)
|
free_sale = models.BooleanField(default=False)
|
||||||
free_sale_balance = models.IntegerField(default=0)
|
free_sale_balance = models.IntegerField(default=0)
|
||||||
|
edited_pricing_features = models.BooleanField(
|
||||||
|
default=False,
|
||||||
|
help_text='True when quota broker values & attribute values changed in distribution of quota'
|
||||||
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"Quota ({self.id}) for {self.product.name}"
|
return f"Quota ({self.id}) for {self.product.name}"
|
||||||
@@ -853,9 +857,13 @@ class QuotaDistribution(BaseModel):
|
|||||||
def pre_sale(self):
|
def pre_sale(self):
|
||||||
return self.quota.pre_sale
|
return self.quota.pre_sale
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, additional_data=None, *args, **kwargs):
|
||||||
if not self.distribution_id:
|
if not self.distribution_id:
|
||||||
self.distribution_id = self.generate_distribution_id()
|
self.distribution_id = self.generate_distribution_id()
|
||||||
|
|
||||||
|
if additional_data:
|
||||||
|
self.additional_data = additional_data
|
||||||
|
print(self.additional_data)
|
||||||
return super(QuotaDistribution, self).save(*args, **kwargs)
|
return super(QuotaDistribution, self).save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -130,6 +130,7 @@ class AttributeValueSerializer(serializers.ModelSerializer):
|
|||||||
fields = [
|
fields = [
|
||||||
"id",
|
"id",
|
||||||
"quota",
|
"quota",
|
||||||
|
"org_quota_stat",
|
||||||
"attribute",
|
"attribute",
|
||||||
"value",
|
"value",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -140,6 +140,7 @@ class QuotaBrokerValueSerializer(serializers.ModelSerializer): # noqa
|
|||||||
fields = [
|
fields = [
|
||||||
"id",
|
"id",
|
||||||
"quota",
|
"quota",
|
||||||
|
"org_quota_stat",
|
||||||
"broker",
|
"broker",
|
||||||
"value",
|
"value",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,14 +1,51 @@
|
|||||||
from apps.product.models import QuotaDistribution, OrganizationQuotaStats
|
import json
|
||||||
|
|
||||||
|
from apps.core.custom_middlewares.request_middleware import get_current_request_body
|
||||||
|
from apps.product.models import QuotaDistribution, OrganizationQuotaStats, AttributeValue, Attribute, QuotaBrokerValue, \
|
||||||
|
Broker
|
||||||
from apps.product.validators.quota_stats_validator import QuotaStatsValidator
|
from apps.product.validators.quota_stats_validator import QuotaStatsValidator
|
||||||
from apps.warehouse.models import InventoryEntry
|
from apps.warehouse.models import InventoryEntry
|
||||||
|
|
||||||
|
|
||||||
|
def import_price_features(data, assigned_stat, quota):
|
||||||
|
"""
|
||||||
|
create price attributes data & broker pricing value for every distribution
|
||||||
|
"""
|
||||||
|
assigned_stat.attribute_values.all().delete()
|
||||||
|
assigned_stat.broker_values.all().delete()
|
||||||
|
|
||||||
|
if 'price_attributes_data' and 'broker_data' in data.keys():
|
||||||
|
attr_price_values = [AttributeValue(
|
||||||
|
attribute=Attribute.objects.get(id=attr['attribute']),
|
||||||
|
value=attr['value'],
|
||||||
|
quota=quota,
|
||||||
|
org_quota_stat=assigned_stat
|
||||||
|
) for attr in data['price_attributes_data']]
|
||||||
|
AttributeValue.objects.bulk_create(attr_price_values) # bulk create
|
||||||
|
|
||||||
|
broker_price_values = [
|
||||||
|
QuotaBrokerValue(
|
||||||
|
quota=quota,
|
||||||
|
broker=Broker.objects.get(id=broker['broker']),
|
||||||
|
value=broker['value'],
|
||||||
|
org_quota_stat=assigned_stat
|
||||||
|
) for broker in data['broker_data']
|
||||||
|
]
|
||||||
|
QuotaBrokerValue.objects.bulk_create(broker_price_values) # bulk create
|
||||||
|
|
||||||
|
quota.edited_pricing_features = True
|
||||||
|
quota.save(update_fields=['edited_pricing_features'])
|
||||||
|
|
||||||
|
|
||||||
class QuotaStatsService:
|
class QuotaStatsService:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def apply_distribution(distribution: QuotaDistribution):
|
def apply_distribution(distribution: QuotaDistribution):
|
||||||
quota = distribution.quota
|
quota = distribution.quota
|
||||||
# origin org
|
# origin org
|
||||||
assigner = distribution.assigner_organization
|
assigner = distribution.assigner_organization
|
||||||
|
if assigner.type.key == 'ADM':
|
||||||
|
assigner = quota.registerer_organization
|
||||||
|
|
||||||
# destination org
|
# destination org
|
||||||
assigned = distribution.assigned_organization
|
assigned = distribution.assigned_organization
|
||||||
|
|
||||||
@@ -23,12 +60,10 @@ class QuotaStatsService:
|
|||||||
organization=assigner,
|
organization=assigner,
|
||||||
quota=quota,
|
quota=quota,
|
||||||
)
|
)
|
||||||
print(assigner_stat.id)
|
|
||||||
if created:
|
if created:
|
||||||
assigner_stat.stat_type = 'distribution'
|
assigner_stat.stat_type = 'distribution'
|
||||||
assigner_stat.save()
|
assigner_stat.save()
|
||||||
if assigner_stat.stat_type == 'distribution':
|
if assigner_stat.stat_type == 'distribution':
|
||||||
print(assigner_stat.remaining_amount)
|
|
||||||
assigner_stat.remaining_amount -= distribution.weight
|
assigner_stat.remaining_amount -= distribution.weight
|
||||||
assigner_stat.total_distributed += distribution.weight
|
assigner_stat.total_distributed += distribution.weight
|
||||||
assigner_stat.save()
|
assigner_stat.save()
|
||||||
@@ -39,12 +74,15 @@ class QuotaStatsService:
|
|||||||
quota=quota,
|
quota=quota,
|
||||||
stat_type='distribution'
|
stat_type='distribution'
|
||||||
)
|
)
|
||||||
print(distribution._request) # noqa
|
|
||||||
assigned_stat.total_amount += distribution.weight
|
assigned_stat.total_amount += distribution.weight
|
||||||
assigned_stat.remaining_amount += distribution.weight
|
assigned_stat.remaining_amount += distribution.weight
|
||||||
assigned_stat.distributions.add(distribution)
|
assigned_stat.distributions.add(distribution)
|
||||||
assigned_stat.save()
|
assigned_stat.save()
|
||||||
|
|
||||||
|
# create pricing attributes and broker pricing data
|
||||||
|
data = json.loads(get_current_request_body().decode())
|
||||||
|
import_price_features(data, assigned_stat, quota)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_distribution(distribution: QuotaDistribution, old_weight: int):
|
def update_distribution(distribution: QuotaDistribution, old_weight: int):
|
||||||
diff = distribution.weight - old_weight
|
diff = distribution.weight - old_weight
|
||||||
@@ -81,6 +119,10 @@ class QuotaStatsService:
|
|||||||
assigned_stat.remaining_amount += diff
|
assigned_stat.remaining_amount += diff
|
||||||
assigned_stat.save()
|
assigned_stat.save()
|
||||||
|
|
||||||
|
# create pricing attributes and broker pricing data
|
||||||
|
data = json.loads(get_current_request_body().decode())
|
||||||
|
import_price_features(data, assigned_stat, quota)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_distribution(distribution: QuotaDistribution):
|
def delete_distribution(distribution: QuotaDistribution):
|
||||||
quota = distribution.quota
|
quota = distribution.quota
|
||||||
|
|||||||
@@ -60,20 +60,19 @@ class QuotaDistributionViewSet(BaseViewSet, SoftDeleteMixin, viewsets.ModelViewS
|
|||||||
except APIException as e:
|
except APIException as e:
|
||||||
raise APIException("unauthorized", code=status.HTTP_401_UNAUTHORIZED)
|
raise APIException("unauthorized", code=status.HTTP_401_UNAUTHORIZED)
|
||||||
|
|
||||||
price_attributes_data = request.data.pop('price_attributes_data')
|
price_attributes_data = request.data.pop(
|
||||||
broker_data = request.data.pop('broker_data')
|
'price_attributes_data'
|
||||||
|
) if 'price_attributes_data' in request.data.keys() else None
|
||||||
|
|
||||||
|
broker_data = request.data.pop(
|
||||||
|
'broker_data'
|
||||||
|
) if 'broker_data' in request.data.keys() else None
|
||||||
|
|
||||||
request.data.update({'assigner_organization': assigner_user.organization.id})
|
request.data.update({'assigner_organization': assigner_user.organization.id})
|
||||||
serializer = self.serializer_class(data=request.data)
|
serializer = self.serializer_class(data=request.data)
|
||||||
if serializer.is_valid():
|
if serializer.is_valid():
|
||||||
distribution = serializer.save()
|
distribution = serializer.save()
|
||||||
|
|
||||||
# to use in signals
|
|
||||||
distribution._request = {
|
|
||||||
'price_attributes_data': price_attributes_data,
|
|
||||||
'broker_data': broker_data
|
|
||||||
}
|
|
||||||
|
|
||||||
# add this organization to quota assigned_organizations
|
# add this organization to quota assigned_organizations
|
||||||
# this org is received a distribution from quota
|
# this org is received a distribution from quota
|
||||||
distribution.quota.assigned_organizations.add(distribution.assigned_organization)
|
distribution.quota.assigned_organizations.add(distribution.assigned_organization)
|
||||||
|
|||||||
Reference in New Issue
Block a user