Merge remote-tracking branch 'origin/v2' into v2
This commit is contained in:
49
BaseModels/exceptions.py
Normal file
49
BaseModels/exceptions.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import asyncio
|
||||
import traceback
|
||||
|
||||
from asgiref.sync import sync_to_async
|
||||
|
||||
from BaseModels.mailSender import techSendMail
|
||||
from BaseModels.print_funcs import print_ext
|
||||
from GeneralApp.funcs_options import get_mail_send_options
|
||||
|
||||
# from MessageBotsApp.telegram.tg_bot import send_message
|
||||
|
||||
|
||||
async def send_exception_msg(msgr_msg, mail_msg):
|
||||
# from MessageBotsApp.funcs import send_msg_to_staff
|
||||
|
||||
|
||||
mail_sets = await sync_to_async(get_mail_send_options)()
|
||||
# await send_msg_to_staff('telegram', msgr_msg)
|
||||
|
||||
await sync_to_async(techSendMail)(sets=mail_sets, html_content=mail_msg, title='iBaked Exception')
|
||||
|
||||
return True
|
||||
|
||||
async def exception_processing(exc, user=None):
|
||||
tb = traceback.format_exc()
|
||||
cutted_tb = tb[:500]
|
||||
|
||||
msgr_msg = f'user {str(user)} Exception = {str(exc)}\n{str(cutted_tb)}'
|
||||
mail_msg = f'user {str(user)} Exception = {str(exc)}<br>\n{str(tb)}'
|
||||
print_ext(msgr_msg)
|
||||
|
||||
# try:
|
||||
# loop = asyncio.get_event_loop()
|
||||
# except RuntimeError:
|
||||
# loop = asyncio.new_event_loop()
|
||||
# asyncio.set_event_loop(loop)
|
||||
#
|
||||
|
||||
# loop = asyncio.new_event_loop()
|
||||
# asyncio.set_event_loop(loop)
|
||||
# async_result = loop.run_until_complete(send_exception_msg(msgr_msg, mail_msg))
|
||||
# loop.close()
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
task = loop.create_task(send_exception_msg(msgr_msg, mail_msg))
|
||||
|
||||
status_code = 400
|
||||
|
||||
return msgr_msg, status_code
|
||||
@@ -1,6 +1,12 @@
|
||||
from asgiref.sync import async_to_sync
|
||||
from django.http import HttpResponse, Http404, FileResponse
|
||||
from django.conf import settings
|
||||
|
||||
from BaseModels.exceptions import exception_processing
|
||||
from BaseModels.print_funcs import print_ext
|
||||
|
||||
|
||||
|
||||
|
||||
def get_and_set_lang(request):
|
||||
from django.utils.translation import activate, get_language
|
||||
@@ -30,6 +36,27 @@ def get_and_set_lang(request):
|
||||
return lang
|
||||
|
||||
|
||||
import json
|
||||
from urllib.parse import unquote
|
||||
def check_post_request_and_get_data(request, allow_unauthorized=False):
|
||||
if request.method != 'POST':
|
||||
return None
|
||||
|
||||
if not allow_unauthorized:
|
||||
if not request.user or not request.user.is_authenticated:
|
||||
return None
|
||||
|
||||
try:
|
||||
data = request.POST.dict()
|
||||
if not data and request.body:
|
||||
data = json.loads(unquote(request.body))
|
||||
except Exception as e:
|
||||
msg, status_code = async_to_sync(exception_processing)(e, request.user)
|
||||
return msg
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def get_add_to_ajax_response_Dict(user):
|
||||
context_Dict = {}
|
||||
|
||||
|
||||
@@ -21,31 +21,30 @@ 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 GeneralApp.funcs import get_and_set_lang, check_post_request_and_get_data
|
||||
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)
|
||||
data = check_post_request_and_get_data(request)
|
||||
if data == None:
|
||||
return Http404
|
||||
elif type(data) == str:
|
||||
return JsonResponse({'error': data}, status=400)
|
||||
|
||||
if not data or not 'route_id' in data:
|
||||
msg = _('Недостаточно данных')
|
||||
return JsonResponse({'errors': msg})
|
||||
return JsonResponse({'errors': msg}, status=400)
|
||||
|
||||
try:
|
||||
route = Route.objects.get(owner=request.user, id=data['route_id'])
|
||||
except Route.DoesNotExist:
|
||||
msg = _('Не найден маршрут')
|
||||
return JsonResponse({'errors': msg})
|
||||
return JsonResponse({'errors': msg}, status=400)
|
||||
|
||||
if not route.get_permission_for_highlight():
|
||||
msg = _('Нет доступа к выделению')
|
||||
return JsonResponse({'errors': msg})
|
||||
return JsonResponse({'errors': msg}, status=403)
|
||||
|
||||
|
||||
from SubscribesApp.funcs import get_cur_user_subscribe
|
||||
@@ -78,12 +77,11 @@ def highlight_route_ajax(request):
|
||||
|
||||
|
||||
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)
|
||||
data = check_post_request_and_get_data(request)
|
||||
if data == None:
|
||||
return Http404
|
||||
elif type(data) == str:
|
||||
return JsonResponse({'error': data}, status=400)
|
||||
|
||||
if not data or not 'route_id' in data:
|
||||
msg = _('Недостаточно данных')
|
||||
@@ -97,7 +95,7 @@ def raise_route_ajax(request):
|
||||
|
||||
if not route.get_permission_for_raise():
|
||||
msg = _('Нет доступных поднятий')
|
||||
return JsonResponse({'errors': msg}, status=400)
|
||||
return JsonResponse({'errors': msg}, status=403)
|
||||
|
||||
route.rising_DT = datetime.now()
|
||||
route.save(update_fields=['rising_DT'])
|
||||
@@ -116,8 +114,11 @@ def raise_route_ajax(request):
|
||||
|
||||
|
||||
def del_route_ajax(request):
|
||||
if request.method != 'POST':
|
||||
raise Http404
|
||||
data = check_post_request_and_get_data(request)
|
||||
if data == None:
|
||||
return Http404
|
||||
elif type(data) == str:
|
||||
return JsonResponse({'error': data}, status=400)
|
||||
|
||||
if not check_option_in_cur_user_subscribe(request.user, 'размещение заявок'):
|
||||
return JsonResponse({'html': 'нет доступа'}, status=403)
|
||||
@@ -126,7 +127,7 @@ def del_route_ajax(request):
|
||||
|
||||
try:
|
||||
|
||||
data = json.loads(request.body)
|
||||
# data = json.loads(request.body)
|
||||
if not 'route_id' in data:
|
||||
msg = f'Недостаточно данных'
|
||||
return JsonResponse({'errors': msg})
|
||||
@@ -156,15 +157,18 @@ def del_route_ajax(request):
|
||||
|
||||
|
||||
def edit_route_ajax(request):
|
||||
if request.method != 'POST':
|
||||
raise Http404
|
||||
data = check_post_request_and_get_data(request)
|
||||
if data == None:
|
||||
return Http404
|
||||
elif type(data) == str:
|
||||
return JsonResponse({'error': data}, status=400)
|
||||
|
||||
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)
|
||||
# data = json.loads(request.body)
|
||||
|
||||
Dict = {}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user