-
Notifications
You must be signed in to change notification settings - Fork 15
/
dataimport.py
285 lines (249 loc) · 11.9 KB
/
dataimport.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
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
277
278
279
280
281
282
283
284
# -*- coding: utf-8 -*-
from data import Meeting, Person, ScoutGroup, Semester, Troop, TroopPerson, UserPrefs
from progress import TaskProgress
from google.appengine.ext import ndb
import logging
import scoutnet
import urllib.error
def RunScoutnetImport(groupid, api_key, user, semester, result):
"""
:type groupid: str
:type api_key: str
:type user: data.UserPrefs
:type semester: data.Semester
:type result: data.TaskProgress
:rtype: bool
"""
result.info('Importerar för termin %s' % semester.getname())
if groupid is None or groupid == "" or api_key is None or api_key == "":
result.error(u"Du måste ange både kårid och api nyckel")
return False
try:
data = scoutnet.GetScoutnetMembersAPIJsonData(groupid, api_key)
except urllib.error.HTTPError as e:
result.error(u"Kunde inte läsa medlemmar från scoutnet, fel:%s" % (str(e)))
if e.code == 401:
result.error(u"Kontrollera: api nyckel och kårid. Se till att du har rollen 'Medlemsregistrerare', och möjligen 'Webbansvarig' i scoutnet")
return False
importer = ScoutnetImporter(result)
success = importer.DoImport(data, semester)
if not success:
return False
if user.activeSemester != semester.key:
user.activeSemester = semester.key
result.append('Sätter %s till vald termin.' % semester.getname())
user.put()
# Don't Connect the group if the user is an admin
if user.hasadminaccess:
return True
# Don't Connect the group if the user already has an connection
if user.groupaccess is not None:
return True
user.groupaccess = importer.importedScoutGroup_key
user.hasaccess = True
user.groupadmin = True
user.put()
if user.groupadmin:
result.append(u"Du är kåradmin och kan dela ut tillgång till din kår för andra användare")
return True
class ScoutnetImporter:
commit = True
rapportID = 1
importedScoutGroup_key = None
result = None
def __init__(self, result):
"""
:type result: data.TaskProgress
"""
self.result = result
self.commit = True
self.rapportID = 1
def GetOrCreateTroop(self, name, troop_id, group_key, semester_key):
if len(name) == 0:
return None
troop = Troop.get_by_id(Troop.getid(troop_id, group_key, semester_key), use_memcache=True)
if troop != None:
if troop.name != name:
troop.name = name
if self.commit:
troop.put()
else:
self.result.append("Ny avdelning %s, ID=%s" % (name, troop_id))
troop = Troop.create(name, troop_id, group_key, semester_key)
troop.rapportID = self.rapportID # TODO: should check the highest number in the sgroup, will work for full imports
troop.scoutnetID = int(troop_id)
self.rapportID += 1
if self.commit:
troop.put()
if troop.scoutnetID != int(troop_id):
troop.scoutnetID = int(troop_id)
self.result.append("Nytt ID=%d för avdelning %s" % (troop.scoutnetID, name))
troop.put()
return troop
def GetOrCreateGroup(self, name, scoutnetID):
if len(name) == 0:
return None
group = ScoutGroup.get_by_id(ScoutGroup.getid(name), use_memcache=True)
if group == None:
self.result.append(u"Ny kår %s, id=%s" % (name, str(scoutnetID)))
group = ScoutGroup.create(name, scoutnetID)
group.scoutnetID = scoutnetID
if self.commit:
group.put()
if group.scoutnetID != scoutnetID:
group.scoutnetID = scoutnetID
if self.commit:
group.put()
self.importedScoutGroup_key = group.key
return group
def DoImport(self, data, semester):
"""
:param data: from scoutnet
:type data: str
:type semester: data.Semester
:rtype bool
"""
if not self.commit:
self.result.append("*** sparar inte, test mode ***")
if data == None or len(data) < 80:
self.result.error(u"ingen data från scoutnet")
return False
list = scoutnet.GetScoutnetDataListJson(data)
self.result.append("antal personer=%d" % len(list))
if len(list) < 1:
self.result.error(u"för få rader: %d st" % len(list))
return False
personsToSave = []
troopPersonsToSave = []
troopPersonsKeysToRemove = []
activePersonIds = set() # person.ids that was seen in this import
leadersToAddToTroops = [] # tuple: (troop_id, person)
allTroops = {} # dict: int(troop_id): troop
for p in list:
person_id = int(p["id"]) # type: int
personnr = p["personnr"].replace('-', '')
person = Person.getByMemberNo(person_id)
if len(personnr) < 12:
self.result.warning("%d %s %s har inte korrekt personnummer: '%s'" % (person_id, p["firstname"], p["lastname"], personnr))
if person != None:
person_id = person.key.id()
person.firstname = p["firstname"]
person.lastname = p["lastname"]
person.setpersonnr(personnr)
if person.notInScoutnet != None:
person.notInScoutnet = False
else:
person = Person.create(
person_id,
p["firstname"],
p["lastname"],
personnr)
self.result.append("Ny person:%s %s %s" % (str(person_id), p["firstname"], p["lastname"]))
if person.key.id() in activePersonIds:
self.result.error(f"{person.key.id()}/{p['member_no']} skickas flera gånger från scoutnet")
continue
activePersonIds.add(person.key.id())
person.removed = False
person.patrool = p["patrool"]
person.email = p["email"]
person.member_no = p["member_no"]
person.phone = p["phone"]
person.mobile = p["mobile"]
person.alt_email = p["contact_alt_email"]
person.mum_name = p["mum_name"]
person.mum_email = p["mum_email"]
person.mum_mobile = p["mum_mobile"]
person.dad_name = p["dad_name"]
person.dad_email = p["dad_email"]
person.dad_mobile = p["dad_mobile"]
person.street = p["street"]
person.zip_code = p["zip_code"]
person.zip_name = p["zip_name"]
person.troop_roles = p["troop_roles"]
person.group_roles = p["group_roles"]
if semester.year not in person.member_years:
person.member_years.append(semester.year)
person._dirty = True
scoutgroup = self.GetOrCreateGroup(p["group"], p["group_id"])
person.scoutgroup = scoutgroup.key
if len(p["troop"]) == 0:
self.result.warning(u"Ingen avdelning vald för %s %s %s" % (str(person.member_no), p["firstname"], p["lastname"]))
troop = self.GetOrCreateTroop(p["troop"], p["troop_id"], scoutgroup.key, semester.key)
if troop is not None:
allTroops[troop.scoutnetID] = troop
if person._dirty:
self.result.append(u"Sparar ändringar:%s %s %s" % (str(person.member_no), p["firstname"], p["lastname"]))
if self.commit:
personsToSave.append(person)
if troop != None:
tps = TroopPerson.get_troop_persons(troop.key, person.key)
tp = None
tpCount = len(tps)
if tpCount >= 1:
tp = tps[0]
if tpCount > 1: # this is a bugfix for duplicate TroopPerson
self.result.append(f"Tar bort {tpCount-1} extra avdelningsperson(er) i {p['troop']} för {p['firstname']} {p['lastname']}")
troopPersonsKeysToRemove.extend([t.key for t in tps[1:]])
if tp is None:
tp = TroopPerson.create(troop.key, person.key)
if tp:
troopPersonsToSave.append(tp)
self.result.append(u"Ny avdelning '%s' för:%s %s" % (p["troop"], p["firstname"], p["lastname"]))
# collect leaders that are assigned to troops in scoutnet
roles = p['roles']
if "value" in roles:
values = roles["value"]
if len(values) > 0:
for name, value in values.items():
if name == "troop":
for troop_id, troop_roles in value.items():
for troop_role_id in troop_roles:
if int(troop_role_id) in scoutnet.Roles.Troop_leader_role_set:
leadersToAddToTroops.append((int(troop_id), person))
break
# pass 2, add leaders to troops according to roles
for troop_leader in leadersToAddToTroops:
troop_id = troop_leader[0]
person = troop_leader[1]
troop = allTroops.get(troop_id)
if troop is None:
troop = Troop.getById(troop_id, semester.key)
if troop is not None:
tp = None
# search among the TroopPersons we are about to add to the database
for tpcandidate in troopPersonsToSave:
if tpcandidate.troop == troop.key and tpcandidate.person == person.key:
tpcandidate.leader = True
tp = tpcandidate
break
if tp is None:
# try finding the TroopPerson in the database and set to leader
tps = TroopPerson.get_troop_persons(troop.key, person.key)
tpCount = len(tps)
if tpCount >= 1:
tp = tps[0]
if tpCount > 1: # this is a bugfix for duplicate TroopPerson
self.result.append(f"Tar bort {tpCount-1} extra avdelningsperson(er) i {troop.getname()} för {person.getname()}")
troopPersonsKeysToRemove.extend([t.key for t in tps[1:]])
if tp is None:
tp = TroopPerson.create(troop.key, person.key)
tp.leader = True
troopPersonsToSave.append(tp)
self.result.append(u"Avdelning '%s' ny ledare:%s" % (troop.getname(), person.getname()))
# check if old persons are still members, mark persons not imported in this import session as removed
if len(personsToSave) > 0: # protect agains a failed import, with no persons marking everyone as removed
previousPersons = Person.query(Person.scoutgroup == scoutgroup.key, Person.removed != True)
for previousPersonKey in previousPersons.iter(keys_only=True):
if previousPersonKey.id() not in activePersonIds:
personToMarkAsRemoved = previousPersonKey.get()
personToMarkAsRemoved.removed = True
self.result.append(u"%s finns inte i scoutnet, markeras som borttagen. id:%s" % (personToMarkAsRemoved.getname(), str(previousPersonKey.id())))
if personToMarkAsRemoved in personsToSave:
raise Exception('A removed person cannot be in that list')
personsToSave.append(personToMarkAsRemoved)
if self.commit:
if len(troopPersonsKeysToRemove) > 0:
ndb.delete_multi(troopPersonsKeysToRemove)
ndb.put_multi(personsToSave)
ndb.put_multi(troopPersonsToSave)
return True