# -*- coding: utf-8 -*-
#
# This file is part of the jabber.at homepage (https://github.com/jabber-at/hp).
#
# This project is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this project. If
# not, see <http://www.gnu.org/licenses/>.
import logging
from django import template
from django.urls import reverse
from django.utils.html import format_html
from django.utils.translation import ugettext as _
from ..forms import SelectOSForm
from ..utils import format_timedelta as _format_timedelta
from ..utils import format_link
from ..utils import mailformat
log = logging.getLogger(__name__)
register = template.Library()
@register.simple_tag
[docs]def path(urlname, text='', title=None, anchor=None, **kwargs):
"""Return a HTML link to the given urlname.
The difference to this tag and Django's builtin ``{% url %}`` template tag is that this tag
returns a complete HTML link. The primary use is to use dynamic links to paths in rendered blog
posts or pages.
Parameters
----------
urlname : str
The URL name to resolve and link to. URL names are configured in an apps ``url.py``,
examples are ``"account:register"`` or ``"feed:rss2"``.
text : str
The link text. If omitted, a warning is logged and the ``urlname`` will be used instead.
title : str, optional
A link title attribute.
.. seealso:: https://www.w3.org/TR/html401/struct/links.html#h-12.1.4
anchor : str, optional
Any HTML anchor to be added to the resolved URL.
**kwargs
Any keyword arguments will be passed as kwargs to the URL resolver.
"""
if not text:
log.warn('No text received path %s', urlname)
text = urlname
try:
path = reverse(urlname, kwargs=kwargs)
if anchor is not None:
path = '%s#%s' % (path, anchor)
return format_link(path, text, title=title)
except Exception as e:
log.exception(e)
return ''
@register.filter
@register.filter
@register.simple_tag(takes_context=True)
[docs]def os_selector(context):
"""Display a selector for OS-specific content."""
form = SelectOSForm()
return format_html('<form class="form-horizontal">{}</form>', form['os'].formgroup())
@register.tag('mailformat')
class UpperNode(template.Node):
def __init__(self, nodelist):
self.nodelist = nodelist
def render(self, context):
return mailformat(self.nodelist.render(context))