Skip to content

Commit

Permalink
Add resend notification
Browse files Browse the repository at this point in the history
  • Loading branch information
zamuzakki committed Aug 16, 2024
1 parent 7e081c6 commit 57fbf77
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 13 deletions.
4 changes: 3 additions & 1 deletion deployment/.template.env
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ MINIO_ACCESS_KEY_ID=miniocplus
MINIO_SECRET_ACCESS_KEY=miniocplus
MINIO_ENDPOINT=http://minio:9000
MINIO_BUCKET_NAME=cplus
MINIO_BROWSER_REDIRECT_URL=
MINIO_BROWSER_REDIRECT_URL=
DEFAULT_FROM_EMAIL=
RESEND_API_KEY=
2 changes: 2 additions & 0 deletions deployment/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ x-common-variables: &common-variables
MINIO_ENDPOINT: ${MINIO_ENDPOINT}
MINIO_BUCKET_NAME: ${MINIO_BUCKET_NAME:-cplus}
MINIO_BROWSER_REDIRECT_URL: ${MINIO_BROWSER_REDIRECT_URL}
DEFAULT_FROM_EMAIL: ${DEFAULT_FROM_EMAIL}
RESEND_API_KEY: ${RESEND_API_KEY}


x-common-django:
Expand Down
5 changes: 4 additions & 1 deletion deployment/docker/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ djangorestframework==3.14.0
djangorestframework-gis==1.0

# Python HTTP library
requests==2.28.2
requests==2.31.0

# The uWSGI server
uwsgi==2.0.23
Expand Down Expand Up @@ -58,3 +58,6 @@ minio==7.2.5

flower==1.2.0
django-revproxy @ https://github.com/jazzband/django-revproxy/archive/refs/tags/0.11.0.zip

# To send resend notification
resend==2.4.0
4 changes: 4 additions & 0 deletions django_project/core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@

LOGIN_URL = '/account/login/'
LOGIN_REDIRECT_URL = '/'

RESEND_API_KEY = os.environ.get('RESEND_API_KEY')
DEFAULT_FROM_EMAIL = os.environ.get(
'DEFAULT_FROM_EMAIL', '[email protected]')
4 changes: 0 additions & 4 deletions django_project/core/settings/prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
EMAIL_USE_SSL = ast.literal_eval(os.environ.get('EMAIL_USE_SSL', 'False'))
EMAIL_SUBJECT_PREFIX = os.environ.get('EMAIL_SUBJECT_PREFIX', '')

SERVER_EMAIL = os.environ.get('ADMIN_EMAIL', '[email protected]')
DEFAULT_FROM_EMAIL = os.environ.get(
'DEFAULT_FROM_EMAIL', '[email protected]')

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
Expand Down
21 changes: 21 additions & 0 deletions django_project/cplus_api/utils/api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import boto3
import math
import resend
from botocore.client import Config
from botocore.exceptions import ClientError
from django.conf import settings
Expand Down Expand Up @@ -276,3 +277,23 @@ def get_layer_type(file_path: str):
return 1
else:
return -1


def send_notification(
subject,
from_email,
recipient_list,
html_message=None
):
if not from_email:
from_email = settings.DEFAULT_FROM_EMAIL
resend.api_key = resend.api_key = settings.RESEND_API_KEY

params: resend.Emails.SendParams = {
"from": from_email,
"to": recipient_list,
"subject": subject,
"html": html_message
}

email: resend.Email = resend.Emails.send(params)
13 changes: 6 additions & 7 deletions django_project/cplus_api/utils/worker_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.conf import settings
from django.utils import timezone
from django.template.loader import render_to_string
from django.core.mail import send_mail
from cplus.models.base import (
Activity,
NcsPathway,
Expand All @@ -24,7 +23,8 @@
convert_size,
todict,
CustomJsonEncoder,
get_layer_type
get_layer_type,
send_notification
)
from cplus_api.utils.default import DEFAULT_VALUES

Expand Down Expand Up @@ -781,11 +781,10 @@ def notify_user(self, is_success: bool):
is_success else
f'Your analysis of {scenario_name} has stopped with errors'
)
send_mail(
subject,
None,
settings.SERVER_EMAIL,
[self.scenario_task.submitted_by.email],
send_notification(
subject=subject,
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[self.scenario_task.submitted_by.email],
html_message=message
)
except Exception as exc:
Expand Down

0 comments on commit 57fbf77

Please sign in to comment.