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