From 47811943c1543526b29ce0f78b50c22c65847ac3 Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Tue, 17 Aug 2021 08:49:19 +0200 Subject: [PATCH] fonction d'anonymisation --- app/models/raw_sql_init.py | 12 ++++++++++++ app/scodoc/sco_dump_db.py | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/raw_sql_init.py b/app/models/raw_sql_init.py index 0e57c3aee..54f01cf9f 100644 --- a/app/models/raw_sql_init.py +++ b/app/models/raw_sql_init.py @@ -35,6 +35,18 @@ BEGIN END LOOP; END; $$ LANGUAGE plpgsql; + +-- Fonction pour anonymisation: +-- inspirée par https://www.simononsoftware.com/random-string-in-postgresql/ +CREATE FUNCTION random_text_md5( integer ) returns text + LANGUAGE SQL + AS $$ + select upper( substring( (SELECT string_agg(md5(random()::TEXT), '') + FROM generate_series( + 1, + CEIL($1 / 32.)::integer) + ), 1, $1) ); + $$; """ ) db.session.commit() diff --git a/app/scodoc/sco_dump_db.py b/app/scodoc/sco_dump_db.py index 529afca01..4f793d5fa 100644 --- a/app/scodoc/sco_dump_db.py +++ b/app/scodoc/sco_dump_db.py @@ -205,7 +205,9 @@ def _drop_ano_db(ano_db_name): """drop temp database if it exists""" existing_databases = [ s.split("|")[0].strip() - for s in subprocess.check_output(["psql", "-l"]).split("\n")[3:] + for s in subprocess.check_output(["psql", "-l"]) + .decode(scu.SCO_ENCODING) + .split("\n")[3:] ] if ano_db_name not in existing_databases: log("_drop_ano_db: no temp db, nothing to drop")