-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapi.py
352 lines (274 loc) · 11.9 KB
/
api.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# from django.db.models import Q
from tastypie.resources import ModelResource
from tastypie.constants import ALL, ALL_WITH_RELATIONS
from tastypie.cache import SimpleCache
from tastypie.cache import NoCache
from tastypie.authorization import Authorization
from tastypie.exceptions import Unauthorized
from tastypie import fields
from course_selection.models import (Nice_User, Schedule, Semester,
Course_Listing, Course, Section,
Meeting, Color_Palette, Professor,
Friend_Request)
def get_nice_user(user):
return Nice_User.objects.get(netid=user.username)
class UserAuthorization(Authorization):
def read_list(self, object_list, bundle):
# This assumes a ``QuerySet`` from ``ModelResource``.
filtered = [obj for obj in object_list if obj.netid ==
bundle.request.user.username]
if len(filtered) > 0:
return filtered
else:
raise Unauthorized("Sorry, no peeking!")
def read_detail(self, object_list, bundle):
# Is the requested object owned by the user?
if bundle.obj.netid == bundle.request.user.username:
return True
else:
raise Unauthorized("Sorry, no peeking!")
def create_list(self, object_list, bundle):
# Assuming they're auto-assigned to ``user``.
return [obj for obj in object_list if obj.netid == bundle.request.user.username]
def create_detail(self, object_list, bundle):
return bundle.obj.netid == bundle.request.user.username
def update_list(self, object_list, bundle):
return [obj for obj in object_list if obj.netid == bundle.request.user.username]
def update_detail(self, object_list, bundle):
return bundle.obj.netid == bundle.request.user.username
def delete_list(self, object_list, bundle):
# Sorry user, no deletes for you!
raise Unauthorized("Sorry, no deletes.")
def delete_detail(self, object_list, bundle):
raise Unauthorized("Sorry, no deletes.")
# you can only use it if you own this object
class UserObjectsOnlyAuthorization(Authorization):
def read_list(self, object_list, bundle):
filtered = [obj for obj in object_list if obj.user.netid ==
bundle.request.user.username]
return filtered
def read_detail(self, object_list, bundle):
# Is the requested object owned by the user?
if bundle.obj.user.netid == bundle.request.user.username:
return True
else:
raise Unauthorized("Sorry, no peeking!")
def create_list(self, object_list, bundle):
# Assuming they're auto-assigned to ``user``.
return [obj for obj in object_list if obj.user.netid == bundle.request.user.username]
def create_detail(self, object_list, bundle):
return bundle.obj.user.netid == bundle.request.user.username
def update_list(self, object_list, bundle):
return [obj for obj in object_list if obj.user.netid == bundle.request.user.username]
def update_detail(self, object_list, bundle):
return bundle.obj.user.netid == bundle.request.user.username
def delete_list(self, object_list, bundle):
# Sorry user, no deletes for you!
raise Unauthorized("Sorry, no deleting lists.")
# return [obj for obj in object_list if obj.user.netid == bundle.request.user.username]
# return object_list
def delete_detail(self, object_list, bundle):
return bundle.obj.user.netid == bundle.request.user.username
# you can see the objects if a) you are the owner OR b) you and the owner are friends
# currently this should apply to schedules
class UserObjectOrFriendAuthorization(UserObjectsOnlyAuthorization):
def is_owner_or_friend(self, user, owner):
"""
Returns true if user is either the owner, or related to the owner
via a Friend_Relationship.
"""
if user.username == owner.netid:
return True
nice_user = get_nice_user(user)
return nice_user.friends.filter(pk=owner.pk).exists()
def read_list(self, object_list, bundle):
filtered = [o for o in object_list if self.is_owner_or_friend(
bundle.request.user, o.user)]
return filtered
def read_detail(self, object_list, bundle):
# Is the requested object owned by the user?
if self.is_owner_or_friend(bundle.request.user, bundle.obj.user):
return True
else:
raise Unauthorized("Sorry, no peeking!")
class SemesterResource(ModelResource):
class Meta:
queryset = Semester.objects.all()
resource_name = 'semester'
excludes = ['']
allowed_methods = ['get']
authorization = Authorization()
filtering = {
'term_code': ALL
}
def dehydrate(self, bundle):
# give the semester a readable name
term_code = bundle.data['term_code']
end_year = int(term_code[1:3])
start_year = end_year - 1
if int(term_code[-1]) == 2:
sem = 'Fall'
else:
sem = 'Spring'
name = str(start_year) + '-' + str(end_year) + ' ' + sem
bundle.data['name'] = name
return bundle
def prepend_urls(self):
return [
# url(r"^(?P<resource_name>%s)/(?P<term_code>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
# url(r"^(?P<resource_name>%s)/(?P<term_code>[\w\d_.-]+)/course%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('get_course'), name="api_get_course"),
]
class CourseListingResource(ModelResource):
course = fields.ForeignKey('course_selection.api.CourseResource', 'course')
class Meta:
queryset = Course_Listing.objects.all()
resource_name = 'course_listing'
excludes = ['course', '']
allowed_methods = ['get']
cache = SimpleCache(timeout=10)
class CourseResource(ModelResource):
semester = fields.ForeignKey(SemesterResource, 'semester', full=True)
course_listings = fields.ToManyField(
CourseListingResource, 'course_listing_set', null=True, full=True)
sections = fields.ToManyField(
'course_selection.api.SectionResource', 'sections', full=True)
def alter_list_data_to_serialize(self, request, data):
if request.GET.get('meta_only'):
return {'meta': data['meta']}
return data
class Meta:
queryset = Course.objects.all()
resource_name = 'course'
excludes = ['']
allowed_methods = ['get']
cache = SimpleCache(timeout=10)
limit = 0
max_limit = 0
filtering = {
'semester': ALL_WITH_RELATIONS
}
class SectionResource(ModelResource):
course = fields.ForeignKey(CourseResource, 'course')
meetings = fields.ToManyField(
'course_selection.api.MeetingResource', 'meetings', full=True)
class Meta:
queryset = Section.objects.all()
resource_name = 'section'
excludes = ['isDefault']
allowed_methods = ['get']
cache = SimpleCache(timeout=10)
class MeetingResource(ModelResource):
section = fields.ForeignKey(SectionResource, 'section')
class Meta:
queryset = Meeting.objects.all()
resource_name = 'meeting'
excludes = ['']
allowed_methods = ['get']
cache = SimpleCache(timeout=10)
class ColorPaletteResource(ModelResource):
class Meta:
queryset = Color_Palette.objects.all()
resource_name = 'color_palette'
excludes = ['']
allowed_methods = ['get']
cache = SimpleCache(timeout=10)
class ScheduleResource(ModelResource):
#enrollments = fields.ToManyField(EnrollmentResource, 'enrollments', full=True, null=True)
semester = fields.ForeignKey(SemesterResource, 'semester', full=True)
user = fields.ForeignKey('course_selection.api.UserResource', 'user')
class Meta:
queryset = Schedule.objects.all()
resource_name = 'schedule'
excludes = ['user']
allowed_methods = ['get', 'post', 'put', 'delete']
cache = NoCache()
authorization = UserObjectOrFriendAuthorization()
always_return_data = True
limit = 0
max_limit = 0
filtering = {
'user': ALL_WITH_RELATIONS,
'semester': ALL_WITH_RELATIONS
}
def obj_create(self, bundle, **kwargs):
nice_user = get_nice_user(bundle.request.user)
return super(ScheduleResource, self).obj_create(bundle, user=nice_user)
# def apply_authorization_limits(self, request, object_list):
# return object_list.filter(Q(user__netid=request.user.username))
class FriendResource(ModelResource):
"""
This is a private resource used for saving Friend_Relationship.
"""
class Meta:
queryset = Nice_User.objects.all()
resource_name = 'friend'
excludes = ['password', 'resource_uri', 'last_login']
allowed_methods = ['get', 'post', 'put']
cache = SimpleCache(timeout=10)
limit = 0
max_limit = 0
# there is no authorization on this class on purpose--
# we want to be able to update a Nice_User object
# when we save a Friend_Relationship object
authorization = Authorization()
filtering = {
'netid': ALL_WITH_RELATIONS
}
class FriendRequestAuthorization(Authorization):
def user_is_in_this_request(self, rel, user):
return rel.from_user == user or rel.to_user == user
def authorize_detail(self, obj, bundle):
nice_user = get_nice_user(bundle.request.user)
return self.user_is_in_this_request(obj, nice_user)
def authorize_list(self, object_list, bundle):
return [o for o in object_list if self.authorize_detail(o, bundle)]
def read_list(self, object_list, bundle):
return self.authorize_list(object_list, bundle)
def read_detail(self, object_list, bundle):
return self.authorize_detail(bundle.obj, bundle)
def create_list(self, object_list, bundle):
raise Unauthorized("Sorry, no operation on lists.")
def create_detail(self, object_list, bundle):
return self.authorize_detail(bundle.obj, bundle)
def update_list(self, object_list, bundle):
raise Unauthorized("Sorry, no operation on lists.")
def update_detail(self, object_list, bundle):
return self.authorize_detail(bundle.obj, bundle)
def delete_list(self, object_list, bundle):
raise Unauthorized("Sorry, no operation on lists.")
def delete_detail(self, object_list, bundle):
return self.authorize_detail(bundle.obj, bundle)
class FriendRequestResource(ModelResource):
from_user = fields.ForeignKey(FriendResource, 'from_user', full=True)
to_user = fields.ForeignKey(FriendResource, 'to_user', full=True)
class Meta:
queryset = Friend_Request.objects.all()
resource_name = 'friend_request'
allowed_methods = ['get', 'post', 'put', 'delete']
authorization = FriendRequestAuthorization()
always_return_data = True
limit = 0
max_limit = 0
filtering = {
'from_user': ALL_WITH_RELATIONS,
'to_user': ALL_WITH_RELATIONS
}
class UserResource(ModelResource):
friends = fields.ToManyField(FriendResource, 'friends', full=True)
class Meta:
queryset = Nice_User.objects.all()
resource_name = 'user'
excludes = ['password']
allowed_methods = ['get', 'post']
cache = SimpleCache(timeout=10)
authorization = UserAuthorization()
filtering = {
'netid': ALL_WITH_RELATIONS
}
class ProfessorResource(ModelResource):
class Meta:
queryset = Professor.objects.all()
resource_name = 'professor'
excludes = []
allowed_methods = ['get']
authorization = Authorization()