forked from aronsky/parking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datamodel.py
194 lines (171 loc) · 6.37 KB
/
datamodel.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
from shared import *
from google.appengine.ext import db
from google.appengine.api import users
class Car(db.Model):
plate = db.IntegerProperty(required=True)
make = db.StringProperty(required=True)
model = db.StringProperty(required=True)
color = db.StringProperty(required=True, choices=set(["red","green","blue","white","turquoise","gray","black","silver","brown","yellow","orange","other"]))
owner = db.UserProperty(required=True, auto_current_user_add=True)
def __eq__(self, other):
if isinstance(other, Car):
return self.plate == other.plate
else:
raise NotImplementedError()
def __ne__(self, other):
if isinstance(other, Car):
return self.plate != other.plate
else:
raise NotImplementedError()
@staticmethod
def Store(plate, make, model, color):
car = Car.get_or_insert(str(plate), plate=plate, model=model, make=make, color=color)
car.plate = plate
car.make = make
car.model = model
car.color = color
car.put()
@staticmethod
def Delete(plate):
car = Car.get(db.Key.from_path("Car", str(plate)))
if car.owner == users.get_current_user():
# Delete all references to the car by leaving all the spots
for spot in list(Spot.all().filter("future = ", False)):
if spot.car == car:
spot.Leave()
if spot.reserved and spot.reserved.car == car:
spot.Reserve(False)
car.delete()
@staticmethod
def GuestCar():
return Car.get_or_insert(
str(GUEST_PLATE),
plate=GUEST_PLATE,
model="Guest",
make="Guest",
color="other"
)
def prettyplate(self):
result = str(self.plate)
if len(result) == 7:
return result[:2] + '-' + result[2:5] + '-' + result[-2:]
elif len(result) == 6:
return result[:3] + '-' + result[-3]
else:
return result
class Spot(db.Model):
number = db.IntegerProperty(required=True)
free = db.BooleanProperty()
reserved = db.SelfReferenceProperty(collection_name='future_set')
car = db.ReferenceProperty(reference_class=Car)
comments = db.StringProperty()
future = db.BooleanProperty()
outside = db.BooleanProperty()
def Take(self, car, comments=""):
if not self.free:
raise SpotTakenException("The spot is taken!")
if not car:
raise ParkingException("Can't reserve for None car!")
self.free = False
self.car = car
self.comments = comments
self.put()
def Leave(self):
if self.free:
SpotNotTakenException()
self.car = None
self.comments = ""
self.free = True
self.put()
def Reserve(self, reserve, car=None, comments=""):
if reserve:
if not car:
car = Car.GuestCar()
if car != Car.GuestCar():
comments = ""
if not self.reserved:
futurespot = Spot(number=self.number, free=False, reserved=None, car=car, comments=comments, future=True, outside=self.outside)
futurespot.put()
self.reserved = futurespot
else:
raise SpotReservedException()
else:
if self.reserved:
self.reserved.delete()
self.reserved = None
else:
raise SpotNotReservedException()
self.put()
def Release(self):
if not self.free:
self.Leave()
if self.reserved:
self.Take(self.reserved.car, self.reserved.comments)
self.Reserve(False)
class UserData(db.Model):
user = db.UserProperty(required=True, auto_current_user_add=True)
themename = db.StringProperty(required=True, choices=set(["nativedroid"]), default="nativedroid")
subtheme = db.StringProperty(required=True, choices=set(["light", "dark"]), default="light")
themecolor = db.StringProperty(required=True, choices=set(["blue", "green", "purple", "red", "yellow"]), default="blue")
mustGetSpot = db.BooleanProperty(required=True, default=False)
multipleSpots = db.BooleanProperty(required=True, default=False)
@staticmethod
def GetTheme():
try:
cfg = list(UserData.all().filter("user = ", users.get_current_user()))[0]
except:
cfg = UserData()
cfg.put()
return cfg.themename, cfg.subtheme, cfg.themecolor
@staticmethod
def SetTheme(themename, subtheme, themecolor):
try:
cfg = list(UserData.all().filter("user = ", users.get_current_user()))[0]
except:
cfg = UserData()
cfg.themename = themename
cfg.subtheme = subtheme
cfg.themecolor = themecolor
cfg.put()
@staticmethod
def GetMustGetSpot():
try:
cfg = list(UserData.all().filter("user = ", users.get_current_user()))[0]
return cfg.mustGetSpot
except:
return False
@staticmethod
def GetMultipleSpots():
try:
cfg = list(UserData.all().filter("user = ", users.get_current_user()))[0]
return cfg.multipleSpots
except:
return False
@staticmethod
def MigrateUserDataSchema():
updated = []
for cfg in UserData.all():
try:
cfg.mustGetSpot = cfg.mustGetSpot
cfg.multipleSpots = cfg.multipleSpots
except:
cfg.mustGetSpot = False
cfg.multipleSpots = False
updated.append(cfg)
if updated:
db.put(updated)
return len(updated)
class TossReservation(db.Model):
preferSpotOutside = db.BooleanProperty()
car = db.ReferenceProperty(reference_class=Car)
def Reserve(self, reserve, car=None, preferSpotOutside = False):
if reserve: #join the toss
if not car:
car = Car.GuestCar()
if car != Car.GuestCar():
comments = ""
self.car = car
self.preferSpotOutside = preferSpotOutside
self.put()
else: #unjoin toss
self.delete()