From 04c3c6257cfee4dd9723a47db425d4a3ef237369 Mon Sep 17 00:00:00 2001 From: "Pavel \"LEdoian\" Turinsky" Date: Mon, 6 Feb 2023 21:56:06 +0100 Subject: [PATCH] =?UTF-8?q?Podpora=20cc=20a=20bcc=20v=20{%maillink%}=20[ne?= =?UTF-8?q?otestov=C3=A1no]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../templates/odevzdavatko/detail.html | 3 +++ odevzdavatko/views.py | 1 + various/templatetags/mail.py | 23 +++++++++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/odevzdavatko/templates/odevzdavatko/detail.html b/odevzdavatko/templates/odevzdavatko/detail.html index 468b0322..73265563 100644 --- a/odevzdavatko/templates/odevzdavatko/detail.html +++ b/odevzdavatko/templates/odevzdavatko/detail.html @@ -21,6 +21,9 @@ ({% maillink r.osoba.email to=r.osoba.email subject=predmetmailu %}){% if forloop.revcounter0 != 0 %}, {% endif %} {% endfor %}

+

+ {% maillink "Poslat mail všem řešitelům" bcc=maily_vsech_resitelu subject=predmetmailu %} +

{% else %}

Řešitelé: {{ object.resitele.all | join:", " }}

{% endif %} diff --git a/odevzdavatko/views.py b/odevzdavatko/views.py index 57822bf4..9ac1ac29 100644 --- a/odevzdavatko/views.py +++ b/odevzdavatko/views.py @@ -240,6 +240,7 @@ class DetailReseniView(DetailView): # 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["maily_vsech_resitelu"] = [y for x in self.reseni.resitele.all().values_list('osoba__email') for y in x] return ctx def get(self, request, *args, **kwargs): diff --git a/various/templatetags/mail.py b/various/templatetags/mail.py index 972040f6..ecbb2a39 100644 --- a/various/templatetags/mail.py +++ b/various/templatetags/mail.py @@ -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, """ 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'{text}'