Add pluralizing macro filter to templating engine

- Only some plural edits are included here - some are locked behind commits coming soon™️
- Also made index template statistics variables read better
- Fixed spelling error in CSRF Error template rendering (hadn't hit the page yet)
This commit is contained in:
Xevion
2022-03-29 18:07:39 -05:00
parent 49547e582c
commit db560b7c41
2 changed files with 21 additions and 11 deletions

9
app.py
View File

@@ -58,7 +58,7 @@ def create_app():
@app.errorhandler(CSRFError)
def handle_csrf_error(e):
return render_template('errprs/csrf.html', reason=e.description), 400
return render_template('errors/csrf.html', reason=e.description), 400
@app.before_request
def update_last_seen():
@@ -68,6 +68,13 @@ def create_app():
db.session.add(current_user)
db.session.commit()
@app.template_filter('pluralize')
def pluralize(number, singular='', plural='s'):
if number == 1:
return singular
else:
return plural
@app.context_processor
def inject():
return dict(now=datetime.utcnow)

View File

@@ -16,19 +16,22 @@
</div>
<div class="statistics" style="margin: 1em;">
<h2>runnerspace Statistics</h2>
{% with comments = stats['total_comments'], posts = stats['total_posts'], users = stats['total_users'] %}
<div>
<div>
<ul>
<li>
<strong>{{ stats['total_comments'] }}</strong> comments across <strong>{{ stats['total_posts'] }}</strong> posts<br>
<strong>{{ comments }}</strong> comment across <strong>{{ posts }}</strong>
post{{ posts|pluralize }}<br>
</li>
<li>
<strong>{{ stats['total_users'] }}</strong> users
<strong>{{ users }}</strong> user{{ users|pluralize }}
</li>
</ul>
</div>
</div>
{% endwith %}
</div>
</div>
{% endblock %}