added report template and enabled send an report emails

This commit is contained in:
Anatoly 2020-02-06 17:16:31 +03:00
parent c8d2884a48
commit a9f3d3de46
5 changed files with 79 additions and 16 deletions

View File

@ -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'),
),
]

View File

@ -1,22 +1,26 @@
from django.conf import settings from django.conf import settings
from django.core.mail import send_mail from django.core.mail import send_mail
from django.db import models 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 django.utils.text import gettext_lazy as _
from report.tasks import send_report_task from report.tasks import send_report_task
from translation.models import SiteInterfaceDictionary
from utils.models import ProjectBaseMixin from utils.models import ProjectBaseMixin
class ReportManager(models.Manager): class ReportManager(models.Manager):
"""Manager for model Report.""" """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.""" """Make object."""
obj = self.create( obj = self.create(
source=source, source=source,
category=category, category=category,
url=url, url=url,
description=description description=description,
locale=locale,
) )
if settings.USE_CELERY: if settings.USE_CELERY:
send_report_task.delay(obj.id) send_report_task.delay(obj.id)
@ -60,6 +64,8 @@ class Report(ProjectBaseMixin):
verbose_name=_('category')) verbose_name=_('category'))
url = models.URLField(verbose_name=_('URL')) url = models.URLField(verbose_name=_('URL'))
description = models.TextField(verbose_name=_('description')) description = models.TextField(verbose_name=_('description'))
locale = models.CharField(max_length=10, null=True,
verbose_name=_('locale'))
objects = ReportManager.from_queryset(ReportQuerySet)() objects = ReportManager.from_queryset(ReportQuerySet)()
@ -70,19 +76,46 @@ class Report(ProjectBaseMixin):
def __str__(self): def __str__(self):
"""Implement `str` dunder method.""" """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""" """Prepare the body of the email message"""
return { return {
'subject': self.get_category_display(), 'subject': _('[TECH_REQUEST] A new technical request has been created.'),
'message': str(self.description), 'message': self.report_message,
'html_message': self.description, 'html_message': self.report_message,
'from_email': settings.EMAIL_HOST_USER, 'from_email': settings.EMAIL_HOST_USER,
'recipient_list': [settings.EMAIL_TECHNICAL_SUPPORT, ], 'recipient_list': [settings.EMAIL_TECHNICAL_SUPPORT, ],
} }
def send_email(self): def send_email(self):
"""Send an email reset user password""" """Send an email reset user password"""
send_mail(**self.get_body_email_message()) send_mail(**self.base_template)

View File

@ -1,4 +1,5 @@
"""DRF-serializers for application report.""" """DRF-serializers for application report."""
from django.utils.functional import cached_property
from rest_framework import serializers from rest_framework import serializers
from report.models import Report from report.models import Report
@ -18,15 +19,25 @@ class ReportBaseSerializer(serializers.ModelSerializer):
'category_display', 'category_display',
'url', 'url',
'description', 'description',
'locale',
] ]
extra_kwargs = { extra_kwargs = {
'source': {'required': False}, '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): def validate(self, attrs):
"""An overridden validate method.""" """An overridden validate method."""
attrs['source'] = self.context.get('view').get_source() attrs['source'] = self.context.get('view').get_source()
attrs['locale'] = self.locale
return attrs return attrs
def create(self, validated_data): def create(self, validated_data):

View File

@ -462,6 +462,7 @@ CONFIRM_EMAIL_TEMPLATE = 'authorization/confirm_email.html'
NEWS_EMAIL_TEMPLATE = 'news/news_email.html' NEWS_EMAIL_TEMPLATE = 'news/news_email.html'
NOTIFICATION_PASSWORD_TEMPLATE = 'account/password_change_email.html' NOTIFICATION_PASSWORD_TEMPLATE = 'account/password_change_email.html'
NOTIFICATION_SUBSCRIBE_TEMPLATE = 'notification/update_email.html' NOTIFICATION_SUBSCRIBE_TEMPLATE = 'notification/update_email.html'
REPORT_TEMPLATE = 'report/tech_support_template.html'
# COOKIES # COOKIES
@ -568,4 +569,4 @@ COUNTRY_CALLING_CODES = {
CALLING_CODES_ANTILLES_GUYANE_WEST_INDIES = [590, 594, 1758, 596] CALLING_CODES_ANTILLES_GUYANE_WEST_INDIES = [590, 594, 1758, 596]
DEFAULT_CALLING_CODE_ANTILLES_GUYANE_WEST_INDIES = 590 DEFAULT_CALLING_CODE_ANTILLES_GUYANE_WEST_INDIES = 590
EMAIL_TECHNICAL_SUPPORT = 'it-report@gaultmillau.com' EMAIL_TECHNICAL_SUPPORT = 'tech_requests@gaultmillau.com'

View File

@ -33,15 +33,15 @@
</div> </div>
<div class="content"> <div class="content">
<p style="margin: 0 0 14px;"> <p style="margin: 0 0 14px;">
<b>Source:</b> %source_value% <b>Source:</b> {{ source_value }}
</p> </p>
<p style="margin: 0 0 14px;"><b>Source page:</b> %source_page_url% </p> <p style="margin: 0 0 14px;"><b>Source page:</b> {{ source_page_url }} </p>
<p style="margin: 0 0 14px;"><b>User language:</b> %source_screen_language%</p> <p style="margin: 0 0 14px;"><b>User language:</b> {{ source_screen_language }}</p>
<p style="margin: 0 0 14px;"><b>Request category:</b> %request_category% Request %request_description%</p> <p style="margin: 0 0 14px;"><b>Request category:</b> {{ request_category }} Request {{ request_description }}</p>
</div> </div>
<div class="footer" style="width: 100%; border-top: 2px solid gray; position: absolute; <div class="footer" style="width: 100%; border-top: 2px solid gray; position: absolute;
bottom: 0;"> bottom: 0;">
<p>$support.email.note$</p> <p>{{ support_email_note }}</p>
</div> </div>
</div> </div>
</body> </body>