|
| 1 | +from celery import shared_task |
| 2 | +from django.template.loader import render_to_string |
| 3 | + |
| 4 | +from local_units.models import LocalUnit, LocalUnitChangeRequest |
| 5 | +from notifications.notification import send_notification |
| 6 | + |
| 7 | +from .utils import get_email_context, get_local_admins |
| 8 | + |
| 9 | + |
| 10 | +@shared_task |
| 11 | +def send_local_unit_email(local_unit_id: int, new: bool = True): |
| 12 | + if not local_unit_id: |
| 13 | + return None |
| 14 | + |
| 15 | + instance = LocalUnit.objects.get(id=local_unit_id) |
| 16 | + users = get_local_admins(instance) |
| 17 | + email_context = get_email_context(instance) |
| 18 | + email_context["new_local_unit"] = True |
| 19 | + email_subject = "Action Required: New Local Unit Pending Validation" |
| 20 | + email_type = "New Local Unit" |
| 21 | + # NOTE: Update case for the local unit |
| 22 | + if not new: |
| 23 | + email_context.pop("new_local_unit") |
| 24 | + email_context["update_local_unit"] = True |
| 25 | + email_subject = "Action Required: Local Unit Pending Validation" |
| 26 | + email_type = "Update Local Unit" |
| 27 | + |
| 28 | + for user in users: |
| 29 | + # NOTE: Adding the validator email to the context |
| 30 | + email_context["validator_email"] = user.email |
| 31 | + email_context["full_name"] = user.get_full_name() |
| 32 | + email_body = render_to_string("email/local_units/local_unit.html", email_context) |
| 33 | + send_notification(email_subject, user.email, email_body, email_type) |
| 34 | + return email_context |
| 35 | + |
| 36 | + |
| 37 | +@shared_task |
| 38 | +def send_validate_success_email(local_unit_id: int, message: str = ""): |
| 39 | + if not local_unit_id: |
| 40 | + return None |
| 41 | + |
| 42 | + instance = LocalUnit.objects.get(id=local_unit_id) |
| 43 | + user = instance.created_by |
| 44 | + email_context = get_email_context(instance) |
| 45 | + email_context["full_name"] = user.get_full_name() |
| 46 | + email_context["validate_success"] = True |
| 47 | + email_subject = "Your Local Unit Addition Request: Approved" |
| 48 | + email_body = render_to_string("email/local_units/local_unit.html", email_context) |
| 49 | + email_type = f"{message} Local Unit" |
| 50 | + |
| 51 | + send_notification(email_subject, user.email, email_body, email_type) |
| 52 | + return email_context |
| 53 | + |
| 54 | + |
| 55 | +@shared_task |
| 56 | +def send_revert_email(local_unit_id: int, change_request_id: int): |
| 57 | + if not local_unit_id: |
| 58 | + return None |
| 59 | + |
| 60 | + instance = LocalUnit.objects.get(id=local_unit_id) |
| 61 | + change_request_instance = LocalUnitChangeRequest.objects.get(id=change_request_id) |
| 62 | + user = instance.created_by |
| 63 | + email_context = get_email_context(instance) |
| 64 | + email_context["full_name"] = user.get_full_name() |
| 65 | + email_context["revert_reason"] = change_request_instance.rejected_reason |
| 66 | + email_subject = "Your Local Unit Addition Request: Reverted" |
| 67 | + email_body = render_to_string("email/local_units/local_unit.html", email_context) |
| 68 | + email_type = "Revert Local Unit" |
| 69 | + |
| 70 | + send_notification(email_subject, user.email, email_body, email_type) |
| 71 | + return email_context |
| 72 | + |
| 73 | + |
| 74 | +@shared_task |
| 75 | +def send_deprecate_email(local_unit_id: int): |
| 76 | + if not local_unit_id: |
| 77 | + return None |
| 78 | + |
| 79 | + instance = LocalUnit.objects.get(id=local_unit_id) |
| 80 | + user = instance.created_by |
| 81 | + email_context = get_email_context(instance) |
| 82 | + email_context["full_name"] = user.get_full_name() |
| 83 | + email_context["deprecate_local_unit"] = True |
| 84 | + email_context["deprecate_reason"] = instance.deprecated_reason_overview |
| 85 | + email_subject = "Your Local Unit Addition Request: Deprecated" |
| 86 | + email_body = render_to_string("email/local_units/local_unit.html", email_context) |
| 87 | + email_type = "Deprecate Local Unit" |
| 88 | + |
| 89 | + send_notification(email_subject, user.email, email_body, email_type) |
| 90 | + return email_context |
0 commit comments