-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/add email notification setup #18
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
Open
sudip-khanal
wants to merge
10
commits into
develop
Choose a base branch
from
feat/add-email-notification-setup
base: develop
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
10 commits
Select commit
Hold shift + click to select a range
dcc3a0a
feat(email): add email backend
sudip-khanal 292d31f
feat(email): add email template
sudip-khanal 6d11825
fix(resources): fix migration
sudip-khanal 93511c9
feat(email): add mailpit
sudip-khanal 4ef241c
feat: add celery
sudip-khanal cad7bed
feat(resources): add email notifications for contact and demo requests
sudip-khanal 771c70a
docs(email): add email backend document
sudip-khanal b6d248a
feature(rc-select): Add email template
barshathakuri 60930f2
feat(template): add logo on email templates
sudip-khanal 4f4f1c4
feat(helm): add redis and celery config on helm values
sudip-khanal 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
This file contains hidden or 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
This file contains hidden or 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,26 @@ | ||
| import shlex | ||
| import subprocess | ||
| import typing | ||
| from pathlib import Path | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
| from django.utils import autoreload | ||
|
|
||
| WORKER_STATE_DIR = Path("/var/run/celery") | ||
|
|
||
| CMD = "celery -A main worker -E --concurrency=2 -l info" | ||
|
|
||
|
|
||
| def restart_celery(*args: typing.Any, **kwargs: typing.Any): | ||
| kill_worker_cmd = "pkill -9 celery" | ||
| subprocess.call(shlex.split(kill_worker_cmd)) # noqa: S603 | ||
| subprocess.call(shlex.split(CMD)) # noqa: S603 | ||
sudip-khanal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| @typing.override | ||
| def handle(self, *args: typing.Any, **options: typing.Any): | ||
| self.stdout.write("Starting celery worker with autoreload...") | ||
| if not Path.exists(WORKER_STATE_DIR): | ||
| Path.mkdir(WORKER_STATE_DIR, parents=True) | ||
| autoreload.run_with_reloader(restart_celery, args=None, kwargs=None) | ||
This file contains hidden or 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
Empty file.
This file contains hidden or 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,15 @@ | ||
| from django import template | ||
| from django.conf import settings | ||
| from django.templatetags.static import static | ||
|
|
||
| register = template.Library() | ||
|
|
||
|
|
||
| @register.filter(is_safe=True) | ||
| def static_full_path(path): | ||
| static_path = static(path) | ||
| # Domain from URL object | ||
| domain = f"{settings.APP_DOMAIN.scheme}://{settings.APP_DOMAIN.hostname}" | ||
| if settings.APP_DOMAIN.port: | ||
| domain += f":{settings.APP_DOMAIN.port}" | ||
| return f"{domain}{static_path}" |
This file contains hidden or 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
2 changes: 0 additions & 2 deletions
2
apps/resources/migrations/0004_alter_casestudy_cover_image_requestdemo.py
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,57 @@ | ||
| import logging | ||
|
|
||
| from celery import shared_task | ||
| from django.conf import settings | ||
| from django.template.loader import render_to_string | ||
|
|
||
| from apps.resources.models import ContactRequest, RequestDemo | ||
| from apps.resources.utils import get_contact_request_email_context, get_demo_request_email_context | ||
| from utils.email.service import send_email | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @shared_task | ||
| def send_contact_request_email(contact_id: int): | ||
| instance = ContactRequest.objects.filter(id=contact_id).first() | ||
| if not instance: | ||
| return None | ||
|
|
||
| recipient = settings.EMAIL_TO | ||
| email_subject = "You got a new feedback!" | ||
sudip-khanal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| email_context = get_contact_request_email_context(instance) | ||
| html_body = render_to_string("email/contact_request.html", email_context) | ||
|
|
||
| send_email( | ||
| subject=email_subject, | ||
| to_email=[recipient], | ||
| html=html_body, | ||
| ) | ||
| return True | ||
|
|
||
|
|
||
| @shared_task | ||
| def send_demo_request_email(request_demo_id: int): | ||
| instance = ( | ||
| RequestDemo.objects.select_related("tool").prefetch_related("tool__tool_owners").filter(id=request_demo_id).first() | ||
| ) | ||
| if not instance: | ||
| return None | ||
|
|
||
| tool_owners = instance.tool.tool_owners.all() | ||
|
|
||
| if not tool_owners.exists(): | ||
| logger.info("skipping cause there are no tool owners associated with this tool") | ||
| return False | ||
|
|
||
| recipients = list(tool_owners.values_list("email", flat=True)) | ||
| email_subject = "You got a new request for demo!" | ||
| email_context = get_demo_request_email_context(instance) | ||
| html = render_to_string("email/request_demo.html", email_context) | ||
| send_email( | ||
| subject=email_subject, | ||
| to_email=recipients, | ||
| html=html, | ||
| cc_email=[settings.EMAIL_TO], | ||
| ) | ||
sudip-khanal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return True | ||
This file contains hidden or 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
This file contains hidden or 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,28 @@ | ||
| from apps.resources.models import ContactRequest, RequestDemo | ||
|
|
||
|
|
||
| def get_contact_request_email_context(instance: ContactRequest): | ||
| from .serializers import ContactRequestSerializer | ||
|
|
||
| data = dict(ContactRequestSerializer(instance).data) | ||
|
|
||
| return { | ||
| "name": data["name"], | ||
| "email": data["email"], | ||
| "national_society": data["national_society"], | ||
| "content": data["content"], | ||
| } | ||
|
|
||
|
|
||
| def get_demo_request_email_context(instance: RequestDemo): | ||
| from .serializers import RequestDemoSerializer | ||
|
|
||
| data = dict(RequestDemoSerializer(instance).data) | ||
| tool_name = instance.tool.name | ||
| return { | ||
| "name": data["name"], | ||
| "email": data["email"], | ||
| "national_society": data["national_society"], | ||
| "content": data["content"], | ||
| "tool": tool_name, | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Also update the version here...