2020-09-26 16:19:37 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""Outils pour environnements de démo.
|
|
|
|
|
2024-04-03 19:02:40 +02:00
|
|
|
Change aléatoirement les identites (nip, civilite, nom, prenom) des étudiants.
|
|
|
|
Un semestre ou tous si non spécifié.
|
2020-10-14 10:07:35 +02:00
|
|
|
|
2020-11-11 22:05:29 +01:00
|
|
|
Le NIP est choisi aléatoirement (nombre entier à 8 chiffres).
|
2020-10-14 10:07:35 +02:00
|
|
|
Les noms et prénoms sont issus des fichiers noms.txt, prenoms-h.txt, prenoms-f.txt
|
|
|
|
(ce sont simlement les plus fréquemment rencontrés en France).
|
2021-02-13 17:28:55 +01:00
|
|
|
La civilité est choisie aléatoirement 50-50 Homme/Femme.
|
2020-09-26 16:19:37 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import random
|
|
|
|
import psycopg2
|
|
|
|
|
2021-08-31 23:31:36 +02:00
|
|
|
from gen_nomprenoms import nomprenom
|
2020-10-21 00:22:25 +02:00
|
|
|
|
2020-10-14 10:07:35 +02:00
|
|
|
|
|
|
|
def usage():
|
2024-04-03 19:02:40 +02:00
|
|
|
print(f"Usage: {sys.argv[0]} dbname [formsemestre_id]")
|
2020-10-14 10:07:35 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2024-04-03 19:02:40 +02:00
|
|
|
if len(sys.argv) not in (2, 3):
|
2020-10-14 10:07:35 +02:00
|
|
|
usage()
|
|
|
|
|
|
|
|
dbname = sys.argv[1]
|
2024-04-03 19:02:40 +02:00
|
|
|
formsemestre_id = sys.argv[2] if len(sys.argv) > 2 else None
|
2020-10-14 10:07:35 +02:00
|
|
|
DBCNXSTRING = f"dbname={dbname}"
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
# Liste des etudiants inscrits à ce semestre
|
2020-10-14 10:07:35 +02:00
|
|
|
cnx = psycopg2.connect(DBCNXSTRING)
|
2020-09-26 16:19:37 +02:00
|
|
|
cursor = cnx.cursor()
|
2020-10-14 10:07:35 +02:00
|
|
|
|
2024-04-03 19:02:40 +02:00
|
|
|
if formsemestre_id is None:
|
|
|
|
cursor.execute("SELECT i.id from identite i")
|
|
|
|
else:
|
|
|
|
cursor.execute(
|
|
|
|
"select count(*) from notes_formsemestre where id=%(formsemestre_id)s",
|
|
|
|
{"formsemestre_id": formsemestre_id},
|
|
|
|
)
|
|
|
|
nsem = cursor.fetchone()[0]
|
|
|
|
if nsem != 1:
|
|
|
|
print(f"{nsem} formsemestre matching {formsemestre_id} in {dbname}")
|
|
|
|
sys.exit(2)
|
|
|
|
cursor.execute(
|
|
|
|
"""select i.id
|
|
|
|
from identite i, notes_formsemestre_inscription ins
|
|
|
|
where i.id=ins.etudid and ins.formsemestre_id=%(formsemestre_id)s
|
|
|
|
""",
|
|
|
|
{"formsemestre_id": formsemestre_id},
|
|
|
|
)
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
wcursor = cnx.cursor()
|
2020-10-14 10:07:35 +02:00
|
|
|
n = 0
|
2020-09-26 16:19:37 +02:00
|
|
|
for (etudid,) in cursor:
|
2021-02-13 17:28:55 +01:00
|
|
|
civilite = random.choice(("M", "F")) # pas de neutre, on pourrait ajouter 'X'
|
|
|
|
nom, prenom = nomprenom(civilite)
|
2020-10-14 10:07:35 +02:00
|
|
|
print(f"{etudid}: {nom}\t{prenom}")
|
|
|
|
args = {
|
|
|
|
"nom": nom,
|
|
|
|
"prenom": prenom,
|
2021-02-13 17:28:55 +01:00
|
|
|
"civilite": civilite,
|
2020-10-14 10:07:35 +02:00
|
|
|
"etudid": etudid,
|
|
|
|
"code_nip": random.randrange(10000000, 99999999),
|
|
|
|
}
|
2022-04-05 14:05:43 +02:00
|
|
|
req = "update identite set nom=%(nom)s, prenom=%(prenom)s, civilite=%(civilite)s where id=%(etudid)s"
|
2020-10-14 10:07:35 +02:00
|
|
|
# print( req % args)
|
|
|
|
wcursor.execute(req, args)
|
|
|
|
n += 1
|
|
|
|
|
2020-09-26 16:19:37 +02:00
|
|
|
|
|
|
|
cnx.commit()
|
|
|
|
cnx.close()
|
|
|
|
|
2020-10-14 10:07:35 +02:00
|
|
|
print(f"changed {n} identities", file=sys.stderr)
|