Basic email template for notifications #96

Merged
Anna Sirota merged 14 commits from notification-templates into main 2024-05-02 14:04:24 +02:00
2 changed files with 11 additions and 4 deletions
Showing only changes of commit 768d95e1ce - Show all commits

View File

@ -114,7 +114,7 @@ class EmailPreviewAdmin(NoAddDeleteMixin, EmailAdmin):
fake_notifications = construct_fake_notifications() fake_notifications = construct_fake_notifications()
for fake_notification in fake_notifications: for fake_notification in fake_notifications:
mail_name = fake_notification.full_template_name mail_name = fake_notification.original_template_name
email_with_fake_context[mail_name] = { email_with_fake_context[mail_name] = {
'template': fake_notification.template_name, 'template': fake_notification.template_name,
**fake_notification.get_template_context(), **fake_notification.get_template_context(),

View File

@ -3,6 +3,7 @@ import logging
from actstream.models import Action from actstream.models import Action
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.db import models from django.db import models
from django.template import loader, TemplateDoesNotExist
from constants.activity import Verb from constants.activity import Verb
from utils import absolutify from utils import absolutify
@ -34,7 +35,12 @@ class Notification(models.Model):
unique_together = ['recipient', 'action'] unique_together = ['recipient', 'action']
@property @property
def full_template_name(self) -> str: def original_template_name(self) -> str:
"""Template name constructed from action type, target and so on.
If we want to override email template for this specific notification,
this is the name of the template that should be created.
"""
action = self.action action = self.action
action_object = action.action_object action_object = action.action_object
target_name = action.target.__class__.__name__.lower() target_name = action.target.__class__.__name__.lower()
@ -43,10 +49,10 @@ class Notification(models.Model):
@property @property
def template_name(self) -> str: def template_name(self) -> str:
"""Template name to be used for constructing notification email."""
default_name = 'new_activity' default_name = 'new_activity'
name = self.full_template_name name = self.original_template_name
args = {'name': name, 'default_name': default_name} args = {'name': name, 'default_name': default_name}
from django.template import loader, TemplateDoesNotExist
try: try:
loader.get_template(f'emails/{name}.html') loader.get_template(f'emails/{name}.html')
@ -56,6 +62,7 @@ class Notification(models.Model):
return name return name
def get_template_context(self): def get_template_context(self):
"""Return template context to be used in the notification email."""
action = self.action action = self.action
action_object = action.action_object action_object = action.action_object
quoted_message = getattr(action_object, 'message', getattr(action_object, 'text', '')) quoted_message = getattr(action_object, 'message', getattr(action_object, 'text', ''))