Revision b896d198 userdata/rest.py
b/userdata/rest.py | ||
---|---|---|
2 | 2 |
from django.template import RequestContext, loader |
3 | 3 |
from django.utils import simplejson as json |
4 | 4 |
from django.core import serializers |
5 |
from django.core.urlresolvers import reverse |
|
5 | 6 |
|
6 | 7 |
# base view class |
7 | 8 |
# https://github.com/bfirsh/django-class-based-views/blob/master/class_based_views/base.py |
... | ... | |
33 | 34 |
key, |
34 | 35 |
)) |
35 | 36 |
|
36 |
def instance_to_dict(self, i, exclude_fields=[]): |
|
37 |
""" |
|
38 |
Convert model instance to python dict |
|
39 |
""" |
|
40 |
d = {} |
|
41 |
for field in i._meta.get_all_field_names(): |
|
42 |
if field in exclude_fields: |
|
43 |
continue |
|
44 |
|
|
45 |
d[field] = i.__getattribute__(field) |
|
46 |
return d |
|
47 |
|
|
48 |
def qs_to_dict_iter(self, qs, exclude_fields=[]): |
|
49 |
""" |
|
50 |
Convert queryset to an iterator of model instances dicts |
|
51 |
""" |
|
52 |
for i in qs: |
|
53 |
yield self.instance_to_dict(i, exclude_fields) |
|
54 |
|
|
55 |
def json_response(self, data): |
|
56 |
return http.HttpResponse(json.dumps(data)) |
|
57 |
|
|
58 | 37 |
@classmethod |
59 | 38 |
def as_view(cls, *initargs, **initkwargs): |
60 | 39 |
""" |
... | ... | |
89 | 68 |
allowed_methods = [m for m in self.method_names if hasattr(self, m)] |
90 | 69 |
return http.HttpResponseNotAllowed(allowed_methods) |
91 | 70 |
|
71 |
class JSONRestView(View): |
|
72 |
""" |
|
73 |
Class that provides helpers to produce a json response |
|
74 |
""" |
|
92 | 75 |
|
93 |
class ResourceView(View): |
|
76 |
url_name = None |
|
77 |
def __init__(self, url_name, *args, **kwargs): |
|
78 |
self.url_name = url_name |
|
79 |
return super(JSONRestView, self).__init__(*args, **kwargs) |
|
80 |
|
|
81 |
def update_instance(self, i, data): |
|
82 |
update_keys = data.keys() |
|
83 |
for field in i._meta.get_all_field_names(): |
|
84 |
if field in update_keys: |
|
85 |
i.__setattr__(field, data[field]) |
|
86 |
|
|
87 |
return i |
|
88 |
|
|
89 |
def instance_to_dict(self, i, exclude_fields=[]): |
|
90 |
""" |
|
91 |
Convert model instance to python dict |
|
92 |
""" |
|
93 |
d = {} |
|
94 |
d['uri'] = reverse(self.url_name, kwargs={'id': i.pk}) |
|
95 |
|
|
96 |
for field in i._meta.get_all_field_names(): |
|
97 |
if field in exclude_fields: |
|
98 |
continue |
|
99 |
|
|
100 |
d[field] = i.__getattribute__(field) |
|
101 |
return d |
|
102 |
|
|
103 |
def qs_to_dict_iter(self, qs, exclude_fields=[]): |
|
104 |
""" |
|
105 |
Convert queryset to an iterator of model instances dicts |
|
106 |
""" |
|
107 |
for i in qs: |
|
108 |
yield self.instance_to_dict(i, exclude_fields) |
|
109 |
|
|
110 |
def json_response(self, data): |
|
111 |
return http.HttpResponse(json.dumps(data), mimetype="application/json") |
|
112 |
|
|
113 |
class ResourceView(JSONRestView): |
|
94 | 114 |
method_names = ['GET', 'POST', 'PUT', 'DELETE'] |
95 | 115 |
|
96 | 116 |
model = None |
... | ... | |
115 | 135 |
self.exclude_fields)) |
116 | 136 |
|
117 | 137 |
def POST(self, request, data, *args, **kwargs): |
118 |
pass |
|
138 |
instance = self.instance() |
|
139 |
self.update_instance(instance, data) |
|
140 |
instance.save() |
|
141 |
return self.GET(request, data, *args, **kwargs) |
|
119 | 142 |
|
120 | 143 |
def DELETE(self, request, data, *args, **kwargs): |
121 | 144 |
self.instance().delete() |
122 |
return HttpResponse()
|
|
145 |
return self.json_response("")
|
|
123 | 146 |
|
124 | 147 |
|
125 |
class CollectionView(View): |
|
126 |
method_names = ['GET', 'POST', 'PUT', 'DELETE']
|
|
148 |
class CollectionView(JSONRestView):
|
|
149 |
method_names = ['GET', 'POST'] |
|
127 | 150 |
|
128 | 151 |
model = None |
129 | 152 |
exclude_fields = [] |
... | ... | |
135 | 158 |
return self.json_response(list(self.qs_to_dict_iter(self.queryset(), |
136 | 159 |
self.exclude_fields))) |
137 | 160 |
|
138 |
def PUT(self, request, data, *args, **kwargs): |
|
139 |
pass |
|
140 |
|
|
141 |
def DELETE(self, request, data, *args, **kwargs): |
|
161 |
def POST(self, request, data, *args, **kwargs): |
|
142 | 162 |
pass |
143 | 163 |
|
144 | 164 |
class UserResourceView(ResourceView): |
Also available in: Unified diff