update all_product
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,17 @@
|
|||||||
|
# Generated by Django 4.2.19 on 2026-02-10 12:00
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('app', '0047_hatching_loadingsum'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddIndex(
|
||||||
|
model_name='allproductstransport',
|
||||||
|
index=models.Index(fields=['date', 'id'], name='app_allprod_date_e7e7e8_idx'),
|
||||||
|
),
|
||||||
|
]
|
||||||
Binary file not shown.
Binary file not shown.
@@ -1,4 +1,5 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
from operator import index
|
||||||
import re
|
import re
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from itertools import product
|
from itertools import product
|
||||||
@@ -671,6 +672,11 @@ class AllProductsTransport(BaseModel): # بارهای تمامی محصولات
|
|||||||
unloading = models.CharField(max_length=255, null=True, blank=True) # تخلیه
|
unloading = models.CharField(max_length=255, null=True, blank=True) # تخلیه
|
||||||
out = models.BooleanField(default=False) # Out (True/False)
|
out = models.BooleanField(default=False) # Out (True/False)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
indexes = [
|
||||||
|
models.Index(fields=['trash', '-date', '-id'], name='apt_trash_date_id_idx'),
|
||||||
|
]
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
# تصحیح استان/شهر مبدا
|
# تصحیح استان/شهر مبدا
|
||||||
if self.origin_province:
|
if self.origin_province:
|
||||||
|
|||||||
@@ -622,66 +622,32 @@ class AllProductsTransportCustomSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
def get_location_origin(self, obj):
|
def get_location_origin(self, obj):
|
||||||
resul = {}
|
resul = {}
|
||||||
# بهینهسازی: استفاده از context cache برای جلوگیری از N+1 queries
|
province_origin = Province.objects.filter(name=obj.origin_province).first()
|
||||||
location_cache = self.context.get('location_cache', {})
|
city_origin = City.objects.filter(name=obj.origin_city).first()
|
||||||
|
|
||||||
if obj.origin_province:
|
|
||||||
cache_key = f"province_{obj.origin_province}"
|
|
||||||
if cache_key not in location_cache:
|
|
||||||
province_origin = Province.objects.filter(name=obj.origin_province).only('Lat', 'Lng').first()
|
|
||||||
if province_origin:
|
if province_origin:
|
||||||
location_cache[cache_key] = {
|
resul.update({
|
||||||
"provinceLat": province_origin.Lat,
|
"provinceLat": province_origin.Lat,
|
||||||
"provinceLng": province_origin.Lng
|
"provinceLng": province_origin.Lng
|
||||||
}
|
})
|
||||||
else:
|
|
||||||
location_cache[cache_key] = {}
|
|
||||||
resul.update(location_cache.get(cache_key, {}))
|
|
||||||
|
|
||||||
if obj.origin_city:
|
|
||||||
cache_key = f"city_{obj.origin_city}"
|
|
||||||
if cache_key not in location_cache:
|
|
||||||
city_origin = City.objects.filter(name=obj.origin_city).only('Lat', 'Lng').first()
|
|
||||||
if city_origin:
|
if city_origin:
|
||||||
location_cache[cache_key] = {
|
resul.update({
|
||||||
"cityLat": city_origin.Lat,
|
"cityLat": city_origin.Lat,
|
||||||
"cityLng": city_origin.Lng
|
"cityLng": city_origin.Lng
|
||||||
}
|
})
|
||||||
else:
|
|
||||||
location_cache[cache_key] = {}
|
|
||||||
resul.update(location_cache.get(cache_key, {}))
|
|
||||||
|
|
||||||
return resul
|
return resul
|
||||||
|
|
||||||
def get_location_destination(self, obj):
|
def get_location_destination(self, obj):
|
||||||
resul = {}
|
resul = {}
|
||||||
# بهینهسازی: استفاده از context cache برای جلوگیری از N+1 queries
|
province_destination = Province.objects.filter(name=obj.destination_province).first()
|
||||||
location_cache = self.context.get('location_cache', {})
|
city_destination = City.objects.filter(name=obj.destination_city).first()
|
||||||
|
|
||||||
if obj.destination_province:
|
|
||||||
cache_key = f"province_{obj.destination_province}"
|
|
||||||
if cache_key not in location_cache:
|
|
||||||
province_destination = Province.objects.filter(name=obj.destination_province).only('Lat', 'Lng').first()
|
|
||||||
if province_destination:
|
if province_destination:
|
||||||
location_cache[cache_key] = {
|
resul.update({
|
||||||
"provinceLat": province_destination.Lat,
|
"provinceLat": province_destination.Lat,
|
||||||
"provinceLng": province_destination.Lng
|
"provinceLng": province_destination.Lng
|
||||||
}
|
})
|
||||||
else:
|
|
||||||
location_cache[cache_key] = {}
|
|
||||||
resul.update(location_cache.get(cache_key, {}))
|
|
||||||
|
|
||||||
if obj.destination_city:
|
|
||||||
cache_key = f"city_{obj.destination_city}"
|
|
||||||
if cache_key not in location_cache:
|
|
||||||
city_destination = City.objects.filter(name=obj.destination_city).only('Lat', 'Lng').first()
|
|
||||||
if city_destination:
|
if city_destination:
|
||||||
location_cache[cache_key] = {
|
resul.update({
|
||||||
"cityLat": city_destination.Lat,
|
"cityLat": city_destination.Lat,
|
||||||
"cityLng": city_destination.Lng
|
"cityLng": city_destination.Lng
|
||||||
}
|
})
|
||||||
else:
|
|
||||||
location_cache[cache_key] = {}
|
|
||||||
resul.update(location_cache.get(cache_key, {}))
|
|
||||||
|
|
||||||
return resul
|
return resul
|
||||||
|
|||||||
@@ -4629,15 +4629,14 @@ class AllProductsTransportViewSet(viewsets.ModelViewSet):
|
|||||||
|
|
||||||
# بهینهسازی: استفاده از select_related برای جلوگیری از N+1 queries
|
# بهینهسازی: استفاده از select_related برای جلوگیری از N+1 queries
|
||||||
# و اعمال order_by فقط یک بار
|
# و اعمال order_by فقط یک بار
|
||||||
transports = AllProductsTransport.objects.select_related('hatching').filter(**filters)
|
transports = AllProductsTransport.objects.filter(**filters)
|
||||||
|
|
||||||
if search and search != 'undefined' and search.strip():
|
if search and search != 'undefined' and search.strip():
|
||||||
transports = transports.filter(
|
transports = transports.filter(
|
||||||
build_query(self.filterset_class.Meta.fields, search)
|
build_query(self.filterset_class.Meta.fields, search)
|
||||||
)
|
)
|
||||||
|
|
||||||
# اعمال order_by فقط یک بار در آخر
|
transports = transports.order_by('-date', '-id')
|
||||||
transports = transports.order_by('-date', '-create_date')
|
|
||||||
|
|
||||||
page_size = request.query_params.get('page_size', None)
|
page_size = request.query_params.get('page_size', None)
|
||||||
if page_size:
|
if page_size:
|
||||||
|
|||||||
Reference in New Issue
Block a user