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) @app.errorhandler(CSRFError)
def handle_csrf_error(e): 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 @app.before_request
def update_last_seen(): def update_last_seen():
@@ -68,6 +68,13 @@ def create_app():
db.session.add(current_user) db.session.add(current_user)
db.session.commit() db.session.commit()
@app.template_filter('pluralize')
def pluralize(number, singular='', plural='s'):
if number == 1:
return singular
else:
return plural
@app.context_processor @app.context_processor
def inject(): def inject():
return dict(now=datetime.utcnow) return dict(now=datetime.utcnow)

View File

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