mails.py 10.4 KB
Newer Older
Nicolas Joyard's avatar
Nicolas Joyard committed
1
# -*- coding: utf-8 -*-
Nicolas Joyard's avatar
Nicolas Joyard committed
2

3
from datetime import datetime, timedelta
Nicolas Joyard's avatar
Nicolas Joyard committed
4
import dateparser
5
import email
Nicolas Joyard's avatar
Nicolas Joyard committed
6
import os
7 8
import re
import subprocess
Nicolas Joyard's avatar
Nicolas Joyard committed
9 10 11
import time

from flask import render_template
Nicolas Joyard's avatar
Nicolas Joyard committed
12

Nicolas Joyard's avatar
Nicolas Joyard committed
13 14
from flask_mail import Mail, Message

15
from sqlalchemy.orm import contains_eager, joinedload
16
from sqlalchemy.orm.exc import NoResultFound
17 18 19

from ..models import Action, User, Parlementaire, db
from ..models.constants import (DELAI_RELANCE, DELAI_REPONSE, ETAPE_NA,
Nicolas Joyard's avatar
Nicolas Joyard committed
20 21
                                ETAPE_A_CONFIRMER, ETAPE_DEMANDE_CADA,
                                ETAPE_DOC_MASQUE)
22
from ..models.functions import normalize_name
Nicolas Joyard's avatar
Nicolas Joyard committed
23 24

from ..tools.files import generer_demande
Nicolas Joyard's avatar
Nicolas Joyard committed
25
from ..tools.text import create_usertoken as token
Nicolas Joyard's avatar
Nicolas Joyard committed
26 27


Nicolas Joyard's avatar
Nicolas Joyard committed
28 29 30 31 32 33 34 35 36 37 38 39 40
def mailing_lists():
    filters = {
        'Liste Membres': User.abo_membres == True,  # noqa
        'Liste IRFM': User.abo_irfm == True,  # noqa
        'Newsletter': User.abo_rc == True,  # noqa
    }

    return {
        k: [u.email for u in User.query.filter(f).all()]
        for k, f in filters.items()
    }


41
def envoyer_alerte(app, etape, parl, commentaire=None):
Nicolas Joyard's avatar
Nicolas Joyard committed
42 43 44
    mail = Mail(app)

    sender = ('Regards Citoyens', app.config['ADMIN_EMAIL'])
45
    subject = 'Transparence IRFM - Alerte pour %s' % parl.nom_complet
Nicolas Joyard's avatar
Nicolas Joyard committed
46

47 48 49
    if not commentaire:
        commentaire = etape['description']

Nicolas Joyard's avatar
Nicolas Joyard committed
50 51
    messages = []
    for user in parl.abonnes:
Nicolas Joyard's avatar
Nicolas Joyard committed
52
        anon_id = user.nick[8:] if user.nick.startswith('anonyme!') else None
Nicolas Joyard's avatar
Nicolas Joyard committed
53 54
        body = render_template('courriers/mail_alerte.txt.j2',
                               user=user,
Nicolas Joyard's avatar
Nicolas Joyard committed
55
                               anon_id=anon_id,
Nicolas Joyard's avatar
Nicolas Joyard committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69
                               etape=etape,
                               parl=parl,
                               commentaire=commentaire)

        messages.append(Message(subject=subject, body=body, sender=sender,
                                recipients=[user.email]))

    with mail.connect() as conn:
        for msg in messages:
            conn.send(msg)

    return len(messages)


Nicolas Joyard's avatar
Nicolas Joyard committed
70 71 72 73
def envoyer_emails(app, envoyer):
    files_root = os.path.join(app.config['DATA_DIR'], 'files')

    mail = Mail(app)
Nicolas Joyard's avatar
Nicolas Joyard committed
74
    parls = Parlementaire.query.filter(Parlementaire.etape > ETAPE_NA) \
Nicolas Joyard's avatar
Nicolas Joyard committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
                               .filter(Parlementaire.mails_envoyes == 0) \
                               .order_by(Parlementaire.nom) \
                               .all()

    missed_addr = []
    missed_email = []

    total = 0

    if envoyer:
        print('')
        print('ENVOI POUR DE VRAI dans 5s')
        print('')

        time.sleep(5)
    else:
        print('Envoi test, ajouter --envoyer pour le bouton rouge')

    for parl in parls:
        total += 1

        if not parl.adresse:
            missed_addr.append(parl.nom_complet)
            continue

        if not parl.emails:
            missed_email.append(parl.nom_complet)
            continue

        filename = generer_demande(parl, files_root)

        sender = ('Regards Citoyens', app.config['ADMIN_EMAIL'])
        subject = 'Demande d\'accès aux dépenses de vos frais de mandat'
        body = render_template('courriers/mail_parlementaire.txt.j2',
                               parlementaire=parl)

        if envoyer:
            recipients = parl.emails.split(',')
            bcc = [app.config['ADMIN_EMAIL']]
        else:
            subject = '[TEST] %s' % subject
            emails = ', '.join(parl.emails.split(','))
            body = '[TEST] Destinataires: %s\n\n%s' % (emails, body)

            recipients = [app.config['ADMIN_EMAIL']]
            bcc = []

        msg = Message(subject=subject, body=body, sender=sender,
                      recipients=recipients, bcc=bcc)

        with open(os.path.join(files_root, filename), 'rb') as f:
            msg.attach(filename, 'application/pdf', f.read())

Nicolas Joyard's avatar
Nicolas Joyard committed
128 129
        print('[%s/%s] Envoi mail à %s (%s)' %
              (total, len(parls), parl.nom_complet, ', '.join(recipients)))
Nicolas Joyard's avatar
Nicolas Joyard committed
130 131 132 133 134 135 136 137 138
        mail.send(msg)

        if envoyer:
            parl.mails_envoyes = 1
            db.session.commit()

        time.sleep(1)

    return missed_addr, missed_email
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166


def envoyer_relances(app, envoyer):
    mail = Mail(app)

    date_min = datetime.now() - timedelta(days=DELAI_RELANCE)
    acts = Action.query.join(Action.parlementaire) \
                       .filter(Action.etape == ETAPE_A_CONFIRMER) \
                       .filter(Parlementaire.etape == ETAPE_A_CONFIRMER) \
                       .filter(Action.suivi == None) \
                       .filter(Action.date < date_min) \
                       .options(contains_eager(Action.parlementaire)) \
                       .options(joinedload(Action.user)) \
                       .order_by(Parlementaire.nom) \
                       .all()  # noqa

    data = {}

    for act in acts:
        if act.user not in data:
            data[act.user] = []
        data[act.user].append(act)

    for user, acts in data.items():
        sender = ('Regards Citoyens', app.config['ADMIN_EMAIL'])
        subject = 'Transparence IRFM - Relance'
        body = render_template('courriers/mail_relance.txt.j2',
                               user=user,
Nicolas Joyard's avatar
Nicolas Joyard committed
167
                               token=token(user.id, app.config['SECRET_KEY']),
168 169 170 171 172 173 174 175 176
                               parls=[a.parlementaire for a in acts],
                               delai_relance=DELAI_RELANCE,
                               delai_reponse=DELAI_REPONSE)

        msg = Message(subject=subject, body=body, sender=sender,
                      recipients=[user.email])

        if envoyer:
            mail.send(msg)
177 178
            for a in acts:
                a.suivi = 'Relancé le %s' % datetime.now().strftime('%x')
179 180 181 182 183 184
            time.sleep(1)
        else:
            print(msg)

    if envoyer:
        db.session.commit()
Nicolas Joyard's avatar
Nicolas Joyard committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219


def erratum_cada(app):
    parls = Parlementaire.query \
        .filter(Parlementaire.etape == ETAPE_DEMANDE_CADA) \
        .all()

    mail = Mail(app)

    for parl in parls:
        print(parl.nom_complet)

        sender = ('Regards Citoyens', app.config['ADMIN_EMAIL'])
        subject = 'Transparence IRFM - Erratum - Alerte pour %s' % \
            parl.nom_complet

        messages = []
        for user in parl.abonnes:
            anon_id = user.nick[8:] if user.nick.startswith('anonyme!') \
                else None
            body = render_template('courriers/mail_erratum.txt.j2',
                                   user=user,
                                   anon_id=anon_id,
                                   parl=parl)

            messages.append(Message(subject=subject, body=body, sender=sender,
                                    recipients=[user.email]))

        if len(messages):
            with mail.connect() as conn:
                for msg in messages:
                    conn.send(msg)

            print('%s e-mails d\'erratum envoyés' % len(messages))
            time.sleep(1)
220 221 222 223


AVIS_INCOMPETENCE = 'La commission ne peut donc que se déclarer ' + \
                    'incompétente pour se prononcer sur la demande.'
Nicolas Joyard's avatar
Nicolas Joyard committed
224 225
AVIS_RE_DEPUTE = re.compile('par( Monsieur| Madame)+ ([^,]*),? députée?')
AVIS_EXCEPTIONS = {
226 227
    'Pierre LELLOUCH': 'Pierre LELLOUCHE',
    'Paul GIACCOBBI': 'Paul GIACOBBI'
Nicolas Joyard's avatar
Nicolas Joyard committed
228
}
229 230 231 232


def extraire_mails_cada(app):
    emails_root = os.path.join(app.config['DATA_DIR'], 'emails')
233
    uploads_root = os.path.join(app.config['DATA_DIR'], 'uploads')
234

Nicolas Joyard's avatar
Nicolas Joyard committed
235 236
    admin = User.query.filter(User.nick == '!rc').one()

237 238 239
    for eml in [f for f in os.listdir(emails_root)
                if os.path.isfile(os.path.join(emails_root, f))
                and f.endswith('.eml')]:
Nicolas Joyard's avatar
Nicolas Joyard committed
240
        remove = False
241 242 243
        with open(os.path.join(emails_root, eml)) as f:
            message = email.message_from_file(f)
            cada_id = message['subject']
Nicolas Joyard's avatar
Nicolas Joyard committed
244
            date_avis = dateparser.parse(message['date'].replace(" (CET)", ""))
245 246 247 248 249 250

            # Extraction pièce jointe 'Avis.pdf'
            pdf = None
            for pl in message.get_payload():
                if pl.get_filename() == 'Avis.pdf':
                    pdf = pl.get_payload(decode=True)
Nicolas Joyard's avatar
Nicolas Joyard committed
251
                    break
252 253 254 255 256 257

            if not pdf:
                print('CADA %s: (!) avis introuvable' % cada_id)
                continue

            # Enregistrement du PDF
Nicolas Joyard's avatar
Nicolas Joyard committed
258
            pdfbase = 'avis-cada-%s.pdf' % cada_id
259
            pdfname = os.path.join(uploads_root, pdfbase)
260 261 262 263
            with open(pdfname, 'wb') as outf:
                outf.write(pdf)

            # Extraction du texte
264 265
            out = str(subprocess.check_output(['pdftotext', pdfname, '-']),
                      encoding='utf-8')
266 267

            # Vérification de la décision
Nicolas Joyard's avatar
Nicolas Joyard committed
268
            if AVIS_INCOMPETENCE not in out:
269 270 271
                print('CADA %s: (!) décision inattendue' % cada_id)

            # Extraction du député concerné
Nicolas Joyard's avatar
Nicolas Joyard committed
272
            match = AVIS_RE_DEPUTE.search(out)
273 274
            if not match:
                print('CADA %s: (!) nom député introuvable' % cada_id)
Nicolas Joyard's avatar
Nicolas Joyard committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
                continue

            nom = match.group(2)
            nom = AVIS_EXCEPTIONS.get(nom, nom)
            condition = normalize_name(Parlementaire.nom_complet) \
                .ilike(normalize_name(nom))

            try:
                parl = Parlementaire.query.filter(condition).one()
            except NoResultFound:
                print('CADA %s: (!) parlementaire inconnu %s'
                      % (cada_id, nom))
                continue

            # Recherche d'un avis existant
            try:
                act = Action.query \
                    .filter(Action.parlementaire == parl) \
                    .filter(Action.attachment.like('avis-cada-%')) \
                    .one()
            except NoResultFound:
                act = None

            if act:
                if act.attachment != pdfbase:
                    print('CADA %s; (!) 2ème avis CADA (existant: %s)'
                          % (cada_id, pdfbase))
Nicolas Joyard's avatar
Nicolas Joyard committed
302 303 304 305
                if act.date != date_avis:
                    act.date = date_avis
                    db.session.add(act)
                    db.session.commit()
Nicolas Joyard's avatar
Nicolas Joyard committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
                continue

            print('CADA %s; Ajout avis CADA pour %s'
                  % (cada_id, parl.nom_complet))

            act = Action(
                parlementaire=parl,
                etape=ETAPE_DOC_MASQUE,
                date=datetime.now(),
                suivi='La CADA s\'est déclarée incompétente pour '
                      + 'répondre à notre demande.',
                attachment=pdfbase,
                user=admin
            )

            db.session.add(act)
            db.session.commit()

        if remove:
            os.unlink(os.path.join(emails_root, eml))

    db.session.commit()