Skip to content

Commit

Permalink
Update: Intgrated the sendgrid service.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmoud-Emad committed Feb 8, 2024
1 parent 7de665f commit 801c5b8
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 40 deletions.
2 changes: 1 addition & 1 deletion client/public/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
window.env = {
SERVER_DOMAIN_NAME_API: 'https://cshrserver.gent01.qa.grid.tf/api', // Added for testing, this is a staging API
SERVER_DOMAIN_NAME_API: 'http://127.0.0.1:8000/api', // Added for testing, this is a staging API
};
1 change: 1 addition & 0 deletions config/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ DJANGO_DEBUG=ON # Keep it on.
EMAIL=<EMAIL>
EMAIL_PASSWORD=<EMAIL_PASSWORD>
EMAIL_HOST=<EMAIL_HOST>
SENDGRID_API_KEY=<SENDGRID_API_KEY> # pass it only if you are going to use the sendgrid provider.


# Servers configurations
Expand Down
39 changes: 33 additions & 6 deletions server/cshr/celery/send_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from django.conf import settings
from celery import Celery
from celery.schedules import crontab
from django.core.mail import EmailMultiAlternatives

import datetime
from django.core.mail import send_mail
from celery import shared_task
Expand Down Expand Up @@ -172,25 +174,50 @@ def send_quarter_evaluation_email():


@shared_task()
def send_email_for_request(user_id, msg, mail_title) -> Response:
from django.core.mail import send_mail
def send_email_for_request(user_id) -> Response:
from cshr.models.users import User
from cshr.utils.send_email import get_email_recievers
from cshr.services.users import get_user_by_id
from cshr.utils.send_email import check_email_configuration

check_email_configuration()
user: User = get_user_by_id(user_id)

if user is None:
return False

recievers: array[str] = get_email_recievers(user)
mail = EmailMultiAlternatives(
subject="Vacation Request Notification",
from_email="Codescalers HR <[email protected]>",
to=recievers,
headers={"Reply-To": "[email protected]"}
)
mail.categories = [
'work',
'urgent',
]
mail.attach_alternative(
f"""
<p>I trust this message finds you and your family in good health.</p>
<p>I wanted to bring to your attention that { user.full_name } has recently submitted a vacation request. To review and manage this notification, please log in to your CSHR account and check your <a href='https://hr.threefold.tech/notifications/' target='_blank'>notifications</a>.</p>
<p>Thank you for your attention to this matter.</p>
<p>Best regards,</p>
<hr/>
<strong>
<small>Codescalers HR Mail System</small>
<br/>
<small>Note: This email was sent via the Codescalers HR email system. Please refrain from replying to this email.</small>
</strong>
""",
"text/html"
)

try:
send_mail(
mail_title, msg, settings.EMAIL_HOST_USER, recievers, fail_silently=False
)
mail.send()
return True
except Exception:
return False
return True


@shared_task()
Expand Down
44 changes: 29 additions & 15 deletions server/cshr/utils/email_messages_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,37 @@


def get_vacation_request_email_template(user: User, data, url) -> str:
msg = """Request information:
Applying user: {user_fname} {user_lname}
Reason: {reason}
Start date : {start_date}
End Date : {end_date}
Status :{status}
Request Url: {request_url}""".format(
user_fname=user.first_name,
user_lname=user.last_name,
reason=data["reason"],
start_date=data["from_date"],
end_date=data["end_date"],
status=data["status"],
request_url=url,
formatted_start_date = data["from_date"]
formatted_end_date = data["end_date"]

message = (
f"""
Hello Mahmoud, I hope this email finds you well. This is a copy of the vacation request applied by Ahmed. Could you kindly take a look?
---------------------------------------------------------
| Applicant: {user.first_name} {user.last_name} |
---------------------------------------------------------
| Reason: {data['reason']} |
---------------------------------------------------------
| Start Date: {formatted_start_date} |
---------------------------------------------------------
---------------------------------------------------------
| End Date: {formatted_end_date} |
---------------------------------------------------------
---------------------------------------------------------
| Status: {data['status']} |
---------------------------------------------------------
Thanks
<small>This email is auto-generated by the Codescalers HR system.</small>
"""
# f"Request URL: {url}"
)
return msg

return message

def get_hr_letter_request_email_template(user: User, data, url) -> str:
msg = """Request information:
Expand Down
24 changes: 10 additions & 14 deletions server/cshr/views/vacations.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,27 +289,23 @@ def post(self, request: Request) -> Response:
message=f"You have an additional pending request that deducts {sum(pinding_vacations)} days from your balance even though the current balance for the '{reason.capitalize().replace('_', ' ')}' category is only {curr_balance} days."
)

saved = serializer.save(
type=TYPE_CHOICES.VACATIONS,
status=STATUS_CHOICES.PENDING,
applying_user=applying_user,
actual_days=vacation_days,
)

msg = get_vacation_request_email_template(
request.user, serializer.data, saved.id
)

try:
ping_redis()
except:
return http_ensure_redis_error()

set_notification_request_redis(serializer.data)

sent = send_email_for_request(request.user.id, msg, "Vacation request")
if not sent:
return CustomResponse.bad_request(message="Error in sending email, can not sent email with this request.")
# sent = send_email_for_request(request.user.id)
# if not sent:
# return CustomResponse.bad_request(message="Error in sending email, can not sent email with this request.")

saved = serializer.save(
type=TYPE_CHOICES.VACATIONS,
status=STATUS_CHOICES.PENDING,
applying_user=applying_user,
actual_days=vacation_days,
)

response_date: Dict = send_vacation_to_calendar(saved)
return CustomResponse.success(
Expand Down
40 changes: 39 additions & 1 deletion server/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ python-decouple = "^3.6"
redis = "^4.3.4"
whitenoise = "^6.2.0"
psycopg2-binary = "^2.9.9"
sendgrid-django = "^4.2.0"

[tool.poetry.dev-dependencies]
black = "^22.6.0"
Expand Down
16 changes: 13 additions & 3 deletions server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,21 @@
# email config

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = config("EMAIL_HOST")
SENDGRID_API_KEY = config('SENDGRID_API_KEY')
SENDGRID_SANDBOX_MODE_IN_DEBUG = True

EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_HOST_USER = 'apikey' # this is exactly the value 'apikey'
EMAIL_HOST_PASSWORD = SENDGRID_API_KEY
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = config("EMAIL")
EMAIL_HOST_PASSWORD = config("EMAIL_PASSWORD")

# EMAIL_HOST = config("EMAIL_HOST")
# EMAIL_PORT = 587
# EMAIL_USE_TLS = True
# EMAIL_HOST_USER = config("EMAIL")
# EMAIL_HOST_PASSWORD = config("EMAIL_PASSWORD")
# SENDGRID_API_KEY = config("SENDGRID_API_KEY")


# import dj_database_url
Expand Down

0 comments on commit 801c5b8

Please sign in to comment.