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

[Emails] Template context variables documentation links #2491

Merged
merged 19 commits into from
Aug 14, 2023

Conversation

pbanaszkiewicz
Copy link
Contributor

@pbanaszkiewicz pbanaszkiewicz commented Jul 26, 2023

This fixes #2482 by providing links to documentation for every variable used in email template context (e.g. person, event, award, etc.).

It builds on top of #2481.

WIP. Needs testing and perhaps some code improvements / refactoring.

@pbanaszkiewicz pbanaszkiewicz self-assigned this Jul 26, 2023
@pbanaszkiewicz pbanaszkiewicz force-pushed the feature/2482-template-context-variables branch from 717c754 to 5542caa Compare July 28, 2023 08:04
@pbanaszkiewicz pbanaszkiewicz force-pushed the feature/2482-template-context-variables branch from 5dd4ee7 to 449e668 Compare August 5, 2023 20:34
@pbanaszkiewicz pbanaszkiewicz marked this pull request as ready for review August 5, 2023 21:50
@pbanaszkiewicz pbanaszkiewicz requested a review from elichad August 5, 2023 22:02
@pbanaszkiewicz
Copy link
Contributor Author

@elichad ready for your review! Thanks.

@pbanaszkiewicz pbanaszkiewicz modified the milestones: v4.2, v4.3 Aug 6, 2023
Copy link
Contributor

@elichad elichad 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 looking good! I have just a couple of questions/suggestions before approving

<strong>Template variables</strong>
</div>
<div class="col-12 col-lg-10">
Visible after saving the template.
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, it would be nice to show the variables as soon as a signal is selected, but I know that would increase the complexity a lot (is it even possible for this case?)
I think we can manage with this and make it part of the workflow to save before entering the full body text, since we shouldn't be creating tons of templates.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's not impossible, but since I have a ton of other features to implement I didn't focus on this case.

One option I can see is to load the variables and their definitions to a JSON object, then switch display in this depending on what user is choosing in the dropdown.

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds like a nice idea – could you make an issue for it and we can come back to it later in the project?

<div class="col-12 col-lg-2">
<strong>Template variables</strong>
</div>
<div class="col-12 col-lg-10">
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add some help text either here or on the "Email body" field to make explicit that these are variables that can be referenced in the text box?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, is there any validation we can do to check that the referenced fields exist when these variables are used in the template? e.g. if I tried to use {{ person.not_a_field }}
Right now it just renders as XXX-unset-variable-XXX when an email is scheduled, which at least doesn't cause a server error, but it happens quite quietly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to update the wording so that it's clearer what they refer to.

About template validation: yes, I think I could build that in here. The previous system contained similar validation (for proper Django template format).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@elichad Actually the validation is not possible at this point.

We'd get the best results if we used Django Template Engine behavior for checking if all variables are accessible. However, to do that we have to have context with real data. At this point we're working with email templates without any context, so proper validation is not possible.

</div>
<div class="col-12 col-lg-10">
<ul>
{% for key, value in body_context_annotations.items %}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can these template variables still be used when editing a scheduled email? I had assumed the templates were rendered once and weren't re-rendered before sending.
If they can't be used, this row should be removed from here and scheduled_email_detail.html.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@elichad You're correct.

There are 2 renderings:

  1. raw user input --(django template enigne)-> Markdown format
  2. Markdown format --(markdown compiler)-> HTML

Currently first rendering occurs when an email is scheduled, and the second should be performed by the worker (at the moment it isn't - I will get back to this).

The advantage of this approach is simplicity, but the clear disadvantage is the final form of the email which can't be updated with newer versions of the objects included. So potentially we need to shift the rendering more towards the worker - it would have to render Django template and then Markdown.

I'll remove these lines, however they may be brought back in future.

Using mocks was necessary because with
metaprogramming Django changes
Model.__class__ to ModelBase and therefore
(only in these tests!) it wasn't possible to use
Person or other models.

A trick was to use MagicMock with `.__class__.__name__`
attribute overwritten.
Updated sections containing variables available in template body.
@pbanaszkiewicz pbanaszkiewicz force-pushed the feature/2482-template-context-variables branch from 3d019ad to cb98109 Compare August 11, 2023 21:49
@pbanaszkiewicz
Copy link
Contributor Author

@elichad Please take a look again and consider my responses to your questions. Thanks!

@pbanaszkiewicz pbanaszkiewicz requested a review from elichad August 13, 2023 09:36
Copy link
Contributor

@elichad elichad left a comment

Choose a reason for hiding this comment

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

LGTM now!

@pbanaszkiewicz pbanaszkiewicz merged commit 58657e4 into develop Aug 14, 2023
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.

[Emails] Display variables available in the context for every signal
2 participants