-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (52 loc) · 2.24 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
from configparser import ConfigParser
import socket
import os
from jinja2 import Environment, FileSystemLoader
from re2oapi import Re2oAPIClient, ApiSendMail
from pprint import pprint
import sys
path =(os.path.dirname(os.path.abspath(__file__)))
config = ConfigParser()
config.read(path+'/config.ini')
api_hostname = config.get('Re2o', 'hostname')
api_password = config.get('Re2o', 'password')
api_username = config.get('Re2o', 'username')
api_client = Re2oAPIClient(api_hostname,api_username,api_password, use_tls=False)
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]
# Création de l'environnement Jinja
ENV = Environment(loader=FileSystemLoader('/'))
def notif_end_adhesion(api_client):
asso_options = api_client.view("preferences/assooption/")
general_options = api_client.view("preferences/generaloption/")
template = ENV.get_template(path + "/templates/email_fin_adhesion")
for result in api_client.list("reminder/get-users"):
for user in result["users_to_remind"]:
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"])
if user["get_mail"]:
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'] == 'notif-users' and \
service['need_regen']:
notif_end_adhesion(api_client)
api_client.patch(service['api_url'], data={'need_regen': False})