ScoDoc-PE/misc/change_enseignant.py

58 lines
1.4 KiB
Python
Raw Normal View History

2020-09-26 16:19:37 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Change un identifiant d'enseignant (pour corriger une erreur, typiquement un doublon)
(à lancer en tant qu'utilisateur postgres)
Emmanuel Viennet, 2007 - 2014
"""
2021-07-09 21:44:39 +02:00
from __future__ import print_function
2020-09-26 16:19:37 +02:00
import pdb, os, sys
import psycopg2
2021-07-09 21:44:39 +02:00
from six.moves import input
2020-09-26 16:19:37 +02:00
if len(sys.argv) != 4:
2021-07-09 21:44:39 +02:00
print("Usage: %s database ancien_utilisateur nouvel_utilisateur" % sys.argv[0])
print("Exemple: change_enseignant.py SCOGEII toto tata")
2020-09-26 16:19:37 +02:00
sys.exit(1)
dbname = sys.argv[1]
OLD_ID = sys.argv[2]
NEW_ID = sys.argv[3]
2021-07-09 21:44:39 +02:00
DBCNXSTRING = "dbname=%s" % dbname
2020-09-26 16:19:37 +02:00
# Confirmation
2021-07-09 21:44:39 +02:00
ans = input(
"Remplacer le l'utilisateur %s par %s dans toute la base du departement %s ?"
% (OLD_ID, NEW_ID, dbname)
).strip()
if not ans or ans[0].lower() not in "oOyY":
print("annulation")
2020-09-26 16:19:37 +02:00
sys.exit(-1)
2021-07-09 21:44:39 +02:00
cnx = psycopg2.connect(DBCNXSTRING)
2020-09-26 16:19:37 +02:00
cursor = cnx.cursor()
req = "update %s set %s=%%(new_id)s where %s=%%(old_id)s"
2021-07-09 21:44:39 +02:00
args = {"old_id": OLD_ID, "new_id": NEW_ID}
2020-09-26 16:19:37 +02:00
tables_attr = {
2021-07-09 21:44:39 +02:00
"notes_formsemestre": "responsable_id",
"entreprise_contact": "enseignant",
"admissions": "rapporteur",
"notes_moduleimpl": "responsable_id",
"notes_modules_enseignants": "ens_id",
"notes_notes": "uid",
"notes_notes_log": "uid",
"notes_appreciations": "author",
}
2020-09-26 16:19:37 +02:00
for (table, attr) in tables_attr.items():
cursor.execute(req % (table, attr, attr), args)
2021-07-09 21:44:39 +02:00
print("table %s: %s" % (table, cursor.statusmessage))
2020-09-26 16:19:37 +02:00
cnx.commit()