From 651f1118395499f10da4e16fdbf2567213c1642d Mon Sep 17 00:00:00 2001 From: Emmanuel Viennet Date: Mon, 27 Sep 2021 22:54:23 +0200 Subject: [PATCH] =?UTF-8?q?ignore=20inscriptions=20r=C3=A9p=C3=A9t=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/scodoc/notesdb.py | 22 ++++++++++++++++++--- app/scodoc/sco_formsemestre_inscriptions.py | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/scodoc/notesdb.py b/app/scodoc/notesdb.py index 99c4ca9cb..5a53c544c 100644 --- a/app/scodoc/notesdb.py +++ b/app/scodoc/notesdb.py @@ -88,7 +88,15 @@ def SimpleDictFetch(query, args, cursor=None): return cursor.dictfetchall() -def DBInsertDict(cnx, table, vals, commit=0, convert_empty_to_nulls=1, return_id=True): +def DBInsertDict( + cnx, + table, + vals, + commit=0, + convert_empty_to_nulls=1, + return_id=True, + ignore_conflicts=False, +): """insert into table values in dict 'vals' Return: id de l'object créé """ @@ -103,13 +111,18 @@ def DBInsertDict(cnx, table, vals, commit=0, convert_empty_to_nulls=1, return_id fmt = ",".join(["%%(%s)s" % col for col in cols]) # print 'insert into %s (%s) values (%s)' % (table,colnames,fmt) oid = None + if ignore_conflicts: + ignore = " ON CONFLICT DO NOTHING" + else: + ignore = "" try: if vals: cursor.execute( - "insert into %s (%s) values (%s)" % (table, colnames, fmt), vals + "insert into %s (%s) values (%s)%s" % (table, colnames, fmt, ignore), + vals, ) else: - cursor.execute("insert into %s default values" % table) + cursor.execute("insert into %s default values%s" % (table, ignore)) if return_id: cursor.execute(f"SELECT CURRVAL('{table}_id_seq')") # id créé oid = cursor.fetchone()[0] @@ -291,6 +304,7 @@ class EditableTable(object): fields_creators={}, # { field : [ sql_command_to_create_it ] } filter_nulls=True, # dont allow to set fields to null filter_dept=False, # ajoute selection sur g.scodoc_dept_id + insert_ignore_conflicts=False, ): self.table_name = table_name self.id_name = id_name @@ -311,6 +325,7 @@ class EditableTable(object): self.filter_nulls = filter_nulls self.filter_dept = filter_dept self.sql_default_values = None + self.insert_ignore_conflicts = insert_ignore_conflicts def create(self, cnx, args): "create object in table" @@ -336,6 +351,7 @@ class EditableTable(object): vals, commit=True, return_id=(self.id_name is not None), + ignore_conflicts=self.insert_ignore_conflicts, ) return new_id diff --git a/app/scodoc/sco_formsemestre_inscriptions.py b/app/scodoc/sco_formsemestre_inscriptions.py index 7d42578d2..7f069a614 100644 --- a/app/scodoc/sco_formsemestre_inscriptions.py +++ b/app/scodoc/sco_formsemestre_inscriptions.py @@ -55,6 +55,7 @@ _formsemestre_inscriptionEditor = ndb.EditableTable( "formsemestre_inscription_id", ("formsemestre_inscription_id", "etudid", "formsemestre_id", "etat", "etape"), sortkey="formsemestre_id", + insert_ignore_conflicts=True, )