-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostgres91plusauditor.py
78 lines (65 loc) · 3.51 KB
/
postgres91plusauditor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from qgis.core import QgsMapLayerRegistry, QgsAction
from PyQt4.QtCore import QUrl
from PyQt4.QtGui import QAction, QIcon, QDesktopServices
from gui.auditdialog import AuditDialog
from gui.loggedactionstablechooserdialog import LoggedActionsTableChooserDialog
actionName = "History audit"
pluginName = "postgres91plusauditor"
import resources
# run manually qgis.utils.plugins["postgres91plusauditor"].audit("district20130425093453834",1)
class Postgres91plusAuditor():
def __init__(self, iface):
self.iface = iface
QgsMapLayerRegistry.instance().layersAdded.connect(self.addLayersActions)
self.addLayersActions()
def initGui(self):
# log layer chooser
self.connectLayerAction = QAction(QIcon(":/plugins/postgres91plusauditor/icons/connect.png"),
"Define logged actions table", self.iface.mainWindow())
self.connectLayerAction.triggered.connect(self.showLogLayerChooser)
self.iface.addPluginToMenu("&Postgres 91 plus Auditor", self.connectLayerAction)
# show history action
self.auditAction = QAction(QIcon(":/plugins/postgres91plusauditor/icons/qaudit-64.png"),
"Audit logged actions", self.iface.mainWindow())
self.auditAction.triggered.connect(self.audit)
self.iface.addToolBarIcon(self.auditAction)
self.iface.addPluginToMenu("&Postgres 91 plus Auditor", self.auditAction)
# help action
self.helpAction = QAction(QIcon(":/plugins/postgres91plusauditor/icons/help.png"),
"Help", self.iface.mainWindow())
self.helpAction.triggered.connect(lambda: QDesktopServices.openUrl(QUrl("http://3nids.github.io/postgres91plusauditor/")))
self.iface.addPluginToMenu("&Postgres 91 plus Auditor", self.helpAction)
def unload(self):
self.iface.removePluginMenu("&Postgres 91 plus Auditor", self.connectLayerAction)
self.iface.removePluginMenu("&Postgres 91 plus Auditor", self.auditAction)
self.iface.removePluginMenu("&Postgres 91 plus Auditor", self.helpAction)
self.iface.removeToolBarIcon(self.auditAction)
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
if layer.dataProvider().name() == "postgres":
while True:
actions = layer.actions()
for i in range(actions.size()):
if actions[i].name() == actionName:
actions.removeAction(i)
continue
break
def showLogLayerChooser(self):
LoggedActionsTableChooserDialog().exec_()
def audit(self, layerId=None, featureId=None):
if layerId is False:
layerId = None
self.auditDlg = AuditDialog(self.iface, layerId, featureId)
self.auditDlg.show()
def addLayersActions(self):
for layerid, layer in QgsMapLayerRegistry.instance().mapLayers().iteritems():
if layer.dataProvider().name() == "postgres":
actionExists = False
actions = layer.actions()
for i in range(actions.size()):
if actions[i].name() == actionName:
actionExists = True
break
if actionExists:
continue
actionStr = "qgis.utils.plugins['%s'].audit('%s',[%% $id %%])" % (pluginName, layerid)
actions.addAction(QgsAction.GenericPython, actionName, actionStr)