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

3
import os
Nicolas Joyard's avatar
Nicolas Joyard committed
4
from getpass import getpass
Nicolas Joyard's avatar
Nicolas Joyard committed
5

Nicolas Joyard's avatar
Nicolas Joyard committed
6
from flask_migrate import Migrate, MigrateCommand
Nicolas Joyard's avatar
Nicolas Joyard committed
7

Nicolas Joyard's avatar
Nicolas Joyard committed
8 9
from flask_script import Manager

10
from .importers.adresses import AdressesImporter
11
from .importers.laposte import LaPosteImporter
12
from .importers.nosdeputes import NosDeputesImporter
Nicolas Joyard's avatar
Nicolas Joyard committed
13

Nicolas Joyard's avatar
Nicolas Joyard committed
14
from .irfm import app
15

Nicolas Joyard's avatar
Nicolas Joyard committed
16
from .models import db
17

Nicolas Joyard's avatar
Nicolas Joyard committed
18
from .tools.files import generer_demandes as generer_demandes_
Nicolas Joyard's avatar
Nicolas Joyard committed
19
from .tools.mails import (envoyer_emails as envoyer_emails_,
20
                          envoyer_relances as envoyer_relances_,
Nicolas Joyard's avatar
Nicolas Joyard committed
21
                          mailing_lists as mailing_lists_,
22 23
                          erratum_cada as erratum_cada_,
                          extraire_mails_cada as extraire_mails_cada_)
24
from .tools.procedure import (fix_procedure as fix_procedure_,
Nicolas Joyard's avatar
Nicolas Joyard committed
25 26
                              avance_procedure as avance_procedure_,
                              export_pour_ta as export_pour_ta_)
Nicolas Joyard's avatar
Nicolas Joyard committed
27
from .tools.text import hash_password
Nicolas Joyard's avatar
Nicolas Joyard committed
28 29 30 31 32 33 34 35 36


manager = Manager(app)
migrate = Migrate(app, db)

manager.add_command('db', MigrateCommand)


@manager.command
Nicolas Joyard's avatar
Nicolas Joyard committed
37 38 39 40 41 42 43
def clear_cache():
    """Vide le cache des fichiers générés"""
    files_root = os.path.join(app.config['DATA_DIR'], 'files')
    if os.path.exists(files_root):
        for item in os.listdir(files_root):
            print('Suppression %s' % item)
            os.unlink(os.path.join(files_root, item))
Nicolas Joyard's avatar
Nicolas Joyard committed
44 45


46 47 48 49 50
@manager.command
@manager.option('--envoyer', action='store_true')
def envoyer_emails(envoyer=False):
    """Envoie des e-mails pour tous les parlementaires"""
    app.config.update(SQLALCHEMY_ECHO=False)
Nicolas Joyard's avatar
Nicolas Joyard committed
51
    missed_addr, missed_email = envoyer_emails_(app, envoyer)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    print('')
    if len(missed_addr):
        print('Parlementaires sans adresse postale :\n')
        print('\n'.join(missed_addr))
    else:
        print('Aucun parlementaire sans adresse postale :)')

    print('')
    if len(missed_email):
        print('Parlementaires sans adresse mail:\n')
        print('\n'.join(missed_email))
    else:
        print('Aucun parlementaire sans adresse mail :)')


68 69 70 71 72 73 74 75
@manager.command
@manager.option('--envoyer', action='store_true')
def envoyer_relances(envoyer=False):
    """Envoie des e-mails de relance"""
    app.config.update(SQLALCHEMY_ECHO=False)
    envoyer_relances_(app, envoyer)


Nicolas Joyard's avatar
Nicolas Joyard committed
76 77 78 79 80 81 82 83
@manager.command
def mailing_lists():
    """Affiche les abonnés aux mailing lists"""
    app.config.update(SQLALCHEMY_ECHO=False)
    for nom, emails in mailing_lists_().items():
        print('%s :\n\t%s' % (nom, '\n\t'.join(emails)))


84 85 86 87 88 89 90
@manager.command
def fix_procedure():
    """Génère les étapes manquantes pour tous les parlementaires"""
    app.config.update(SQLALCHEMY_ECHO=False)
    fix_procedure_(app)


Nicolas Joyard's avatar
Nicolas Joyard committed
91 92 93 94 95 96 97
@manager.command
def export_pour_ta():
    """Génère un export CSV pour les requêtes TA"""
    app.config.update(SQLALCHEMY_ECHO=False)
    export_pour_ta_(app)


98 99 100 101 102 103 104 105
@manager.command
@manager.option('-e', '--etape', dest='etape', default=None)
def avance_procedure(etape):
    """
    Avance la procédure pour tous les parlementaire, et envoie les alertes
    correspondantes.
    """
    app.config.update(SQLALCHEMY_ECHO=False)
106
    avance_procedure_(app, int(etape))
107 108


Nicolas Joyard's avatar
Nicolas Joyard committed
109 110 111 112 113 114 115 116 117
@manager.command
def erratum_cada():
    """
    Envoi les e-mails d'erratum CADA
    """
    app.config.update(SQLALCHEMY_ECHO=False)
    erratum_cada_(app)


118 119 120 121 122 123 124 125 126
@manager.command
def extraire_mails_cada():
    """
    Extraire les e-mails de réponse CADA
    """
    app.config.update(SQLALCHEMY_ECHO=False)
    extraire_mails_cada_(app)


127 128 129 130
@manager.command
def generer_demandes():
    """Génère les demandes pour tous les parlementaires"""
    app.config.update(SQLALCHEMY_ECHO=False)
Nicolas Joyard's avatar
Nicolas Joyard committed
131
    missed = generer_demandes_(app)
132

133 134 135 136 137 138 139
    print('')
    if len(missed):
        print('Parlementaires sans adresse:\n')
        print('\n'.join(missed))
    else:
        print('Aucun parlementaire sans adresse :)')

140

141 142 143
@manager.command
def import_nd():
    """Importe les députés depuis NosDéputés.fr"""
144
    app.config.update(SQLALCHEMY_ECHO=False)
145
    NosDeputesImporter(app).run()
146

Nicolas Joyard's avatar
Nicolas Joyard committed
147

148 149 150 151 152
@manager.command
def import_adresses():
    """Importe adresses postales des parlementaires"""
    app.config.update(SQLALCHEMY_ECHO=False)
    AdressesImporter(app).run()
153 154


155 156 157 158 159 160 161
@manager.command
def import_laposte():
    """Importe l'état des courriers suivi depuis La Poste"""
    app.config.update(SQLALCHEMY_ECHO=False)
    LaPosteImporter(app).run()


162
@manager.command
Nicolas Joyard's avatar
Nicolas Joyard committed
163 164
def password():
    """Chiffre un mot de passe admin"""
Nicolas Joyard's avatar
Nicolas Joyard committed
165
    print(hash_password(getpass(), app.config['SECRET_KEY']))
Nicolas Joyard's avatar
Nicolas Joyard committed
166 167 168 169 170 171


@manager.command
def runserver():
    """Exécute le serveur web flask intégré"""
    app.run()