|
|
@ -4,21 +4,34 @@ from urllib.request import quote as urlencode |
|
|
|
register = template.Library() |
|
|
|
|
|
|
|
@register.simple_tag |
|
|
|
def mailurl(*, subject=None, body=None, to=[]): |
|
|
|
"""TODO: Dokumentace""" |
|
|
|
def mailurl(*, subject=None, body=None, to=[], cc=[], bcc=[]): |
|
|
|
"""Tag na vytváření správně zakódované mailto: adresy |
|
|
|
|
|
|
|
Ref: RFC 6068, <https://en.wikipedia.org/wiki/Mailto>""" |
|
|
|
if isinstance(to, str): |
|
|
|
to = [to] |
|
|
|
if isinstance(cc, str): |
|
|
|
cc = [cc] |
|
|
|
if isinstance(bcc, str): |
|
|
|
bcc = [bcc] |
|
|
|
assert isinstance(to, list) |
|
|
|
assert isinstance(cc, list) |
|
|
|
assert isinstance(bcc, list) |
|
|
|
# FIXME: adresa není správně zakódovaná, rozbije se to na adresách s divnými znaky |
|
|
|
parts = [ |
|
|
|
f'mailto:{str.join(",", to)}', |
|
|
|
] |
|
|
|
if len(to) < 1: |
|
|
|
if len(to) + len(cc) + len(bcc) < 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(cc) > 0: |
|
|
|
parts.append(f'cc={str.join(",", cc)}') |
|
|
|
if len(bcc) > 0: |
|
|
|
parts.append(f'bcc={str.join(",", bcc)}') |
|
|
|
|
|
|
|
if len(parts) > 1: |
|
|
|
url = parts[0] + '?' + str.join('&', parts[1:]) |
|
|
@ -27,8 +40,8 @@ def mailurl(*, subject=None, body=None, to=[]): |
|
|
|
return url |
|
|
|
|
|
|
|
@register.simple_tag |
|
|
|
def maillink(text, subject=None, body=None, to=[], attrs=None): |
|
|
|
url = mailurl(subject=subject, body=body, to=to) |
|
|
|
def maillink(text, subject=None, body=None, to=[], cc=[], bcc=[], attrs=None): |
|
|
|
url = mailurl(subject=subject, body=body, to=to, cc=cc, bcc=bcc) |
|
|
|
if not attrs: attrs = '' |
|
|
|
mezera = ' '*bool(attrs) |
|
|
|
full_link = f'<a href="{url}"{mezera}{attrs}>{text}</a>' |
|
|
|