-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
executable file
·276 lines (253 loc) · 11.1 KB
/
Copy pathdatabase.py
File metadata and controls
executable file
·276 lines (253 loc) · 11.1 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import datetime
from kivy.factory import Factory
from kivy.storage.jsonstore import JsonStore
import uuid
import json
import requests
from kivy.logger import Logger
class DataBase:
base_url = 'https://osptest-3ddc5.firebaseio.com/'
url = ''
secret = ''
admin_password = ''
def __init__(self, path, heroes_path, password_path, trucks_path, version):
self.version = version
self.heroes_path = heroes_path
self.password_path = password_path
self.trucks_path = trucks_path
self.path = path
self.store = JsonStore(path)
with open("login_data/login", 'r') as file:
self.user = file.read().split("\n")[0]
with open("login_data/secret", 'r') as file:
self.secret = file.read().split("\n")[0]
self.url = self.base_url + self.user + self.secret
self.firebase_patch_all()
self.update_osp_data()
def update_osp_data(self):
try:
string = str(requests.get(self.url).json()['heroes'])
if string:
with open(self.heroes_path, 'w') as file:
file.write(string)
except Exception as connection_error:
Logger.exception(str(connection_error))
try:
with open(self.heroes_path, 'r') as file:
self.heroes = json.loads(str(file.read()).replace("'", '"'))
except Exception as no_heroes_in_db:
Logger.exception(str(no_heroes_in_db))
self.heroes = ["brak strażaków w bazie"]
try:
string = str(requests.get(self.url).json()['trucks'])
if string:
with open(self.trucks_path, 'w') as file:
file.write(string)
except Exception as connection_error:
Logger.exception(str(connection_error))
try:
with open(self.trucks_path, 'r') as file:
self.trucks = json.loads(str(file.read()).replace("'", '"'))
except Exception as no_trucks_in_db:
Logger.exception(str(no_trucks_in_db))
self.trucks = ["brak zastepów w bazie"]
try:
string = str(requests.get(self.url).json()['passwd'])
if string:
with open(self.password_path, 'w') as file:
file.write(string)
except Exception as no_password_in_db:
Logger.exception(str(no_password_in_db))
try:
with open(self.password_path, 'r') as file:
self.admin_password = file.readline()
except Exception as no_password_file:
Logger.exception(str(no_password_file))
def update_reports_data(self):
result = self.firebase_patch_all()
if result == -1:
Factory.connectionPopout().open()
else:
self.update_osp_data()
Factory.updateOK().open()
def put_report(self, uuid_num, id_number, dep_date, dep_time, spot_time, location, type_of_action,
section_com, action_com, driver, perpetrator, victim, section, details,
return_date, end_time, home_time, meter_reading, km_location, completed, truck_num):
if uuid_num == "":
uuid_num = str(uuid.uuid1())
if section_com == "Dowódca sekcji":
section_com = ""
if action_com == "Dowódca akcji":
action_com = ""
if driver == "Kierowca":
driver = ""
if truck_num == "Zastęp":
truck_num = ""
self.store.put(uuid_num, innerID=id_number, depDate=dep_date, depTime=dep_time, spotTime=spot_time,
location=location, type=type_of_action,
sectionCom=section_com, actionCom=action_com, driver=driver, perpetrator=perpetrator,
victim=victim, section=section, details=details,
returnDate=return_date, endTime=end_time, homeTime=home_time, stanLicznika=meter_reading,
KM=km_location, modDate=self.get_date(), ready=completed, truck=truck_num)
try:
string = "{'" + uuid_num + "': " + str(self.store.get(uuid_num)) + "}"
to_database = json.loads(string.replace("'", '"'))
requests.patch(url=self.url, json=to_database)
return 0
except Exception as connection_error:
Logger.exception(str(connection_error))
return -1
def delete_report(self, uuid_num):
if self.store.exists(uuid_num):
try:
# requests.delete(url=self.url[:-5] + id_number + ".json")
string = '{"deleted-' + uuid_num + '": "true"}'
to_database = json.loads(string)
requests.patch(url=self.url, json=to_database)
self.store.delete(uuid_num)
except Exception as connection_error:
Factory.deletePopout().open()
Logger.exception(str(connection_error))
else:
return -1
def get_heroes(self):
return self.heroes.get("all")
def get_driver(self):
return self.heroes.get("driver")
def get_section_comm(self):
return self.heroes.get("section")
def get_action_comm(self):
return self.heroes.get("action")
def get_trucks(self):
return self.trucks
def firebase_patch_all(self):
try:
with open(self.path, 'r') as file:
data = file.read()
to_database = json.loads(data)
requests.patch(url=self.url, json=to_database)
to_database = json.loads('{"' + self.user + '": "' + self.version + '"}')
requests.patch(url=self.base_url + "mobileVersion/" + self.secret, json=to_database)
return 0
except Exception as connection_error:
Logger.exception(str(connection_error))
return -1
def delete_all(self):
string_all = ""
try:
for key in self.store.keys():
# requests.delete(url=self.url[:-5] + key + ".json")
string = '{"deleted-' + key + '": "true"}'
string_all = string_all + string
to_database = json.loads(string)
requests.patch(url=self.url, json=to_database)
self.store.clear()
except Exception as connection_error:
Factory.deletePopout().open()
Logger.exception(str(connection_error))
def get_report(self, id_number):
for item in self.store.find(innerID=id_number):
dane = self.store.get(item[0])
id_number = dane['innerID']
dep_date = dane['depDate']
dep_time = dane['depTime']
spot_time = dane['spotTime']
location = dane['location']
type_of_action = dane['type']
section_com = dane['sectionCom']
action_com = dane['actionCom']
driver = dane['driver']
perpetrator = dane['perpetrator']
victim = dane['victim']
section = dane['section']
details = dane['details']
return_date = dane['returnDate']
end_time = dane['endTime']
home_time = dane['homeTime']
meter_reading = dane['stanLicznika']
km_location = dane['KM']
date = dane['modDate']
completed = dane['ready']
truck_num = dane['truck']
return [item[0], id_number, dep_date, dep_time, spot_time, location, type_of_action, section_com,
action_com, driver, perpetrator, victim, section, details,
return_date, end_time, home_time, meter_reading, km_location, date, completed, truck_num]
else:
return -1
def get_all_friendly(self):
result = []
for item in self.store.keys():
data = self.store.get(item)
result.append(data["innerID"] + " " + data["location"])
return result
def find_inner_id(self, id_number):
for _ in self.store.find(innerID=id_number):
return True
return False
@staticmethod
def get_date():
return str(datetime.datetime.now()).split(".")[0]
def get_user(self):
return self.user
def get_password(self):
return self.admin_password
def change_osp(self, user, password):
with open("login_data/login", 'r') as file:
old_user = file.read().split("\n")[0]
if user != old_user:
try:
user_password = str(requests.get(self.base_url + "passwd/" + user + self.secret).json())
to_database = json.loads('{"' + user + '": "' + self.version + '"}')
requests.patch(url=self.base_url + "mobileVersion/" + self.secret, json=to_database)
except Exception as connection_error:
Logger.exception(str(connection_error))
return -2 # no connection
if user_password == "UPDATE":
return -3 # update neeeded
if user_password == "None" or password != user_password:
return -1 # wrong password
self.url = self.base_url + user + self.secret
try:
string = str(requests.get(self.url).json()['heroes'])
with open(self.heroes_path, 'w') as file:
file.write(string)
except Exception as connection_error:
Logger.exception(str(connection_error))
with open(self.heroes_path, 'w') as file:
file.write("")
try:
with open(self.heroes_path, 'r') as file:
self.heroes = json.loads(str(file.read()).replace("'", '"'))
except Exception as no_heroes_in_db:
Logger.exception(str(no_heroes_in_db))
self.heroes = ["brak strażaków w bazie"]
try:
string = str(requests.get(self.url).json()['trucks'])
with open(self.trucks_path, 'w') as file:
file.write(string)
except Exception as connection_error:
Logger.exception(str(connection_error))
try:
with open(self.trucks_path, 'r') as file:
self.trucks = json.loads(str(file.read()).replace("'", '"'))
except Exception as no_trucks_in_db:
Logger.exception(str(no_trucks_in_db))
self.trucks = ["brak zastepów w bazie"]
try:
string = str(requests.get(self.url).json()['passwd'])
with open(self.password_path, 'w') as file:
file.write(string)
except Exception as connection_error:
Logger.exception(str(connection_error))
self.url = self.base_url + self.user + self.secret
return -2
try:
with open(self.password_path, 'r') as file:
self.admin_password = file.readline()
except Exception as no_password:
Logger.exception(str(no_password))
self.store.clear()
self.user = user
with open("login_data/login", 'w') as file:
file.write(user)
return 0