diff --git a/main.py b/main.py index aa19966..b28b754 100644 --- a/main.py +++ b/main.py @@ -2,9 +2,9 @@ from configparser import ConfigParser import socket -from re2oapi import Re2oAPIClient -from django.core.mail import send_mail -from django.template import loader, Context +from jinja2 import Environment, FileSystemLoader + +from re2oapi import Re2oAPIClient, ApiSendMail from pprint import pprint import sys @@ -18,37 +18,46 @@ api_username = config.get('Re2o', 'username') api_client = Re2oAPIClient(api_hostname,api_username,api_password) +api_mailserver = config.get('Mail', 'mailserver') +api_port = config.get('Mail', 'port') + +api_sendmail = ApiSendMail(api_mailserver, api_port) + client_hostname = socket.gethostname().split('.',1)[0] -for arg in sys.argv: - if arg=="--force": - notif_end_adhesion(api_client) +# Création de l'environnement Jinja +ENV = Environment(loader=FileSystemLoader('.')) def notif_end_adhesion(api_client): - asso_options = api_client.list("preferences/assooption") - from_mail = api_client.list("preferences/generaloption")["email_from"] - template = loader.get_template('email_fin_adhesion') + asso_options = api_client.view("preferences/assooption/") + general_options = api_client.view("preferences/generaloption/") + template = ENV.get_template("templates/email_fin_adhesion") for result in api_client.list("reminder/get-users"): for user in result["users_to_remind"]: - context = Context({ - 'nom': user["get_full_name"], - 'temps': result["days"], - 'asso_name': asso_options["name"], - 'link': asso_options["site_url"] - }) - print('mail envoyé à {}, reminder {} days'.format(user["get_full_name"],result["days"])) - send_mail("Avis de fin d'adhésion / End of subscription notice", - '', - from_mail, - user["email"], - html_message = template.render(context) + if "--verbose" in sys.argv: + print('Mail envoyé à {}, reminder {} days'.format(user["get_full_name"],result["days"])) + reminder_mail = template.render( + nom=user["get_full_name"], + temps=result["days"], + asso_name=asso_options["name"], + message=result["message"], + link=general_options["main_site_url"]) + api_sendmail.send_mail( + general_options["email_from"], + user["get_mail"], + "Avis de fin d'adhésion / End of subscription notice", + reminder_mail ) +## Manual command +if "--force" in sys.argv: + notif_end_adhesion(api_client) +## Automatic regen for service in api_client.list("services/regen/"): if service['hostname'] == client_hostname and \ - service['service_name'] == 'mail' and \ + service['service_name'] == 'notif-users' and \ service['need_regen']: notif_end_adhesion(api_client) api_client.patch(service['api_url'], data={'need_regen': False}) diff --git a/templates/email_fin_adhesion b/templates/email_fin_adhesion index 7f16f13..71d1647 100644 --- a/templates/email_fin_adhesion +++ b/templates/email_fin_adhesion @@ -1,27 +1,21 @@
Bonjour {{nom}},
-Ton adhésion arrive à son therme dans {{temps}} jours.
+Ta connexion internet va expirer dans {{temps}} jours.
-{% if online %} -Tu peux renouveller ton adhésion en ligne sur Mon Profil.
-Tu peux également contacter un membre de {{asso_name}} pour renouveller ton adhésion
-{% else %} -Pour renouveller ton adhésion, contacte un membre de {{asso_name}}.
-{% endif %} +{{message}} -A bientôt.
+Tu peux gérer ta connexion en ligne sur Mon profil
+Tu peux bien évidemment également contacter un membre de {{asso_name}} pour renouveller ton adhésion
+ +A bientôt !
Hi {{nom}},
-Your subscription comes to an end in {{temps}} days.
+Your subscription comes to an end in {{temps}} days.
-{% if online %} -You can renew it online on My Profil.
+You can manage it online on My Profil.
You can also contat a member of {{asso_name}} to renew it.
-{% else %} -To renew it, contact a member of {{asso_name}}
-{% endif %}Regards.