main.py 2.09 KB
Newer Older
Grizzly's avatar
Grizzly committed
1 2 3 4
#!/usr/bin/env python3
from configparser import ConfigParser
import socket

5 6 7
from jinja2 import Environment, FileSystemLoader

from re2oapi import Re2oAPIClient, ApiSendMail
Grizzly's avatar
Grizzly committed
8 9

from pprint import pprint
10
import sys
Grizzly's avatar
Grizzly committed
11 12 13 14 15 16 17 18 19 20

config = ConfigParser()
config.read('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)

21 22 23 24 25
api_mailserver = config.get('Mail', 'mailserver')
api_port = config.get('Mail', 'port')

api_sendmail = ApiSendMail(api_mailserver, api_port)

Grizzly's avatar
Grizzly committed
26 27
client_hostname = socket.gethostname().split('.',1)[0]

28 29
# Création de l'environnement Jinja
ENV = Environment(loader=FileSystemLoader('.'))
30

Grizzly's avatar
Grizzly committed
31
def notif_end_adhesion(api_client):
32 33 34
    asso_options = api_client.view("preferences/assooption/")
    general_options = api_client.view("preferences/generaloption/")
    template = ENV.get_template("templates/email_fin_adhesion")
Grizzly's avatar
Grizzly committed
35 36 37

    for result in api_client.list("reminder/get-users"):
        for user in result["users_to_remind"]:
38 39 40 41 42 43 44 45 46 47 48 49 50
            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
Grizzly's avatar
Grizzly committed
51 52
            )

53 54 55
## Manual command
if "--force" in sys.argv:
    notif_end_adhesion(api_client)
56

57
## Automatic regen
58 59
for service in api_client.list("services/regen/"):
    if service['hostname'] == client_hostname and \
60
        service['service_name'] == 'notif-users' and \
61 62 63
        service['need_regen']:
        notif_end_adhesion(api_client)
        api_client.patch(service['api_url'], data={'need_regen': False})