This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 329
/
api.py
369 lines (293 loc) · 13.1 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
from collections import namedtuple
from operator import itemgetter
from urllib2 import unquote
from urlparse import urljoin
from django.conf import settings
from django.core.urlresolvers import reverse
from django.db.models import Count, Q
from funfactory import utils
from tastypie import fields
from tastypie import http
from tastypie.bundle import Bundle
from tastypie.exceptions import ImmediateHttpResponse
from tastypie.resources import ModelResource, Resource
from tastypie.serializers import Serializer
from mozillians.api.authenticators import AppAuthentication
from mozillians.api.authorisers import MozillaOfficialAuthorization
from mozillians.api.paginator import Paginator
from mozillians.api.resources import (AdvancedSortingResourceMixIn,
ClientCacheResourceMixIn,
GraphiteMixIn)
from mozillians.users.models import COUNTRIES, UserProfile
Country = namedtuple('Country', ['country', 'country_name', 'population'])
City = namedtuple('City', ['city', 'country', 'country_name', 'population'])
class CustomQuerySet(object):
"""A custom queryset class.
Supports database count() on len(), order_by, filter, array
slicing.
"""
def __init__(self, queryset):
self._query = queryset
def __len__(self):
return self._query.count()
def order_by(self, *args):
return self._query.order_by(*args)
def filter(self, *args, **kwargs):
return self._query.filter(*args, **kwargs)
def __getitem__(self, key):
if isinstance(key, slice):
return self._query[key.start:key.stop]
return self._query[key]
class FakeQuerySet(object):
"""
Wrap an iterable and make it work like a pretty dumb queryset.
"""
def __init__(self, iterable):
# We won't be able to get the data out of this without evaluating
# the iterable (to sort or index it), so we might as well evaluate
# it once now.
self.values = list(iterable)
def order_by(self, *args):
return sorted(self.values, key=itemgetter(*args))
def __getitem__(self, key):
if isinstance(key, slice):
return self.values[key.start:key.stop]
return self.values[key]
class LocationCustomResource(AdvancedSortingResourceMixIn,
ClientCacheResourceMixIn, GraphiteMixIn,
Resource):
class Meta:
authentication = AppAuthentication()
authorization = MozillaOfficialAuthorization()
list_allowed_methods = ['get']
serializer = Serializer(formats=['json', 'jsonp'])
paginator_class = Paginator
include_resource_uri = False
detail_allowed_methods = []
cache_control = {'max-age': 0}
def get_object_list(self):
queryset = self.Meta.queryset
return CustomQuerySet(queryset)
def obj_get_list(self, request=None, **kwargs):
obj_list = self.get_object_list()
filters = self.build_filters(getattr(request, 'GET', None))
if filters:
obj_list = self.apply_filters(obj_list, filters)
return obj_list
def apply_filters(self, obj_list, applicable_filters):
mega_filter = Q()
for db_filter in applicable_filters.values():
mega_filter &= db_filter
return obj_list.filter(mega_filter)
def collapse_locations(obj_list, keyname):
"""
Given a CustomQuerySet object, filter/aggregate it down
so we just have one item per country or city.
keyname is 'country' or 'city'.
Also drop the 'privacy_country' or 'privacy_city' field.
On input, we might have:
country privacy_country population
Gr 1 27
Gr 2 16
Us 1 13
Us 2 12
and we want to end up with
country population
Gr 43
Us 25
Returns a CustomQuerySet.
"""
locations = {}
delkey = 'privacy_%s' % keyname
for item in obj_list:
location = item[keyname]
if location in locations:
locations[location]['population'] += item['population']
else:
if delkey in item:
del item[delkey]
locations[location] = item
# Turn the dictionary into an iterable of the dict values.
locations = locations.itervalues()
# Now we have an iterable of dicts, but an iterable is not a queryset
queryset = FakeQuerySet(locations)
# And let's make it a CustomQuerySet again
queryset = CustomQuerySet(queryset)
return queryset
class CountryResource(LocationCustomResource):
country = fields.CharField(attribute='country')
country_name = fields.CharField(attribute='country_name')
population = fields.IntegerField(attribute='population')
url = fields.CharField()
class Meta(LocationCustomResource.Meta):
resource_name = 'countries'
ordering = ['country', 'population']
queryset = (UserProfile.objects
.vouched()
.exclude(country='')
.values('country', 'privacy_country')
.annotate(population=Count('country')))
default_order = ('country',)
object_class = Country
def obj_get_list(self, request=None, **kwargs):
obj_list = super(CountryResource, self).obj_get_list(request, **kwargs)
return collapse_locations(obj_list, 'country')
def build_filters(self, filters=None):
return None
def full_dehydrate(self, queryset):
queryset.obj = self.Meta.object_class(
country=queryset.obj['country'],
population=queryset.obj['population'],
country_name=COUNTRIES[queryset.obj['country']])
return super(LocationCustomResource, self).full_dehydrate(queryset)
def dehydrate_url(self, bundle):
url = reverse('phonebook:list_country', args=[bundle.obj.country])
return utils.absolutify(url)
class CityResource(LocationCustomResource):
city = fields.CharField(attribute='city')
country = fields.CharField(attribute='country')
country_name = fields.CharField(attribute='country_name')
population = fields.IntegerField(attribute='population')
url = fields.CharField()
class Meta(LocationCustomResource.Meta):
resource_name = 'cities'
ordering = ['city', 'country', 'population']
queryset = (UserProfile.objects
.vouched()
.exclude(city='')
.exclude(country='')
.values('city', 'privacy_city',
'country', 'privacy_country')
.annotate(population=Count('city')))
default_order = ('country', 'city')
object_class = City
def obj_get_list(self, request=None, **kwargs):
obj_list = super(CityResource, self).obj_get_list(request, **kwargs)
return collapse_locations(obj_list, 'city')
def build_filters(self, filters=None):
database_filters = {}
valid_filters = [f for f in filters if f in ['country', 'city']]
getvalue = lambda x: unquote(filters[x].lower())
for valid_filter in valid_filters:
database_filters[valid_filter] = (
Q(**{'{0}__iexact'.format(valid_filter):
getvalue(valid_filter)}))
return database_filters
def full_dehydrate(self, queryset):
queryset.obj = self.Meta.object_class(
country=queryset.obj['country'],
country_name=COUNTRIES[queryset.obj['country']],
population=queryset.obj['population'],
city=queryset.obj['city'])
return super(LocationCustomResource, self).full_dehydrate(queryset)
def dehydrate_url(self, bundle):
url = reverse('phonebook:list_city',
args=[bundle.obj.country, bundle.obj.city])
return utils.absolutify(url)
class UserResource(ClientCacheResourceMixIn, GraphiteMixIn, ModelResource):
"""User Resource."""
email = fields.CharField(attribute='user__email', null=True, readonly=True)
username = fields.CharField(attribute='user__username', null=True, readonly=True)
vouched_by = fields.IntegerField(attribute='vouched_by__id',
null=True, readonly=True)
groups = fields.CharField()
skills = fields.CharField()
languages = fields.CharField()
url = fields.CharField()
accounts = fields.CharField()
class Meta:
queryset = UserProfile.objects.all()
authentication = AppAuthentication()
authorization = MozillaOfficialAuthorization()
serializer = Serializer(formats=['json', 'jsonp'])
paginator_class = Paginator
cache_control = {'max-age': 0}
list_allowed_methods = ['get']
detail_allowed_methods = ['get']
resource_name = 'users'
restrict_fields = False
restricted_fields = ['email', 'is_vouched']
fields = ['id', 'full_name', 'is_vouched', 'vouched_by',
'date_vouched', 'groups', 'skills',
'bio', 'photo', 'ircname', 'country', 'region', 'city',
'date_mozillian', 'timezone', 'email', 'allows_mozilla_sites',
'allows_community_sites']
def build_filters(self, filters=None):
database_filters = {}
valid_filters = [f for f in filters if f in
['email', 'country', 'region', 'city', 'ircname',
'username', 'groups', 'skills',
'is_vouched', 'name', 'accounts']]
getvalue = lambda x: unquote(filters[x].lower())
if 'accounts' in valid_filters:
database_filters['accounts'] = Q(
externalaccount__identifier__icontains=getvalue('accounts'))
if 'email' in valid_filters:
database_filters['email'] = Q(
user__email__iexact=getvalue('email'))
if 'username' in valid_filters:
database_filters['username'] = Q(
user__username__iexact=getvalue('username'))
if 'name' in valid_filters:
database_filters['name'] = Q(full_name__iexact=getvalue('name'))
if 'is_vouched' in valid_filters:
value = getvalue('is_vouched')
if value == 'true':
database_filters['is_vouched'] = Q(is_vouched=True)
elif value == 'false':
database_filters['is_vouched'] = Q(is_vouched=False)
for possible_filter in ['country', 'region', 'city', 'ircname']:
if possible_filter in valid_filters:
database_filters[possible_filter] = Q(
**{'{0}__iexact'.format(possible_filter):
getvalue(possible_filter)})
for group_filter in ['groups', 'skills']:
if group_filter in valid_filters:
database_filters[group_filter] = Q(
**{'{0}__name__in'.format(group_filter):
getvalue(group_filter).split(',')})
return database_filters
def dehydrate(self, bundle):
if (bundle.request.GET.get('restricted', False)
or not bundle.data['allows_mozilla_sites']):
data = {}
for key in self._meta.restricted_fields:
data[key] = bundle.data[key]
bundle = Bundle(obj=bundle.obj, data=data, request=bundle.request)
return bundle
def dehydrate_accounts(self, bundle):
accounts = [{'identifier': a.identifier, 'type': a.type}
for a in bundle.obj.externalaccount_set.all()]
return accounts
def dehydrate_groups(self, bundle):
groups = bundle.obj.groups.values_list('name', flat=True)
return list(groups)
def dehydrate_skills(self, bundle):
skills = bundle.obj.skills.values_list('name', flat=True)
return list(skills)
def dehydrate_languages(self, bundle):
languages = bundle.obj.languages.values_list('code', flat=True)
return list(languages)
def dehydrate_photo(self, bundle):
if bundle.obj.photo:
return urljoin(settings.SITE_URL, bundle.obj.photo.url)
return ''
def dehydrate_url(self, bundle):
url = reverse('phonebook:profile_view',
args=[bundle.obj.user.username])
return utils.absolutify(url)
def get_detail(self, request, **kwargs):
if request.GET.get('restricted', False):
raise ImmediateHttpResponse(response=http.HttpForbidden())
return super(UserResource, self).get_detail(request, **kwargs)
def apply_filters(self, request, applicable_filters):
if (request.GET.get('restricted', False)
and 'email' not in applicable_filters
and len(applicable_filters) != 1):
raise ImmediateHttpResponse(response=http.HttpForbidden())
mega_filter = Q()
for db_filter in applicable_filters.values():
mega_filter &= db_filter
if request.GET.get('restricted', False):
mega_filter &= Q(allows_community_sites=True)
return UserProfile.objects.complete().filter(mega_filter).distinct().order_by('id')