-
Notifications
You must be signed in to change notification settings - Fork 1
/
sync_occupancies.py
53 lines (40 loc) · 1.21 KB
/
sync_occupancies.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
from __future__ import print_function
from database import olddb, db
import database.orm as m
import time
OldReservation = olddb.orm.accom_guide_reservation
old_ballot_id = 12
new_ballot_id = 2015
def update():
os = olddb.Session()
ns = db.Session()
ballot = ns.query(m.BallotSeason).get(new_ballot_id)
reserves = os.query(OldReservation).filter(OldReservation.ballot == old_ballot_id)
for r in reserves:
room = ns.query(m.Room).get(r.room)
if not room:
print("ER: room {} does not exist".format(r.room))
continue
user = ns.query(m.Person).get(r.person)
if not user:
print("EU: user {} does not exist".format(r.person))
continue
listing = room.listing_for.get(ballot)
if not listing:
print("EL: no listing for room {} ({})".format(r.room, room.pretty_name()))
continue
if not any(user == o.resident for o in listing.occupancies):
listing.occupancies.append(m.Occupancy(
resident=user,
chosen_at=r.ts_chosen
))
print("B: {:6s} -> {:3d} ({})".format(user.crsid, room.id, room.pretty_name()))
ns.commit()
ns.close()
os.close()
while True:
try:
update()
except Exception as e:
print("EE: {}".format(e))
time.sleep(30)