files in chat download
This commit is contained in:
SDE
2023-09-17 18:31:50 +03:00
parent 1747b0ebb5
commit da7b1cb549
4 changed files with 49 additions and 1 deletions

View File

@@ -13,4 +13,6 @@ urlpatterns = [
# path('send_msg/', send_msg_ajax, name='send_msg_ajax'), # path('send_msg/', send_msg_ajax, name='send_msg_ajax'),
# path('update_chat/', update_chat_ajax2, name='update_chat_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('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'),
] ]

View File

@@ -18,6 +18,31 @@ from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync 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/') @login_required(login_url='/profile/login/')
def show_chat_w_user_ajax(request): def show_chat_w_user_ajax(request):

View File

@@ -16,6 +16,27 @@ register = template.Library()
def get_ws_address(): def get_ws_address():
return settings.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 @register.simple_tag
def get_msg_side(cur_user, ticket, msg): def get_msg_side(cur_user, ticket, msg):

View File

@@ -20,7 +20,7 @@
</div> </div>
<div class="right_part_file_w"> <div class="right_part_file_w">
<div>{{ file.file_name }}</div> <div>{{ file.file_name }}</div>
<div>{{ file.file_size }}</div> <div>{% get_filesize file.file_size %}</div>
</div> </div>
<div class="clear_both"></div> <div class="clear_both"></div>
</div> </div>