From d52fb4c7e46307c2387b645b0aed65e65edb28ea Mon Sep 17 00:00:00 2001 From: Deimos Date: Tue, 12 Nov 2019 20:53:55 -0700 Subject: [PATCH] Simplify custom Jinja filters/tests --- tildes/tildes/jinja.py | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/tildes/tildes/jinja.py b/tildes/tildes/jinja.py index 2065dcf..42f68ec 100644 --- a/tildes/tildes/jinja.py +++ b/tildes/tildes/jinja.py @@ -3,7 +3,6 @@ """Contains configuration, functions, etc. for the Jinja template system.""" -from typing import Any from urllib.parse import quote_plus from pyramid.config import Configurator @@ -18,26 +17,6 @@ from tildes.models.group import Group from tildes.models.topic import Topic -def is_comment(item: Any) -> bool: - """Return whether the item is a Comment.""" - return isinstance(item, Comment) - - -def is_group(item: Any) -> bool: - """Return whether the item is a Group.""" - return isinstance(item, Group) - - -def is_topic(item: Any) -> bool: - """Return whether the item is a Topic.""" - return isinstance(item, Topic) - - -def do_quote_plus(string: str) -> str: - """Escape the string using the urllib quote_plus function.""" - return quote_plus(string) - - def includeme(config: Configurator) -> None: """Configure Jinja2 template renderer.""" settings = config.get_settings() @@ -49,15 +28,15 @@ def includeme(config: Configurator) -> None: settings["jinja2.filters"] = { "adaptive_date": adaptive_date, "ago": descriptive_timedelta, - "quote_plus": do_quote_plus, + "quote_plus": quote_plus, "vague_timedelta_description": vague_timedelta_description, } # add custom jinja tests settings["jinja2.tests"] = { - "comment": is_comment, - "group": is_group, - "topic": is_topic, + "comment": lambda obj: isinstance(obj, Comment), + "group": lambda obj: isinstance(obj, Group), + "topic": lambda obj: isinstance(obj, Topic), } config.include("pyramid_jinja2")