Skip to content

Allow theme template block overrides. - #16856

Merged
tuchfarber merged 1 commit into
openedx:masterfrom
open-craft:mtyaka/template-block-overrides
Dec 22, 2017
Merged

Allow theme template block overrides.#16856
tuchfarber merged 1 commit into
openedx:masterfrom
open-craft:mtyaka/template-block-overrides

Conversation

@mtyaka

@mtyaka mtyaka commented Dec 11, 2017

Copy link
Copy Markdown
Contributor

This allows an overridding template from a theme to inherit from the same corresponding standard template.

This is useful when you only want to override one or more named blocks, but otherwise make no modifications to the standard template.

This functionality was already implemented in https://github.com/edx/edx-platform/pull/15947, but then reverted in https://github.com/edx/edx-platform/pull/16729 after some issues were discovered.

This PR fixes the issue and adds tests for it.

Dependencies: None

Sandbox URL:

Deployment targets: edx.org and edge.edx.org

Merge deadline: None

Testing instructions:

  1. Create a test theme:
    • make a directory /edx/app/edxapp/themes/testtheme
    • create a template at /edx/app/edxapp/themes/testtheme/lms/templates/dashboard.html, and put this content into it:
<%inherit file="dashboard.html" />
<%block name="pagetitle">Overridden Title!</%block>
${parent.body()}
  1. After you restart the LMS, you should be able to see "Overriddden Title!" used as the page title on the student dashboard, if the patch is working correctly.

Author notes and concerns:
Note: Some templates are cached, so if you don't see the changes, you can try wiping the cache by doing rm -rf /tmp/mako_* and sudo service memcached restart.

@openedx-webhooks

Copy link
Copy Markdown

Thanks for the pull request, @mtyaka! It looks like you're a member of a company that does contract work for edX. If you're doing this work as part of a paid contract with edX, you should talk to edX about who will review this pull request. If this work is not part of a paid contract with edX, then you should ensure that there is an OSPR issue to track this work in JIRA, so that we don't lose track of your pull request.

Create an OSPR issue for this pull request.

@clemente clemente left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@mtyaka Thanks! Great work! And yes, this is more complex than the initial PR.

I tested this in many ways (I'll write the list later) and it works.
In the strange cases it fails with the same errors as master (e.g. maximum recursion depth exceeded). I don't think we need tests for that. These include: invalid templates, inheriting from the wrong one, …

My comments are mainly to try to make the code easier to understand and you can do any other changes that help with that.

It would be much better if everything could be done from get_template, including knowing where to find the file (whether in theme or in standard theme), but I suppose you tried and couldn''t find it and therefore did the „marker class“. Maybe it's useful to add why it can't be done from get_template.

I didn't run the tests yet but I see they passed.

I'll test this more when I use it in some theme adaptation, but I hope I won't find major changes.

Comment thread common/djangoapps/edxmako/paths.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

dasboard → dashboard

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks.

Comment thread common/djangoapps/edxmako/paths.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In comments there are words „inherit“, „call“, „import“, though I think they refer to the same and it's confusing; would it improve by e.g. „importing“ → „inheriting from“? Or any other simplification.
Maybe the comments can mention a single example? Only if it helps. If adding more text makes it more confusing, then not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This method is called when a mako template either <%inherit>s or <%include>s another template.
I can see why the comments are confusing, I'll try to make them clearer.

Comment thread common/djangoapps/edxmako/paths.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This part is also computed in line 109. Although now the flow is clear, I think the repeated part (and the comment in 108) can be computed earlier into a variable, to avoid duplication.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea - I put the repeated parts into a separate private method.

Comment thread common/djangoapps/edxmako/paths.py Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

At first I didn't understand self-inheritance; maybe it helps to mention that it's the same as before. 1-word fix: just saying „When this self-inheritance […]“ already links it to the description before.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why is HTML(…) needed, when we already have "h"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

HTML is the reverse of h. <%page expression_filter="h"/> at the top of the template means all interpolated strings are HTML escaped by default, so you have to use HTML to explicitly disable that behavior.

EdX quality scripts enforce that all mako templates have <%page expression_filter="h"/> enabled, which is why I used it here even though it's technically unnecessary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you should also self.assertContains(resp, "Explore courses") or similar, to check that we're getting the full dashboard with our modifications on top, not just a blank template with the content of the overridden blocks.
Same in the other „dashboard“ test. Or this could be in its own test.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the test_parent_content_in_self_inherited_template test covers this case. They are testing the same theme with one test testing the overwritten fields and one testing the non overwritten fields.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, the test_parent_content_in_self_inherited_template covers this case, but this test did not yet exist when @clemente made that comment, I added it later.

@mtyaka
mtyaka force-pushed the mtyaka/template-block-overrides branch from 31e4917 to a70a475 Compare December 14, 2017 10:12

@mtyaka mtyaka left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review @clemente!

It would be much better if everything could be done from get_template, including knowing where to find the file (whether in theme or in standard theme), but I suppose you tried and couldn''t find it and therefore did the „marker class“.

We cannot do it from get_template because we can't get access to the calling template uri in that method. Make does not pass the calling uri when calling get_template on the template loader.

Comment thread common/djangoapps/edxmako/paths.py Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks.

Comment thread common/djangoapps/edxmako/paths.py Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread common/djangoapps/edxmako/paths.py Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This method is called when a mako template either <%inherit>s or <%include>s another template.
I can see why the comments are confusing, I'll try to make them clearer.

Comment thread common/djangoapps/edxmako/paths.py Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea - I put the repeated parts into a separate private method.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

HTML is the reverse of h. <%page expression_filter="h"/> at the top of the template means all interpolated strings are HTML escaped by default, so you have to use HTML to explicitly disable that behavior.

EdX quality scripts enforce that all mako templates have <%page expression_filter="h"/> enabled, which is why I used it here even though it's technically unnecessary.

@clemente clemente left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍

  • I tested this:
    • expectedly fails when defining two times the same block
    • expectedly fails when including non-existing file
    • expectedly fails when inheriting from two templates at the same time
    • when defining unexisting block, its content appears
    • I tried to copy/paste the original courses.html to a theme courses.html, still works
    • I tried an include inside a block
    • complex things and invalid values, like inheriting from a totally different template, fail the same as in the original code
    • tests run
    • I used this in one file of a client theme
  • I read through the code
  • I checked for accessibility issues: none
  • Includes documentation, as comments in the code and functions/classes

@mtyaka everything works, thanks

@mtyaka

mtyaka commented Dec 15, 2017

Copy link
Copy Markdown
Contributor Author

@bderusha @tuchfarber This reimplements ability to override specific named block overrides from a theme template without having to copy the entire template, which was first introduced in https://github.com/edx/edx-platform/pull/15947, but later reverted in https://github.com/edx/edx-platform/pull/16729 after some issues were discovered.

This PR fixes the issues and adds extra tests to cover some edge cases.

BTW, the OSPR form currently doesn't work - I get a 500 error when I try to create a new OSPR issues for this PR.

@tuchfarber

Copy link
Copy Markdown
Contributor

I've tested with the themes it previously broke on and them seem to be working now. Excellent work! Still need to go through the code though.

@tuchfarber tuchfarber left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the test_parent_content_in_self_inherited_template test covers this case. They are testing the same theme with one test testing the overwritten fields and one testing the non overwritten fields.

This allows an overridding template from a theme to inherit from the
same corresponding standard template.

This is useful when you only want to override one or more named blocks,
but otherwise make no modifications to the standard template.
@mtyaka

mtyaka commented Dec 22, 2017

Copy link
Copy Markdown
Contributor Author

Squash from a70a4755ed28c95898d29b3e18c7b50f7ee19319.

@mtyaka
mtyaka force-pushed the mtyaka/template-block-overrides branch from a70a475 to f6f29ca Compare December 22, 2017 08:42
@mtyaka

mtyaka commented Dec 22, 2017

Copy link
Copy Markdown
Contributor Author

Thanks @tuchfarber! I don't have permission to merge this, any chance you could do that after the build is green?

@edx-pipeline-bot

Copy link
Copy Markdown
Contributor

EdX Release Notice: This PR has been deployed to the staging environment in preparation for a release to production on Thursday, December 28, 2017.

@edx-pipeline-bot

Copy link
Copy Markdown
Contributor

EdX Release Notice: This PR has been deployed to the production environment.

clemente referenced this pull request in open-craft/edx-theme Jan 4, 2018
Rewritten most of the course_about.html template through blocks. This depends on the new template block inheritance system (merged at https://github.com/edx/edx-platform/pull/16856), and it requires the new blocks to be defined in edx-platform too.
@edx-pipeline-bot

Copy link
Copy Markdown
Contributor

EdX Release Notice: This PR has been rolled back from the production environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants