From 670c698b7746173d56200befcd11ed2909502709 Mon Sep 17 00:00:00 2001 From: tlaveille Date: Tue, 10 Feb 2026 10:17:45 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20de=20bug=20et=20optimisation=20d'ex=C3=A9?= =?UTF-8?q?cution.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CenRa_FLUX/flux_editor.py | 270 ++++++++++++++++++++------------------ 1 file changed, 140 insertions(+), 130 deletions(-) diff --git a/CenRa_FLUX/flux_editor.py b/CenRa_FLUX/flux_editor.py index 53e9847..1e9f17d 100644 --- a/CenRa_FLUX/flux_editor.py +++ b/CenRa_FLUX/flux_editor.py @@ -188,7 +188,7 @@ class Flux_Editor(QDialog, EDITOR_CLASS): con = psycopg2.connect("host=" + host + " port=" + port + " dbname=" + dbtype + " user=" + user + " password=" + mdp) cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor) self.combobox_custom() - # self.initialisation_flux() + self.initialisation_flux() # Create the dialog with elements (after translation) and keep reference # Only create GUI ONCE in callback, so that it will only load when the plugin is started if self.first_start is True: @@ -212,7 +212,7 @@ class Flux_Editor(QDialog, EDITOR_CLASS): con = psycopg2.connect("host=" + host + " port=" + port + " dbname=" + dbtype + " user=" + user + " password=" + mdp) cur = con.cursor(cursor_factory=psycopg2.extras.DictCursor) self.combobox_custom() - # self.initialisation_flux() + self.initialisation_flux() # Create the dialog with elements (after translation) and keep reference # Only create GUI ONCE in callback, so that it will only load when the plugin is started if self.first_start is True: @@ -231,111 +231,114 @@ class Flux_Editor(QDialog, EDITOR_CLASS): self.tableWidget_2.removeRow(self.tableWidget_2.currentRow()) def initialisation_flux(self): - print('run') - if dbtype == sigdb: - # self.toolButton.setEnabled(1) - if self.comboBox.currentText() == 'toutes les catégories': - custom_list = schemaname_list - elif self.comboBox.currentText() == 'travaux': - custom_list = """(SELECT schemaname,tablename from pg_catalog.pg_tables - where schemaname like '""" + str(self.comboBox.currentText()) + """%' order by schemaname,tablename) UNION (SELECT schemaname,matviewname AS tablename FROM pg_catalog.pg_matviews where schemaname like '""" + str(self.comboBox.currentText()) + """%' order by schemaname,tablename) order by schemaname,tablename;""" - else: - custom_list = """(SELECT schemaname,tablename from pg_catalog.pg_tables - where schemaname like '\\_""" + str(self.comboBox.currentText()) + """%' order by schemaname,tablename) UNION (SELECT schemaname,matviewname AS tablename FROM pg_catalog.pg_matviews where schemaname like '\\_""" + str(self.comboBox.currentText()) + """%' order by schemaname,tablename) order by schemaname,tablename;""" - else: - if self.comboBox.currentText() == 'toutes les catégories': - custom_list = schemaname_list_ref - else: - custom_list = """SELECT schemaname, tablename from pg_catalog.pg_tables WHERE schemaname LIKE '""" + str(self.comboBox.currentText()) + """' AND tablename NOT LIKE 'qgis_projects' order by schemaname, tablename;""" - cur.execute(custom_list) - list_schema = cur.fetchall() - - SQLcountRaster = """SELECT schemaname,viewname FROM pg_catalog.pg_views - WHERE schemaname LIKE 'public' AND viewname LIKE 'raster_columns';""" - cur.execute(SQLcountRaster) - RasterIF = len(cur.fetchall()) - - if RasterIF == 1: - SQLloadRaster = """SELECT concat(r_table_schema,'.',r_table_name) from public.raster_columns; """ - cur.execute(SQLloadRaster) - list_raster = cur.fetchall() - RasterList = [] - for rasterFind in list_raster: - RasterList.append(rasterFind[0]) - else: - RasterList = [] - - SQLprojects = """SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE tablename LIKE 'qgis_projects'""" - cur.execute(SQLprojects) - list_projects = cur.fetchall() - list_projects_qgis = [] - if len(list_projects) <= 1: - for ProjectName in list_projects: - SQLProjectsQgis = """SELECT name, metadata, content FROM """ + '"' + ProjectName[0] + '"."' + ProjectName[1] + '"' - cur.execute(SQLProjectsQgis) - list_projects_qgis.append(cur.fetchall()) - - if self.comboBox.currentText() == 'toutes les catégories': - SQLGrands = """SELECT concat(table_schema,'.',table_name) FROM information_schema.role_table_grants WHERE grantee in(SELECT rolname FROM pg_catalog.pg_roles WHERE oid in(SELECT roleid FROM pg_auth_members WHERE member = (SELECT usesysid FROM pg_catalog.pg_user WHERE usename = '""" + user + """'))) and privilege_type = 'SELECT';""" - else: - SQLGrands = """SELECT concat(table_schema,'.',table_name) FROM information_schema.role_table_grants WHERE grantee in(SELECT rolname FROM pg_catalog.pg_roles WHERE oid in(SELECT roleid FROM pg_auth_members WHERE member = (SELECT usesysid FROM pg_catalog.pg_user WHERE usename = '""" + user + """'))) and privilege_type = 'SELECT' AND table_schema LIKE '_""" + str(self.comboBox.currentText()) + """_%';""" - cur.execute(SQLGrands) - list_grands = cur.fetchall() - GrandUser = [] - for grandsFind in list_grands: - GrandUser.append(grandsFind[0]) - - self.tableWidget.setRowCount(len(list_schema)) - self.tableWidget.setColumnCount(4) - i = 0 - for value in list_schema: + if NoSignals == 0: + # print('Initiaisation de la connection a la DB: ' + dbtype) if dbtype == sigdb: - type_val = str(value[0])[1:3] - schema_name = str(value[0])[4:] - table_name = str(value[1][len(value[0]) + 1:]) - if value[0] == value[1][:len(value[0])]: - table_name = str(value[1][len(value[0]) + 1:]) + # self.toolButton.setEnabled(1) + if self.comboBox.currentText() == 'toutes les catégories': + custom_list = schemaname_list + elif self.comboBox.currentText() == 'travaux': + custom_list = """(SELECT schemaname,tablename from pg_catalog.pg_tables + where schemaname like '""" + str(self.comboBox.currentText()) + """%' order by schemaname,tablename) UNION (SELECT schemaname,matviewname AS tablename FROM pg_catalog.pg_matviews where schemaname like '""" + str(self.comboBox.currentText()) + """%' order by schemaname,tablename) order by schemaname,tablename;""" else: - table_name = str(value[1]) + custom_list = """(SELECT schemaname,tablename from pg_catalog.pg_tables + where schemaname like '\\_""" + str(self.comboBox.currentText()) + """%' order by schemaname,tablename) UNION (SELECT schemaname,matviewname AS tablename FROM pg_catalog.pg_matviews where schemaname like '\\_""" + str(self.comboBox.currentText()) + """%' order by schemaname,tablename) order by schemaname,tablename;""" else: - type_val = '' - schema_name = str(value[0]) - table_name = str(value[1]) - if type_val == 'fo': - type_val = str(value[0])[1:5] - schema_name = str(value[0])[6:] - if value[0] == value[1][:len(value[0])]: - table_name = str(value[1][len(value[0]) + 1:]) + if self.comboBox.currentText() == 'toutes les catégories': + custom_list = schemaname_list_ref else: - table_name = str(value[1]) - elif type_val == 'ra': - type_val = 'travaux' - schema_name = str(value[0]) - table_name = str(value[1]) - elif type_val != '00' and type_val != '01' and type_val != '07' and type_val != '26' and type_val != '42' and type_val != '69' and type_val != 'ra': - type_val = 'agregation' - schema_name = str(value[0]) - table_name = str(value[1]) + custom_list = """SELECT schemaname, tablename from pg_catalog.pg_tables WHERE schemaname LIKE '""" + str(self.comboBox.currentText()) + """' AND tablename NOT LIKE 'qgis_projects' order by schemaname, tablename;""" + cur.execute(custom_list) + list_schema = cur.fetchall() - if (str(value[0]) + '.' + str(value[1])) in RasterList: - SVG = 'mIconRaster.svg' + SQLcountRaster = """SELECT schemaname,viewname FROM pg_catalog.pg_views + WHERE schemaname LIKE 'public' AND viewname LIKE 'raster_columns';""" + cur.execute(SQLcountRaster) + RasterIF = len(cur.fetchall()) + + if RasterIF == 1: + SQLloadRaster = """SELECT concat(r_table_schema,'.',r_table_name) from public.raster_columns; """ + cur.execute(SQLloadRaster) + list_raster = cur.fetchall() + RasterList = [] + for rasterFind in list_raster: + RasterList.append(rasterFind[0]) else: - SVG = 'mIconVecteur.svg' + RasterList = [] - itemIcon = QTableWidgetItem() - icon = QIcon() - icon.addPixmap(QtGui.QPixmap(resources_path('icons', SVG)), QIcon.Mode(0), QIcon.State(1)) - itemIcon.setIcon(icon) + SQLprojects = """SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE tablename LIKE 'qgis_projects'""" + cur.execute(SQLprojects) + list_projects = cur.fetchall() + list_projects_qgis = [] + if len(list_projects) <= 1: + for ProjectName in list_projects: + SQLProjectsQgis = """SELECT name, metadata, content FROM """ + '"' + ProjectName[0] + '"."' + ProjectName[1] + '"' + cur.execute(SQLProjectsQgis) + list_projects_qgis.append(cur.fetchall()) - self.tableWidget.setItem(i, 0, itemIcon) - item = QTableWidgetItem(type_val) - self.tableWidget.setItem(i, 1, item) - item = QTableWidgetItem(schema_name) - self.tableWidget.setItem(i, 2, item) - item = QTableWidgetItem(table_name) - self.tableWidget.setItem(i, 3, item) + if self.comboBox.currentText() == 'toutes les catégories': + SQLGrands = """SELECT concat(table_schema,'.',table_name) FROM information_schema.role_table_grants WHERE grantee in(SELECT rolname FROM pg_catalog.pg_roles WHERE oid in(SELECT roleid FROM pg_auth_members WHERE member = (SELECT usesysid FROM pg_catalog.pg_user WHERE usename = '""" + user + """'))) and privilege_type = 'SELECT';""" + else: + if dbtype == sigdb: + SQLGrands = """SELECT concat(table_schema,'.',table_name) FROM information_schema.role_table_grants WHERE grantee in(SELECT rolname FROM pg_catalog.pg_roles WHERE oid in(SELECT roleid FROM pg_auth_members WHERE member = (SELECT usesysid FROM pg_catalog.pg_user WHERE usename = '""" + user + """'))) and privilege_type = 'SELECT' AND table_schema LIKE '_""" + str(self.comboBox.currentText()) + """_%';""" + elif dbtype == refdb: + SQLGrands = """SELECT concat(table_schema,'.',table_name) FROM information_schema.role_table_grants WHERE grantee in(SELECT rolname FROM pg_catalog.pg_roles WHERE oid in(SELECT roleid FROM pg_auth_members WHERE member = (SELECT usesysid FROM pg_catalog.pg_user WHERE usename = '""" + user + """'))) and privilege_type = 'SELECT' AND table_schema LIKE '""" + str(self.comboBox.currentText()) + """%';""" + cur.execute(SQLGrands) + list_grands = cur.fetchall() + GrandUser = [] + for grandsFind in list_grands: + GrandUser.append(grandsFind[0]) + self.tableWidget.setRowCount(len(list_schema)) + self.tableWidget.setColumnCount(4) + i = 0 + for value in list_schema: + if dbtype == sigdb: + type_val = str(value[0])[1:3] + schema_name = str(value[0])[4:] + table_name = str(value[1][len(value[0]) + 1:]) + if value[0] == value[1][:len(value[0])]: + table_name = str(value[1][len(value[0]) + 1:]) + else: + table_name = str(value[1]) + else: + type_val = '' + schema_name = str(value[0]) + table_name = str(value[1]) + if type_val == 'fo': + type_val = str(value[0])[1:5] + schema_name = str(value[0])[6:] + if value[0] == value[1][:len(value[0])]: + table_name = str(value[1][len(value[0]) + 1:]) + else: + table_name = str(value[1]) + elif type_val == 'ra': + type_val = 'travaux' + schema_name = str(value[0]) + table_name = str(value[1]) + elif type_val != '00' and type_val != '01' and type_val != '07' and type_val != '26' and type_val != '42' and type_val != '69' and type_val != 'ra': + type_val = 'agregation' + schema_name = str(value[0]) + table_name = str(value[1]) - if dbtype == sigdb: + if (str(value[0]) + '.' + str(value[1])) in RasterList: + SVG = 'mIconRaster.svg' + else: + SVG = 'mIconVecteur.svg' + + itemIcon = QTableWidgetItem() + icon = QIcon() + icon.addPixmap(QtGui.QPixmap(resources_path('icons', SVG)), QIcon.Mode(0), QIcon.State(1)) + itemIcon.setIcon(icon) + + self.tableWidget.setItem(i, 0, itemIcon) + item = QTableWidgetItem(type_val) + self.tableWidget.setItem(i, 1, item) + item = QTableWidgetItem(schema_name) + self.tableWidget.setItem(i, 2, item) + item = QTableWidgetItem(table_name) + self.tableWidget.setItem(i, 3, item) + + # if dbtype == sigdb: if (str(value[0]) + '.' + str(value[1])) in GrandUser: pass else: @@ -344,45 +347,48 @@ class Flux_Editor(QDialog, EDITOR_CLASS): self.tableWidget.item(i, j).setBackground(QtGui.QColor(187, 134, 192, 50)) self.tableWidget.item(i, j).setToolTip('Droit insuffisant pour ouvrire la couche !') - i = i + 1 + i = i + 1 - if self.comboBox.currentText() == 'toutes les catégories': - SQLprojects = """SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE tablename LIKE 'qgis_projects'""" - else: - SQLprojects = """SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE tablename LIKE 'qgis_projects' AND schemaname LIKE '""" + str(self.comboBox.currentText()) + """';""" - cur.execute(SQLprojects) - list_projects = cur.fetchall() + if self.comboBox.currentText() == 'toutes les catégories': + SQLprojects = """SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE tablename LIKE 'qgis_projects'""" + else: + SQLprojects = """SELECT schemaname, tablename FROM pg_catalog.pg_tables WHERE tablename LIKE 'qgis_projects' AND schemaname LIKE '""" + str(self.comboBox.currentText()) + """';""" + cur.execute(SQLprojects) + list_projects = cur.fetchall() - list_projects_qgis = [] - if len(list_projects) >= 1: - for ProjectName in list_projects: - SQLProjectsQgis = """SELECT name, metadata, content FROM """ + '"' + ProjectName[0] + '"."' + ProjectName[1] + '"' - cur.execute(SQLProjectsQgis) - list_projects_qgis = cur.fetchall() - for Project in list_projects_qgis: - self.tableWidget.setRowCount(i + 1) - itemIcon = QTableWidgetItem() - icon = QIcon() - icon.addPixmap(QtGui.QPixmap(resources_path('icons', 'mIconQgis.svg')), QIcon.Mode(0), QIcon.State(1)) - itemIcon.setIcon(icon) - self.tableWidget.setItem(i, 0, itemIcon) + list_projects_qgis = [] + if len(list_projects) >= 1: + for ProjectName in list_projects: + SQLProjectsQgis = """SELECT name, metadata, content FROM """ + '"' + ProjectName[0] + '"."' + ProjectName[1] + '"' + cur.execute(SQLProjectsQgis) + list_projects_qgis = cur.fetchall() + for Project in list_projects_qgis: + self.tableWidget.setRowCount(i + 1) + itemIcon = QTableWidgetItem() + icon = QIcon() + icon.addPixmap(QtGui.QPixmap(resources_path('icons', 'mIconQgis.svg')), QIcon.Mode(0), QIcon.State(1)) + itemIcon.setIcon(icon) + self.tableWidget.setItem(i, 0, itemIcon) - item = QTableWidgetItem('qgis') - self.tableWidget.setItem(i, 1, item) + item = QTableWidgetItem('qgis') + self.tableWidget.setItem(i, 1, item) - item = QTableWidgetItem(ProjectName[0]) - self.tableWidget.setItem(i, 2, item) + item = QTableWidgetItem(ProjectName[0]) + self.tableWidget.setItem(i, 2, item) - item = QTableWidgetItem(Project[0]) - self.tableWidget.setItem(i, 3, item) + item = QTableWidgetItem(Project[0]) + self.tableWidget.setItem(i, 3, item) - i = i + 1 + i = i + 1 - self.tableWidget.setColumnWidth(0, 20) - self.tableWidget.setColumnWidth(1, 70) - self.tableWidget.setColumnWidth(2, 300) - self.tableWidget.setColumnWidth(3, 300) - self.tableWidget.setHorizontalHeaderLabels(["Type", "Code", "Schema", "Table"]) + self.tableWidget.setColumnWidth(0, 20) + self.tableWidget.setColumnWidth(1, 70) + self.tableWidget.setColumnWidth(2, 300) + self.tableWidget.setColumnWidth(3, 300) + self.tableWidget.setHorizontalHeaderLabels(["Type", "Code", "Schema", "Table"]) + + if self.lineEdit.text() != 'Recherche par mots-clés': + self.filtre_dynamique(self.lineEdit.text()) def selection_flux(self): selected_row = 0 @@ -554,6 +560,9 @@ class Flux_Editor(QDialog, EDITOR_CLASS): self.QMBquestion = QMessageBox.question(iface.mainWindow(), u"Attention !", "La couche «" + str(self.tableWidget_2.item(row, 1).text()) + ' ' + str(self.tableWidget_2.item(row, 2).text()) + "» ne ce trouve pas dans cette BD !", QMessageBox.StandardButton(0x00004000)) def combobox_custom(self): + # print('Chargement de la list pour la BD: ' + dbtype) + global NoSignals + NoSignals = 1 if dbtype == sigdb: self.toolButton.setEnabled(1) self.comboBox.clear() @@ -577,6 +586,7 @@ class Flux_Editor(QDialog, EDITOR_CLASS): self.comboBox.addItem("toutes les catégories") for baxval in list_schema: self.comboBox.addItem(baxval[0]) + NoSignals = 0 def filtre_dynamique(self, filter_text): if filter_text.find(' ') >= 0: