from .models import *
def get_routes_for_user(user):
    from ReferenceDataApp.models import Airport, Country, City

    if not user and user.is_authenticated:
        errors_Dict = {
            'errors': {
                'all__': f'ошибка идентификации пользователя'
            }
        }
        return errors_Dict

    routes = Route.objects.filter(owner=user)

    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
        }
        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