Skip to content

Commit 5c8aaad

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

File tree

27 files changed

+507
-451
lines changed

27 files changed

+507
-451
lines changed

django_email_learning/personalised/views.py

Lines changed: 135 additions & 53 deletions
Large diffs are not rendered by default.

django_email_learning/platform/views.py

Lines changed: 284 additions & 20 deletions
Large diffs are not rendered by default.

django_email_learning/public/views.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.views.generic import TemplateView
22
from django.db.models import Prefetch
33
from django_email_learning.models import Organization, Course
4+
from django.utils.translation import get_language_info, get_language
45
from django.http import Http404
56
from django.urls import reverse
67
from django.utils.translation import gettext as _
@@ -54,11 +55,26 @@ def get_context_data(self, **kwargs) -> dict: # type: ignore[no-untyped-def]
5455
courses=courses,
5556
)
5657
enroll_api_path = reverse("django_email_learning:api_public:enroll")
57-
context[
58-
"enroll_api_url"
59-
] = f"{settings.DJANGO_EMAIL_LEARNING['SITE_BASE_URL']}{enroll_api_path}"
60-
context["organization_json"] = organization_data.model_dump_json()
61-
context["organization"] = organization_data.model_dump()
58+
current_lang_code = get_language()
59+
lang_info = get_language_info(current_lang_code)
60+
context["appContext"] = {
61+
"organization": organization_data.model_dump(),
62+
"enrollApiUrl": f"{settings.DJANGO_EMAIL_LEARNING['SITE_BASE_URL']}{enroll_api_path}",
63+
"direction": "rtl" if lang_info["bidi"] else "ltr",
64+
"localeMessages": {
65+
"courses": _("Courses"),
66+
"enroll_now": _("Enroll Now"),
67+
"enrol_for_course": _("Enroll for COURSE_NAME"),
68+
"email": _("email"),
69+
"cancel": _("Cancel"),
70+
"submit": _("Submit"),
71+
"enrollment_success": _("You are enrolled in this course."),
72+
"enrollment_failed": _("Enrollment failed. Please try again."),
73+
"no_courses_available": _("No courses available."),
74+
"email_required": _("Email is required"),
75+
"email_invalid": _("Please enter a valid email address"),
76+
},
77+
}
6278
context["page_title"] = organization.name
6379
return context
6480

django_email_learning/templates/personalised/base.html

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@
77
{% vite_hmr_client %}
88
{% vite_react_refresh %}
99
{% get_current_language_bidi as IS_RTL %}
10-
<script id="app-context" type="application/json">
11-
{
12-
"direction": "{% if IS_RTL %}rtl{% else %}ltr{% endif %}",
13-
"ref": "{{ ref }}",
14-
"errorMessage": "{{ error_message }}",{% block extra_context %}{% endblock %}
15-
"localeMessages": {{% block extra_locale_messages %}{% endblock %}
16-
}
17-
}
18-
</script>
10+
{{ appContext | json_script:"app-context" }}
1911
{% block head_script %}{% endblock %}
2012
<meta charset="UTF-8" />
2113
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
{% extends "personalised/base.html" %}
2-
{% load i18n %}
32
{% load django_vite %}
4-
{% block extra_context %}
5-
"name": "{{ name }}",
6-
"issueDate": "{{ issue_date }}",
7-
"certificateNumber": "{{ certificate_number }}",
8-
"qrcodeUrl": "{{ qrcode_url }}",
9-
"logoUrl": "{{ logo_url }}",
10-
{% endblock %}
11-
{% block extra_locale_messages %}
12-
"title": "{% translate 'Certificate Of Completion' %}",
13-
"description": "{% blocktranslate with name=name course_title=course_title %}This certifies that {{ name }} has successfully completed the {{ course_title }} course{% endblocktranslate %}",
14-
"issue_date": "{% translate 'Issued on' %}",
15-
"certificate_number": "{% translate 'Certificate Number' %}",
16-
"organization_team": "{% blocktranslate with organization_name=organization_name %}{{ organization_name }} Team{% endblocktranslate %}"
17-
{% endblock %}
183
{% block head_script %}
194
{% vite_asset 'personalised/certificate/Certificate.jsx' %}
205
{% endblock %}
Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
{% extends "personalised/base.html" %}
2-
{% load i18n %}
32
{% load django_vite %}
4-
{% block extra_context %}
5-
"token": "{{ token }}",
6-
"csrfToken": "{{ csrf_token }}",
7-
"apiEndpoint": "{{ api_endpoint }}",
8-
{% endblock %}
9-
{% block extra_locale_messages %}
10-
"form_intro": "{% translate 'Congratulations on completing the course! To issue your certificate, please enter the name you would like displayed on it.' %}",
11-
"full_name": "{% translate 'Full Name' %}",
12-
"full_name_required": "{% translate 'Full Name is required' %}",
13-
"error_sending_data": "{% translate 'An error occurred while sending data. Please try again later.' %}",
14-
"form_submission_success": "{% translate 'Your certificate name has been submitted successfully! You can now close this window.' %}",
15-
"submit": "{% translate 'Submit' %}"
16-
{% endblock %}
173
{% block head_script %}
184
{% vite_asset 'personalised/certificate_form/CertificateForm.jsx' %}
195
{% endblock %}
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
{% extends "personalised/base.html" %}
2-
{% load i18n %}
32
{% load django_vite %}
4-
{% block extra_context %}
5-
"successMessage": "{{ success_message|escapejs }}",
6-
"confirmationMessage": "{{ confirmation_message|escapejs }}",
7-
"confirmUrl": "{{ confirm_url|escapejs }}",
8-
{% endblock %}
9-
{% block extra_locale_messages %}
10-
"Confirm": "{% translate 'Confirm' %}"
11-
{% endblock %}
123
{% block head_script %}
134
{% vite_asset 'personalised/command_result/CommandResult.jsx' %}
145
{% endblock %}
Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
{% extends "personalised/base.html" %}
2-
{% load i18n %}
32
{% load django_vite %}
4-
{% block extra_context %}
5-
"quiz": {{ quiz|default:"null"|safe }},
6-
"token": "{{ token }}",
7-
"csrfToken": "{{ csrf_token }}",
8-
"apiEndpoint": "{{ api_endpoint }}",{% endblock %}
9-
{% block extra_locale_messages %}
10-
"quiz_intro": "{% translate '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.' %}",
11-
"no_answer_warning": "{% translate 'You have not selected any answers for this question. Are you sure you want to proceed?' %}",
12-
"your_score": "{% translate 'Your score' %}",
13-
"error_loading_quiz": "{% translate 'Error loading quiz' %}",
14-
"ready_to_submit": "{% translate 'Ready to submit?' %}",
15-
"submit_quiz_note": "{% translate '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.' %}",
16-
"cancel": "{% translate 'Cancel' %}",
17-
"submit": "{% translate 'Submit' %}"
18-
{% endblock %}
193
{% block head_script %}
204
{% vite_asset 'personalised/quiz_public/Quiz.jsx' %}
215
{% endblock %}

django_email_learning/templates/platform/base.html

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,12 @@
1-
{% load django_vite %}
2-
{% load static %}
3-
{% load i18n %}
4-
{% get_current_language_bidi as IS_RTL %}
5-
<!doctype html>
1+
{% load django_vite %}{% load static %}{% load i18n %}{% get_current_language_bidi as IS_RTL %}<!doctype html>
62
<html lang="en" dir="{% block direction %}{% if IS_RTL %}rtl{% else %}ltr{% endif %}{% endblock %}">
73
<head>
84
{% vite_hmr_client %}
95
{% vite_react_refresh %}
106
<script>
11-
localStorage.setItem('activeOrganizationId', '{{ active_organization_id }}');
12-
</script>
13-
14-
<script id="app-context" type="application/json">
15-
{
16-
"apiBaseUrl": "{{ api_base_url }}",
17-
"platformBaseUrl": "{{ platform_base_url }}",
18-
"userRole": "{{ user_role }}",
19-
"isPlatformAdmin": {{ is_platform_admin|yesno:"true,false" }},
20-
"isOrganizationAdmin": {{ is_organization_admin|yesno:"true,false" }},
21-
"direction": "{% if IS_RTL %}rtl{% else %}ltr{% endif %}",{% block extra_context %}{% endblock %}
22-
"localeMessages": {
23-
"organizations": "{% translate 'Organizations' %}",
24-
"course_management": "{% translate 'Course Management' %}",
25-
"learners": "{% translate 'Learners' %}",
26-
"settings": "{% translate 'Settings' %}",
27-
"api_keys": "{% translate 'API Keys' %}",
28-
"content_delivery_job": "{% translate 'Content Delivery Job' %}",
29-
"last_run": "{% translate 'Last Run:' %}",
30-
"content_delivery_tooltip": "{% translate 'This job should run on a regular schedule to ensure timely content delivery. Configure a cron job or cloud scheduler to execute it at appropriate intervals, such as every five minutes.' %}",{% block extra_locale_messages %}{% endblock %}
31-
}
32-
}
7+
localStorage.setItem('activeOrganizationId', '{{ activeOrganizationId | escapejs }}');
338
</script>
9+
{{ appContext | json_script:"app-context" }}
3410
{% block extra_head %}{% endblock %}
3511
<meta charset="UTF-8" />
3612
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,5 @@
11
{% extends "platform/base.html" %}
22
{% load django_vite %}
3-
{% load i18n %}
4-
{%block extra_context %}
5-
"course_title": "{{ course.title|escapejs }}",
6-
"course_id": "{{ course.id }}",
7-
{% endblock %}
8-
{% block extra_locale_messages %}
9-
"actions": "{% translate 'Actions' %}",
10-
"published": "{% translate 'Published' %}",
11-
"type": "{% translate 'Type' %}",
12-
"waiting_time": "{% translate 'Waiting Time' %}",
13-
"title": "{% translate 'Title' %}",
14-
"add_quiz": "{% translate 'Add Quiz' %}",
15-
"add_lesson": "{% translate 'Add Lesson' %}",
16-
"lesson": "{% translate 'Lesson' %}",
17-
"quiz": "{% translate 'Quiz' %}",
18-
"add": "{% translate 'Add' %}",
19-
"new_lesson": "{% translate 'New Lesson' %}",
20-
"update_lesson": "{% translate 'Update Lesson' %}",
21-
"new_quiz": "{% translate 'New Quiz' %}",
22-
"update_quiz": "{% translate 'Update Quiz' %}",
23-
"lesson_title": "{% translate 'Lesson Title' %}",
24-
"lesson_waiting_tooltip": "{% translate 'Set the amount of time that we should wait after the previous lesson or quiz submission before sending this lesson' %}",
25-
"days": "{% translate 'Days' %}",
26-
"hours": "{% translate 'Hours' %}",
27-
"back": "{% translate 'Back' %}",
28-
"cancel": "{% translate 'Cancel' %}",
29-
"delete": "{% translate 'Delete' %}",
30-
"save_lesson": "{% translate 'Save Lesson' %}",
31-
"save_quiz": "{% translate 'Save Quiz' %}",
32-
"quiz_title": "{% translate 'Quiz Title' %}",
33-
"add_question": "{% translate 'Add Question' %}",
34-
"quiz_settings": "{% translate 'Quiz Settings' %}",
35-
"waiting_period": "{% translate 'Waiting Period' %}",
36-
"quiz_waiting_tooltip": "{% translate 'Time to wait after the previous content delivery before sending this quiz' %}",
37-
"required_score": "{% translate 'Required Score to Pass (%)' %}",
38-
"score_tooltip": "{% translate 'Minimum percentage score required to pass this quiz' %}",
39-
"period": "{% translate 'Period' %}",
40-
"period_tooltip": "{% translate 'Time to wait after the previous content delivery before sending this quiz' %}",
41-
"percentage": "{% translate 'Percentage' %}",
42-
"quiz_deadline": "{% translate 'Deadline to Complete Quiz' %}",
43-
"deadline_tooltip": "{% translate 'Maximum time allowed to complete the quiz' %}",
44-
"question_selection_strategy": "{% translate 'Selection Strategy' %}",
45-
"question_selection_strategy_tooltip": "{% translate 'Choose how questions are selected for each quiz attempt, if total questions is less than 6, all questions will be used even if \'Random Questions\' is selected' %}",
46-
"all_questions": "{% translate 'All Questions' %}",
47-
"random_questions": "{% translate 'Random Questions' %}",
48-
"question": "{% translate 'Question' %}",
49-
"add_option": "{% translate 'Add Option' %}",
50-
"option_text": "{% translate 'Option Text' %}",
51-
"options": "{% translate 'Options' %}",
52-
"correct_answer": "{% translate 'Correct Answer' %}",
53-
"actions": "{% translate 'Actions' %}",
54-
"quiz_title_empty": "{% translate 'Quiz title cannot be empty.' %}",
55-
"at_least_one_question": "{% translate 'Quiz must have at least one question' %}",
56-
"at_least_two_options": "{% translate 'Question QUESTION_NUMBER must have at least two answer options' %}",
57-
"at_least_one_correct": "{% translate 'Question QUESTION_NUMBER must have at least one correct answer' %}",
58-
"fix_errors": "{% translate 'Please fix the errors in the form before submitting.' %}",
59-
"lesson_title_required": "{% translate 'Lesson title is required.' %}",
60-
"lesson_content_required": "{% translate 'Lesson content is required.' %}",
61-
"delete_content_confirmation": "{% translate 'Are you sure you want to delete the content: CONTENT_TITLE?' %}",
62-
"unverified": "{% translate 'Unverified' %}",
63-
"active": "{% translate 'Active' %}",
64-
"deactivated": "{% translate 'Deactivated' %}",
65-
"completed": "{% translate 'Completed' %}",
66-
"enrollments_distribution": "{% translate 'Enrollments Distribution' %}",
67-
"weekly_enrollments": "{% translate 'Weekly Enrollments' %}"
68-
{% endblock %}
693
{% block extra_head %}
704
{% vite_asset 'platform/course/Course.jsx' %}
715
{% endblock %}

0 commit comments

Comments
 (0)