-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
266 lines (193 loc) · 7.49 KB
/
models.py
File metadata and controls
266 lines (193 loc) · 7.49 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
from flask_mongoengine import MongoEngine
from configuration import db, app
from flask import request
import datetime
from notification import send_announcement_update, send_event_update, send_mentor_update, broadcast_apns
from pbkdf2 import pbkdf2_hex
import os, binascii
class EventLocation(db.Document):
LOCATION_IMAGE_PATH = 'location/image/'
name = db.StringField()
map = db.FileField()
rank = db.IntField(default=-1)
def dictionary_representation(self):
if request is not None and request.url_root:
map_url = "https://api.menlohacks.com/" + self.LOCATION_IMAGE_PATH + str(self.id)
dictionary = {
'name' : self.name,
'map' : map_url,
'id' : str(self.id)
}
else:
dictionary = {
'name' : self.name,
'id' : str(self.id)
}
if self.rank > 0:
dictionary['rank'] = self.rank
dictionary['is_primary'] = True
else:
dictionary['is_primary'] = False
return dictionary
def __unicode__(self):
return self.name
class Event(db.Document):
start_time = db.DateTimeField(default=datetime.datetime.now())
end_time = db.DateTimeField(default=datetime.datetime.now())
short_description = db.StringField()
long_description = db.StringField()
location = db.ReferenceField(EventLocation, required=False)
def save(self):
super(Event, self).save()
send_event_update(event=self)
def dictionary_representation(self):
return {
'start_time' : self.start_time.isoformat(),
'end_time' : self.end_time.isoformat(),
'short_description' : self.short_description,
'long_description' : self.long_description,
'location' : self.location.dictionary_representation(),
'id' : str(self.id)
}
def __unicode__(self):
return self.long_description
class Announcement(db.Document):
message = db.StringField()
time = db.DateTimeField(default=datetime.datetime.now())
push_notification_sent = db.BooleanField(default=False)
def save(self):
push_sent = self.push_notification_sent
self.push_notification_sent = True
super(Announcement, self).save()
if push_sent == False:
broadcast_apns(self)
self.push_notification_sent = True
send_announcement_update(announcement=self)
def dictionary_representation(self):
return {
'message' : self.message,
'time' : self.time.isoformat(),
'id': str(self.id)
}
def __unicode__(self):
return self.title
from itsdangerous import (TimedJSONWebSignatureSerializer
as Serializer, BadSignature, SignatureExpired)
class User(db.Document):
username = db.StringField(unique=True)
hashed_password = db.StringField()
name = db.StringField()
school = db.StringField()
photo_form_url = db.StringField()
liability_form_url = db.StringField()
tshirt_size = db.StringField()
check_in_times = db.ListField(db.DateTimeField())
check_out_times = db.ListField(db.DateTimeField())
checked_in = db.BooleanField(default=False)
is_admin = db.BooleanField(default=False)
def dictionary_representation(self):
return {
'username' : self.username,
'name' : self.name
}
def check_in_dictionary_representation(self):
return {
'username' : self.username,
'name' : self.name,
'tshirt_size' : self.tshirt_size,
'photo_form' : self.photo_form_url,
'liability_form' : self.liability_form_url,
}
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return self.username
def set_password(self, password):
prefix = "pbkdf2_hex"
salt = binascii.b2a_hex(os.urandom(64))
rounds = 10000
hashed = pbkdf2_hex(str(password), str(salt.decode("hex")), rounds, 64)
self.hashed_password = "$".join([str(prefix), str(rounds), str(hashed), str(salt)])
def generate_auth_token(self, expiration=604800):
s = Serializer(app.config['SECRET_KEY'], expires_in = expiration)
return s.dumps({ 'username': self.username })
@staticmethod
def verify_auth_token(token):
s = Serializer(app.config['SECRET_KEY'])
try:
data = s.loads(token)
except SignatureExpired:
return None # valid token, but ex
# pired
except BadSignature:
return None # invalid token
user = User.objects(username=data['username']).first()
return user
@staticmethod
def validate_login(password_hash, password):
password_components = password_hash.split("$")
return pbkdf2_hex(str(password), str(password_components[3].decode("hex")), int(password_components[1]), 64) == str(password_components[2])
def __unicode__(self):
return self.username
meta = {
'strict': False,
'indexes': [
{'fields': ['username'], 'unique': True}
]
}
class MentorTicket(db.Document):
EXPIRATION_TIME = 3600 #automatically expire after 60 minutes.
description = db.StringField()
location = db.StringField()
contact = db.StringField()
claimed_by = db.ReferenceField(User, required=False)
created_by = db.ReferenceField(User)
time_created = db.DateTimeField(default=datetime.datetime.now())
time_opened = db.DateTimeField(default=datetime.datetime.now())
time_claimed = db.DateTimeField()
time_complete = db.DateTimeField()
def save(self):
super(MentorTicket, self).save()
send_mentor_update(ticket=self)
def dictionary_representation_simple(self):
current_time = datetime.datetime.now()
expiry_time = self.time_opened + datetime.timedelta(seconds=MentorTicket.EXPIRATION_TIME)
dictionary = {
'description': self.description,
'location': self.location,
'contact': self.contact,
'claimed': self.claimed_by != None,
'expired': current_time > expiry_time,
'time_created': self.time_created.isoformat(),
'id': str(self.id),
}
if self.time_complete is None:
dictionary['time_complete'] = None
else:
dictionary['time_complete'] = self.time_complete.isoformat()
return dictionary
def dictionary_representation(self):
from authentication import current_user
current_time = datetime.datetime.now()
expiry_time = self.time_opened + datetime.timedelta(seconds=MentorTicket.EXPIRATION_TIME)
user = current_user()
dictionary = {
'description' : self.description,
'location' : self.location,
'contact' : self.contact,
'claimed' : self.claimed_by != None,
'claimed_by_me' : self.claimed_by == user,
'expired' : current_time > expiry_time,
'time_created' : self.time_created.isoformat(),
'id' : str(self.id),
'is_mine' : self.created_by == user
}
if self.time_complete is None:
dictionary['time_complete'] = None
else:
dictionary['time_complete'] = self.time_complete.isoformat()
return dictionary