added report template and enabled send an report emails
This commit is contained in:
parent
c8d2884a48
commit
a9f3d3de46
18
apps/report/migrations/0002_report_locale.py
Normal file
18
apps/report/migrations/0002_report_locale.py
Normal 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'),
|
||||
),
|
||||
]
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
|
|
|||
|
|
@ -33,15 +33,15 @@
|
|||
</div>
|
||||
<div class="content">
|
||||
<p style="margin: 0 0 14px;">
|
||||
<b>Source:</b> %source_value%
|
||||
<b>Source:</b> {{ source_value }}
|
||||
</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>Request category:</b> %request_category% Request %request_description%</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>Request category:</b> {{ request_category }} Request {{ request_description }}</p>
|
||||
</div>
|
||||
<div class="footer" style="width: 100%; border-top: 2px solid gray; position: absolute;
|
||||
bottom: 0;">
|
||||
<p>$support.email.note$</p>
|
||||
<p>{{ support_email_note }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user