update all_product

This commit is contained in:
2026-02-10 15:46:44 +03:30
parent 543ba5875b
commit e9117d881d
10 changed files with 49 additions and 61 deletions

Binary file not shown.

View File

@@ -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'),
),
]

View File

@@ -1,4 +1,5 @@
import datetime
from operator import index
import re
from datetime import timedelta
from itertools import product
@@ -671,6 +672,11 @@ class AllProductsTransport(BaseModel): # بارهای تمامی محصولات
unloading = models.CharField(max_length=255, null=True, blank=True) # تخلیه
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):
# تصحیح استان/شهر مبدا
if self.origin_province:

View File

@@ -622,66 +622,32 @@ class AllProductsTransportCustomSerializer(serializers.ModelSerializer):
def get_location_origin(self, obj):
resul = {}
# بهینه‌سازی: استفاده از context cache برای جلوگیری از N+1 queries
location_cache = self.context.get('location_cache', {})
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:
location_cache[cache_key] = {
"provinceLat": province_origin.Lat,
"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:
location_cache[cache_key] = {
"cityLat": city_origin.Lat,
"cityLng": city_origin.Lng
}
else:
location_cache[cache_key] = {}
resul.update(location_cache.get(cache_key, {}))
province_origin = Province.objects.filter(name=obj.origin_province).first()
city_origin = City.objects.filter(name=obj.origin_city).first()
if province_origin:
resul.update({
"provinceLat": province_origin.Lat,
"provinceLng": province_origin.Lng
})
if city_origin:
resul.update({
"cityLat": city_origin.Lat,
"cityLng": city_origin.Lng
})
return resul
def get_location_destination(self, obj):
resul = {}
# بهینه‌سازی: استفاده از context cache برای جلوگیری از N+1 queries
location_cache = self.context.get('location_cache', {})
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:
location_cache[cache_key] = {
"provinceLat": province_destination.Lat,
"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:
location_cache[cache_key] = {
"cityLat": city_destination.Lat,
"cityLng": city_destination.Lng
}
else:
location_cache[cache_key] = {}
resul.update(location_cache.get(cache_key, {}))
province_destination = Province.objects.filter(name=obj.destination_province).first()
city_destination = City.objects.filter(name=obj.destination_city).first()
if province_destination:
resul.update({
"provinceLat": province_destination.Lat,
"provinceLng": province_destination.Lng
})
if city_destination:
resul.update({
"cityLat": city_destination.Lat,
"cityLng": city_destination.Lng
})
return resul

View File

@@ -4629,15 +4629,14 @@ class AllProductsTransportViewSet(viewsets.ModelViewSet):
# بهینه‌سازی: استفاده از select_related برای جلوگیری از N+1 queries
# و اعمال order_by فقط یک بار
transports = AllProductsTransport.objects.select_related('hatching').filter(**filters)
transports = AllProductsTransport.objects.filter(**filters)
if search and search != 'undefined' and search.strip():
transports = transports.filter(
build_query(self.filterset_class.Meta.fields, search)
)
# اعمال order_by فقط یک بار در آخر
transports = transports.order_by('-date', '-create_date')
transports = transports.order_by('-date', '-id')
page_size = request.query_params.get('page_size', None)
if page_size: