-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DI-2715] Implementing send notification task for account app #93
Open
denys-chura
wants to merge
3
commits into
main
Choose a base branch
from
implementing-send-notification-task
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
...oject_slug }}/api/{{ cookiecutter.project_slug }}/apps/accounts/services/notifications.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import logging | ||
|
||
from dataclasses import dataclass | ||
|
||
from django.conf import settings | ||
from django.core.mail import send_mail | ||
from django.template.loader import get_template | ||
from django.utils.html import strip_tags | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@dataclass | ||
class NotificationHandlerConfig: | ||
# Unique identifier of the notification. | ||
# if no templates are specified, the templates will be named after this | ||
kind: str | ||
subject: str | ||
# Name of the template to use for the html version of the email. | ||
# It should be relative to the account/notifications directory. | ||
template_html_filename: str | None = None | ||
# Name of the template to use for the plain text version of the email. | ||
# It should be relative to the account/notifications directory. | ||
template_plain_filename: str | None = None | ||
# Email address to use as the sender of the email. | ||
from_email: str = settings.DEFAULT_FROM_EMAIL | ||
|
||
@property | ||
def template_html_name(self): | ||
filename = self.template_html_filename or f"{self.kind}.html" | ||
return f"accounts/notifications/{filename}" | ||
|
||
@property | ||
def template_plain_name(self): | ||
filename = self.template_plain_filename or f"{self.kind}.html" | ||
return f"accounts/notifications/{filename}" | ||
|
||
@property | ||
def html_only(self) -> bool: | ||
return self.template_plain_name == self.template_html_name | ||
|
||
|
||
_HANDLERS = [ | ||
NotificationHandlerConfig( | ||
kind="user-reset-password", | ||
subject="Reset your password", | ||
), | ||
] | ||
|
||
HANDLERS = {handler.kind: handler for handler in _HANDLERS} | ||
|
||
|
||
def notification(user_email: str, kind: str, context: dict) -> None: | ||
try: | ||
config = HANDLERS[kind] | ||
except KeyError: | ||
logger.warning("Attempted to send notification of unknown kind %s", kind) | ||
return | ||
|
||
template_html = get_template(config.template_html_name) | ||
template_plain = get_template(config.template_plain_name) | ||
|
||
message_html = template_html.render(context) | ||
|
||
if config.html_only: | ||
message_plain = strip_tags(message_html) | ||
else: | ||
message_plain = template_plain.render(context) | ||
|
||
res = send_mail( | ||
subject=config.subject, | ||
message=message_plain, | ||
from_email=config.from_email, | ||
recipient_list=[user_email], | ||
html_message=message_html, | ||
) | ||
|
||
if res != 1: | ||
logger.warning(f"Failed to send {kind} notification to user {user_email}") | ||
|
||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
{{ cookiecutter.project_slug }}/api/{{ cookiecutter.project_slug }}/apps/accounts/tasks.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import logging | ||
|
||
from {{ cookiecutter.project_slug }} import celery_app | ||
from {{ cookiecutter.project_slug }}.apps.accounts.services.notifications import notification | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@celery_app.task | ||
def send_notification(user_email: str, kind: str, context: dict): | ||
logger.debug(f"Sending {kind} notification to user {user_email}") | ||
|
||
notification(user_email, kind, context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...g }}/api/{{ cookiecutter.project_slug }}/templates/accounts/notifications/base-email.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{% raw %}<!doctype html> | ||
{% load static %} | ||
<html> | ||
<head> | ||
<meta name="viewport" content="width=device-width" /> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | ||
</head> | ||
<body class=""> | ||
<table role="presentation" border="0" cellpadding="0" cellspacing="0" class="body"> | ||
<tr> | ||
<td> </td> | ||
<td class="container"> | ||
<div class="content"> | ||
{% block content %} | ||
{% endblock %} | ||
|
||
{% block footer %} | ||
{% endblock footer %} | ||
</div> | ||
</td> | ||
<td> </td> | ||
</tr> | ||
</table> | ||
</body> | ||
</html>{% endraw %} |
20 changes: 20 additions & 0 deletions
20
...{{ cookiecutter.project_slug }}/templates/accounts/notifications/user-reset-password.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{% raw %}{% extends 'accounts/notifications/base-email.html' %} | ||
|
||
{% block content %} | ||
<table role="presentation" class="main"> | ||
<tr> | ||
<td class="wrapper"> | ||
<table role="presentation" border="0" cellpadding="0" cellspacing="0"> | ||
<tr> | ||
<td> | ||
<h1>Let’s reset your password</h1> | ||
<p>If you want to reset your password, use the link below. This will take you to the secure page to reset your password.</p> | ||
<p><a href="{{ reset_password_link }}" target="_blank">Reset password</a></p> | ||
<p>If you don't want to reset your password, please ignore this message. Your password will not be reset.</p> | ||
</td> | ||
</tr> | ||
</table> | ||
</td> | ||
</tr> | ||
</table> | ||
{% endblock %}{% endraw %} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For me it make more sense to pass
email
insteead ofpk
here