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

Add view partials for repeated content on post or topic writer #221

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

carofun
Copy link
Contributor

@carofun carofun commented Feb 9, 2021

Purpose

If we want to override the way the last writer of a post or a topic is shown, the project forces us to repeat
our modification in multiple templates. We would like to make it easier.

Proposal

We introduce a partial that deals with the different usages.
It can be call with different options to take in charge the different cases of the project :

  • username_highlighted : if username has to be highlighted
  • creation_date : if we show the creation date
  • no_trans : if True we won't display the translation text By:

To ease the overload, we've added a block when we could. Two files only call once the new partial so we introduce a block written_by around it.
This way if we just want to change this in the template, we won't have to overload the whole file, just this block.
It's probably a case that will happen. For instance, in our personal use case, we will need to send an extra parameter to the partial.

@carofun carofun force-pushed the custom/partial_written_by branch 3 times, most recently from b06f81a to 60e0cb1 Compare February 9, 2021 13:19
Copy link
Owner

@ellmetha ellmetha left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this, I think it's fair to have a partial for this use case! Though this PR might need more a bit work in order to be merged (see my comments below).

By: {{ poster_username }}
{% endblocktrans %}
{% endif %}
{% include "partials/posted_by.html" with poster=node.last_post.poster username=poster_username %}
Copy link
Owner

Choose a reason for hiding this comment

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

{{ poster_username }} doesn't exist in the context. Shouldn't it be {{ node.last_post.username }} instead? Otherwise a blank string will be displayed for anonymous users. This question also applies to all the where the posted_by.html partial was inserted. 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for you feedback, and sorry about that, it is now fixed !

Comment on lines 3 to 10
{% if display_label_by == None or display_label_by %}
{% trans "By:" %}
{% endif %}
{% if poster %}
{% include "partials/profile_link.html" %}
{% else %}
{% if username_highlighted %}<b>{% endif %}{{ username }}{% if username_highlighted %}</b>{%endif%}
{% endif %}
Copy link
Owner

Choose a reason for hiding this comment

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

This block is not i18n-friendly: the "By:" string is inserted as a prefix whereas in the previous version of the code this was fully handled as a proper translation with variable substitutions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for you feedback, I rewrote the partial and didn't cut any translation.

Comment on lines 12 to 33
{% if creation_date %}
{% blocktrans trimmed%}
on {{ creation_date }}
{% endblocktrans %}
{% endif %}
Copy link
Owner

Choose a reason for hiding this comment

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

Same comment!

@carofun carofun force-pushed the custom/partial_written_by branch 4 times, most recently from afcae03 to 006824a Compare March 5, 2021 15:27
If we want to override the way the last writer of a post or a topic is shown,
the project forces us to repeat our modification in multiple templates. We
introduce a partials to simplify the overload.
@carofun carofun requested a review from ellmetha March 5, 2021 15:59
By: {{ poster_username }} on {{ creation_date }}
{% endblocktrans %}
{% endif %}
{% include "partials/posted_by.html" with poster=topic.poster creation_date=topic.created username=topic.first_post.username %}
Copy link
Owner

Choose a reason for hiding this comment

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

This will result in a new SQL query for each topic because the topic.first_post is done systematically now.

@@ -0,0 +1,33 @@
{% load forum_member_tags %}
Copy link
Owner

Choose a reason for hiding this comment

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

I think we could probably improve how this partial works so that it - kindof - keep working like things were working previously.

For example why not:

  • have a partial that either works from a single post variable or topic variable (depending on whether you want to display the poster info for the post or the topic itself) - this would allow to avoid passing poster, username and creation_date systematically in each partial use (which would be less error prone in my opinion)
  • then in order to control whether the creation date has to be displayed, you could use an extra show_creation_date variable (True / False)
  • no_trans would be replaced by something more explicit like show_by_prefix
  • username_highlighted would be replaced by highlight_username (True / False) to improve consistency

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, definitely, I think it's way simpler this way. I'll work around it and suggest it as a fix.
thank you for you feedback

Copy link
Contributor Author

@carofun carofun Mar 19, 2021

Choose a reason for hiding this comment

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

I've commited with a fixup, I had forgotten to add a file with the search in the previous commit which was another case.
I don't know what to think of this new version. I had to make more cases to be able to read username as it is not specified anymore.
Tell me please what you think of it.

@carofun carofun requested a review from ellmetha March 19, 2021 15:27
Comment on lines +4 to +15
{% if is_search %}
{% if item.poster %}
{% url 'forum_member:profile' item.poster as poster_url %}
{% blocktrans trimmed with username=item.poster_name creation_date=item.created %}
By: <a href="{{ poster_url }}">{{ username }}</a> on {{ creation_date }}
{% endblocktrans %}
{% else %}
{% blocktrans trimmed with poster_username=item.poster_name creation_date=item.created %}
By: {{ poster_username }} on {{ creation_date }}
{% endblocktrans %}
{% endif %}
{% else %}
Copy link
Owner

Choose a reason for hiding this comment

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

The fact that we need to pass is_search=True in order to render this partial templates makes me think that it's probably equivalent to simply let the initial search template untouched. Otherwise would it be possible to make this partial template generic enough so that we don't have to use this is_search variable? If the partial is generic enough, it shouldn't make any assumption regarding the context in which it is used. 🤷‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi I had a second look at it, IMO, we should pass through as we did before (before the fixup), the username, the creation date and poster now that we have 3 cases : topics, posts and search, this way the partial will be generic.
Before I go for it, I prefer to have your feedback.
Thank you

Copy link
Owner

Choose a reason for hiding this comment

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

We can try! Though I think the resulting partial should be as simple as possible and not contain too much logic.

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.

None yet

2 participants