generated from palewire/python-open-source-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviews.py
31 lines (21 loc) · 944 Bytes
/
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
import json
from django.core import serializers
from django.http import HttpResponse
from django.views.generic import TemplateView
from anss.models import Feed
class BaseJsonView(TemplateView):
def to_json_response(self, content, **response_kwargs):
return HttpResponse(content, content_type="application/json", **response_kwargs)
class LatestFeedView(BaseJsonView):
def get_context_data(self, **kwargs):
return Feed.objects.latest()
def render_to_response(self, context, **response_kwargs):
f = json.load(context.content)
d = json.dumps(f, indent=4)
return self.to_json_response(d)
class FeedListView(BaseJsonView):
def get_context_data(self, **kwargs):
return Feed.objects.all()[:100]
def render_to_response(self, context, **response_kwargs):
object_list = serializers.serialize("json", context, indent=4)
return self.to_json_response(object_list)