Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move mako logic to init method #98

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions eox_nelp/course_experience/frontend/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
Classes:
CourseExperienceFrontendTestCase: Test CourseExperienceFrontendView template.
"""
from django.apps import apps
from django.http import HttpResponse
from django.shortcuts import render
from django.test import TestCase
from django.urls import reverse
from mock import patch
from rest_framework.test import APIClient


Expand All @@ -21,24 +17,7 @@ def setUp(self): # pylint: disable=invalid-name
self.client = APIClient()
self.url_endpoint = reverse("course-experience-frontend:feedback-courses")

@patch("eox_nelp.templates_config.edxmako")
def test_edxmako_render_correct_call(self, edxmako_mock):
""" Test edxmako functions from edxapp_wrapper are called with the right values.
Expected behavior:
- `edxmako_mock.paths.add_lookup` is called with course_experience_template_path.
(The path of course xp templates is used)
- The get request to the url_endpoint is using the template `feedback_courses.html`.
"""
course_experience_template_path = apps.get_app_config('eox_nelp').path + "/course_experience/frontend/templates"
edxmako_mock.shortcuts.render_to_response.return_value = HttpResponse(content='Template mock')

self.client.get(self.url_endpoint)

edxmako_mock.paths.add_lookup.assert_called_with('main', course_experience_template_path)
edxmako_mock.shortcuts.render_to_response.assert_called_with("feedback_courses.html", {}, 'main', None)

@patch("eox_nelp.course_experience.frontend.views.render_to_response")
def test_feedback_course_template_behaviour(self, render_to_response_mock):
def test_feedback_course_template_behaviour(self):
""" The correct rendering of the feedback courses template using the url_endpoint
for frontend feedback courses.
Expected behavior:
Expand All @@ -48,8 +27,6 @@ def test_feedback_course_template_behaviour(self, render_to_response_mock):
- Response has the correct path to load styles with feedback carrousel css.
- Response has the correct path to load script with feedback carrousel js.
"""
render_to_response_mock.return_value = render(None, "feedback_courses.html")

response = self.client.get(self.url_endpoint)

self.assertContains(response, '<title>Feedback courses general</title>', status_code=200)
Expand Down
4 changes: 2 additions & 2 deletions eox_nelp/course_experience/frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""
from django.views import View

from eox_nelp.templates_config import render_to_response
from eox_nelp.edxapp_wrapper.edxmako import edxmako


class FeedbackCoursesTemplate(View):
Expand All @@ -19,4 +19,4 @@ class FeedbackCoursesTemplate(View):

def get(self, request): # pylint: disable=unused-argument
"""Render start html"""
return render_to_response("feedback_courses.html", {})
return edxmako.shortcuts.render_to_response("feedback_courses.html", {}, "main", request)
24 changes: 24 additions & 0 deletions eox_nelp/init_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
patch_user_gender_choices: Change the current openedx gender options (Male, Female, Other)

"""
import os

from django.utils.translation import gettext_noop


Expand All @@ -15,6 +17,7 @@ def run_init_pipeline():
Executes multiple processes that must run before starting the django application.
"""
patch_user_gender_choices()
set_mako_templates()


def patch_user_gender_choices():
Expand All @@ -30,3 +33,24 @@ def patch_user_gender_choices():
('m', gettext_noop('Male')),
('f', gettext_noop('Female')),
)


def set_mako_templates():
"""This method adds the plugin templates to mako configuration."""
# pylint: disable=import-error, import-outside-toplevel
# This cannot be at the top of the file since this file is imported the plugin initialization
# and therefore the settings has not been set yet
from eox_nelp.course_experience.frontend import templates as course_experience_templates
from eox_nelp.edxapp_wrapper.edxmako import edxmako
from eox_nelp.stats import templates as stats_templates

templates_modules_to_include = [
stats_templates,
course_experience_templates,
]

for module in templates_modules_to_include:
path_to_templates = os.path.dirname(module.__file__)

if path_to_templates not in edxmako.LOOKUP['main'].directories:
edxmako.paths.add_lookup('main', path_to_templates)
4 changes: 2 additions & 2 deletions eox_nelp/stats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
classes:
get_tenant_stats: function based view.
"""
from eox_nelp.templates_config import render_to_response
from eox_nelp.edxapp_wrapper.edxmako import edxmako

STATS_QUERY_PARAMS = [
"show_courses",
Expand Down Expand Up @@ -47,4 +47,4 @@ def get_tenant_stats(request):
context = {query_param: "true" for query_param in STATS_QUERY_PARAMS}
context.update(request.GET.dict())

return render_to_response("tenant_stats.html", context)
return edxmako.shortcuts.render_to_response("tenant_stats.html", context, "main", request)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is need the arg "main" to this render_to_response method,

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I think this is related to that main dirs or something to do it with that way.
https://github.com/eduNEXT/eox-nelp/pull/98/files#diff-ec9dfd84694298d271197a0909b7b62e0301e9df3dce5c29d0cec0a8255ebd42L19

if path_to_templates not in edxmako.LOOKUP['main'].directories:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works without those extra parameters(main and request), however when I was testing that raised an missing parameters error so I decided to pass the default value for the namespace as we do in the current implementation and the current request

21 changes: 0 additions & 21 deletions eox_nelp/templates_config.py

This file was deleted.

51 changes: 51 additions & 0 deletions eox_nelp/tests/test_init_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""This file contains all the test for the init_pipeline.py file.

Classes:
RunInitPipelineTestCase: Tests cases for run_init_pipeline method.
SetMakoTemplatesTestCase: Tests cases for set_mako_templates method.
"""
import os
from unittest.mock import call

from django.test import TestCase
from mock import patch

from eox_nelp.course_experience.frontend import templates as course_experience_templates
from eox_nelp.edxapp_wrapper.edxmako import edxmako
from eox_nelp.init_pipeline import run_init_pipeline, set_mako_templates
from eox_nelp.stats import templates as stats_templates


class RunInitPipelineTestCase(TestCase):
"""Test class for run_init_pipeline method."""

@patch("eox_nelp.init_pipeline.patch_user_gender_choices")
@patch("eox_nelp.init_pipeline.set_mako_templates")
def test_pipeline_execute_expected_methods(self, set_mako_templates_mock, patch_user_gender_choices_mock):
""" Test that method calls the expected methods during the pipeline execution.

Expected behavior:
- set_mako_templates_mock is called once.
- patch_user_gender_choices_mock is called once.
"""
run_init_pipeline()

set_mako_templates_mock.assert_called_once()
patch_user_gender_choices_mock.assert_called_once()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok this add gender choices called test xD



class SetMakoTemplatesTestCase(TestCase):
"""Test class for set_mako_templates method."""

def test_edxmako_adds_expected_paths(self):
""" Test that method adds the expected template paths.

Expected behavior:
- `edxmako.paths.add_lookup` is called with the expected paths.
"""
set_mako_templates()

edxmako.paths.add_lookup.assert_has_calls([
call('main', os.path.dirname(stats_templates.__file__)),
call('main', os.path.dirname(course_experience_templates.__file__)),
])
Loading