from .models import * def get_city_by_type_transport_and_address_point(type_transport, address_point): from ReferenceDataApp.models import Airport, City try: if type_transport == 'avia': return Airport.objects.get(id=address_point).city else: return City.objects.get(id=address_point) except Exception as e: msg = f'get_city_by_type_transport_and_address_point Error = {str(e)}, type_transport = {type_transport}, address_point = {address_point}' print(msg) return None def get_routes_Dict(user=None, data=None): from ReferenceDataApp.models import Airport, Country, City # if not user and user.is_authenticated: # errors_Dict = { # 'errors': { # 'all__': f'ошибка идентификации пользователя' # } # } # return errors_Dict kwargs = {} if user: kwargs.update({ 'owner': user }) from_el = None to_el = None if data: for key, val in data.items(): if val: if key == 'weight': weight_list = val.split(';') if weight_list[0]: kwargs.update({f'{key}__gte': int(weight_list[0])}) if weight_list[1]: kwargs.update({f'{key}__lte': int(weight_list[1])}) if key == 'type_transport': items_list = val.split(',') kwargs.update({f'{key}__in': items_list}) if key not in ( 'from_address_point_txt', 'to_address_point_txt', 'csrfmiddlewaretoken', 'sort', 'weight', 'from_el', 'to_el', 'from_address_point', 'to_address_point', 'type_transport' ): kwargs.update({key: val}) if key == 'from_address_point': kwargs.update({f'from_city__id': val}) if key == 'to_address_point': kwargs.update({f'to_city__id': val}) if key == 'from_el': from_el = int(val) if key == 'to_el': to_el = int(val) routes = Route.objects.filter(**kwargs).order_by('-modifiedDT') routes_count = routes.count() if from_el and to_el: routes = routes[from_el:to_el] elif from_el: routes = routes[from_el:] elif to_el: routes = routes[:to_el] else: routes = routes[:25] last_block_routes = False if to_el and to_el >= routes_count: last_block_routes = True res_Dict = {} try: for route in routes: try: if route.type_transport == 'avia': route.from_airport = Airport.objects.get(id=route.from_address_point) route.to_airport = Airport.objects.get(id=route.to_address_point) route.from_city = route.from_airport.city route.to_city = route.to_airport.city else: route.from_city = City.objects.get(id=route.from_address_point) route.to_city = City.objects.get(id=route.to_address_point) route.from_country = route.from_city.country route.to_country = route.to_city.country except Exception as e: msg = f'get_routes_for_user get route Error = {str(e)}' print(msg) res_Dict = { 'routes': routes, 'last_block_routes': last_block_routes } return res_Dict except Exception as e: msg = f'get_routes_for_user = {str(e)}' print(msg) errors_Dict = { 'errors': { 'all__': msg } } return errors_Dict