Skip to content

Commit aa79d74

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

File tree

27 files changed

+485
-435
lines changed

27 files changed

+485
-435
lines changed

django_email_learning/personalised/views.py

Lines changed: 113 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.views.generic.base import TemplateResponseMixin
33
from django.http import HttpResponse
44
from django.utils.translation import gettext as _
5+
from django.utils.translation import get_language_info, get_language
56
from django.urls import reverse
67
from django_email_learning.models import ContentDelivery, EnrollmentStatus, Certificate
78
from django_email_learning.services import jwt_service
@@ -15,12 +16,11 @@
1516
from django.core.files.storage import default_storage
1617
import qrcode
1718
import uuid
18-
import json
1919
import logging
2020
import io
2121

2222

23-
class ErrrorLoggingMixin(TemplateResponseMixin):
23+
class BaseTemplateView(View, TemplateResponseMixin):
2424
def errr_response(
2525
self,
2626
message: str,
@@ -38,7 +38,10 @@ def errr_response(
3838
f"{message} - Ref: {error_ref}", extra={"error_ref": error_ref}
3939
)
4040
return self.render_to_response(
41-
context={"ref": error_ref, "error_message": message, "page_title": title},
41+
context={
42+
"appContext": {"ref": error_ref, "errorMessage": message},
43+
"page_title": title,
44+
},
4245
status=status_code,
4346
)
4447

@@ -69,8 +72,15 @@ def get_decoded_token(self, request) -> dict | HttpResponse: # type: ignore[no-
6972
title=_("Expired Link"),
7073
)
7174

75+
def get_app_context(self) -> dict: # type: ignore[no-untyped-def]
76+
current_lang_code = get_language()
77+
lang_info = get_language_info(current_lang_code)
78+
return {
79+
"direction": "rtl" if lang_info["bidi"] else "ltr",
80+
}
81+
7282

73-
class QuizPublicView(View, ErrrorLoggingMixin):
83+
class QuizPublicView(BaseTemplateView):
7484
template_name = "personalised/quiz_public.html"
7585

7686
def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-untyped-def]
@@ -111,12 +121,31 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
111121
]
112122
return self.render_to_response(
113123
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-
),
124+
"appContext": {
125+
"quiz": quiz_data,
126+
"token": token,
127+
"csrfToken": request.META.get("CSRF_COOKIE", ""),
128+
"apiEndpoint": reverse(
129+
"django_email_learning:api_personalised:quiz_submission"
130+
),
131+
"localeMessages": {
132+
"quiz_intro": _(
133+
"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."
134+
),
135+
"no_answer_warning": _(
136+
"You have not selected any answers for this question. Are you sure you want to proceed?"
137+
),
138+
"your_score": _("Your score"),
139+
"error_loading_quiz": _("Error loading quiz"),
140+
"ready_to_submit": _("Ready to submit?"),
141+
"submit_quiz_note": _(
142+
"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."
143+
),
144+
"cancel": _("Cancel"),
145+
"submit": _("Submit"),
146+
},
147+
}
148+
| self.get_app_context(),
120149
}
121150
)
122151

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

151180

152-
class CertificateFormView(View, ErrrorLoggingMixin):
181+
class CertificateFormView(BaseTemplateView):
153182
template_name = "personalised/certificate_form.html"
154183

155184
def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-untyped-def]
@@ -159,17 +188,33 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
159188

160189
return self.render_to_response(
161190
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-
),
191+
"appContext": {
192+
"csrfToken": request.META.get("CSRF_COOKIE", ""),
193+
"token": request.GET.get("token", ""),
194+
"apiEndpoint": reverse(
195+
"django_email_learning:api_personalised:submit_certificate_form"
196+
),
197+
"localeMessages": {
198+
"form_intro": _(
199+
"Congratulations on completing the course! To issue your certificate, please enter the name you would like displayed on it."
200+
),
201+
"full_name": _("Full Name"),
202+
"full_name_required": _("Full Name is required"),
203+
"error_sending_data": _(
204+
"An error occurred while sending data. Please try again later."
205+
),
206+
"form_submission_success": _(
207+
"Your certificate name has been submitted successfully! You can now close this window."
208+
),
209+
"submit": _("Submit"),
210+
},
211+
}
212+
| self.get_app_context(),
168213
}
169214
)
170215

171216

172-
class VerifyEnrollmentView(View, ErrrorLoggingMixin):
217+
class VerifyEnrollmentView(BaseTemplateView):
173218
template_name = "personalised/command_result.html"
174219

175220
def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-untyped-def]
@@ -196,12 +241,18 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
196241
return self.render_to_response(
197242
context={
198243
"page_title": _("Enrollment Verified"),
199-
"success_message": _("Your enrollment has been successfully verified."),
244+
"appContext": {
245+
"successMessage": _(
246+
"Your enrollment has been successfully verified."
247+
),
248+
"localeMessages": {"Confirm": _("Confirm")},
249+
}
250+
| self.get_app_context(),
200251
}
201252
)
202253

203254

204-
class UnsubscribeView(View, ErrrorLoggingMixin):
255+
class UnsubscribeView(BaseTemplateView):
205256
template_name = "personalised/command_result.html"
206257

207258
def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-untyped-def]
@@ -212,10 +263,14 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
212263
return self.render_to_response(
213264
context={
214265
"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",
266+
"appContext": {
267+
"confirmationMessage": _(
268+
"Are you sure you want to unsubscribe from our mailing list?"
269+
),
270+
"confirmUrl": f"{request.path}?token={request.GET.get('token')}&confirm=true",
271+
"localeMessages": {"Confirm": _("Confirm")},
272+
}
273+
| self.get_app_context(),
219274
}
220275
)
221276
command = UnsubscribeCommand(
@@ -234,14 +289,18 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
234289
return self.render_to_response(
235290
context={
236291
"page_title": _("Unsubscribed"),
237-
"success_message": _(
238-
"You have been successfully unsubscribed from our mailing list."
239-
),
292+
"appContext": {
293+
"successMessage": _(
294+
"You have been successfully unsubscribed from our mailing list."
295+
),
296+
"localeMessages": {"Confirm": _("Confirm")},
297+
}
298+
| self.get_app_context(),
240299
}
241300
)
242301

243302

244-
class CertificateView(View, ErrrorLoggingMixin):
303+
class CertificateView(BaseTemplateView):
245304
template_name = "personalised/certificate.html"
246305

247306
def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-untyped-def]
@@ -299,14 +358,31 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
299358
return self.render_to_response(
300359
context={
301360
"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,
361+
"appContext": {
362+
"name": certificate.name_on_certificate,
363+
"courseTitle": certificate.enrollment.course.title,
364+
"issueDate": certificate.issued_at.strftime("%B %d, %Y"),
365+
"certificateNumber": certificate_number,
366+
"organizationName": certificate.enrollment.course.organization.name,
367+
"logoUrl": certificate.enrollment.course.organization.logo.url
368+
if certificate.enrollment.course.organization.logo
369+
else "",
370+
"qrcodeUrl": absolute_media_url,
371+
"localeMessages": {
372+
"title": _("Certificate Of Completion"),
373+
"description": _(
374+
"This certifies that {name} has successfully completed the {course_title} course"
375+
).format(
376+
name=certificate.name_on_certificate,
377+
course_title=certificate.enrollment.course.title,
378+
),
379+
"issue_date": _("Issued on"),
380+
"certificate_number": _("Certificate Number"),
381+
"organization_team": _("{organization_name} Team").format(
382+
organization_name=certificate.enrollment.course.organization.name
383+
),
384+
},
385+
}
386+
| self.get_app_context(),
311387
}
312388
)

0 commit comments

Comments
 (0)