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 %} | ||||
|   {% endfor %} | ||||
|   </p> | ||||
|   <p> | ||||
|     {% maillink "Poslat mail všem řešitelům" bcc=maily_vsech_resitelu subject=predmetmailu %} | ||||
|   </p> | ||||
| {% else %} | ||||
|   <p>Řešitelé: {{ object.resitele.all | join:", " }}</p> | ||||
| {% 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) | ||||
| 		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): | ||||
|  |  | |||
|  | @ -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>' | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 Pavel "LEdoian" Turinsky
						Pavel "LEdoian" Turinsky