Skip to content

Commit af55715

Browse files
authored
chore: convert mako template into django template
1 parent d49de13 commit af55715

File tree

12 files changed

+229
-216
lines changed

12 files changed

+229
-216
lines changed

lms/djangoapps/courseware/tests/test_discussion_xblock.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111
import uuid
1212

1313
from unittest import mock
14+
from unittest.mock import patch
1415
import ddt
1516
from django.conf import settings
1617
from django.test.utils import override_settings
1718
from django.urls import reverse
1819
from opaque_keys.edx.keys import CourseKey
1920
from web_fragments.fragment import Fragment
2021
from xblock.field_data import DictFieldData
21-
from xmodule.discussion_block import DiscussionXBlock, loader
22+
from xmodule.discussion_block import DiscussionXBlock
2223
from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE, SharedModuleStoreTestCase
2324
from xmodule.modulestore.tests.factories import BlockFactory, ToyCourseFactory
25+
from xmodule.tests.helpers import mock_render_template
2426

2527
from lms.djangoapps.course_api.blocks.tests.helpers import deserialize_usage_key
2628
from lms.djangoapps.courseware.block_render import get_block_for_descriptor
@@ -153,15 +155,15 @@ def get_rendered_template(self):
153155
assert self.render_template.call_count == 1
154156
return self.render_template.call_args_list[0][0][0]
155157

156-
def test_studio_view(self):
158+
@patch('xblock.utils.resources.ResourceLoader.render_django_template', side_effect=mock_render_template)
159+
def test_studio_view(self, mock_render_django_template):
157160
"""
158161
Test for the studio view.
159162
"""
160163
fragment = self.block.author_view()
161164
assert isinstance(fragment, Fragment)
162-
assert fragment.content == self.template_canary
163-
self.render_template.assert_called_once_with(
164-
'discussion/_discussion_inline_studio.html',
165+
mock_render_django_template.assert_called_once_with(
166+
'templates/discussion/_discussion_inline_studio.html',
165167
{
166168
'discussion_id': self.discussion_id,
167169
'is_visible': True,
@@ -192,10 +194,11 @@ def test_student_perms_are_correct(self, permissions):
192194
}
193195

194196
self.block.has_permission = lambda perm: permission_dict[perm]
195-
with mock.patch.object(loader, 'render_template', mock.Mock):
197+
with mock.patch('xmodule.discussion_block.render_to_string', return_value='') as mock_render:
196198
self.block.student_view()
197-
198-
context = self.get_template_context()
199+
# Get context from the mock call
200+
assert mock_render.call_count == 1
201+
context = mock_render.call_args_list[0][0][1]
199202

200203
for permission_name, expected_value in expected_permissions.items():
201204
assert expected_value == context[permission_name]
@@ -204,7 +207,7 @@ def test_js_init(self):
204207
"""
205208
Test proper js init function is called.
206209
"""
207-
with mock.patch.object(loader, 'render_template', mock.Mock):
210+
with mock.patch('xmodule.discussion_block.render_to_string', return_value=''):
208211
fragment = self.block.student_view()
209212
assert fragment.js_init_fn == 'DiscussionInlineBlock'
210213

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,76 @@
1-
## mako
1+
{% load static i18n %}
22

3-
<%namespace name='static' file='../static_content.html'/>
4-
5-
<%page expression_filter="h"/>
6-
7-
<%!
8-
import json
9-
from django.utils.translation import gettext as _
10-
from django.template.defaultfilters import escapejs
11-
from django.urls import reverse
12-
13-
from lms.djangoapps.discussion.django_comment_client.permissions import has_permission
14-
from openedx.core.djangolib.js_utils import dump_js_escaped_json, js_escaped_string
15-
from openedx.core.djangolib.markup import HTML
16-
%>
17-
18-
<section class="discussion discussion-board page-content-container" id="discussion-container"
19-
data-course-id="${course.id}"
20-
data-user-create-comment="${json.dumps(can_create_comment)}"
21-
data-user-create-subcomment="${json.dumps(can_create_subcomment)}"
3+
<section class="discussion discussion-board page-content-container"
4+
id="discussion-container"
5+
data-course-id="{{ course.id }}"
6+
data-user-create-comment="{{ can_create_comment|yesno:'true,false' }}"
7+
data-user-create-subcomment="{{ can_create_subcomment|yesno:'true,false' }}"
228
data-read-only="false"
23-
data-sort-preference="${sort_preference}"
24-
data-flag-moderator="${json.dumps(flag_moderator)}"
25-
data-user-group-id="${user_group_id}">
9+
data-sort-preference="{{ sort_preference }}"
10+
data-flag-moderator="{{ flag_moderator|yesno:'true,false' }}"
11+
data-user-group-id="{{ user_group_id }}">
12+
2613
<header class="page-header has-secondary">
27-
## Breadcrumb navigation
14+
{# Breadcrumb navigation #}
2815
<div class="page-header-main">
29-
<nav aria-label="${_('Discussions')}" class="sr-is-focusable" tabindex="-1">
16+
<nav aria-label="{% trans 'Discussions' as tmsg %} {{tmsg|force_escape}}" class="sr-is-focusable" tabindex="-1">
3017
<div class="has-breadcrumbs"></div>
3118
</nav>
3219
</div>
20+
3321
<div class="page-header-secondary">
34-
## Add Post button
35-
% if has_permission(user, 'create_thread', course.id):
22+
{# Add Post button #}
23+
{% if can_create_thread %}
3624
<div class="forum-actions">
37-
<button class="btn btn-outline-primary btn-small new-post-btn">${_("Add a Post")}</button>
25+
<button class="btn btn-outline-primary btn-small new-post-btn">
26+
{% trans "Add a Post" as tmsg %} {{tmsg|force_escape}}
27+
</button>
3828
</div>
39-
% endif
40-
## Search box
29+
{% endif %}
30+
{# Search box #}
4131
<div class="forum-search"></div>
4232
</div>
4333
</header>
44-
% if course_expiration_fragment:
45-
${HTML(course_expiration_fragment.content)}
46-
% endif
34+
35+
{% if course_expiration_fragment %}
36+
{{ course_expiration_fragment.content|safe }}
37+
{% endif %}
38+
4739
<div class="page-content"
48-
% if getattr(course, 'language'):
49-
lang="${course.language}"
50-
% endif
40+
{% if course.language %}
41+
lang="{{ course.language }}"
42+
{% endif %}
5143
>
5244
<div class="discussion-body">
5345
<main id="main" class="discussion-column" aria-label="Content" tabindex="-1">
54-
<article class="new-post-article is-hidden" style="display: none" tabindex="-1" aria-label="${_("New topic form")}"></article>
46+
<article class="new-post-article is-hidden" style="display: none" tabindex="-1" aria-label="{% trans 'New topic form' as tmsg %} {{tmsg|force_escape}}">
47+
</article>
5548
<div class="forum-content"></div>
5649
</main>
57-
<aside class="forum-nav" role="complementary" aria-label="${_("Discussion thread list")}">
58-
<%include file="_filter_dropdown.html" />
50+
<aside class="forum-nav" role="complementary" aria-label="{% trans 'Discussion thread list' as tmsg %} {{tmsg|force_escape}}">
51+
{% include "discussion/_filter_dropdown.html" %}
5952
<div class="discussion-thread-list-container"></div>
6053
</aside>
6154
</div>
6255
</div>
6356
</section>
6457

65-
<%include file="_underscore_templates.html" />
66-
<%include file="_thread_list_template.html" />
67-
68-
<%static:require_module_async module_name="js/commerce/track_ecommerce_events" class_name="TrackECommerceEvents">
58+
{% include "discussion/_underscore_templates.html" %}
59+
{% include "discussion/_thread_list_template.html" %}
6960

70-
var fbeLink = $("#FBE_banner");
61+
<script>
62+
require(["js/commerce/track_ecommerce_events"], function (TrackECommerceEvents) {
7163

72-
TrackECommerceEvents.trackUpsellClick(fbeLink, 'discussion_audit_access_expires', {
73-
pageName: "discussion_tab",
74-
linkType: "link",
75-
linkCategory: "FBE_banner"
76-
});
64+
var fbeLink = $("#FBE_banner");
7765

78-
</%static:require_module_async>
66+
TrackECommerceEvents.trackUpsellClick(
67+
fbeLink,
68+
'discussion_audit_access_expires',
69+
{
70+
pageName: "discussion_tab",
71+
linkType: "link",
72+
linkCategory: "FBE_banner"
73+
}
74+
);
75+
});
76+
</script>

lms/djangoapps/discussion/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ def _create_discussion_board_context(request, base_context, thread=None):
560560
'is_commentable_divided': is_commentable_divided(course_key, discussion_id, course_discussion_settings),
561561
# If the default topic id is None the front-end code will look for a topic that contains "General"
562562
'discussion_default_topic_id': _get_discussion_default_topic_id(course),
563-
'enable_daily_digest': is_forum_daily_digest_enabled()
563+
'enable_daily_digest': is_forum_daily_digest_enabled(),
564+
'PLATFORM_NAME': settings.PLATFORM_NAME
564565
})
565566
context.update(
566567
get_experiment_user_metadata_context(

lms/templates/discussion/_discussion_inline.html

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1-
<%page expression_filter="h"/>
1+
{% load i18n %}
22

3-
<%include file="_underscore_templates.html" />
4-
<%include file="_thread_list_template.html" />
3+
{% include "discussion/_underscore_templates.html" %}
4+
{% include "discussion/_thread_list_template.html" %}
55

6-
<%!
7-
from django.utils.translation import gettext as _
8-
from json import dumps as json_dumps
9-
from openedx.core.djangolib.js_utils import js_escaped_string
10-
%>
11-
12-
<div class="discussion-module" data-discussion-id="${discussion_id}"
13-
data-user-create-comment="${json_dumps(can_create_comment)}"
14-
data-user-create-subcomment="${json_dumps(can_create_subcomment)}"
15-
data-read-only="${'false' if can_create_thread else 'true'}">
16-
% if not user.is_authenticated:
6+
<div class="discussion-module" data-discussion-id="{{ discussion_id }}"
7+
data-user-create-comment="{{ can_create_comment|yesno:"true,false" }}"
8+
data-user-create-subcomment="{{ can_create_subcomment|yesno:"true,false" }}"
9+
data-read-only="{% if can_create_thread %}false{% else %}true{% endif %}">
10+
{% if not user.is_authenticated %}
1711
<div class="page-banner">
1812
<div class="alert alert-warning" role="alert">
19-
<span class="icon icon-alert fa fa fa-warning" aria-hidden="true"></span>
20-
<div class="message-content">${login_msg}</div>
13+
<span class="icon icon-alert fa fa-warning" aria-hidden="true"></span>
14+
<div class="message-content">{{ login_msg }}</div>
2115
</div>
2216
</div>
2317
<br>
24-
% endif
18+
{% endif %}
19+
2520
<div class="discussion-module-header">
26-
<h3 class="hd hd-3 discussion-module-title">${_(display_name)}</h3>
27-
<div class="inline-discussion-topic"><span class="inline-discussion-topic-title">${_("Topic:")}</span> ${discussion_category}
28-
% if discussion_target:
29-
/ ${discussion_target}
30-
%endif
31-
</div>
21+
<h3 class="hd hd-3 discussion-module-title">
22+
{% filter force_escape %}
23+
{% blocktrans with title=display_name %}{{ title }}{% endblocktrans %}
24+
{% endfilter %}
25+
</h3>
26+
<div class="inline-discussion-topic">
27+
<span class="inline-discussion-topic-title">{% trans "Topic:" as tmsg %} {{tmsg|force_escape}}</span>
28+
{{ discussion_category }}
29+
{% if discussion_target %}
30+
/ {{ discussion_target }}
31+
{% endif %}
32+
</div>
3233
</div>
3334
<button class="discussion-show btn"
34-
data-discussion-id="${discussion_id}"
35-
${"disabled=disabled" if not user.is_authenticated else ""}>
36-
<span class="button-text">${_("Show Discussion")}</span>
35+
data-discussion-id="{{ discussion_id }}"
36+
{% if not user.is_authenticated %}disabled="disabled"{% endif %}>
37+
<span class="button-text">{% trans "Show Discussion" as tmsg %} {{tmsg|force_escape}}</span>
3738
</button>
3839

3940
</div>
4041
<script type="text/javascript">
41-
var $$course_id = "${course_id | n, js_escaped_string}";
42+
var $$course_id = "{{ course_id|escapejs }}";
4243

4344
function DiscussionInlineBlock(runtime, element) {
4445
'use strict';
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
<%! from django.utils.translation import gettext as _ %>
2-
<%page expression_filter="h"/>
1+
{% load i18n %}
32

4-
<div class="discussion-module" data-discussion-id="${discussion_id}">
3+
<div class="discussion-module" data-discussion-id="{{ discussion_id }}">
54
<p>
65
<span class="discussion-preview">
7-
<span class="icon fa fa-comment"/>
8-
${_("To view live discussions, click Preview or View Live in Unit Settings.")}<br />
9-
${_("Discussion ID: {discussion_id}").format(discussion_id=discussion_id)}
10-
% if not is_visible:
11-
<br /><b>${_('The discussion block is disabled for this course as it is not using a compatible discussion provider.')}</b>
12-
% endif
6+
<span class="icon fa fa-comment"></span>
7+
{% trans "To view live discussions, click Preview or View Live in Unit Settings." as tmsg %} {{tmsg|force_escape}}<br />
8+
{% filter force_escape %}
9+
{% blocktrans with discussion_id=discussion_id %}Discussion ID: {{ discussion_id }}{% endblocktrans %}
10+
{% endfilter %}
11+
{% if not is_visible %}
12+
<br /><b>{% trans "The discussion block is disabled for this course as it is not using a compatible discussion provider." as tmsg %} {{tmsg|force_escape}}</b>
13+
{% endif %}
1314
</span>
1415
</p>
1516
</div>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<li class="forum-nav-browse-menu-item"
2+
id='{{ category_name|force_escape }}'
3+
role="option">
4+
<span class="forum-nav-browse-title">{{ category_name|force_escape }}</span>
5+
<ul class="forum-nav-browse-submenu" role="presentation">
6+
{% if topic_list %}
7+
{% with new_topic_list=topic_list|add:", "|add:category_name %}
8+
{% include "discussion/_dropdown_items.html" with category_map=category topic_list=new_topic_list %}
9+
{% endwith %}
10+
{% endif %}
11+
</ul>
12+
</li>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<li class="forum-nav-browse-menu-item"
2+
data-discussion-id='{{ entry.id|force_escape }}'
3+
id='{{ entry.id|force_escape }}'
4+
data-divided="{{ entry.is_divided|yesno:'true,false' }}"
5+
role="option">
6+
{% if entry_name %}
7+
<span class="forum-nav-browse-title">
8+
{% if topic_list %}
9+
<span class="sr">{{ topic_list|force_escape }}, </span>
10+
{% endif %}
11+
{{ entry_name|force_escape }}
12+
</span>
13+
{% endif %}
14+
</li>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% for child, c_type in map.children %}
2+
{% if c_type == "entry" %}
3+
{% for entry_key, entry_value in map.entries.items %}
4+
{% if entry_key == child %}
5+
{% include "discussion/_dropdown_entry.html" with entry_name=child entry=entry_value topic_list=topic_list %}
6+
{% endif %}
7+
{% endfor %}
8+
{% else %}
9+
{% for category_key, category_value in map.subcategories.items %}
10+
{% if category_key == child %}
11+
{% include "discussion/_dropdown_category.html" with category_name=child category=category_value topic_list=topic_list %}
12+
{% endif %}
13+
{% endfor %}
14+
{% endif %}
15+
{% endfor %}

0 commit comments

Comments
 (0)