forked from hasadna/Open-Knesset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
54 lines (45 loc) · 1.79 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
from datetime import date, timedelta
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from tastypie.constants import ALL
from apis.resources.base import BaseResource
from committees.models import Committee
from mks.models import Member
from models import Video
class VideoResource(BaseResource):
class Meta(BaseResource.Meta):
limit = 500
queryset = Video.objects.all()
allowed_methods = ['get']
filtering = dict(
object_pk=ALL,
group=ALL,
)
def apply_filters(self, request, applicable_filters):
qs = super(VideoResource, self).apply_filters(request,
applicable_filters)
if 'object_type' in request.GET:
object_type = request.GET['object_type']
if object_type == 'member':
modelObj = Member
elif object_type == 'committee':
modelObj = Committee
else:
modelObj = None
if modelObj is not None:
object_type = ContentType.objects.get_for_model(modelObj)
qs = qs.filter(content_type__pk=object_type.id)
if 'recent_published_days' in request.GET:
recent_published_days = request.GET['recent_published_days']
qs = qs.filter(
Q(published__gt=date.today(
) - timedelta(days=int(recent_published_days)))
| Q(sticky=True)
)
return qs
def apply_sorting(self, obj_list, options=None):
if 'order_by_published_sticky_first' in options:
qs = obj_list.order_by('-sticky', '-published')
else:
qs = super(VideoResource, self).apply_sorting(obj_list, options)
return qs