481 lines
15 KiB
Python
481 lines
15 KiB
Python
import copy
|
||
import json
|
||
from copy import deepcopy
|
||
|
||
from django.conf import settings
|
||
from django.shortcuts import render
|
||
|
||
from uuid import uuid1
|
||
from .models import *
|
||
from django.contrib import auth
|
||
from django.http import HttpResponse, Http404, JsonResponse
|
||
from django.template import loader, RequestContext
|
||
from django.contrib.auth.decorators import login_required
|
||
from BaseModels.mailSender import techSendMail
|
||
from django.utils.translation import gettext as _
|
||
from datetime import datetime, timedelta
|
||
from django.template.loader import render_to_string
|
||
from django.urls import reverse
|
||
from .forms import *
|
||
from .funcs import *
|
||
from GeneralApp.funcs import get_and_set_lang
|
||
from SubscribesApp.funcs import check_option_in_cur_user_subscribe
|
||
|
||
|
||
def highlight_route_ajax(request):
|
||
if request.method != 'POST':
|
||
raise Http404
|
||
|
||
data = request.POST
|
||
if not data and request.body:
|
||
data = json.loads(request.body)
|
||
|
||
if not data or not 'route_id' in data:
|
||
msg = _('Недостаточно данных')
|
||
return JsonResponse({'errors': msg})
|
||
|
||
try:
|
||
route = Route.objects.get(owner=request.user, id=data['route_id'])
|
||
except Route.DoesNotExist:
|
||
msg = _('Не найден маршрут')
|
||
return JsonResponse({'errors': msg})
|
||
|
||
if not route.get_permission_for_highlight():
|
||
msg = _('Нет доступа к выделению')
|
||
return JsonResponse({'errors': msg})
|
||
|
||
|
||
from SubscribesApp.funcs import get_cur_user_subscribe
|
||
user_subscribe = get_cur_user_subscribe(request.user)
|
||
user_subscribe.used_route_highlight_count += 1
|
||
user_subscribe.save(update_fields=['used_route_highlight_count'])
|
||
|
||
|
||
route.highlight_color = '#FF0000'
|
||
highlight_hours = user_subscribe.get_highlight_hours()
|
||
route.highlight_end_DT = datetime.now() + timedelta(hours=highlight_hours)
|
||
route.save(update_fields=['highlight_color', 'highlight_end_DT'])
|
||
|
||
|
||
|
||
Dict = {
|
||
'route': route,
|
||
}
|
||
|
||
html = render_to_string('widgets/routes/w_my_route.html', Dict, request=request)
|
||
|
||
res_Dict = {
|
||
'html': html
|
||
}
|
||
res_Dict.update(user_subscribe.remains_route_adding_options())
|
||
|
||
from GeneralApp.funcs import get_add_to_ajax_response_Dict
|
||
res_Dict.update(get_add_to_ajax_response_Dict(request.user))
|
||
return JsonResponse(res_Dict)
|
||
|
||
|
||
def raise_route_ajax(request):
|
||
if request.method != 'POST':
|
||
raise Http404
|
||
|
||
data = request.POST
|
||
if not data and request.body:
|
||
data = json.loads(request.body)
|
||
|
||
if not data or not 'route_id' in data:
|
||
msg = _('Недостаточно данных')
|
||
return JsonResponse({'errors': msg})
|
||
|
||
try:
|
||
route = Route.objects.get(owner=request.user, id=data['route_id'])
|
||
except Route.DoesNotExist:
|
||
msg = _('Не найден маршрут')
|
||
return JsonResponse({'errors': msg})
|
||
|
||
if not route.get_permission_for_raise():
|
||
msg = _('Нет доступных поднятий')
|
||
return JsonResponse({'errors': msg})
|
||
|
||
route.rising_DT = datetime.now()
|
||
route.save(update_fields=['rising_DT'])
|
||
|
||
from SubscribesApp.funcs import get_cur_user_subscribe
|
||
user_subscribe = get_cur_user_subscribe(request.user)
|
||
user_subscribe.used_route_rising_count += 1
|
||
user_subscribe.save(update_fields=['used_route_rising_count'])
|
||
|
||
res_Dict = {'status': 'ok'}
|
||
res_Dict.update(user_subscribe.remains_route_adding_options())
|
||
|
||
from GeneralApp.funcs import get_add_to_ajax_response_Dict
|
||
res_Dict.update(get_add_to_ajax_response_Dict(request.user))
|
||
return JsonResponse(res_Dict)
|
||
|
||
|
||
def del_route_ajax(request):
|
||
if request.method != 'POST':
|
||
raise Http404
|
||
|
||
if not check_option_in_cur_user_subscribe(request.user, 'размещение заявок'):
|
||
return JsonResponse({'html': 'нет доступа'}, status=403)
|
||
|
||
lang = get_and_set_lang(request)
|
||
|
||
try:
|
||
|
||
data = json.loads(request.body)
|
||
if not 'route_id' in data:
|
||
msg = f'Недостаточно данных'
|
||
return JsonResponse({'errors': msg})
|
||
|
||
route = Route.objects.filter(id=data['route_id']).delete()
|
||
|
||
routes_Dict = get_routes_Dict(request.user)
|
||
if 'errors' in routes_Dict:
|
||
return JsonResponse(routes_Dict, status=400)
|
||
|
||
html = render_to_string('v2/blocks/b_my_routes.html', routes_Dict, request=request)
|
||
|
||
res_Dict = {
|
||
'html': html
|
||
}
|
||
|
||
from GeneralApp.funcs import get_add_to_ajax_response_Dict
|
||
res_Dict.update(get_add_to_ajax_response_Dict(request.user))
|
||
return JsonResponse(res_Dict)
|
||
|
||
except Exception as e:
|
||
# form.errors.append({'__all__': f'Ошибка: {str(e)}'})
|
||
msg = f'Ошибка del_route_ajax = {str(e)}'
|
||
print(msg)
|
||
return JsonResponse({'errors': msg})
|
||
|
||
|
||
|
||
def edit_route_ajax(request):
|
||
if request.method != 'POST':
|
||
raise Http404
|
||
|
||
if not check_option_in_cur_user_subscribe(request.user, 'размещение заявок'):
|
||
return JsonResponse({'html': 'нет доступа'}, status=403)
|
||
|
||
lang = get_and_set_lang(request)
|
||
|
||
data = json.loads(request.body)
|
||
|
||
Dict = {}
|
||
|
||
try:
|
||
|
||
if not 'route_id' in data:
|
||
msg = f'Недостаточно данных'
|
||
return JsonResponse({'errors': msg})
|
||
|
||
route = Route.objects.get(id=data['route_id'])
|
||
|
||
form = RouteForm(instance=route)
|
||
form = get_cargo_types_by_type_transport(route.type_transport, form)
|
||
# routeForm_assign_choices_by_type_transport(form, route.type_transport)
|
||
|
||
route_address_points_Dict = route.get_address_points()
|
||
form.initial.update({
|
||
'from_address_point_txt': route_address_points_Dict['from_address_point_txt'],
|
||
'to_address_point_txt': route_address_points_Dict['to_address_point_txt'],
|
||
})
|
||
|
||
|
||
Dict = {
|
||
'form': form,
|
||
'route': route
|
||
}
|
||
|
||
except Exception as e:
|
||
# form.errors.append({'__all__': f'Ошибка: {str(e)}'})
|
||
msg = f'Ошибка edit_route = {str(e)}'
|
||
print(msg)
|
||
return JsonResponse({'errors': msg})
|
||
|
||
html = render_to_string('blocks/profile/b_new_route.html', Dict, request=request)
|
||
res_Dict = {
|
||
'html': html,
|
||
'btn_title': _('Сохранить изменения')
|
||
}
|
||
from GeneralApp.funcs import get_add_to_ajax_response_Dict
|
||
res_Dict.update(get_add_to_ajax_response_Dict(request.user))
|
||
return JsonResponse(res_Dict)
|
||
|
||
|
||
|
||
|
||
|
||
|
||
def new_route_view_ajax(request):
|
||
if request.method != 'POST':
|
||
raise Http404
|
||
if not check_option_in_cur_user_subscribe(request.user, 'размещение заявок'):
|
||
return JsonResponse({'html': 'нет доступа'}, status=403)
|
||
|
||
lang = get_and_set_lang(request)
|
||
|
||
# form = RouteForm()
|
||
# Dict = {
|
||
# 'form': form
|
||
# }
|
||
try:
|
||
|
||
errors_off = True
|
||
|
||
data = request.POST.dict()
|
||
if not data and request.body:
|
||
data = json.loads(request.body)
|
||
# show_errors = False
|
||
|
||
html = get_profile_new_route_page_html(request, data)
|
||
|
||
|
||
except Exception as e:
|
||
# form.errors.append({'__all__': f'Ошибка: {str(e)}'})
|
||
msg = f'new_route_view_ajax Error = {str(e)}'
|
||
print(msg)
|
||
html = msg
|
||
|
||
|
||
# html = render_to_string('blocks/profile/b_new_route.html', Dict, request=request)
|
||
res_Dict = {'html': html}
|
||
from GeneralApp.funcs import get_add_to_ajax_response_Dict
|
||
res_Dict.update(get_add_to_ajax_response_Dict(request.user))
|
||
return JsonResponse(res_Dict)
|
||
|
||
|
||
def find_routes_ajax(request):
|
||
|
||
|
||
if request.method != 'POST':
|
||
raise Http404
|
||
|
||
lang = get_and_set_lang(request)
|
||
|
||
try:
|
||
|
||
|
||
data = request.POST.dict()
|
||
if not data and request.body:
|
||
data = json.loads(request.body)
|
||
|
||
if not 'owner_type' in data:
|
||
data['owner_type'] = 'mover'
|
||
routes_Dict = get_routes_Dict(data=data)
|
||
if 'errors' in routes_Dict:
|
||
return JsonResponse(routes_Dict, status=400)
|
||
|
||
|
||
if routes_Dict['routes']:
|
||
html = render_to_string('blocks/b_search_routes.html', routes_Dict, request=request)
|
||
else:
|
||
html = render_to_string('templates_js_translate/not_found_find_routes.html', routes_Dict, request=request)
|
||
|
||
res_Dict = {
|
||
'html': html,
|
||
'last_block': routes_Dict['last_block'],
|
||
'next_page_els_count': routes_Dict['next_page_els_count'],
|
||
# 'form': RouteForm(initial=data)
|
||
}
|
||
|
||
from GeneralApp.funcs import get_add_to_ajax_response_Dict
|
||
res_Dict.update(get_add_to_ajax_response_Dict(request.user))
|
||
return JsonResponse(res_Dict)
|
||
|
||
except Exception as e:
|
||
|
||
errors_Dict = {
|
||
'errors': {
|
||
'all__': f'ошибка в запросе = {str(e)}'
|
||
}
|
||
}
|
||
return JsonResponse(errors_Dict, status=400)
|
||
|
||
|
||
def get_my_routes_ajax(request):
|
||
|
||
|
||
if request.method != 'POST':
|
||
raise Http404
|
||
|
||
lang = get_and_set_lang(request)
|
||
|
||
data = request.POST.dict()
|
||
if not data and request.body:
|
||
data = json.loads(request.body)
|
||
|
||
try:
|
||
|
||
if not request.user or request.user.is_anonymous:
|
||
msg = (f'get_my_routes_ajax not have user - user={str(request.user)}<br>'
|
||
f'data={str(data)}<br>'
|
||
f'request={str(request)}')
|
||
mail_sets = get_mail_send_options()
|
||
techSendMail(mail_sets, msg)
|
||
|
||
routes_Dict = get_routes_Dict(request.user, data)
|
||
if 'errors' in routes_Dict:
|
||
return JsonResponse(routes_Dict, status=400)
|
||
|
||
from SubscribesApp.funcs import get_cur_user_subscribe
|
||
user_subscribe = get_cur_user_subscribe(request.user)
|
||
if user_subscribe:
|
||
routes_Dict.update(user_subscribe.remains_route_adding_options())
|
||
|
||
|
||
html = render_to_string('v2/blocks/b_my_routes.html', routes_Dict, request=request)
|
||
|
||
res_Dict = {
|
||
'html': html
|
||
}
|
||
|
||
from GeneralApp.funcs import get_add_to_ajax_response_Dict
|
||
res_Dict.update(get_add_to_ajax_response_Dict(request.user))
|
||
return JsonResponse(res_Dict)
|
||
|
||
except Exception as e:
|
||
|
||
errors_Dict = {
|
||
'errors': {
|
||
'all__': f'ошибка в запросе = {str(e)}'
|
||
}
|
||
}
|
||
return JsonResponse(errors_Dict, status=400)
|
||
|
||
|
||
def get_cargo_type_by_transport_type_ajax(request):
|
||
if request.method != 'POST':
|
||
raise Http404
|
||
|
||
try:
|
||
|
||
data = request.POST
|
||
if not data:
|
||
data = json.loads(request.body)
|
||
|
||
if not data or not 'type_transport' in data:
|
||
return JsonResponse({'html': 'недостаточно данных'}, status=400)
|
||
|
||
cargo_types = get_cargo_types_by_type_transport(data['type_transport'])
|
||
|
||
return JsonResponse({'cargo_types': cargo_types})
|
||
|
||
except Exception as e:
|
||
msg = f'get_cargo_type_by_transport_type_ajax Exception = {str(e)}'
|
||
return JsonResponse({'error': msg}, status=400)
|
||
|
||
def create_or_change_route_ajax(request, route_id=None):
|
||
|
||
if request.method != 'POST':
|
||
raise Http404
|
||
|
||
if not check_option_in_cur_user_subscribe(request.user, 'размещение заявок'):
|
||
return JsonResponse({'html': 'нет доступа'}, status=403)
|
||
|
||
lang = get_and_set_lang(request)
|
||
|
||
Dict = {}
|
||
route_old_Dict = None
|
||
|
||
owner_type = None
|
||
tpl_form_by_owner_type = 'v2/forms/f_make_customer_route.html'
|
||
tpl_block_by_owner_type = 'v2/blocks/b_make_customer_route.html'
|
||
|
||
try:
|
||
|
||
data = request.POST
|
||
if not data:
|
||
data = json.loads(request.body)
|
||
|
||
|
||
if 'owner_type' in data and data['owner_type']:
|
||
owner_type = data['owner_type']
|
||
|
||
if owner_type == 'mover':
|
||
tpl_form_by_owner_type = 'v2/forms/f_make_mover_order.html'
|
||
tpl_block_by_owner_type = 'v2/blocks/b_make_mover_order.html'
|
||
|
||
route = None
|
||
if route_id:
|
||
route = Route.objects.get(id=route_id)
|
||
|
||
if route:
|
||
form = RouteForm(data, instance=route)
|
||
Dict.update({'route': route})
|
||
route_old_Dict = deepcopy(route.__dict__)
|
||
else:
|
||
form = RouteForm(data)
|
||
|
||
if not form.is_valid():
|
||
form.initial = form.cleaned_data
|
||
Dict.update({
|
||
'form': form,
|
||
'owner_type': owner_type,
|
||
})
|
||
|
||
html = render_to_string(tpl_form_by_owner_type, Dict, request=request)
|
||
return JsonResponse({'html': html}, status=400)
|
||
|
||
obj = form.save(commit=False)
|
||
if owner_type:
|
||
obj.owner_type = owner_type
|
||
|
||
# if obj.from_address_point:
|
||
# obj.from_city = get_city_by_type_transport_and_address_point(obj.type_transport, obj.from_address_point)
|
||
#
|
||
# if obj.to_address_point:
|
||
# obj.to_city = get_city_by_type_transport_and_address_point(obj.type_transport, obj.to_address_point)
|
||
|
||
if route_old_Dict:
|
||
if route_old_Dict['highlight_color'] != obj.highlight_color:
|
||
obj.highlight_color = route_old_Dict['highlight_color']
|
||
|
||
if route_old_Dict['highlight_end_DT'] != obj.highlight_end_DT:
|
||
obj.highlight_end_DT = route_old_Dict['highlight_end_DT']
|
||
|
||
if route_old_Dict['rising_DT'] != obj.rising_DT:
|
||
obj.rising_DT = route_old_Dict['rising_DT']
|
||
|
||
obj.owner = request.user
|
||
obj.save()
|
||
|
||
route_id = obj.id
|
||
|
||
routes_Dict = get_routes_Dict(request.user)
|
||
|
||
from SubscribesApp.funcs import get_cur_user_subscribe
|
||
user_subscribe = get_cur_user_subscribe(request.user)
|
||
if user_subscribe:
|
||
routes_Dict.update(user_subscribe.remains_route_adding_options())
|
||
|
||
if 'errors' in routes_Dict:
|
||
form.errors.update(routes_Dict['errors'])
|
||
Dict.update({'form': form})
|
||
html = render_to_string(tpl_form_by_owner_type, Dict, request=request)
|
||
return JsonResponse({'html': html}, status=400)
|
||
|
||
html = render_to_string('v2/blocks/b_my_routes.html', routes_Dict, request=request)
|
||
|
||
res_Dict = {
|
||
'html': html,
|
||
'route_id': route_id
|
||
}
|
||
|
||
from GeneralApp.funcs import get_add_to_ajax_response_Dict
|
||
res_Dict.update(get_add_to_ajax_response_Dict(request.user))
|
||
return JsonResponse(res_Dict)
|
||
|
||
except Exception as e:
|
||
|
||
errors_Dict = {
|
||
'errors': {
|
||
'all__': f'ошибка в запросе = {str(e)}'
|
||
}
|
||
}
|
||
# Dict = {'form': errors_Dict}
|
||
Dict.update({'form': errors_Dict})
|
||
html = render_to_string(tpl_block_by_owner_type, Dict, request=request)
|
||
return JsonResponse({'html': html}, status=400) |