319 lines
8.6 KiB
Python
319 lines
8.6 KiB
Python
import json
|
|
|
|
from django.http import HttpResponse, Http404, FileResponse
|
|
from django.template import loader, RequestContext
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from ReferenceDataApp.models import Country, City, Airport
|
|
from .models import *
|
|
from django.conf import settings
|
|
from .funcs import get_inter_http_response
|
|
from django.http.response import JsonResponse, HttpResponse
|
|
from django.views.decorators.http import require_GET, require_POST
|
|
from django.shortcuts import get_object_or_404
|
|
from django.contrib.auth.models import User
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from webpush import send_user_notification
|
|
import json
|
|
from datetime import datetime, timedelta, date
|
|
|
|
|
|
def generate_routes(request, routes_count):
|
|
if (not request.user
|
|
or not request.user.is_active
|
|
or not request.user.is_authenticated
|
|
or not request.user.is_staff
|
|
):
|
|
raise Http404
|
|
|
|
from RoutesApp.models import Route
|
|
from ReferenceDataApp.models import Airport, City
|
|
|
|
res = None
|
|
|
|
from_air = Airport.objects.get(iata_code='MSQ')
|
|
to_air = Airport.objects.get(iata_code='SVO')
|
|
|
|
routes = [
|
|
Route(
|
|
type_transport='road',
|
|
departure_DT=datetime.now() + timedelta(days=7),
|
|
arrival_DT=datetime.now() + timedelta(days=8),
|
|
from_address_point=to_air.city.id,
|
|
to_address_point=from_air.city.id,
|
|
from_city=to_air.city,
|
|
to_city=from_air.city,
|
|
weight=item,
|
|
phone='0987654321',
|
|
owner=request.user
|
|
) for item in range(routes_count)
|
|
]
|
|
|
|
Route.objects.bulk_create(routes)
|
|
|
|
if res:
|
|
if type(res) == str:
|
|
return HttpResponse(res)
|
|
else:
|
|
return res
|
|
|
|
return HttpResponse('finished')
|
|
|
|
|
|
def export_cities():
|
|
|
|
def fix_fields_for_json(items_list):
|
|
rec_list = []
|
|
for rec in list(recs):
|
|
rec_Dict = {}
|
|
for key, val in rec.items():
|
|
if type(val) in [datetime, date]:
|
|
val = str(val)
|
|
rec_Dict[key] = val
|
|
rec_list.append(rec_Dict)
|
|
return rec_list
|
|
|
|
fields = [item.name for item in Country.objects.model._meta.fields if item.name not in ['name_plural']]
|
|
recs = Country.objects.all().values(*fields)
|
|
rec_list = fix_fields_for_json(recs)
|
|
with open('countries.json', 'w', encoding='utf-8') as f:
|
|
f.write(json.dumps(rec_list, ensure_ascii=False, indent=4))
|
|
|
|
fields = [item.name for item in City.objects.model._meta.fields if item.name not in ['name_plural']]
|
|
recs = City.objects.all().values(*fields)
|
|
rec_list = fix_fields_for_json(recs)
|
|
with open('cities.json', 'w', encoding='utf-8') as f:
|
|
f.write(json.dumps(rec_list, ensure_ascii=False, indent=4))
|
|
|
|
fields = [item.name for item in Airport.objects.model._meta.fields if item.name not in ['name_plural']]
|
|
recs = Airport.objects.all().values()
|
|
rec_list = fix_fields_for_json(recs)
|
|
with open('airports.json', 'w', encoding='utf-8') as f:
|
|
f.write(json.dumps(rec_list, ensure_ascii=False, indent=4))
|
|
|
|
return True
|
|
|
|
|
|
def test_code(request):
|
|
|
|
if (not request.user
|
|
or not request.user.is_active
|
|
or not request.user.is_authenticated
|
|
or not request.user.is_staff
|
|
):
|
|
raise Http404
|
|
|
|
res = ''
|
|
|
|
export_cities()
|
|
|
|
# from RoutesApp.search_matches import search_matches
|
|
# from RoutesApp.models import Route
|
|
# search_matches(Route.objects.filter(id=16))
|
|
|
|
# from RoutesApp.funcs import get_city_by_type_transport_and_address_point
|
|
# from RoutesApp.models import Route
|
|
# from ReferenceDataApp.models import Airport, City
|
|
#
|
|
# res = None
|
|
#
|
|
# from_air = Airport.objects.get(iata_code='MSQ')
|
|
# to_air = Airport.objects.get(iata_code='SVO')
|
|
#
|
|
# routes = [
|
|
# Route(
|
|
# type_transport='road',
|
|
# departure_DT=datetime.now() + timedelta(days=7),
|
|
# arrival_DT=datetime.now() + timedelta(days=8),
|
|
# from_address_point=to_air.city.id,
|
|
# to_address_point=from_air.city.id,
|
|
# from_city=to_air.city,
|
|
# to_city=from_air.city,
|
|
# weight=item,
|
|
# phone='0987654321',
|
|
# owner=request.user
|
|
# ) for item in range(100)
|
|
# ]
|
|
|
|
# routes = [
|
|
# Route(
|
|
# type_transport='avia',
|
|
# departure_DT=datetime(year=2024, month=9, day=1),
|
|
# arrival_DT=datetime(year=2024, month=9, day=3),
|
|
# from_address_point = from_air.id,
|
|
# to_address_point = to_air.id,
|
|
# from_city = from_air.city,
|
|
# to_city = to_air.city,
|
|
# weight = item,
|
|
# phone = '1234567890',
|
|
# owner = request.user
|
|
# ) for item in range(1000)
|
|
# ]
|
|
#
|
|
# Route.objects.bulk_create(routes)
|
|
|
|
|
|
# from RoutesApp.search_matches import search_matches
|
|
# routes = Route.objects.filter()[:10]
|
|
# msg = search_matches(routes)
|
|
|
|
# from ReferenceDataApp.funcs import parse_data
|
|
# parse_data()
|
|
|
|
# from SubscribesApp.reports import send_mail_for_user_subscribes_that_is_going_to_finish
|
|
# send_mail_for_user_subscribes_that_is_going_to_finish()
|
|
|
|
if res:
|
|
if type(res) == str:
|
|
return HttpResponse(res)
|
|
else:
|
|
return res
|
|
|
|
return HttpResponse('finished')
|
|
|
|
|
|
|
|
def Page404(request, exeption=None):
|
|
|
|
Dict = {}
|
|
|
|
t = loader.get_template('404.html')
|
|
try:
|
|
res = get_inter_http_response(t, Dict, request)
|
|
return HttpResponse(res, status=404)
|
|
except Exception as e:
|
|
return HttpResponse(str(e))
|
|
|
|
def LandingMoverPage(request):
|
|
|
|
from .init_options import init_options
|
|
init_options()
|
|
|
|
|
|
print(f'LOCALE_PATHS = {str(settings.LOCALE_PATHS)}')
|
|
|
|
page, is_created = StaticPage.objects.get_or_create(url='landing_mover')
|
|
|
|
Dict = {
|
|
'page': page,
|
|
}
|
|
|
|
|
|
|
|
breadcrumbs_Dict = {
|
|
}
|
|
Dict.update({'breadcrumbs': breadcrumbs_Dict})
|
|
|
|
t = loader.get_template('pages/p_mover_landing_page.html')
|
|
return get_inter_http_response(t, Dict, request)
|
|
|
|
|
|
def LandingCustomerPage(request):
|
|
|
|
from .init_options import init_options
|
|
init_options()
|
|
|
|
|
|
print(f'LOCALE_PATHS = {str(settings.LOCALE_PATHS)}')
|
|
|
|
page, is_created = StaticPage.objects.get_or_create(url='landing_customer')
|
|
|
|
Dict = {
|
|
'page': page,
|
|
}
|
|
|
|
|
|
|
|
breadcrumbs_Dict = {
|
|
}
|
|
Dict.update({'breadcrumbs': breadcrumbs_Dict})
|
|
|
|
t = loader.get_template('pages/p_customer_landing_page.html')
|
|
return get_inter_http_response(t, Dict, request)
|
|
|
|
|
|
def MainPage(request):
|
|
from RoutesApp.forms import RouteForm
|
|
|
|
from .init_options import init_options
|
|
init_options()
|
|
|
|
|
|
print(f'LOCALE_PATHS = {str(settings.LOCALE_PATHS)}')
|
|
|
|
page = StaticPage.objects.get(url='main')
|
|
|
|
from ArticlesApp.models import ArticleModel
|
|
arts = ArticleModel.objects.filter(enable=True).order_by('-createDT')[:3]
|
|
|
|
owner_type = 'mover'
|
|
|
|
Dict = {
|
|
'page': page,
|
|
'FAQ': page.FAQ_items.filter(enable=True),
|
|
'route_form': RouteForm(owner_type=owner_type),
|
|
'articles': arts,
|
|
'owner_type': 'mover'
|
|
}
|
|
|
|
|
|
|
|
breadcrumbs_Dict = {
|
|
}
|
|
Dict.update({'breadcrumbs': breadcrumbs_Dict})
|
|
|
|
t = loader.get_template('pages/p_main.html')
|
|
return get_inter_http_response(t, Dict, request)
|
|
# return HttpResponse(t.render(Dict, request))
|
|
|
|
|
|
|
|
def StaticPageView(request, url):
|
|
from RoutesApp.forms import RouteForm
|
|
from SubscribesApp.funcs import get_subscribes_w_options
|
|
|
|
Dict = {}
|
|
|
|
owner_type = None
|
|
if url == '':
|
|
return MainPage(request)
|
|
elif url == 'for_movers':
|
|
owner_type = 'customer'
|
|
elif url == 'for_customers':
|
|
owner_type = 'mover'
|
|
if owner_type:
|
|
Dict.update({
|
|
'route_form': RouteForm(owner_type=owner_type),
|
|
'owner_type': owner_type,
|
|
})
|
|
|
|
# elif url == 'works':
|
|
# return WorksPage(request)
|
|
elif url in ['main']:
|
|
raise Http404
|
|
|
|
if url in ['for_movers', 'for_customers']:
|
|
subscribes, all_options = get_subscribes_w_options()
|
|
Dict.update({
|
|
'subscribes': subscribes,
|
|
})
|
|
|
|
try:
|
|
page = StaticPage.objects.get(url=url)
|
|
except StaticPage.DoesNotExist:
|
|
raise Http404
|
|
|
|
|
|
Dict.update({
|
|
'page': page,
|
|
})
|
|
|
|
# from PushMessages.views import send_push
|
|
# send_push(user=request.user, title='title', text='text')
|
|
|
|
t = loader.get_template('pages/p_static_page.html')
|
|
return get_inter_http_response(t, Dict, request)
|
|
# return HttpResponse(t.render(Dict, request))
|
|
|
|
|