1

I tried to create a QGIS action in order to refresh several materialized views at once. for that I wanted to execute a simple loop in my action in order to recover the database where the view will be refreshed at each iteration. I then did an if / else because the path is not the same depending on the database. However, n executing the code in the python console nothing happens, the code is read well, without errors, but it looks like the loop is not executed. when I run a simpler refresh, from a single view the code works, but at the slightest loop nothing, even with a very simple code in the loop. I would like to understand what mistakes I made in this action.

Here is the full action:

import psycopg2

databases = ['RIP_FTTH_17', 'RIP_FTTH_24', 'RIP_FTTH_33', 'RIP_FTTH_40', 'RIP_FTTH_47', 'RIP_FTTH_79', 'CEM_ORANGE', 'AMEL_47']

for db in databases:
     conn = psycopg2.connect(dbname=db,user="postgres",password="INEO_Infracom_33",host="bordeaux04")
     cur = conn.cursor()
     if db not in ['CEM_ORANGE', 'AMEL_47']:
          sql = "REFRESH MATERIALIZED VIEW genie_civil.vm_appui_avancement;"
     else:
          sql = "REFRESH MATERIALIZED VIEW geofibre_cem.vm_appui_avancement;"
     cur.execute(sql)
     iface.messageBar().pushMessage("Success ", " Mise à jour reussie !")
     conn.commit()
     cur.close()
     conn.close()

here's the action with a simpler loop that doesn't work either:

    import psycopg2

    databases = ["RIP_FTTH_17", "RIP_FTTH_24", "RIP_FTTH_33", "RIP_FTTH_40", "RIP_FTTH_47", "RIP_FTTH_79", "CEM_ORANGE", "AMEL_47"]

    for db in databases:
         conn = psycopg2.connect(dbname="RIP_FTTH_17",user="postgres",password="INEO_Infracom_33",host="bordeaux04")
         cur = conn.cursor()
         sql = "REFRESH MATERIALIZED VIEW genie_civil.vm_appui_avancement;"
         cur.execute(sql)
         iface.messageBar().pushMessage("Success ", " Mise à jour reussie !")
         conn.commit()
         cur.close()
         conn.close()
5
  • Refresh materialized views I would do else where like crontab, pgAgent or task scheduler. But if you want to use an action, did you set the action type to Python, does you code run as a standalone script, did you look at the default Python actions coming with QGIS?
    – Jakob
    Nov 27, 2020 at 10:42
  • thanks for the response. this action is useful because the data is often updated and a manual update is necessary to have the data up to date at all times. So I update my info suddenly, sorry I wasn't precise enough. it looks like the code is correct, because although it does not execute the way i would like in the console the action does execute in the attribute table, but not on the layer with the button. I put logs in the code, if the action is launched in the table all the logs are displayed, if I launch from the layer, nothing, no log. Nov 27, 2020 at 10:53
  • Have you tried to pan the map to trigger a redraw after action mview update. Or some code like layer.dataProvider().forceReload() layer.triggerRepaint()
    – Jakob
    Nov 27, 2020 at 12:03
  • precisely, the problem is that the action by clicking on the button does not start, pgadmin confirms that no request is running, while refreshing the 8 views takes about a minute. so I'm trying to figure out why the button on the diaper doesn't trigger the action. Nov 27, 2020 at 13:16
  • Well does the default Python action 'Get feature id' run?
    – Jakob
    Nov 27, 2020 at 13:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.