diff --git a/archilance/settings/base.py b/archilance/settings/base.py index 2c61f25..4327834 100644 --- a/archilance/settings/base.py +++ b/archilance/settings/base.py @@ -249,16 +249,17 @@ REST_FRAMEWORK = { SITE_ID = 1 -EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' +# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'localhost' EMAIL_HOST_PASSWORD = '' EMAIL_HOST_USER = '' EMAIL_PORT = 25 -EMAIL_USE_TLS = False -EMAIL_USE_SSL = False -EMAIL_TIMEOUT = 60 -EMAIL_DEFAULT = 'noreply@archilance.ru' +# EMAIL_USE_TLS = False +# EMAIL_USE_SSL = False +# EMAIL_TIMEOUT = 60 +# EMAIL_DEFAULT = 'noreply@archilance.ru' +EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' SHELL_PLUS_POST_IMPORTS = ( # Extra auto imports 'natsort', diff --git a/archilance/views.py b/archilance/views.py index adc0eab..b397333 100644 --- a/archilance/views.py +++ b/archilance/views.py @@ -1,8 +1,10 @@ from django.views.generic import TemplateView, View from django.shortcuts import render +from django.template.loader import render_to_string from projects.models import Order -from common.models import MainPage +from common.models import MainPage, PrintDocuments + class HomeTemplateView(View): template_name = 'home.html' @@ -11,9 +13,33 @@ class HomeTemplateView(View): main_settings = MainPage.objects.get(pk=1) return render(request, self.template_name, {'main_settings': main_settings}) + class TestChatTemplateView(View): template_name = 'chat_test.html' - def get(self,request,*args,**kwargs): - order = Order.objects.get(pk=6) - return render(request, self.template_name, {'order': order}) + def get(self, request, *args, **kwargs): + from common.models import PrintDocuments + + print_documents = PrintDocuments.objects.all() + limit_size = 10 * 1024 * 1024 + attachments = [] + link_files = [] + + for f in print_documents: + if f.file.size > limit_size: + link_files.append(f.file.path) + else: + attachments.append(f.file.path) + + + html_content = render_to_string('document_email.txt', + { + 'username': 'Mukhtar', + 'phone': '89634004278', + 'files': link_files, + } + ) + + print(html_content) + + return render(request, 'chat_test.html', {'html_content': html_content}) diff --git a/common/management/__init__.py b/common/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/common/templates/document_email.html b/common/templates/document_email.html index 547defd..6be5a45 100644 --- a/common/templates/document_email.html +++ b/common/templates/document_email.html @@ -1,4 +1,10 @@ -Welcome {{ username }}! +Заявка на распечатку от {{ username }}! -Your username is {{ username }} and your password is {{ files }}. +Контактные данные: +Телефон : {{ phone }} + +

Прикрепленные файлы

+{% for f in files %} +

{{ f }}

+{% endfor %} diff --git a/common/templates/document_email.txt b/common/templates/document_email.txt new file mode 100644 index 0000000..f35e637 --- /dev/null +++ b/common/templates/document_email.txt @@ -0,0 +1,10 @@ + +Заявка на распечатку от {{ username }}! + +Контактные данные: +Телефон : {{ phone }} + +Прикрепленные файлы +{% for f in files %} + {{ f }} +{% endfor %} diff --git a/common/views.py b/common/views.py index ef0b94d..b360bea 100644 --- a/common/views.py +++ b/common/views.py @@ -1,9 +1,7 @@ from django.shortcuts import render, redirect from django.contrib import messages -from django.core.mail import send_mail -from django.template.loader import get_template -from django.template import Context -from django.http import HttpResponse +from django.core.mail import send_mail, EmailMultiAlternatives +from django.template.loader import get_template, render_to_string from django.views.generic import View from archilance.mixins import BaseMixin @@ -29,17 +27,33 @@ class PrintDocumentCreate(BaseMixin, View): print_doc = PrintDocuments.objects.create(file=file, printorder=print_order) print_doc.save() - send_mail('Заявка на распечатку', - get_template('document_email.html').render( - { - 'username': 'Mukhtar', - 'files': 'This is files list' - } - ), - 'dagdahzub@mail.ru', - ['muhtarzubanchi05@gmail.com'], - fail_silently=True - ) + print_documents = PrintDocuments.objects.all() + limit_size = 10 * 1024 * 1024 + attachments = [] + link_files = [] + + for f in print_documents: + if f.file.size > limit_size: + link_files.append(f.file.path) + else: + attachments.append(f.file.path) + + ctx_dict = { + 'username': print_order.sender, + 'phone': print_order.phone, + 'address': print_order.address, + 'files': link_files, + } + + subject, from_email, to = 'Заявка на распечатку', 'mukhtar@mukhtar', 'muhtarzubanchi05@gmail.com' + text_content = render_to_string('document_email.txt', ctx_dict) + html_content = get_template('document_email.html').render(ctx_dict) + msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) + msg.attach_alternative(html_content, "text/html") + for attach in attachments: + msg.attach_file(attach) + msg.send() + messages.info(request, 'Заявка на распечатку принята') return redirect('common:create') # import code; code.interact(local=dict(globals(), **locals())) diff --git a/templates/chat_test.html b/templates/chat_test.html index c39479f..ac875bb 100644 --- a/templates/chat_test.html +++ b/templates/chat_test.html @@ -10,61 +10,8 @@
- {% for msg in order.order_messages.all %} -

{{msg}} Автор: {{msg.sender.get_full_name}}

- {% endfor%} + {{ html_content }}
-
- - -
- - -