diff --git a/docbow_project/docbow/management/commands/sync-tabellio.py b/docbow_project/docbow/management/commands/sync-tabellio.py index 0beab08..8019b75 100644 --- a/docbow_project/docbow/management/commands/sync-tabellio.py +++ b/docbow_project/docbow/management/commands/sync-tabellio.py @@ -32,6 +32,8 @@ class Command(BaseCommand): parl_list = MailingList.objects.get(id=settings.PARLEMENTAIRES_MAILING_ID) ministres_list = MailingList.objects.get(id=settings.MINISTRES_MAILING_ID) + comppols = {} + conn = psycopg2.connect("host=%s dbname=%s user=%s password=%s" % ( settings.TABELLIO_DBHOST, settings.TABELLIO_DBNAME, @@ -74,10 +76,11 @@ class Command(BaseCommand): ministres_list.members.remove(user) ministres_list.save() - cur.execute('''SELECT t_pers.nom, t_pers.prenom, t_adresse.email - FROM t_pers, t_pershistoline, t_adresse + cur.execute('''SELECT t_pers.nom, t_pers.prenom, t_adresse.email, t_comppol.abbr + FROM t_pers, t_pershistoline, t_adresse, t_comppol WHERE t_pers.st = 'S_PARL' AND t_pers.prenom is not NULL + AND t_pershistoline.description = t_comppol.id AND t_pers.id = t_pershistoline.pers AND t_pershistoline.type = 'P_CMPL' AND t_pershistoline.fin IS NULL @@ -91,7 +94,7 @@ class Command(BaseCommand): t = cur.fetchone() if not t: break - last_name, first_name, email = t + last_name, first_name, email, comppol = t username = get_username(last_name, first_name) @@ -119,6 +122,19 @@ class Command(BaseCommand): ministres_list.members.remove(user) ministres_list.save() + if not comppol in comppols: + comppols[comppol] = [] + comppols[comppol].append(user) + + for comppol, members in comppols.items(): + maillist, created = MailingList.objects.get_or_create( + name=u'Appartenance politique - %s' % comppol) + for member in maillist.members.all(): + if member not in members: + maillist.members.remove(member) + for member in members: + if member not in maillist.members.all(): + maillist.members.add(member) cur.execute('''SELECT t_pers.nom, t_pers.prenom, t_adresse.email FROM t_pers, t_pershistoline, t_adresse