summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Péters <fpeters@entrouvert.com>2020-03-25 07:04:30 (GMT)
committerFrédéric Péters <fpeters@entrouvert.com>2020-03-25 07:04:30 (GMT)
commit2ffccb15d6d1af249bf5b5977bccc86405e15b5b (patch)
tree46915eea4310d4374acae143a0a1dd150e40f0f2
parentdacf7029e270b8d4f11d380b5703db88776e2283 (diff)
downloadmisc-fred-2ffccb15d6d1af249bf5b5977bccc86405e15b5b.zip
misc-fred-2ffccb15d6d1af249bf5b5977bccc86405e15b5b.tar.gz
misc-fred-2ffccb15d6d1af249bf5b5977bccc86405e15b5b.tar.bz2
grand lyon: add script to update field ids on signalement forms (#40803)
-rw-r--r--grandlyon/scripts/fsck-signalements.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/grandlyon/scripts/fsck-signalements.py b/grandlyon/scripts/fsck-signalements.py
new file mode 100644
index 0000000..65ef014
--- /dev/null
+++ b/grandlyon/scripts/fsck-signalements.py
@@ -0,0 +1,57 @@
+from wcs.formdef import FormDef
+from wcs import sql
+
+slugs = [
+ 'ambroisie',
+ 'arbres-et-espaces-verts',
+ 'collecte-et-proprete',
+ 'dechets-et-proprete',
+ 'deneigement',
+ 'eau-et-assainissement',
+ 'eclairage',
+ 'nuisance-olfactives-animales-vegetales',
+ 'objets-perdus',
+ 'voirie-et-signalisation',
+ ]
+
+#modele = FormDef.get_by_urlname('modele-signalement')
+#modele_fields = {x.varname: x.id for x in modele.fields if x.varname}
+modele_fields = {'Information_usager': '133', 'numero': '104', 'prenom_agent': '142', 'nom_usager': '135', 'voie': '105', 'tel_usager': '138', 'nom': '121', 'photo': '116', 'signalement_proximite': '108', 'type_probleme': '113', 'courriel_usager': '137', 'piecejointe_backoffice': '130', 'consentement_formulaire': '127', 'carte': '103', 'prenom_usager': '136', 'destinataire_backoffice': '131', 'prenom': '122', 'support_entree': '129', 'telephone': '124', 'nom_agent': '143', 'commentaire_backoffice': '140', 'commune': '106', 'message': '115', 'mode_reponse': '139', 'photo2': '117', 'date_demande': '132', 'courriel': '123', 'civilite': '120'}
+
+
+conn, cur = sql.get_connection_and_cursor()
+
+for slug in slugs:
+ formdef = FormDef.get_by_urlname(slug)
+ mapping = {x.id: modele_fields[x.varname] for x in formdef.fields if x.varname}
+ existing_ids = [x.id for x in formdef.fields]
+ changes = {x: y for x, y in mapping.items() if x != y}
+ if not changes:
+ continue
+ table_name = sql.get_formdef_table_name(formdef)
+ cur.execute('''SELECT column_name FROM information_schema.columns
+ WHERE table_schema = 'public'
+ AND table_name = %s''', (table_name,))
+ existing_fields = set([x[0] for x in cur.fetchall()])
+
+ for orig_id, target_id in mapping.items():
+ if target_id in existing_ids:
+ print(target_id, 'exists')
+ else:
+ field = [x for x in formdef.fields if x.id == orig_id][0]
+ field.id = target_id
+ cur.execute('''ALTER TABLE %s RENAME COLUMN f%s TO f%s''' % (
+ table_name, orig_id, target_id))
+ if 'f%s_display' % orig_id in existing_fields:
+ cur.execute('''ALTER TABLE %s RENAME COLUMN f%s_display TO f%s_display''' % (
+ table_name, orig_id, target_id))
+ if 'f%s_structured' % orig_id in existing_fields:
+ cur.execute('''ALTER TABLE %s RENAME COLUMN f%s_structured TO f%s_structured''' % (
+ table_name, orig_id, target_id))
+ sql.drop_views(None, conn, cur)
+ conn.commit()
+ formdef.store()
+
+sql.migrate_views(conn, cur)
+conn.commit()
+cur.close()