Podpora cc a bcc v {%maillink%} [neotestováno]
This commit is contained in:
parent
f6cb669277
commit
04c3c6257c
3 changed files with 22 additions and 5 deletions
|
@ -21,6 +21,9 @@
|
||||||
({% maillink r.osoba.email to=r.osoba.email subject=predmetmailu %}){% if forloop.revcounter0 != 0 %}, {% endif %}
|
({% maillink r.osoba.email to=r.osoba.email subject=predmetmailu %}){% if forloop.revcounter0 != 0 %}, {% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</p>
|
</p>
|
||||||
|
<p>
|
||||||
|
{% maillink "Poslat mail všem řešitelům" bcc=maily_vsech_resitelu subject=predmetmailu %}
|
||||||
|
</p>
|
||||||
{% else %}
|
{% else %}
|
||||||
<p>Řešitelé: {{ object.resitele.all | join:", " }}</p>
|
<p>Řešitelé: {{ object.resitele.all | join:", " }}</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
@ -240,6 +240,7 @@ class DetailReseniView(DetailView):
|
||||||
|
|
||||||
# Subject případného mailu (template neumí použitelně spojovat řetězce: https://stackoverflow.com/q/4386168)
|
# Subject případného mailu (template neumí použitelně spojovat řetězce: https://stackoverflow.com/q/4386168)
|
||||||
ctx["predmetmailu"] = "Oprava řešení M&M "+self.reseni.problem.first().hlavni_problem.nazev
|
ctx["predmetmailu"] = "Oprava řešení M&M "+self.reseni.problem.first().hlavni_problem.nazev
|
||||||
|
ctx["maily_vsech_resitelu"] = [y for x in self.reseni.resitele.all().values_list('osoba__email') for y in x]
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
|
|
|
@ -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>'
|
||||||
|
|
Loading…
Reference in a new issue