-
Notifications
You must be signed in to change notification settings - Fork 5
/
PostNAS_Logging.py
123 lines (101 loc) · 4.51 KB
/
PostNAS_Logging.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
import os, getpass, datetime
from qgis.PyQt.QtCore import QSettings
from qgis.PyQt.QtSql import QSqlDatabase, QSqlQuery
from .PostNAS_AccessControl import PostNAS_AccessControl
from qgis.core import *
import qgis.core
import json
class PostNAS_Logging:
def __init__(self):
self.db = self.__loadDB()
self.accessControl = PostNAS_AccessControl()
self.username = getpass.getuser()
def logEigentuemerList(self,search,result):
if(self.__checkLoggingActive() == True) :
if(self.__checkLoggingTableExists() == False):
self.__createLoggingTable()
self.__insertLogEntry("eigentuemerList",search,result)
def logEigentuemerFlurstueck(self,search,result):
if(self.__checkLoggingActive() == True) :
if(self.__checkLoggingTableExists() == False):
self.__createLoggingTable()
self.__insertLogEntry("flurstueckList",search,result)
def __insertLogEntry(self,requestType,search,result):
self.__openDB()
sql = "INSERT INTO public.postnas_search_logging (datum,username,requestType,search,result) VALUES (:datum,:username,:requestType,:search,:result)"
query = QSqlQuery(self.db)
if (self.dbSchema.lower() != "public"):
sql = sql.replace("public.", self.dbSchema + ".")
query.prepare(sql)
query.bindValue(":datum",datetime.datetime.now().isoformat())
query.bindValue(":username",self.username)
query.bindValue(":requestType",requestType)
query.bindValue(":search",search)
query.bindValue(":result",str(result).replace("u'","'").replace("\'","\"").replace("[","{").replace("]","}"))
query.exec_()
if(query.lastError().number() == -1):
return True
else:
return False
def __checkLoggingActive(self):
return True
def __checkLoggingTableExists(self):
sql = "SELECT table_name FROM information_schema.tables WHERE table_name = 'postnas_search_logging'";
self.__openDB()
query = QSqlQuery(self.db)
query.exec_(sql)
if(query.size() > 0):
return True
else:
return False
def __createLoggingTable(self):
file_path = os.path.dirname(os.path.realpath(__file__)) + "/create_loggingtable/create_logging_table.sql"
sql = open(file_path).read()
if (self.dbSchema.lower() != "public"):
sql = sql.replace("public.", self.dbSchema + ".")
self.__openDB()
query = QSqlQuery(self.db)
query.exec_(sql)
if(query.lastError().number() == -1):
return True
else:
return False
def __loadDB(self):
if os.path.isfile(os.path.dirname(os.path.realpath(__file__)) + '\config.json'):
with open(os.path.dirname(os.path.realpath(__file__)) + '\config.json') as config_file:
config = json.load(config_file)
dbHost = config['db']['host']
dbDatabasename = config['db']['database']
self.dbSchema = config['db']['schema']
dbPort = config['db']['port']
dbUsername = config['db']['user']
dbPassword = config['db']['password']
authcfg = config['authcfg']
else:
settings = QSettings("PostNAS", "PostNAS-Suche")
dbHost = settings.value("host", "")
dbDatabasename = settings.value("dbname", "")
self.dbSchema = settings.value("schema", "public")
dbPort = settings.value("port", "5432")
dbUsername = settings.value("user", "")
dbPassword = settings.value("password", "")
authcfg = settings.value( "authcfg", "" )
if authcfg != "" and hasattr(qgis.core,'QgsAuthManager'):
amc = qgis.core.QgsAuthMethodConfig()
if hasattr(qgis.core, "QGis"):
qgis.core.QgsAuthManager.instance().loadAuthenticationConfig( authcfg, amc, True)
else:
QgsApplication.instance().authManager().loadAuthenticationConfig( authcfg, amc, True)
dbUsername = amc.config( "username", dbUsername )
dbPassword = amc.config( "password", dbPassword )
db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName(dbHost)
db.setPort(int(dbPort))
db.setDatabaseName(dbDatabasename)
db.setUserName(dbUsername)
db.setPassword(dbPassword)
return db
def __openDB(self):
if(self.db.isOpen() == False):
self.db.open()