0.7.33
files in chat download
This commit is contained in:
@@ -107,6 +107,9 @@ def get_update_chat_Dict(data):
|
||||
|
||||
def send_msg(data):
|
||||
from AuthApp.models import User
|
||||
import base64
|
||||
import os
|
||||
|
||||
res_Dict = {}
|
||||
msg = None
|
||||
required_update_tickets_list_wo_managers = False
|
||||
@@ -178,13 +181,27 @@ def send_msg(data):
|
||||
if 'text' in data:
|
||||
msg_create_kwargs.update({'text': data['text']})
|
||||
|
||||
msg = Message.objects.create(**msg_create_kwargs)
|
||||
|
||||
if 'files' in data:
|
||||
files_list = []
|
||||
for file in data['files']:
|
||||
files_list.append(json.loads(file))
|
||||
msg_create_kwargs.update({'files': files_list})
|
||||
file_data =json.loads(file)
|
||||
|
||||
if not os.path.exists(f'chat_file_storage/{msg.id}'):
|
||||
os.makedirs(f'chat_file_storage/{msg.id}')
|
||||
f = open(f'chat_file_storage/{msg.id}/{file_data["file_name"]}', 'wb+')
|
||||
head, content = file_data['file'].split(',')
|
||||
content = base64.b64decode(content)
|
||||
f.write(content)
|
||||
f.close()
|
||||
|
||||
del file_data['file']
|
||||
files_list.append(file_data)
|
||||
msg.files = files_list
|
||||
msg.save(update_fields=['files'])
|
||||
|
||||
|
||||
msg = Message.objects.create(**msg_create_kwargs)
|
||||
|
||||
# if ticket:
|
||||
# msgs = get_messages_for_ticket(ticket)
|
||||
|
||||
11
ChatServiceApp/urls.py
Normal file
11
ChatServiceApp/urls.py
Normal file
@@ -0,0 +1,11 @@
|
||||
# coding=utf-8
|
||||
from django.urls import path
|
||||
# from AuthApp.js_views import *
|
||||
# from AuthApp.import_funcs import *
|
||||
from .views import *
|
||||
from django.contrib.auth import views
|
||||
from RoutesApp.js_views import new_route_view_ajax
|
||||
|
||||
urlpatterns = [
|
||||
path('get_file/<str:msg_id>/<str:file_name>', get_file_from_message, name='get_file_from_message'),
|
||||
]
|
||||
@@ -1,3 +1,26 @@
|
||||
from django.shortcuts import render
|
||||
import json
|
||||
|
||||
# Create your views here.
|
||||
from django.http import HttpResponse, Http404, FileResponse
|
||||
from django.template import loader, RequestContext
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from .models import *
|
||||
from django.conf import settings
|
||||
|
||||
def get_file_from_message(request, msg_id, file_name):
|
||||
from django.http import FileResponse
|
||||
|
||||
try:
|
||||
|
||||
msg = Message.objects.get(id=msg_id)
|
||||
|
||||
if request.user not in (msg.sender, msg.receiver):
|
||||
raise Http404
|
||||
|
||||
for file in msg.files:
|
||||
if file['file_name'] == file_name:
|
||||
f = open(f'chat_file_storage/{msg.id}/{file["file_name"]}', 'rb')
|
||||
|
||||
return FileResponse(f)
|
||||
|
||||
except Exception as e:
|
||||
raise Http404
|
||||
|
||||
@@ -7,9 +7,9 @@ from django.conf import settings
|
||||
urlpatterns = [
|
||||
# path('admin/', admin.site.urls),
|
||||
path('ckeditor/', include('ckeditor_uploader.urls')),
|
||||
|
||||
|
||||
path('i18n/', include('django.conf.urls.i18n')),
|
||||
|
||||
path('messages/', include('ChatServiceApp.urls')),
|
||||
]
|
||||
|
||||
from django.conf.urls.i18n import i18n_patterns
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
{% for file in msg.files %}
|
||||
<div class="file_border_cont {{ msg.id }}_ident">
|
||||
<div class="left_part_file_w">
|
||||
<a href="" download><img class="img_download_file {% get_msg_side user ticket msg %}" src="{% static "/img/svg/download_file.svg" %}"></a>
|
||||
<a href="{% url 'get_file_from_message' msg.id file.file_name %}" download><img class="img_download_file {% get_msg_side user ticket msg %}" src="{% static "/img/svg/download_file.svg" %}"></a>
|
||||
</div>
|
||||
<div class="right_part_file_w {% get_msg_side user ticket msg %}">
|
||||
<div>{{ file.file_name }}</div>
|
||||
|
||||
Reference in New Issue
Block a user