Commit d16522f1 by Nicolas Joyard

Ajout importer pour maj emails

parent ebc29a27
Showing with 58 additions and 0 deletions
......@@ -8,6 +8,7 @@ from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
from .importers.adresses import AdressesImporter
from .importers.emails import EmailImporter
from .importers.laposte import LaPosteImporter
from .importers.nosdeputes import NosDeputesImporter
......@@ -161,6 +162,16 @@ def import_laposte():
@manager.command
def import_emails():
"""
Importe les emails des députés depuis un fichier DATA_DIR/emails.csv au
format "nom complet;email1,email2,..."
"""
app.config.update(SQLALCHEMY_ECHO=False)
EmailImporter(app).run()
@manager.command
def password():
"""Chiffre un mot de passe admin"""
print(hash_password(getpass(), app.config['SECRET_KEY']))
......
# -*- coding: utf-8 -*-
import csv
from os.path import join
from .base import BaseImporter
from ..models import Parlementaire, db
class NonTrouve(Exception):
pass
class EmailImporter(BaseImporter):
def import_emails(self, nom, emails):
parl = Parlementaire.query.filter(Parlementaire.nom_complet == nom) \
.one()
if not parl:
raise NonTrouve('%s non trouvé' % nom)
if parl.emails != emails:
self.info('%s => %s' % (nom, emails))
parl.emails = emails
db.session.commit()
return True
return False
def run(self):
self.info('Début import emails')
erreurs = 0
changes = 0
with open(join(self.app.config['DATA_DIR'], 'emails.csv'),
newline='') as csvfile:
reader = csv.DictReader(csvfile, delimiter=';')
for row in reader:
try:
if self.import_emails(row['nom'], row['emails']):
changes = changes + 1
except NonTrouve:
self.error('Non trouvé: %s' % row['nom'])
erreurs = erreurs + 1
self.info('Import emails terminé, %s changements, %s erreurs' %
(changes, erreurs))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment