Skip to content

Commit f2b385a

Browse files
committed
refactor: #224 use django json_script filter for passing context
1 parent acca6ad commit f2b385a

26 files changed

Lines changed: 461 additions & 433 deletions

django_email_learning/personalised/views.py

Lines changed: 99 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
from django.core.files.storage import default_storage
1616
import qrcode
1717
import uuid
18-
import json
1918
import logging
2019
import io
2120

2221

23-
class ErrrorLoggingMixin(TemplateResponseMixin):
22+
class BaseTemplateView(View, TemplateResponseMixin):
2423
def errr_response(
2524
self,
2625
message: str,
@@ -38,7 +37,10 @@ def errr_response(
3837
f"{message} - Ref: {error_ref}", extra={"error_ref": error_ref}
3938
)
4039
return self.render_to_response(
41-
context={"ref": error_ref, "error_message": message, "page_title": title},
40+
context={
41+
"appContext": {"ref": error_ref, "errorMessage": message},
42+
"page_title": title,
43+
},
4244
status=status_code,
4345
)
4446

@@ -70,7 +72,7 @@ def get_decoded_token(self, request) -> dict | HttpResponse: # type: ignore[no-
7072
)
7173

7274

73-
class QuizPublicView(View, ErrrorLoggingMixin):
75+
class QuizPublicView(BaseTemplateView):
7476
template_name = "personalised/quiz_public.html"
7577

7678
def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-untyped-def]
@@ -111,12 +113,30 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
111113
]
112114
return self.render_to_response(
113115
context={
114-
"quiz": json.dumps(quiz_data),
115-
"token": token,
116-
"csrf_token": request.META.get("CSRF_COOKIE", ""),
117-
"api_endpoint": reverse(
118-
"django_email_learning:api_personalised:quiz_submission"
119-
),
116+
"appContext": {
117+
"quiz": quiz_data,
118+
"token": token,
119+
"csrfToken": request.META.get("CSRF_COOKIE", ""),
120+
"apiEndpoint": reverse(
121+
"django_email_learning:api_personalised:quiz_submission"
122+
),
123+
"localeMessages": {
124+
"quiz_intro": _(
125+
"Please select all correct answers for each question. Note that some questions may have multiple correct answers. This quiz uses negative marking for incorrect choices; if you are unsure, it is better to leave the question unanswered."
126+
),
127+
"no_answer_warning": _(
128+
"You have not selected any answers for this question. Are you sure you want to proceed?"
129+
),
130+
"your_score": _("Your score"),
131+
"error_loading_quiz": _("Error loading quiz"),
132+
"ready_to_submit": _("Ready to submit?"),
133+
"submit_quiz_note": _(
134+
"Please keep in mind that this quiz uses negative marking for incorrect answers. If you are unsure of an answer, it may be better to leave it blank."
135+
),
136+
"cancel": _("Cancel"),
137+
"submit": _("Submit"),
138+
},
139+
},
120140
}
121141
)
122142

@@ -149,7 +169,7 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
149169
)
150170

151171

152-
class CertificateFormView(View, ErrrorLoggingMixin):
172+
class CertificateFormView(BaseTemplateView):
153173
template_name = "personalised/certificate_form.html"
154174

155175
def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-untyped-def]
@@ -159,17 +179,32 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
159179

160180
return self.render_to_response(
161181
context={
162-
"enrollment_id": decoded_token["enrollment_id"],
163-
"csrf_token": request.META.get("CSRF_COOKIE", ""),
164-
"token": request.GET.get("token", ""),
165-
"api_endpoint": reverse(
166-
"django_email_learning:api_personalised:submit_certificate_form"
167-
),
182+
"appContext": {
183+
"csrfToken": request.META.get("CSRF_COOKIE", ""),
184+
"token": request.GET.get("token", ""),
185+
"apiEndpoint": reverse(
186+
"django_email_learning:api_personalised:submit_certificate_form"
187+
),
188+
"localeMessages": {
189+
"form_intro": _(
190+
"Congratulations on completing the course! To issue your certificate, please enter the name you would like displayed on it."
191+
),
192+
"full_name": _("Full Name"),
193+
"full_name_required": _("Full Name is required"),
194+
"error_sending_data": _(
195+
"An error occurred while sending data. Please try again later."
196+
),
197+
"form_submission_success": _(
198+
"Your certificate name has been submitted successfully! You can now close this window."
199+
),
200+
"submit": _("Submit"),
201+
},
202+
}
168203
}
169204
)
170205

171206

172-
class VerifyEnrollmentView(View, ErrrorLoggingMixin):
207+
class VerifyEnrollmentView(BaseTemplateView):
173208
template_name = "personalised/command_result.html"
174209

175210
def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-untyped-def]
@@ -196,12 +231,17 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
196231
return self.render_to_response(
197232
context={
198233
"page_title": _("Enrollment Verified"),
199-
"success_message": _("Your enrollment has been successfully verified."),
234+
"appContext": {
235+
"successMessage": _(
236+
"Your enrollment has been successfully verified."
237+
),
238+
"localeMessages": {"Confirm": _("Confirm")},
239+
},
200240
}
201241
)
202242

203243

204-
class UnsubscribeView(View, ErrrorLoggingMixin):
244+
class UnsubscribeView(BaseTemplateView):
205245
template_name = "personalised/command_result.html"
206246

207247
def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-untyped-def]
@@ -212,10 +252,13 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
212252
return self.render_to_response(
213253
context={
214254
"page_title": _("Confirm Unsubscription"),
215-
"confirmation_message": _(
216-
"Are you sure you want to unsubscribe from our mailing list?"
217-
),
218-
"confirm_url": f"{request.path}?token={request.GET.get('token')}&confirm=true",
255+
"appContext": {
256+
"confirmationMessage": _(
257+
"Are you sure you want to unsubscribe from our mailing list?"
258+
),
259+
"confirmUrl": f"{request.path}?token={request.GET.get('token')}&confirm=true",
260+
"localeMessages": {"Confirm": _("Confirm")},
261+
},
219262
}
220263
)
221264
command = UnsubscribeCommand(
@@ -234,14 +277,17 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
234277
return self.render_to_response(
235278
context={
236279
"page_title": _("Unsubscribed"),
237-
"success_message": _(
238-
"You have been successfully unsubscribed from our mailing list."
239-
),
280+
"appContext": {
281+
"successMessage": _(
282+
"You have been successfully unsubscribed from our mailing list."
283+
),
284+
"localeMessages": {"Confirm": _("Confirm")},
285+
},
240286
}
241287
)
242288

243289

244-
class CertificateView(View, ErrrorLoggingMixin):
290+
class CertificateView(BaseTemplateView):
245291
template_name = "personalised/certificate.html"
246292

247293
def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-untyped-def]
@@ -299,14 +345,30 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
299345
return self.render_to_response(
300346
context={
301347
"page_title": f"{_("Certificate of Completion")} | {certificate.enrollment.course.title} | {certificate.name_on_certificate}",
302-
"name": certificate.name_on_certificate.upper(),
303-
"course_title": certificate.enrollment.course.title,
304-
"issue_date": certificate.issued_at.strftime("%B %d, %Y"),
305-
"certificate_number": certificate_number,
306-
"organization_name": certificate.enrollment.course.organization.name,
307-
"logo_url": certificate.enrollment.course.organization.logo.url
308-
if certificate.enrollment.course.organization.logo
309-
else "",
310-
"qrcode_url": absolute_media_url,
348+
"appContext": {
349+
"name": certificate.name_on_certificate,
350+
"courseTitle": certificate.enrollment.course.title,
351+
"issueDate": certificate.issued_at.strftime("%B %d, %Y"),
352+
"certificateNumber": certificate_number,
353+
"organizationName": certificate.enrollment.course.organization.name,
354+
"logoUrl": certificate.enrollment.course.organization.logo.url
355+
if certificate.enrollment.course.organization.logo
356+
else "",
357+
"qrcodeUrl": absolute_media_url,
358+
"localeMessages": {
359+
"title": _("Certificate Of Completion"),
360+
"description": _(
361+
"This certifies that {name} has successfully completed the {course_title} course"
362+
).format(
363+
name=certificate.name_on_certificate,
364+
course_title=certificate.enrollment.course.title,
365+
),
366+
"issue_date": _("Issued on"),
367+
"certificate_number": _("Certificate Number"),
368+
"organization_team": _("{organization_name} Team").format(
369+
organization_name=certificate.enrollment.course.organization.name
370+
),
371+
},
372+
},
311373
}
312374
)

0 commit comments

Comments
 (0)