1
0
forked from ScoDoc/ScoDoc
ScoDoc/tests/demo/demo_reset_noms.py

77 lines
1.9 KiB
Python
Raw Normal View History

2020-09-26 16:19:37 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Outils pour environnements de démo.
2020-10-14 10:07:35 +02:00
Change aléatoirement les identites (nip, sexe, nom, prenom) des étudiants d'un semestre.
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).
Le sexe est choisi aléatoirement 50-50.
2020-09-26 16:19:37 +02:00
"""
import sys
import random
import psycopg2
2020-10-21 00:22:25 +02:00
from gen_nomprenoms import nomprenom
2020-10-14 10:07:35 +02:00
def usage():
print(f"Usage: {sys.argv[0]} dbname formsemestre_id")
sys.exit(1)
if len(sys.argv) != 3:
usage()
dbname = sys.argv[1]
formsemestre_id = sys.argv[2]
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
cursor.execute(
"select count(*) from notes_formsemestre where formsemestre_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.etudid
2020-09-26 16:19:37 +02:00
from identite i, notes_formsemestre_inscription ins
where i.etudid=ins.etudid and ins.formsemestre_id=%(formsemestre_id)s
2020-10-14 10:07:35 +02:00
""",
{"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:
2020-10-14 10:07:35 +02:00
sexe = random.choice(("M.", "MME"))
2020-09-26 16:19:37 +02:00
nom, prenom = nomprenom(sexe)
2020-10-14 10:07:35 +02:00
print(f"{etudid}: {nom}\t{prenom}")
args = {
"nom": nom,
"prenom": prenom,
"sexe": sexe,
"etudid": etudid,
"code_nip": random.randrange(10000000, 99999999),
}
2020-09-26 16:19:37 +02:00
req = "update identite set nom=%(nom)s, prenom=%(prenom)s, sexe=%(sexe)s where etudid=%(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)