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

Explore using Jinja for templating #376

Merged
merged 40 commits into from
Aug 24, 2021
Merged

Explore using Jinja for templating #376

merged 40 commits into from
Aug 24, 2021

Conversation

H-Shay
Copy link
Contributor

@H-Shay H-Shay commented Aug 9, 2021

This PR implements Jinja as a templating engine for emails in Sydent.

Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

Definitely on the right track. Don't think we will need too much back and forth on this.

I had in mind that we were going to keep support for old-school templates; if someone else has said we're not, then let me know :).

res/matrix-org/invite_template.eml Outdated Show resolved Hide resolved
res/matrix-org/invite_template.eml Outdated Show resolved Hide resolved
sydent/util/emailutils.py Show resolved Hide resolved
changelog.d/376.misc Outdated Show resolved Hide resolved
setup.py Outdated Show resolved Hide resolved
@clokep clokep requested a review from reivilibre August 11, 2021 16:58
Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

very close to ready to roll, I think :)

matrix_is_test/res/is-test/verification_template.eml Outdated Show resolved Hide resolved
res/matrix-org/invite_template.eml Outdated Show resolved Hide resolved
res/matrix-org/invite_template.eml.j2 Outdated Show resolved Hide resolved
res/matrix-org/invite_template.eml.j2 Outdated Show resolved Hide resolved
sydent/util/emailutils.py Outdated Show resolved Hide resolved
res/matrix-org/invite_template.eml Outdated Show resolved Hide resolved
changelog.d/376.feature Outdated Show resolved Hide resolved
sydent/terms/terms.py Outdated Show resolved Hide resolved
sydent/util/emailutils.py Outdated Show resolved Hide resolved
@H-Shay H-Shay requested a review from reivilibre August 11, 2021 17:47
@H-Shay
Copy link
Contributor Author

H-Shay commented Aug 12, 2021

Okay so we've got some tests, jinja is now using an environment and autoescaping is enabled-turns out we were already manually cleaning the substitutions for html/url, so I left that in the code. Jinja doesn't seem to be double escaping anything so I think we are okay? Happy to rework that if this solution isn't amenable. Let me know!

@H-Shay H-Shay requested a review from clokep August 12, 2021 17:06
sydent/util/emailutils.py Outdated Show resolved Hide resolved
tests/test_templates.py Outdated Show resolved Hide resolved
tests/test_templates.py Outdated Show resolved Hide resolved
@H-Shay H-Shay requested a review from reivilibre August 16, 2021 18:34
Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

I think the tests are likely to need a little bit of change once you make some of these changes.

I wonder if it might be sensible to, in one of the tests:

  • patch out the path to the templates (so you can load a test-only template)
  • have a little test template, testing a variety of things
  • check the entire output to see that it's what we wanted.

You might be able to inject templates straight into the Jinja environment, that might be a good way to go about it instead.

res/matrix-org/invite_template.eml.j2 Outdated Show resolved Hide resolved
res/matrix-org/verification_template.eml.j2 Outdated Show resolved Hide resolved
res/vector-im/invite_template.eml.j2 Outdated Show resolved Hide resolved
sydent/sydent.py Outdated Show resolved Hide resolved
sydent/util/emailutils.py Outdated Show resolved Hide resolved
@H-Shay H-Shay requested a review from reivilibre August 18, 2021 17:47
Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

this is coming along :)

res/matrix-org/verification_template.eml.j2 Outdated Show resolved Hide resolved
res/matrix-org/verification_template.eml.j2 Outdated Show resolved Hide resolved
res/matrix-org/verification_template.eml.j2 Outdated Show resolved Hide resolved
res/vector-im/verification_template.eml.j2 Outdated Show resolved Hide resolved
res/vector-im/verification_template.eml.j2 Outdated Show resolved Hide resolved
res/vector_verification_sample.txt Outdated Show resolved Hide resolved
tests/test_jinja_templates.py Show resolved Hide resolved
tests/test_jinja_templates.py Outdated Show resolved Hide resolved
tests/test_jinja_templates.py Outdated Show resolved Hide resolved
@H-Shay H-Shay requested a review from reivilibre August 19, 2021 18:08
Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

looking great, let's get another pair of eyes on this

tests/test_jinja_templates.py Show resolved Hide resolved
Copy link
Member

@clokep clokep left a comment

Choose a reason for hiding this comment

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

Looks good overall, but some of the code is a bit confusing still!


<p>
<a
href="https://app.element.io/#/room/{{ room_id|urlencode }}?email={{ to|urlencode }}&signurl=https%3A%2F%2Fmatrix.org%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3D{{ token|urlencode }}%26private_key%3D{{ ephemeral_private_key|urlencode }}&room_name={{ room_name|urlencode }}&room_avatar_url={{ room_avatar_url|urlencode }}&inviter_name={{ sender_display_name|urlencode }}&guest_access_token={{ guest_access_token|urlencode }}&guest_user_id={{ guest_user_id|urlencode }}">Join the conversation.</a>
Copy link
Member

Choose a reason for hiding this comment

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

Huh, should this be using {{ web_client_location }} here too? (Probably copied from the non-Jinja version though.) We might want to fix that separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is indeed from the older version, I will fix in separate PR.

res/matrix-org/verification_template.eml.j2 Outdated Show resolved Hide resolved
res/vector-im/invite_template.eml Outdated Show resolved Hide resolved

<p>
<a
href="https://app.element.io/#/room/{{ room_id|urlencode }}?email={{ to|urlencode }}&signurl=https%3A%2F%2Fvector.im%2F_matrix%2Fidentity%2Fapi%2Fv1%2Fsign-ed25519%3Ftoken%3D{{ token|urlencode }}%26private_key%3D{{ ephemeral_private_key|urlencode }}&room_name={{ room_name|urlencode }}&room_avatar_url={{ room_avatar_url|urlencode }}&inviter_name={{ sender_display_name|urlencode }}&guest_access_token={{ guest_access_token|urlencode }}&guest_user_id={{ guest_user_id|urlencode }}">Join the conversation.</a>
Copy link
Member

Choose a reason for hiding this comment

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

Same thing about web_client_location.

sydent/util/emailutils.py Outdated Show resolved Hide resolved
sydent/util/emailutils.py Outdated Show resolved Hide resolved
tests/test_jinja_templates.py Show resolved Hide resolved
sydent/util/emailutils.py Outdated Show resolved Hide resolved
@H-Shay H-Shay requested a review from clokep August 23, 2021 16:50
Copy link
Member

@clokep clokep left a comment

Choose a reason for hiding this comment

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

I think this looks good with the one proposed change! It would be nice to have some upgrade notes for this too, although I'm not sure if we have a normal place for that in sydent or if we just put them in the release notes.

Comment on lines 76 to 83
with open(templateFile) as template_file:
for k, v in substitutions.items():
allSubstitutions[k + "_forhtml"] = escape(v)
allSubstitutions[k + "_forurl"] = urllib.parse.quote(v)
allSubstitutions["multipart_boundary"] = generateAlphanumericTokenOfLength(
32
)
mailString = template_file.read() % allSubstitutions
Copy link
Member

Choose a reason for hiding this comment

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

Since allSubstitutions is now only used in this branch, we can move it into here. It is also usually best to keep the file open for as short as possible! (I think I gave you the previous code, my bad!)

Suggested change
with open(templateFile) as template_file:
for k, v in substitutions.items():
allSubstitutions[k + "_forhtml"] = escape(v)
allSubstitutions[k + "_forurl"] = urllib.parse.quote(v)
allSubstitutions["multipart_boundary"] = generateAlphanumericTokenOfLength(
32
)
mailString = template_file.read() % allSubstitutions
allSubstitutions = {}
for k, v in substitutions.items():
allSubstitutions[k + "_forhtml"] = escape(v)
allSubstitutions[k + "_forurl"] = urllib.parse.quote(v)
allSubstitutions["multipart_boundary"] = generateAlphanumericTokenOfLength(
32
)
with open(templateFile) as template_file:
mailString = template_file.read() % allSubstitutions

Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

veerrry close now :)

sydent/sydent.py Show resolved Hide resolved
sydent/util/emailutils.py Outdated Show resolved Hide resolved
Copy link
Contributor

@reivilibre reivilibre left a comment

Choose a reason for hiding this comment

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

seems good to me

@reivilibre reivilibre merged commit 17ba907 into matrix-org:main Aug 24, 2021
clokep added a commit to t3chguy/sydent that referenced this pull request Aug 30, 2021
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.

3 participants