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.

31 lines
803 B

from django import template
from django.utils.safestring import mark_safe
from urllib.request import quote as urlencode
register = template.Library()
@register.simple_tag
def maillink(text: str, subject=None, body=None, to=[], attrs=None):
"""TODO: Dokumentace"""
if isinstance(to, str):
to = [to]
assert isinstance(to, list)
parts = [
f'mailto:{str.join(",", to)}',
]
if len(to) < 1:
raise ValueError('Cannot mail to empty set of people')
if subject:
parts.append(f'subject={urlencode(subject)}')
if body:
parts.append(f'body={urlencode(body)}')
if len(parts) > 1:
url = parts[0] + '?' + str.join('&', parts[1:])
else:
url = parts[0]
if not attrs: attrs = ''
mezera = ' '*bool(attrs)
full_link = f'<a href="{url}"{mezera}{attrs}>{text}</a>'
return mark_safe(full_link)