forked from sbutler/spotseeker_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
258 lines (213 loc) · 9.78 KB
/
models.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
""" Copyright 2012, 2013 UW Information Technology, University of Washington
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from django.db import models
from django.core.exceptions import ValidationError
import hashlib
import datetime
import time
from wsgiref.handlers import format_date_time
import random
from PIL import Image
from cStringIO import StringIO
import oauth_provider.models
from django.core.cache import cache
class SpotType(models.Model):
""" The type of Spot.
"""
name = models.SlugField(max_length=50)
def __unicode__(self):
return self.name
class Spot(models.Model):
""" Represents a place for students to study.
"""
name = models.CharField(max_length=100, blank=True)
spottypes = models.ManyToManyField(SpotType, max_length=50, related_name='spots')
latitude = models.DecimalField(max_digits=11, decimal_places=8, null=True)
longitude = models.DecimalField(max_digits=11, decimal_places=8, null=True)
height_from_sea_level = models.DecimalField(max_digits=11, decimal_places=8, null=True, blank=True)
building_name = models.CharField(max_length=100, blank=True)
floor = models.CharField(max_length=50, blank=True)
room_number = models.CharField(max_length=25, blank=True)
capacity = models.IntegerField(null=True, blank=True)
display_access_restrictions = models.CharField(max_length=200, blank=True)
organization = models.CharField(max_length=50, blank=True)
manager = models.CharField(max_length=50, blank=True)
etag = models.CharField(max_length=40)
last_modified = models.DateTimeField(auto_now=True, auto_now_add=True)
def __unicode__(self):
return self.name
def save(self, *args, **kwargs):
if cache.get(self.pk):
cache.delete(self.pk)
self.etag = hashlib.sha1("{0} - {1}".format(random.random(), time.time())).hexdigest()
super(Spot, self).save(*args, **kwargs)
def rest_url(self):
return "/api/v1/spot/{0}".format(self.pk)
def json_data_structure(self):
spot_json = cache.get(self.pk)
if not spot_json:
extended_info = {}
info = SpotExtendedInfo.objects.filter(spot=self)
for attr in info:
extended_info[attr.key] = attr.value
available_hours = {
'monday': [],
'tuesday': [],
'wednesday': [],
'thursday': [],
'friday': [],
'saturday': [],
'sunday': [],
}
hours = SpotAvailableHours.objects.filter(spot=self).order_by('start_time')
for window in hours:
available_hours[window.get_day_display()].append([window.start_time.strftime("%H:%M"), window.end_time.strftime("%H:%M")])
images = []
spot_images = SpotImage.objects.filter(spot=self)
for img in spot_images:
images.append({
"id": img.pk,
"url": img.rest_url(),
"content-type": img.content_type,
"width": img.width,
"height": img.height,
"creation_date": format_date_time(time.mktime(img.creation_date.timetuple())),
"modification_date": format_date_time(time.mktime(img.modification_date.timetuple())),
"upload_user": img.upload_user,
"upload_application": img.upload_application,
"thumbnail_root": "{0}/thumb".format(img.rest_url()),
"description": img.description
})
types = []
spot_types = self.spottypes.all()
for t in spot_types:
types.append(t.name)
spot_json = {
"id": self.pk,
"uri": self.rest_url(),
"name": self.name,
"type": types,
"location": {
# If any changes are made to this location dict, MAKE SURE to reflect those changes in the
# location_descriptors list in views/schema_gen.py
"latitude": self.latitude,
"longitude": self.longitude,
"height_from_sea_level": self.height_from_sea_level,
"building_name": self.building_name,
"floor": self.floor,
"room_number": self.room_number,
},
"capacity": self.capacity,
"display_access_restrictions": self.display_access_restrictions,
"images": images,
"available_hours": available_hours,
"organization": self.organization,
"manager": self.manager,
"extended_info": extended_info,
"last_modified": self.last_modified.isoformat()
}
cache.add(self.pk, spot_json)
return spot_json
def delete(self, *args, **kwargs):
cache.delete(self.pk)
super(Spot, self).delete(*args, **kwargs)
class SpotAvailableHours(models.Model):
""" The hours a Spot is available, i.e. the open or closed hours for the building the spot is located in.
"""
spot = models.ForeignKey(Spot)
day = models.CharField(max_length=3, choices=(
('m', 'monday'),
('t', 'tuesday'),
('w', 'wednesday'),
('th', 'thursday'),
('f', 'friday'),
('sa', 'saturday'),
('su', 'sunday'),
))
start_time = models.TimeField()
end_time = models.TimeField()
class Meta:
verbose_name_plural = "Spot available hours"
def __unicode__(self):
return "%s: %s, %s-%s" % (self.spot.name, self.day, self.start_time, self.end_time)
def save(self, *args, **kwargs):
self.full_clean()
if self.start_time >= self.end_time:
raise Exception("Invalid time range - start time must be before end time")
other_hours = SpotAvailableHours.objects.filter(spot=self.spot, day=self.day).exclude(id=self.id)
for h in other_hours:
if h.start_time <= self.start_time <= h.end_time or self.start_time <= h.start_time <= self.end_time:
self.start_time = min(h.start_time, self.start_time)
self.end_time = max(h.end_time, self.end_time)
h.delete()
self.spot.save() # Update the spot's last_modified
super(SpotAvailableHours, self).save(*args, **kwargs)
class SpotExtendedInfo(models.Model):
""" Additional institution-provided metadata about a spot. If providing custom metadata, you should provide a validator for that data, as well.
"""
key = models.CharField(max_length=50)
value = models.CharField(max_length=255)
spot = models.ForeignKey(Spot)
class Meta:
verbose_name_plural = "Spot extended info"
def __unicode__(self):
return "%s[%s: %s]" % (self.spot.name, self.key, self.value)
def save(self, *args, **kwargs):
self.full_clean()
self.spot.save() # Update the last_modified on the spot
super(SpotExtendedInfo, self).save(*args, **kwargs)
class SpotImage(models.Model):
""" An image of a Spot. Multiple images can be associated with a Spot, and Spot objects have a 'Spot.spotimage_set' method that will return all SpotImage objects for the Spot.
"""
description = models.CharField(max_length=200, blank=True)
image = models.ImageField(upload_to="space_images", height_field="height",
width_field="width")
spot = models.ForeignKey(Spot)
content_type = models.CharField(max_length=40)
width = models.IntegerField()
height = models.IntegerField()
creation_date = models.DateTimeField(auto_now_add=True)
modification_date = models.DateTimeField(auto_now=True)
etag = models.CharField(max_length=40)
upload_user = models.CharField(max_length=40)
upload_application = models.CharField(max_length=100)
def __unicode__(self):
if self.description:
return "%s" % self.description
else:
return "%s" % self.image.name
def save(self, *args, **kwargs):
self.etag = hashlib.sha1("{0} - {1}".format(random.random(),
time.time())).hexdigest()
content_types = {"JPEG": "image/jpeg", "GIF": "image/gif",
"PNG": "image/png"}
if self.image.file.multiple_chunks():
img = Image.open(self.image.file.temporary_file_path())
else:
img = Image.open(self.image)
if not img.format in content_types:
raise ValidationError('Not an accepted image format')
self.content_type = content_types[img.format]
super(SpotImage, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
self.etag = hashlib.sha1("{0} - {1}".format(random.random(), time.time())).hexdigest()
self.image.delete(save=False)
super(SpotImage, self).delete(*args, **kwargs)
def rest_url(self):
return "{0}/image/{1}".format(self.spot.rest_url(), self.pk)
class TrustedOAuthClient(models.Model):
consumer = models.ForeignKey(oauth_provider.models.Consumer)
is_trusted = models.BooleanField()
class Meta:
verbose_name_plural = "Trusted OAuth clients"
def __unicode__(self):
return self.consumer.name