-
Notifications
You must be signed in to change notification settings - Fork 84
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
Conversation
Signed-off-by: H.Shay <shaysquared@gmail.com>
There was a problem hiding this 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 :).
There was a problem hiding this 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 :)
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! |
There was a problem hiding this 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.
There was a problem hiding this 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 :)
There was a problem hiding this 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
There was a problem hiding this 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> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
||
<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> |
There was a problem hiding this comment.
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
.
There was a problem hiding this 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.
sydent/util/emailutils.py
Outdated
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 |
There was a problem hiding this comment.
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!)
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
veerrry close now :)
There was a problem hiding this 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
This PR implements Jinja as a templating engine for emails in Sydent.