This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
views.py
75 lines (59 loc) · 2.71 KB
/
views.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
from django.core.urlresolvers import reverse
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from django.views.generic import ListView, DetailView
from .models import Code
from source.base.utils import paginate
from source.tags.utils import filter_queryset_by_tags
class CodeList(ListView):
model = Code
def dispatch(self, *args, **kwargs):
self.render_json = kwargs.get('render_json', False)
self.tag_slugs = kwargs.get('tag_slugs', None)
self.tags = []
return super(CodeList, self).dispatch(*args, **kwargs)
def get_queryset(self):
queryset = Code.live_objects.prefetch_related('organizations')
if self.tag_slugs:
queryset, self.tags = filter_queryset_by_tags(queryset, self.tag_slugs, self.tags)
return queryset
def get_context_data(self, **kwargs):
context = super(CodeList, self).get_context_data(**kwargs)
context['active_nav'] = 'Code'
if self.tags:
context['tags'] = self.tags
context['rss_link'] = reverse('code_list_by_tag_feed', kwargs={'tag_slugs': self.tag_slugs})
context['json_link'] = reverse('code_list_by_tag_feed_json', kwargs={'tag_slugs': self.tag_slugs})
else:
context['rss_link'] = reverse('code_list_feed')
context['json_link'] = reverse('code_list_feed_json')
# No pagination required for current alpha list display
#page, paginator = paginate(self.request, self.object_list, 50)
#context.update({
# 'page': page,
# 'paginator': paginator
#})
return context
def render_to_response(self, context):
if self.render_json:
'''
JSON export runs through a hand-rolled template for now, so we can
attach things like related names and urls. If we start doing more
with providing JSON, we should definitly go full django-tastypie.
'''
if 'callback' in self.request.GET:
# provide jsonp support for requests
# with ?callback=foo paramater
context['jsonp_callback'] = self.request.GET['callback']
return render_to_response(
'code/code_list.json',
context,
context_instance = RequestContext(self.request),
mimetype='application/json'
)
return super(CodeList, self).render_to_response(context)
class CodeDetail(DetailView):
model = Code
def get_queryset(self):
queryset = Code.live_objects.prefetch_related('codelink_set', 'people', 'organizations', 'article_set')
return queryset