|
| 1 | +from django.http import HttpResponse |
| 2 | +from django.template import loader |
| 3 | +from rest_framework import permissions |
| 4 | +from rest_framework.views import APIView |
| 5 | + |
| 6 | + |
| 7 | +class EAPEmailPreview(APIView): |
| 8 | + permission_classes = [permissions.IsAuthenticated] |
| 9 | + |
| 10 | + def get(self, request): |
| 11 | + type_param = request.GET.get("type") |
| 12 | + |
| 13 | + template_map = { |
| 14 | + "registration": "email/eap/registration.html", |
| 15 | + "submission": "email/eap/submission.html", |
| 16 | + "feedback_to_national_society": "email/eap/feedback_to_national_society.html", |
| 17 | + "resubmission_of_revised_eap": "email/eap/re-submission.html", |
| 18 | + "feedback_for_revised_eap": "email/eap/feedback_to_revised_eap.html", |
| 19 | + "technically_validated_eap": "email/eap/technically_validated_eap.html", |
| 20 | + "pending_pfa": "email/eap/pending_pfa.html", |
| 21 | + "approved_eap": "email/eap/approved.html", |
| 22 | + "reminder": "email/eap/reminder.html", |
| 23 | + } |
| 24 | + |
| 25 | + if type_param not in template_map: |
| 26 | + valid_values = ", ".join(template_map.keys()) |
| 27 | + return HttpResponse( |
| 28 | + f"Invalid 'type' parameter. Please use one of the following values: {valid_values}.", |
| 29 | + ) |
| 30 | + |
| 31 | + context_map = { |
| 32 | + "registration": { |
| 33 | + "eap_type_display": "FULL EAP", |
| 34 | + "country_name": "Test Country", |
| 35 | + "national_society": "Test National Society", |
| 36 | + "supporting_partners": [ |
| 37 | + {"society_name": "Partner 1"}, |
| 38 | + {"society_name": "Partner 2"}, |
| 39 | + ], |
| 40 | + "disaster_type": "Flood", |
| 41 | + "ns_contact_name": "Test registration name", |
| 42 | + "ns_contact_email": "[email protected]", |
| 43 | + "ns_contact_phone": "1234567890", |
| 44 | + }, |
| 45 | + "submission": { |
| 46 | + "eap_type_display": "SIMPLIFIED EAP", |
| 47 | + "country_name": "Test Country", |
| 48 | + "national_society": "Test National Society", |
| 49 | + "people_targated": 100, |
| 50 | + "latest_eap_id": 1, |
| 51 | + "supporting_partners": [ |
| 52 | + {"society_name": "Partner NS 1"}, |
| 53 | + {"society_name": "Partner NS 2"}, |
| 54 | + ], |
| 55 | + "disaster_type": "Flood", |
| 56 | + "total_budget": "250,000 CHF", |
| 57 | + "ns_contact_name": "Test Ns Contact name", |
| 58 | + "ns_contact_email": "[email protected]", |
| 59 | + "ns_contact_phone": "+977-9800000000", |
| 60 | + }, |
| 61 | + "feedback_to_national_society": { |
| 62 | + "registration_id": 1, |
| 63 | + "eap_type_display": "FULL EAP", |
| 64 | + "country_name": "Test Country", |
| 65 | + "national_society": "Test National Society", |
| 66 | + }, |
| 67 | + "resubmission_of_revised_eap": { |
| 68 | + "latest_eap_id": 1, |
| 69 | + "eap_type_display": "SIMPLIFIED EAP", |
| 70 | + "country_name": "Test Country", |
| 71 | + "national_society": "Test National Society", |
| 72 | + "supporting_partners": [ |
| 73 | + {"society_name": "Partner NS 1"}, |
| 74 | + {"society_name": "Partner NS 2"}, |
| 75 | + ], |
| 76 | + "version": 2 or 3, |
| 77 | + "people_targated": 100, |
| 78 | + "disaster_type": "Flood", |
| 79 | + "total_budget": "250,000 CHF", |
| 80 | + "ns_contact_name": "Test Ns Contact name", |
| 81 | + "ns_contact_email": "[email protected]", |
| 82 | + "ns_contact_phone": "+977-9800000000", |
| 83 | + }, |
| 84 | + "feedback_for_revised_eap": { |
| 85 | + "registration_id": 1, |
| 86 | + "eap_type_display": "FULL EAP", |
| 87 | + "country_name": "Test Country", |
| 88 | + "national_society": "Test National Society", |
| 89 | + "version": 2, |
| 90 | + }, |
| 91 | + "technically_validated_eap": { |
| 92 | + "registration_id": 1, |
| 93 | + "eap_type_display": "FULL EAP", |
| 94 | + "country_name": "Test Country", |
| 95 | + "national_society": "Test National Society", |
| 96 | + "disaster_type": "Flood", |
| 97 | + }, |
| 98 | + "pending_pfa": { |
| 99 | + "eap_type_display": "FULL EAP", |
| 100 | + "country_name": "Test Country", |
| 101 | + "national_society": "Test National Society", |
| 102 | + "disaster_type": "Flood", |
| 103 | + }, |
| 104 | + "approved_eap": { |
| 105 | + "eap_type_display": "FULL EAP", |
| 106 | + "country_name": "Test Country", |
| 107 | + "national_society": "Test National Society", |
| 108 | + "disaster_type": "Flood", |
| 109 | + }, |
| 110 | + "reminder": { |
| 111 | + "eap_type_display": "FULL EAP", |
| 112 | + "country_name": "Test Country", |
| 113 | + "national_society": "Test National Society", |
| 114 | + "disaster_type": "Flood", |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + context = context_map.get(type_param) |
| 119 | + if context is None: |
| 120 | + return HttpResponse("No context found for the email preview.") |
| 121 | + template_file = template_map[type_param] |
| 122 | + template = loader.get_template(template_file) |
| 123 | + return HttpResponse(template.render(context, request)) |
0 commit comments