update
This commit is contained in:
@@ -622,32 +622,66 @@ class AllProductsTransportCustomSerializer(serializers.ModelSerializer):
|
||||
|
||||
def get_location_origin(self, obj):
|
||||
resul = {}
|
||||
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
|
||||
})
|
||||
# بهینهسازی: استفاده از 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, {}))
|
||||
|
||||
return resul
|
||||
|
||||
def get_location_destination(self, obj):
|
||||
resul = {}
|
||||
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
|
||||
})
|
||||
# بهینهسازی: استفاده از 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, {}))
|
||||
|
||||
return resul
|
||||
|
||||
Reference in New Issue
Block a user