diff --git a/gestion/mail/mail.py b/gestion/mail/mail.py index 0f8546a9..61f32424 100644 --- a/gestion/mail/mail.py +++ b/gestion/mail/mail.py @@ -24,62 +24,68 @@ templateLoader = jinja2.FileSystemLoader( searchpath=["/", template_path] ) templateEnv = jinja2.Environment( loader=templateLoader ) +# file extension to rendering function map +markup = { + 'md' : markdown +} + ### For an example: ### print generate('bienvenue', {'From':'respbats@crans.org', 'To':'admin@genua.fr', 'lang_info':'English version below'}).as_string() def submessage(playload, type, charset='utf-8'): - """Renvois un sous message à mettre dans un message multipart""" + """Renvois un sous message à mettre dans un message multipart""" submsg=MIMEText('', type, charset) del(submsg['Content-Transfer-Encoding']) submsg['Content-Transfer-Encoding']='8bit' submsg.set_payload(playload) return submsg - -def htmlmultilang(mail, lang1, lang2, params, lang_info='', charset='utf-8'): - """Génère un html bilingue""" - file1 = template_path + mail + '/body/' + lang1 - file2 = template_path + mail + '/body/' + lang2 - if lang1 == lang2 or not os.path.isfile(file2): - txt = templateEnv.get_template(file1).render(params) - html = templateEnv.get_template(html_template).render({'body': markdown(txt)}) - return submessage(html.encode(charset), 'html', charset) - else: - txt1 = templateEnv.get_template(file1).render(params) - txt2 = templateEnv.get_template(file2).render(params) - params.update({'lang1':lang1, 'lang2':lang2, 'body1': markdown(txt1), 'body2':markdown(txt2)}) - html = templateEnv.get_template(html_mutilang_template).render(params) - return submessage(html.encode(charset), 'html', charset) -def textmultilang(mail, lang1, lang2, params, charset='utf-8'): - """Génère un plain text bilingue""" +def get_lang(mail, part, lang, lang_fallback): + for l in [lang, lang_fallback]: + for ext in markup.keys(): + if os.path.isfile(template_path + mail + '/' + part + '/' + l + '.' + ext): + return l, ext, template_path + mail + '/' + part + '/' + l + '.' + ext + if os.path.isfile(template_path + mail + '/' + part + '/' + l): + return l, None, template_path + mail + '/' + part + '/' + l + raise ValueError("Language %s nor %s found" % (lang, lang_fallback)) + +def body(mail, lang1, lang2, mk, params, charset): file1 = template_path + mail + '/body/' + lang1 file2 = template_path + mail + '/body/' + lang2 - if lang1 == lang2 or not os.path.isfile(file2): + if mk: + file1 = file1 + '.' + mk + file2 = file2 + '.' + mk + if lang1 == lang2 or not os.path.isfile(file2): # No alt language txt = templateEnv.get_template(file1).render(params) - return submessage(txt.encode(charset), 'plain', charset) + ret = [ submessage(txt.encode(charset), 'plain', charset) ] + if mk: # compute the html version + html = templateEnv.get_template(html_template).render({'body': markup[mk](txt)}) + ret.append(submessage(html.encode(charset), 'html', charset)) else: txt1 = templateEnv.get_template(file1).render(params) txt2 = templateEnv.get_template(file2).render(params) - params.update({'body1': txt1, 'body2':txt2}) - txt = templateEnv.get_template(text_mutilang_template).render(params) - return submessage(txt.encode(charset), 'plain', charset) + params_txt=dict(params) + params_txt.update({'body1': txt1, 'body2':txt2}) + txt = templateEnv.get_template(text_mutilang_template).render(params_txt) + ret=[ submessage(txt.encode(charset), 'plain', charset) ] + if mk: # compute the html version + params_html=dict(params) + params_html.update({'lang1':lang1, 'lang2':lang2, 'body1': markup[mk](txt1), 'body2': markup[mk](txt2)}) + html = templateEnv.get_template(html_mutilang_template).render(params_html) + ret.append(submessage(html.encode(charset), 'html', charset)) + return ret def generate(mail, params, lang=default_language, lang_fallback=default_language, lang_alt='en', charset='utf-8'): - """Génère un message multipart""" + """Génère un message multipart""" msg = MIMEMultipart('alternative') if os.path.isdir(template_path + mail): for filename in [dir for dir in os.listdir(template_path + mail) if os.path.isdir(template_path + mail + '/' + dir)]: - if os.path.isfile(template_path + mail + '/' + filename + '/' + lang): - lang_tmp = lang - file = template_path + mail + '/' + filename + '/' + lang - else: - lang_tmp = lang_fallback - file = template_path + mail + '/' + filename + '/' + lang_tmp + lang_tmp, mk, file = get_lang(mail, filename, lang, lang_fallback) if filename == 'body': - msg.attach(textmultilang(mail, lang_tmp, lang_alt, params, charset)) - msg.attach(htmlmultilang(mail, lang_tmp, lang_alt, params, charset)) + for part in body(mail, lang_tmp, lang_alt, mk, params, charset): + msg.attach(part) else: txt = templateEnv.get_template(file).render(params) if filename in ['From', 'To', 'Cc', 'Bcc']: diff --git a/gestion/mail/template/bienvenue/body/en b/gestion/mail/template/bienvenue/body/en.md similarity index 100% rename from gestion/mail/template/bienvenue/body/en rename to gestion/mail/template/bienvenue/body/en.md diff --git a/gestion/mail/template/bienvenue/body/fr b/gestion/mail/template/bienvenue/body/fr.md similarity index 100% rename from gestion/mail/template/bienvenue/body/fr rename to gestion/mail/template/bienvenue/body/fr.md