2.1.26 check_post_request_and_get_data
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.http import HttpResponse, Http404, FileResponse
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
from BaseModels.exceptions import exception_processing
|
||||||
|
from BaseModels.print_funcs import print_ext
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_and_set_lang(request):
|
def get_and_set_lang(request):
|
||||||
from django.utils.translation import activate, get_language
|
from django.utils.translation import activate, get_language
|
||||||
@@ -30,6 +36,27 @@ def get_and_set_lang(request):
|
|||||||
return lang
|
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):
|
def get_add_to_ajax_response_Dict(user):
|
||||||
context_Dict = {}
|
context_Dict = {}
|
||||||
|
|
||||||
|
|||||||
@@ -18,31 +18,30 @@ 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 *
|
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
|
from SubscribesApp.funcs import check_option_in_cur_user_subscribe
|
||||||
|
|
||||||
|
|
||||||
def highlight_route_ajax(request):
|
def highlight_route_ajax(request):
|
||||||
if request.method != 'POST':
|
data = check_post_request_and_get_data(request)
|
||||||
raise Http404
|
if data == None:
|
||||||
|
return Http404
|
||||||
data = request.POST
|
elif type(data) == str:
|
||||||
if not data and request.body:
|
return JsonResponse({'error': data}, status=400)
|
||||||
data = json.loads(request.body)
|
|
||||||
|
|
||||||
if not data or not 'route_id' in data:
|
if not data or not 'route_id' in data:
|
||||||
msg = _('Недостаточно данных')
|
msg = _('Недостаточно данных')
|
||||||
return JsonResponse({'errors': msg})
|
return JsonResponse({'errors': msg}, status=400)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
route = Route.objects.get(owner=request.user, id=data['route_id'])
|
route = Route.objects.get(owner=request.user, id=data['route_id'])
|
||||||
except Route.DoesNotExist:
|
except Route.DoesNotExist:
|
||||||
msg = _('Не найден маршрут')
|
msg = _('Не найден маршрут')
|
||||||
return JsonResponse({'errors': msg})
|
return JsonResponse({'errors': msg}, status=400)
|
||||||
|
|
||||||
if not route.get_permission_for_highlight():
|
if not route.get_permission_for_highlight():
|
||||||
msg = _('Нет доступа к выделению')
|
msg = _('Нет доступа к выделению')
|
||||||
return JsonResponse({'errors': msg})
|
return JsonResponse({'errors': msg}, status=403)
|
||||||
|
|
||||||
|
|
||||||
from SubscribesApp.funcs import get_cur_user_subscribe
|
from SubscribesApp.funcs import get_cur_user_subscribe
|
||||||
@@ -75,12 +74,11 @@ def highlight_route_ajax(request):
|
|||||||
|
|
||||||
|
|
||||||
def raise_route_ajax(request):
|
def raise_route_ajax(request):
|
||||||
if request.method != 'POST':
|
data = check_post_request_and_get_data(request)
|
||||||
raise Http404
|
if data == None:
|
||||||
|
return Http404
|
||||||
data = request.POST
|
elif type(data) == str:
|
||||||
if not data and request.body:
|
return JsonResponse({'error': data}, status=400)
|
||||||
data = json.loads(request.body)
|
|
||||||
|
|
||||||
if not data or not 'route_id' in data:
|
if not data or not 'route_id' in data:
|
||||||
msg = _('Недостаточно данных')
|
msg = _('Недостаточно данных')
|
||||||
@@ -94,7 +92,7 @@ def raise_route_ajax(request):
|
|||||||
|
|
||||||
if not route.get_permission_for_raise():
|
if not route.get_permission_for_raise():
|
||||||
msg = _('Нет доступных поднятий')
|
msg = _('Нет доступных поднятий')
|
||||||
return JsonResponse({'errors': msg}, status=400)
|
return JsonResponse({'errors': msg}, status=403)
|
||||||
|
|
||||||
route.rising_DT = datetime.now()
|
route.rising_DT = datetime.now()
|
||||||
route.save(update_fields=['rising_DT'])
|
route.save(update_fields=['rising_DT'])
|
||||||
@@ -113,8 +111,11 @@ def raise_route_ajax(request):
|
|||||||
|
|
||||||
|
|
||||||
def del_route_ajax(request):
|
def del_route_ajax(request):
|
||||||
if request.method != 'POST':
|
data = check_post_request_and_get_data(request)
|
||||||
raise Http404
|
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, 'размещение заявок'):
|
if not check_option_in_cur_user_subscribe(request.user, 'размещение заявок'):
|
||||||
return JsonResponse({'html': 'нет доступа'}, status=403)
|
return JsonResponse({'html': 'нет доступа'}, status=403)
|
||||||
@@ -123,7 +124,7 @@ def del_route_ajax(request):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
data = json.loads(request.body)
|
# data = json.loads(request.body)
|
||||||
if not 'route_id' in data:
|
if not 'route_id' in data:
|
||||||
msg = f'Недостаточно данных'
|
msg = f'Недостаточно данных'
|
||||||
return JsonResponse({'errors': msg})
|
return JsonResponse({'errors': msg})
|
||||||
@@ -153,15 +154,18 @@ def del_route_ajax(request):
|
|||||||
|
|
||||||
|
|
||||||
def edit_route_ajax(request):
|
def edit_route_ajax(request):
|
||||||
if request.method != 'POST':
|
data = check_post_request_and_get_data(request)
|
||||||
raise Http404
|
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, 'размещение заявок'):
|
if not check_option_in_cur_user_subscribe(request.user, 'размещение заявок'):
|
||||||
return JsonResponse({'html': 'нет доступа'}, status=403)
|
return JsonResponse({'html': 'нет доступа'}, status=403)
|
||||||
|
|
||||||
lang = get_and_set_lang(request)
|
lang = get_and_set_lang(request)
|
||||||
|
|
||||||
data = json.loads(request.body)
|
# data = json.loads(request.body)
|
||||||
|
|
||||||
Dict = {}
|
Dict = {}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user