From a9f3d3de469507cd083230a4c52fa6fd6d00a15f Mon Sep 17 00:00:00 2001 From: Anatoly Date: Thu, 6 Feb 2020 17:16:31 +0300 Subject: [PATCH] added report template and enabled send an report emails --- apps/report/migrations/0002_report_locale.py | 18 +++++++ apps/report/models.py | 51 +++++++++++++++---- apps/report/serializers.py | 13 ++++- project/settings/base.py | 3 +- .../report/tech_support_template.html | 10 ++-- 5 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 apps/report/migrations/0002_report_locale.py diff --git a/apps/report/migrations/0002_report_locale.py b/apps/report/migrations/0002_report_locale.py new file mode 100644 index 00000000..d49c2ded --- /dev/null +++ b/apps/report/migrations/0002_report_locale.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.7 on 2020-02-06 13:58 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('report', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='report', + name='locale', + field=models.CharField(max_length=10, null=True, verbose_name='locale'), + ), + ] diff --git a/apps/report/models.py b/apps/report/models.py index b89e992d..856d1f04 100644 --- a/apps/report/models.py +++ b/apps/report/models.py @@ -1,22 +1,26 @@ from django.conf import settings from django.core.mail import send_mail from django.db import models +from django.template.loader import render_to_string +from django.utils.functional import cached_property from django.utils.text import gettext_lazy as _ from report.tasks import send_report_task +from translation.models import SiteInterfaceDictionary from utils.models import ProjectBaseMixin class ReportManager(models.Manager): """Manager for model Report.""" - def make(self, source: int, category, url: str, description: str): + def make(self, source: int, category, url: str, description: str, locale: str): """Make object.""" obj = self.create( source=source, category=category, url=url, - description=description + description=description, + locale=locale, ) if settings.USE_CELERY: send_report_task.delay(obj.id) @@ -60,6 +64,8 @@ class Report(ProjectBaseMixin): verbose_name=_('category')) url = models.URLField(verbose_name=_('URL')) description = models.TextField(verbose_name=_('description')) + locale = models.CharField(max_length=10, null=True, + verbose_name=_('locale')) objects = ReportManager.from_queryset(ReportQuerySet)() @@ -70,19 +76,46 @@ class Report(ProjectBaseMixin): def __str__(self): """Implement `str` dunder method.""" - return f'{self.id}: {self.get_category_display()} ({self.url})' + return f'{self.id}: {self.get_category_display()} ({self.url}, {self.locale})' - def get_body_email_message(self): + @cached_property + def support_email_note(self): + keyword = 'support.email.note' + default_note = ( + 'You received this message because you are an ' + 'administrator with privileges to manage this request.' + ) + + note_qs = SiteInterfaceDictionary.objects.filter(keywords=keyword) + if note_qs.exists(): + return note_qs.first().text.get(self.locale, default_note) + return default_note + + @cached_property + def report_message(self): + return render_to_string( + template_name=settings.REPORT_TEMPLATE, + context={ + 'source_value': self.get_source_display(), + 'source_page_url': self.url, + 'source_screen_language': self.locale, + 'request_category': self.get_category_display(), + 'request_description': self.description, + 'support_email_note': self.support_email_note, + } + ) + + @cached_property + def base_template(self): """Prepare the body of the email message""" return { - 'subject': self.get_category_display(), - 'message': str(self.description), - 'html_message': self.description, + 'subject': _('[TECH_REQUEST] A new technical request has been created.'), + 'message': self.report_message, + 'html_message': self.report_message, 'from_email': settings.EMAIL_HOST_USER, 'recipient_list': [settings.EMAIL_TECHNICAL_SUPPORT, ], } def send_email(self): """Send an email reset user password""" - send_mail(**self.get_body_email_message()) - + send_mail(**self.base_template) diff --git a/apps/report/serializers.py b/apps/report/serializers.py index ef94836d..7eab76d4 100644 --- a/apps/report/serializers.py +++ b/apps/report/serializers.py @@ -1,4 +1,5 @@ """DRF-serializers for application report.""" +from django.utils.functional import cached_property from rest_framework import serializers from report.models import Report @@ -18,15 +19,25 @@ class ReportBaseSerializer(serializers.ModelSerializer): 'category_display', 'url', 'description', + 'locale', ] extra_kwargs = { 'source': {'required': False}, - 'category': {'write_only': True} + 'category': {'write_only': True}, + 'locale': {'write_only': True} } + @cached_property + def locale(self) -> str: + """Return locale from request.""" + request = self.context.get('request') + if hasattr(request, 'locale'): + return request.locale + def validate(self, attrs): """An overridden validate method.""" attrs['source'] = self.context.get('view').get_source() + attrs['locale'] = self.locale return attrs def create(self, validated_data): diff --git a/project/settings/base.py b/project/settings/base.py index 70eb553e..237d634c 100644 --- a/project/settings/base.py +++ b/project/settings/base.py @@ -462,6 +462,7 @@ CONFIRM_EMAIL_TEMPLATE = 'authorization/confirm_email.html' NEWS_EMAIL_TEMPLATE = 'news/news_email.html' NOTIFICATION_PASSWORD_TEMPLATE = 'account/password_change_email.html' NOTIFICATION_SUBSCRIBE_TEMPLATE = 'notification/update_email.html' +REPORT_TEMPLATE = 'report/tech_support_template.html' # COOKIES @@ -568,4 +569,4 @@ COUNTRY_CALLING_CODES = { CALLING_CODES_ANTILLES_GUYANE_WEST_INDIES = [590, 594, 1758, 596] DEFAULT_CALLING_CODE_ANTILLES_GUYANE_WEST_INDIES = 590 -EMAIL_TECHNICAL_SUPPORT = 'it-report@gaultmillau.com' +EMAIL_TECHNICAL_SUPPORT = 'tech_requests@gaultmillau.com' diff --git a/project/templates/report/tech_support_template.html b/project/templates/report/tech_support_template.html index 40b8f465..acb085ff 100644 --- a/project/templates/report/tech_support_template.html +++ b/project/templates/report/tech_support_template.html @@ -33,15 +33,15 @@

- Source: %source_value% + Source: {{ source_value }}

-

Source page: %source_page_url%

-

User language: %source_screen_language%

-

Request category: %request_category% Request %request_description%

+

Source page: {{ source_page_url }}

+

User language: {{ source_screen_language }}

+

Request category: {{ request_category }} Request {{ request_description }}