main.py 1.8 KB
Newer Older
Grizzly's avatar
Grizzly committed
1 2 3 4 5 6 7 8 9
#!/usr/bin/env python3
from configparser import ConfigParser
import socket

from re2oapi import Re2oAPIClient
from django.core.mail import send_mail
from django.template import loader, Context

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

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)

client_hostname = socket.gethostname().split('.',1)[0]

23 24 25 26
for arg in sys.argv:
    if arg=="--force":
        notif_end_adhesion(api_client)

Grizzly's avatar
Grizzly committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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')

    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)
            )

48 49 50 51 52 53 54

for service in api_client.list("services/regen/"):
    if service['hostname'] == client_hostname and \
        service['service_name'] == 'mail' and \
        service['need_regen']:
        notif_end_adhesion(api_client)
        api_client.patch(service['api_url'], data={'need_regen': False})