Skip to content

Commit acca6ad

Browse files
authored
Merge pull request #224 from AvaCodeSolutions/refactor/223/frontend-contex
refactor: #223 use AppContext in frontend for passing Django context
2 parents 3fd546f + d581bf0 commit acca6ad

48 files changed

Lines changed: 300 additions & 268 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

django_email_learning/personalised/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from django.core.files.storage import default_storage
1616
import qrcode
1717
import uuid
18+
import json
1819
import logging
1920
import io
2021

@@ -110,7 +111,7 @@ def get(self, request, *args, **kwargs) -> HttpResponse: # type: ignore[no-unty
110111
]
111112
return self.render_to_response(
112113
context={
113-
"quiz": quiz_data,
114+
"quiz": json.dumps(quiz_data),
114115
"token": token,
115116
"csrf_token": request.META.get("CSRF_COOKIE", ""),
116117
"api_endpoint": reverse(

django_email_learning/templates/personalised/base.html

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@
44
<!doctype html>
55
<html lang="en">
66
<head>
7-
<script>
8-
const error_message = "{{ error_message }}";
9-
const ref = "{{ ref }}";
10-
</script>
117
{% vite_hmr_client %}
128
{% vite_react_refresh %}
9+
{% 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>
1319
{% block head_script %}{% endblock %}
1420
<meta charset="UTF-8" />
1521
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
1622
<link rel="icon" type="image/png" href="{% static 'logo.png' %}" />
1723
<title>{% block title %}{{ page_title }}{% endblock %}</title>
1824
</head>
19-
{% get_current_language_bidi as IS_RTL %}
25+
2026
<body {% if IS_RTL %}dir="rtl"{% endif %}>
2127
<div id="root">
2228
</div>
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
{% extends "personalised/base.html" %}
22
{% load i18n %}
33
{% load django_vite %}
4-
{% block head_script %}
5-
<script>
6-
const name = "{{ name }}";
7-
const course_title = "{{ course_title }}";
8-
const issue_date = "{{ issue_date }}";
9-
const certificate_number = "{{ certificate_number }}";
10-
const organization_name = "{{ organization_name }}";
11-
const qrcode_url = "{{ qrcode_url }}";
12-
const logo_url = "{{ logo_url }}";
13-
const localeMessages = {
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 %}
1412
"title": "{% translate 'Certificate Of Completion' %}",
1513
"description": "{% blocktranslate with name=name course_title=course_title %}This certifies that {{ name }} has successfully completed the {{ course_title }} course{% endblocktranslate %}",
1614
"issue_date": "{% translate 'Issued on' %}",
1715
"certificate_number": "{% translate 'Certificate Number' %}",
18-
"organization_team": "{% blocktranslate with organization_name=organization_name %}{{ organization_name }} Team{% endblocktranslate %}",
19-
}
20-
</script>
16+
"organization_team": "{% blocktranslate with organization_name=organization_name %}{{ organization_name }} Team{% endblocktranslate %}"
17+
{% endblock %}
18+
{% block head_script %}
2119
{% vite_asset 'personalised/certificate/Certificate.jsx' %}
2220
{% endblock %}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{% extends "personalised/base.html" %}
22
{% load i18n %}
33
{% 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 %}
417
{% block head_script %}
5-
<script>
6-
const token = "{{ token }}";
7-
const csrfToken = "{{ csrf_token }}";
8-
const apiEndpoint = "{{ api_endpoint }}";
9-
const localeMessages = {
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-
}
17-
</script>
1818
{% vite_asset 'personalised/certificate_form/CertificateForm.jsx' %}
1919
{% endblock %}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{% extends "personalised/base.html" %}
22
{% load i18n %}
33
{% 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 %}
412
{% block head_script %}
5-
<script>
6-
const success_message = "{{ success_message|escapejs }}";
7-
const confirmation_message = "{{ confirmation_message|escapejs }}";
8-
const confirm_url = "{{ confirm_url|escapejs }}";
9-
const localeMessages = {
10-
"Confirm": "{% translate 'Confirm' %}",
11-
}
12-
</script>
1313
{% vite_asset 'personalised/command_result/CommandResult.jsx' %}
1414
{% endblock %}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
{% extends "personalised/base.html" %}
22
{% load i18n %}
33
{% load django_vite %}
4-
{% block head_script %}
5-
<script>
6-
const quiz = {{ quiz|default:"null"|safe }};
7-
const token = "{{ token }}";
8-
const csrfToken = "{{ csrf_token }}";
9-
const apiEndpoint = "{{ api_endpoint }}";
10-
const localeMessages = {
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 %}
1110
"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.' %}",
1211
"no_answer_warning": "{% translate 'You have not selected any answers for this question. Are you sure you want to proceed?' %}",
1312
"your_score": "{% translate 'Your score' %}",
1413
"error_loading_quiz": "{% translate 'Error loading quiz' %}",
1514
"ready_to_submit": "{% translate 'Ready to submit?' %}",
1615
"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.' %}",
1716
"cancel": "{% translate 'Cancel' %}",
18-
"submit": "{% translate 'Submit' %}",
19-
}
20-
</script>
17+
"submit": "{% translate 'Submit' %}"
18+
{% endblock %}
19+
{% block head_script %}
2120
{% vite_asset 'personalised/quiz_public/Quiz.jsx' %}
2221
{% endblock %}

django_email_learning/templates/platform/base.html

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@
99
{% vite_react_refresh %}
1010
<script>
1111
localStorage.setItem('activeOrganizationId', '{{ active_organization_id }}');
12-
localStorage.setItem('apiBaseUrl', '{{ api_base_url }}');
13-
localStorage.setItem('platformBaseUrl', '{{ platform_base_url }}');
14-
localStorage.setItem('userRole', '{{ user_role }}');
15-
localStorage.setItem('isPlatformAdmin', {{ is_platform_admin|yesno:"true,false" }});
16-
localStorage.setItem('isOrganizationAdmin', {{ is_organization_admin|yesno:"true,false" }});
17-
const direction = "{% if IS_RTL %}rtl{% else %}ltr{% endif %}";
18-
let localeMessages = {
19-
"organizations": "{% translate 'Organizations' %}",
20-
"course_management": "{% translate 'Course Management' %}",
21-
"learners": "{% translate 'Learners' %}",
22-
"settings": "{% translate 'Settings' %}",
23-
"api_keys": "{% translate 'API Keys' %}",
24-
"content_delivery_job": "{% translate 'Content Delivery Job' %}",
25-
"last_run": "{% translate 'Last Run:' %}",
26-
"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.' %}"
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 %}
2731
}
32+
}
2833
</script>
2934
{% block extra_head %}{% endblock %}
3035
<meta charset="UTF-8" />

django_email_learning/templates/platform/course.html

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
{% extends "platform/base.html" %}
22
{% load django_vite %}
3-
{% block extra_head %}
43
{% load i18n %}
5-
<script>
6-
let course_title = "{{ course.title|escapejs }}";
7-
const course_id = "{{ course.id }}";
8-
localeMessages = {
9-
...localeMessages,
4+
{%block extra_context %}
5+
"course_title": "{{ course.title|escapejs }}",
6+
"course_id": "{{ course.id }}",
7+
{% endblock %}
8+
{% block extra_locale_messages %}
109
"actions": "{% translate 'Actions' %}",
1110
"published": "{% translate 'Published' %}",
1211
"type": "{% translate 'Type' %}",
@@ -65,8 +64,8 @@
6564
"deactivated": "{% translate 'Deactivated' %}",
6665
"completed": "{% translate 'Completed' %}",
6766
"enrollments_distribution": "{% translate 'Enrollments Distribution' %}",
68-
"weekly_enrollments": "{% translate 'Weekly Enrollments' %}",
69-
}
70-
</script>
67+
"weekly_enrollments": "{% translate 'Weekly Enrollments' %}"
68+
{% endblock %}
69+
{% block extra_head %}
7170
{% vite_asset 'platform/course/Course.jsx' %}
7271
{% endblock %}
Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,52 @@
11
{% extends "platform/base.html" %}
22
{% load django_vite %}
33
{% load i18n %}
4+
{% block extra_locale_messages %}
5+
"actions": "{% translate 'Actions' %}",
6+
"all": "{% translate 'All' %}",
7+
"enable_course": "{% translate 'Enable COURSE_NAME' %}",
8+
"disable_course": "{% translate 'Disable COURSE_NAME' %}",
9+
"delete_course": "{% translate 'Delete COURSE_NAME' %}",
10+
"enabled": "{% translate 'Enabled' %}",
11+
"disabled": "{% translate 'Disabled' %}",
12+
"title": "{% translate 'Title' %}",
13+
"slug": "{% translate 'Slug' %}",
14+
"filter": "{% translate 'Filter' %}",
15+
"add_course": "{% translate 'Add a Course' %}",
16+
"course_status": "{% translate 'Course Status' %}",
17+
"cancel": "{% translate 'Cancel' %}",
18+
"delete": "{% translate 'Delete' %}",
19+
"create": "{% translate 'Create' %}",
20+
"continue": "{% translate 'Continue' %}",
21+
"update": "{% translate 'Update' %}",
22+
"course_title": "{% translate 'Course Title' %}",
23+
"course_description": "{% translate 'Course Description' %}",
24+
"course_slug": "{% translate 'Course Slug' %}",
25+
"add_imap_connection": "{% translate 'Add IMAP Connection' %}",
26+
"imap_connection_tooltip": "{% translate 'You don\'t need an IMAP connection to build your course, but you will need one if you want your users to interact via email. For example, they can sign up or check their progress just by sending a message. This is a great solution if your audience has limited platform access.' %}",
27+
"new_imap_connection": "{% translate 'New IMAP Connection' %}",
28+
"imap_connection": "{% translate 'IMAP Connection' %}",
29+
"add": "{% translate 'Add' %}",
30+
"email": "{% translate 'Email' %}",
31+
"password": "{% translate 'Password' %}",
32+
"server": "{% translate 'Server' %}",
33+
"port": "{% translate 'Port' %}",
34+
"course_enable_confirmation": "{% translate 'Are you sure you want to enable the course COURSE_NAME?' %}",
35+
"course_disable_confirmation": "{% translate 'Are you sure you want to disable the course COURSE_NAME?' %}",
36+
"course_delete_confirmation": "{% translate 'Are you sure you want to delete the course COURSE_NAME?' %}",
37+
"title_required_helper_text": "{% translate 'The course title is required.' %}",
38+
"description_required_helper_text": "{% translate 'The course description is required.' %}",
39+
"slug_required_helper_text": "{% translate 'The course slug is required.' %}",
40+
"email_required_helper_text": "{% translate 'The email is required.' %}",
41+
"password_required_helper_text": "{% translate 'The password is required.' %}",
42+
"server_required_helper_text": "{% translate 'The server is required.' %}",
43+
"port_required_helper_text": "{% translate 'The port is required.' %}",
44+
"invalid_port_helper_text": "{% translate 'The port must be a valid number.' %}",
45+
"invalid_email_helper_text": "{% translate 'The email must be a valid email address.' %}",
46+
"total_enrollments": "{% translate 'Total Enrollments' %}",
47+
"upload_button_label": "{% translate 'Upload Image' %}",
48+
"remove_image": "{% translate 'Remove Image' %}",
49+
"uploaded_image_alt": "{% translate 'Course Image' %}"{% endblock %}
450
{% block extra_head %}
5-
<script>
6-
localeMessages = {
7-
...localeMessages,
8-
"actions": "{% translate 'Actions' %}",
9-
"all": "{% translate 'All' %}",
10-
"enable_course": "{% translate 'Enable COURSE_NAME' %}",
11-
"disable_course": "{% translate 'Disable COURSE_NAME' %}",
12-
"delete_course": "{% translate 'Delete COURSE_NAME' %}",
13-
"enabled": "{% translate 'Enabled' %}",
14-
"disabled": "{% translate 'Disabled' %}",
15-
"title": "{% translate 'Title' %}",
16-
"slug": "{% translate 'Slug' %}",
17-
"filter": "{% translate 'Filter' %}",
18-
"add_course": "{% translate 'Add a Course' %}",
19-
"course_status": "{% translate 'Course Status' %}",
20-
"cancel": "{% translate 'Cancel' %}",
21-
"delete": "{% translate 'Delete' %}",
22-
"create": "{% translate 'Create' %}",
23-
"continue": "{% translate 'Continue' %}",
24-
"update": "{% translate 'Update' %}",
25-
"course_title": "{% translate 'Course Title' %}",
26-
"course_description": "{% translate 'Course Description' %}",
27-
"course_slug": "{% translate 'Course Slug' %}",
28-
"add_imap_connection": "{% translate 'Add IMAP Connection' %}",
29-
"imap_connection_tooltip": "{% translate 'You don\'t need an IMAP connection to build your course, but you will need one if you want your users to interact via email. For example, they can sign up or check their progress just by sending a message. This is a great solution if your audience has limited platform access.' %}",
30-
"new_imap_connection": "{% translate 'New IMAP Connection' %}",
31-
"imap_connection": "{% translate 'IMAP Connection' %}",
32-
"add": "{% translate 'Add' %}",
33-
"email": "{% translate 'Email' %}",
34-
"password": "{% translate 'Password' %}",
35-
"server": "{% translate 'Server' %}",
36-
"port": "{% translate 'Port' %}",
37-
"course_enable_confirmation": "{% translate 'Are you sure you want to enable the course COURSE_NAME?' %}",
38-
"course_disable_confirmation": "{% translate 'Are you sure you want to disable the course COURSE_NAME?' %}",
39-
"course_delete_confirmation": "{% translate 'Are you sure you want to delete the course COURSE_NAME?' %}",
40-
"title_required_helper_text": "{% translate 'The course title is required.' %}",
41-
"description_required_helper_text": "{% translate 'The course description is required.' %}",
42-
"slug_required_helper_text": "{% translate 'The course slug is required.' %}",
43-
"email_required_helper_text": "{% translate 'The email is required.' %}",
44-
"password_required_helper_text": "{% translate 'The password is required.' %}",
45-
"server_required_helper_text": "{% translate 'The server is required.' %}",
46-
"port_required_helper_text": "{% translate 'The port is required.' %}",
47-
"invalid_port_helper_text": "{% translate 'The port must be a valid number.' %}",
48-
"invalid_email_helper_text": "{% translate 'The email must be a valid email address.' %}",
49-
"total_enrollments": "{% translate 'Total Enrollments' %}",
50-
"upload_button_label": "{% translate 'Upload Image' %}",
51-
"remove_image": "{% translate 'Remove Image' %}",
52-
"uploaded_image_alt": "{% translate 'Course Image' %}",
53-
}
54-
</script>
5551
{% vite_asset 'platform/courses/Courses.jsx' %}
5652
{% endblock %}

django_email_learning/templates/platform/learners.html

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{% extends "platform/base.html" %}
22
{% load django_vite %}
33
{% load i18n %}
4-
{% block extra_head %}
5-
<script>
6-
localeMessages = {
7-
...localeMessages,
4+
{% block extra_locale_messages %}
85
"search_learners": "{% translate 'Search learners...' %}",
96
"learners_list": "{% translate 'Learners List' %}",
107
"nor_enrollments_found": "{% translate 'No enrollments found.' %}",
@@ -30,8 +27,7 @@
3027
"deactivated": "{% translate 'Deactivated' %}",
3128
"canceled": "{% translate 'Canceled' %}",
3229
"blcoked": "{% translate 'Blocked' %}",
33-
"inactive": "{% translate 'Inactive' %}",
34-
};
35-
</script>
30+
"inactive": "{% translate 'Inactive' %}"{% endblock %}
31+
{% block extra_head %}
3632
{% vite_asset 'platform/learners/Learners.jsx' %}
3733
{% endblock %}

0 commit comments

Comments
 (0)