You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.9 KiB
81 lines
1.9 KiB
""" views package |
|
|
|
some view helpers are defined here |
|
""" |
|
|
|
import re |
|
from collections import UserDict |
|
|
|
RE_SIMPLE_URL = re.compile("((?:www|http)\\S+)") |
|
|
|
|
|
def get_offset(request): |
|
offset_param = request.GET.get("o", 0) |
|
try: |
|
return int(offset_param) |
|
except ValueError: |
|
return 0 |
|
|
|
|
|
class DefaultQueryParams(UserDict): |
|
def __call__(self, **kwargs): |
|
copied = dict(**self) |
|
copied.update(kwargs) |
|
return {k: v for k, v in copied.items() if v is not None} |
|
|
|
|
|
def jinja_date(some_date): |
|
return some_date.strftime("%Y-%m-%d") |
|
|
|
|
|
def jinja_time(some_date): |
|
return some_date.strftime("%H:%I") |
|
|
|
|
|
def jinja_datetime(some_date): |
|
return some_date.strftime("%Y-%m-%d %H:%I") |
|
|
|
|
|
def _as_html_link(url, css_class): |
|
return ( |
|
f'<a href="{url}" class="{css_class} d-inline-block text-truncate">' |
|
f"{url}</a>" |
|
) |
|
|
|
|
|
def jinja_extract_links(text, css_class=""): |
|
links = RE_SIMPLE_URL.findall(text) |
|
return [_as_html_link(url, css_class) for url in links] |
|
|
|
|
|
def jinja_view_comment(text, css_class=""): |
|
links = RE_SIMPLE_URL.findall(text) |
|
for url in links: |
|
html = _as_html_link(url, css_class) |
|
text = text.replace(url, html) |
|
return text |
|
|
|
|
|
def jinja_nl2br(text, replacement="<br>"): |
|
return replacement.join(text.splitlines()) |
|
|
|
|
|
def includeme(config): |
|
""" adding request helpers |
|
|
|
Activate this setup using ``config.include('ordr3.views')``. |
|
""" |
|
import pyramid_jinja2.filters |
|
|
|
config.include("pyramid_jinja2") |
|
config.commit() |
|
|
|
j2_env = config.get_jinja2_environment() |
|
j2_env.filters["resource_url"] = pyramid_jinja2.filters.resource_url_filter |
|
|
|
j2_env.filters["as_date"] = jinja_date |
|
j2_env.filters["as_time"] = jinja_time |
|
j2_env.filters["as_datetime"] = jinja_datetime |
|
j2_env.filters["view_comment"] = jinja_view_comment |
|
j2_env.filters["extract_links"] = jinja_extract_links |
|
j2_env.filters["nl2br"] = jinja_nl2br
|
|
|