-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDownloadTeams.py
182 lines (153 loc) · 6.26 KB
/
DownloadTeams.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os, json, traceback, time
from qgis.core import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import *
from DlgWaiting import DlgWaiting
from GeosismaWindow import GeosismaWindow as gw
# SpatiaLite imports
from pyspatialite import dbapi2 as db
class DownloadTeams(DlgWaiting):
# signals
done = pyqtSignal(bool)
message = pyqtSignal(str, int)
def __init__(self, parent=None):
DlgWaiting.__init__(self, parent)
self.singleFinished = True
#self.setWindowModality(Qt.ApplicationModal)
def __del__(self):
try:
self.manager.finished.disconnect(self.replyFinished)
self.manager.authenticationRequired.disconnect(self.authenticationRequired)
except Exception:
pass
def run(self):
try:
# init progress bar
self.reset()
self.setRange( 0, 0 )
self.setWindowTitle( self.tr("Scarica i Sopralluoghi associati ai Team") )
QApplication.setOverrideCursor(Qt.WaitCursor)
# create db
self.downloadTeams()
# wait end of download
self.done.connect(self.setFinished)
while (not self.singleFinished):
qApp.processEvents()
time.sleep(0.1)
except Exception as e:
try:
traceback.print_exc()
except:
pass
self.done.emit(False)
self.message.emit(e.message, QgsMessageLog.CRITICAL)
raise e
finally:
QApplication.restoreOverrideCursor()
def setFinished(self, success):
self.singleFinished = True
def downloadTeams(self):
# get connection conf
settings = QSettings()
teamUrl = settings.value("/rt_geosisma_offline/teamUrl", "/api/v1/team/")
self.baseApiUrl = settings.value("/rt_geosisma_offline/baseApiUrl", "http://geosisma-test.faunalia.it/")
self.manager = QgsNetworkAccessManager.instance()
# clean listeners to avoid overlap
try:
self.manager.authenticationRequired.disconnect()
except:
pass
try:
self.manager.finished.disconnect()
except:
pass
# add new listeners
self.manager.finished.connect(self.replyFinished)
self.manager.authenticationRequired.connect(self.authenticationRequired)
request = QNetworkRequest()
url = QUrl(self.baseApiUrl + teamUrl)
url.addQueryItem("format", "json")
request.setUrl(url)
# start download
QApplication.setOverrideCursor(Qt.WaitCursor)
self.manager.get(request)
self.singleFinished = False
def authenticationRequired(self, reply, authenticator ):
if self is None:
return
# check if reached mas retry
gw.instance().authenticationRetryCounter += 1
if (gw.instance().authenticationRetryCounter % gw.instance().maxAuthenticationError) == 0:
gw.instance().authenticationRetryCounter = 0 # reset counter
message = self.tr("Autenticazione fallita piu' di %d volte" % gw.instance().maxAuthenticationError)
self.message.emit(message, QgsMessageLog.CRITICAL)
QMessageBox.critical(self, gw.MESSAGELOG_CLASS, message)
# abort continuing request
reply.abort()
self.done.emit(False)
return
# if not authenticated ask credentials
if not gw.instance().autenthicated:
(ok, gw.instance().user, gw.instance().pwd) = QgsCredentials.instance().get("", gw.instance().user, gw.instance().pwd, self.tr("Inserisci User e PWD della tua utenza Geosisma"))
if not ok: # MEANS PRESED CANCEL
gw.instance().authenticationRetryCounter = 0
reply.abort()
message = self.tr("Mancata autenticazione")
self.message.emit(message, QgsMessageLog.WARNING)
self.done.emit(False)
return
# do authentication
authenticator.setUser(gw.instance().user)
authenticator.setPassword(gw.instance().pwd)
def replyFinished(self, reply):
if self is None:
return
# need auth
if reply.error() == QNetworkReply.AuthenticationRequiredError:
gw.instance().autenthicated = False
# do again until authenticated or reached max retry
self.manager.get(reply.request())
return
# received error
if reply.error():
message = self.tr("Errore nella HTTP Request: %d - %s" % (reply.error(), reply.errorString()) )
self.message.emit(message, QgsMessageLog.WARNING)
self.done.emit(False)
return
# well authenticated :)
gw.instance().autenthicated = True
gw.instance().authenticationRetryCounter = 0
from json import loads
raw = reply.readAll()
try:
json = loads(raw.data())
except Exception as e:
try:
traceback.print_exc()
except:
pass
self.done.emit(False)
return
# check if return more than 20 elements (e.g. for the super user)
if "objects" in json:
jsonTeams = json["objects"] # get array of dicts
else:
jsonTeams = [json]
for team in jsonTeams:
gw.instance().downloadedTeams.append(team)
# manage get of other elements if available
if "meta" in json:
if "next" in json["meta"]:
nextUrl = json["meta"]["next"]
if nextUrl:
self.message.emit(nextUrl, QgsMessageLog.INFO)
request = QNetworkRequest()
url = QUrl(self.baseApiUrl + nextUrl)
request.setUrl(url)
self.manager.get(request)
return
# successfully end
self.manager.finished.disconnect(self.replyFinished)
self.manager.authenticationRequired.disconnect(self.authenticationRequired)
self.done.emit(True)