Merge remote-tracking branch 'origin/main'
This commit is contained in:
52
RoutesApp/funcs.py
Normal file
52
RoutesApp/funcs.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
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
|
||||||
@@ -12,6 +12,7 @@ from datetime import datetime
|
|||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
from .forms import *
|
from .forms import *
|
||||||
|
from .funcs import get_routes_for_user
|
||||||
|
|
||||||
|
|
||||||
def new_route_view_ajax(request):
|
def new_route_view_ajax(request):
|
||||||
@@ -27,39 +28,18 @@ def new_route_view_ajax(request):
|
|||||||
|
|
||||||
|
|
||||||
def get_routes_ajax(request):
|
def get_routes_ajax(request):
|
||||||
from ReferenceDataApp.models import Airport, Country, City
|
|
||||||
|
|
||||||
if request.method != 'POST':
|
if request.method != 'POST':
|
||||||
raise Http404
|
raise Http404
|
||||||
|
|
||||||
try:
|
try:
|
||||||
routes = []
|
routes_Dict = get_routes_for_user(request.user)
|
||||||
if request.user and request.user.is_authenticated:
|
if 'errors' in routes_Dict:
|
||||||
routes = Route.objects.filter(owner=request.user)
|
return JsonResponse(routes_Dict, status=400)
|
||||||
|
|
||||||
for route in routes:
|
|
||||||
|
|
||||||
try:
|
html = render_to_string('blocks/profile/b_my_routes.html', routes_Dict, request=request)
|
||||||
|
|
||||||
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 route points = {str(e)}'
|
|
||||||
print(msg)
|
|
||||||
|
|
||||||
Dict = {
|
|
||||||
'routes': routes
|
|
||||||
}
|
|
||||||
html = render_to_string('blocks/profile/b_my_routes.html', Dict, request=request)
|
|
||||||
|
|
||||||
res_Dict = {
|
res_Dict = {
|
||||||
'html': html
|
'html': html
|
||||||
@@ -97,11 +77,15 @@ def create_route_ajax(request):
|
|||||||
obj.owner = request.user
|
obj.owner = request.user
|
||||||
obj.save()
|
obj.save()
|
||||||
|
|
||||||
routes = Route.objects.filter(owner=request.user)
|
routes_Dict = get_routes_for_user(request.user)
|
||||||
Dict = {
|
|
||||||
'routes': routes
|
if 'errors' in routes_Dict:
|
||||||
}
|
form.errors.update(routes_Dict['errors'])
|
||||||
html = render_to_string('blocks/profile/b_my_routes.html', Dict, request=request)
|
Dict = {'form': form}
|
||||||
|
html = render_to_string('blocks/profile/b_new_route.html', Dict, request=request)
|
||||||
|
return JsonResponse({'html': html}, status=400)
|
||||||
|
|
||||||
|
html = render_to_string('blocks/profile/b_my_routes.html', routes_Dict, request=request)
|
||||||
|
|
||||||
res_Dict = {
|
res_Dict = {
|
||||||
'html': html
|
'html': html
|
||||||
|
|||||||
Reference in New Issue
Block a user