diff --git a/ChatServiceApp/js_urls.py b/ChatServiceApp/js_urls.py index 41e9561..139b91c 100644 --- a/ChatServiceApp/js_urls.py +++ b/ChatServiceApp/js_urls.py @@ -13,4 +13,6 @@ urlpatterns = [ # path('send_msg/', send_msg_ajax, name='send_msg_ajax'), # path('update_chat/', update_chat_ajax2, name='update_chat_ajax'), path('show_chat_w_user/', show_chat_w_user_ajax, name='show_chat_w_user_ajax'), + + path('get_file_from_msg/', get_file_from_msg_ajax, name='get_file_from_msg_ajax'), ] \ No newline at end of file diff --git a/ChatServiceApp/js_views.py b/ChatServiceApp/js_views.py index 2fda826..a7b9512 100644 --- a/ChatServiceApp/js_views.py +++ b/ChatServiceApp/js_views.py @@ -18,6 +18,31 @@ from channels.layers import get_channel_layer from asgiref.sync import async_to_sync +@login_required(login_url='/profile/login/') +def get_file_from_msg_ajax(request): + + if request.method != 'POST': + raise Http404 + + try: + + data = json.loads(request.body) + msg = Message.objects.get(id=data['message_id']) + + res_Dict = {} + for file in msg.files: + if file['file_name'] == data['file_name']: + res_Dict = file + break + + return JsonResponse(res_Dict, status=200) + + except Exception as e: + msg = f'get_file_from_msg_ajax Error = {str(e)}' + return JsonResponse({'error': msg}, status=400) + + + @login_required(login_url='/profile/login/') def show_chat_w_user_ajax(request): diff --git a/ChatServiceApp/templatetags/tt_chat.py b/ChatServiceApp/templatetags/tt_chat.py index 6a99e84..c0813c6 100644 --- a/ChatServiceApp/templatetags/tt_chat.py +++ b/ChatServiceApp/templatetags/tt_chat.py @@ -16,6 +16,27 @@ register = template.Library() def get_ws_address(): return settings.WS_ADDRESS +@register.simple_tag +def get_filesize(size): + if size: + unit = 'B' + if size / 1024 > 1: + unit = 'KB' + size = size / 1024 + if size / 1024 > 1: + unit = 'MB' + size = size / 1024 + if size / 1024 > 1: + unit = 'GB' + size = size / 1024 + if size / 1024 > 1: + unit = 'TB' + size = size / 1024 + size = round(size, 2) + return f'{str(size)}{unit}' + else: + return '' + @register.simple_tag def get_msg_side(cur_user, ticket, msg): diff --git a/templates/widgets/w_message.html b/templates/widgets/w_message.html index 0bb1a4b..6702472 100644 --- a/templates/widgets/w_message.html +++ b/templates/widgets/w_message.html @@ -20,7 +20,7 @@