Revision 49f50673 ui/userdata/rest.py
b/ui/userdata/rest.py | ||
---|---|---|
1 |
# |
|
2 |
# Copyright 2011 GRNET S.A. All rights reserved. |
|
3 |
# |
|
4 |
# Redistribution and use in source and binary forms, with or |
|
5 |
# without modification, are permitted provided that the following |
|
6 |
# conditions are met: |
|
7 |
# |
|
8 |
# 1. Redistributions of source code must retain the above |
|
9 |
# copyright notice, this list of conditions and the following |
|
10 |
# disclaimer. |
|
11 |
# |
|
12 |
# 2. Redistributions in binary form must reproduce the above |
|
13 |
# copyright notice, this list of conditions and the following |
|
14 |
# disclaimer in the documentation and/or other materials |
|
15 |
# provided with the distribution. |
|
16 |
# |
|
17 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS |
|
18 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
19 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
20 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR |
|
21 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
22 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
23 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
|
24 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
|
25 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
26 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
27 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
28 |
# POSSIBILITY OF SUCH DAMAGE. |
|
29 |
# |
|
30 |
# The views and conclusions contained in the software and |
|
31 |
# documentation are those of the authors and should not be |
|
32 |
# interpreted as representing official policies, either expressed |
|
33 |
# or implied, of GRNET S.A. |
|
34 |
|
|
1 | 35 |
from django import http |
2 | 36 |
from django.template import RequestContext, loader |
3 | 37 |
from django.utils import simplejson as json |
... | ... | |
8 | 42 |
|
9 | 43 |
# base view class |
10 | 44 |
# https://github.com/bfirsh/django-class-based-views/blob/master/class_based_views/base.py |
45 |
|
|
46 |
|
|
11 | 47 |
class View(object): |
12 | 48 |
""" |
13 | 49 |
Intentionally simple parent class for all views. Only implements |
... | ... | |
25 | 61 |
# instance, or raise an error. |
26 | 62 |
for key, value in kwargs.items(): |
27 | 63 |
if key in self.method_names: |
28 |
raise TypeError(u"You tried to pass in the %s method name as a "
|
|
29 |
u"keyword argument to %s(). Don't do that." |
|
64 |
raise TypeError(u"You tried to pass in the %s method name as a" |
|
65 |
u" keyword argument to %s(). Don't do that."
|
|
30 | 66 |
% (key, self.__class__.__name__)) |
31 | 67 |
if hasattr(self, key): |
32 | 68 |
setattr(self, key, value) |
... | ... | |
76 | 112 |
allowed_methods = [m for m in self.method_names if hasattr(self, m)] |
77 | 113 |
return http.HttpResponseNotAllowed(allowed_methods) |
78 | 114 |
|
115 |
|
|
79 | 116 |
class JSONRestView(View): |
80 | 117 |
""" |
81 | 118 |
Class that provides helpers to produce a json response |
... | ... | |
118 | 155 |
def json_response(self, data): |
119 | 156 |
return http.HttpResponse(json.dumps(data), mimetype="application/json") |
120 | 157 |
|
158 |
|
|
121 | 159 |
class ResourceView(JSONRestView): |
122 | 160 |
method_names = ['GET', 'POST', 'PUT', 'DELETE'] |
123 | 161 |
|
... | ... | |
175 | 213 |
return self.json_response(self.instance_to_dict(instance, |
176 | 214 |
self.exclude_fields)) |
177 | 215 |
|
216 |
|
|
178 | 217 |
class UserResourceView(ResourceView): |
179 | 218 |
""" |
180 | 219 |
Filter resource queryset for request user entries |
... | ... | |
183 | 222 |
return super(UserResourceView, |
184 | 223 |
self).queryset().filter(user=self.request.user) |
185 | 224 |
|
225 |
|
|
186 | 226 |
class UserCollectionView(CollectionView): |
187 | 227 |
""" |
188 | 228 |
Filter collection queryset for request user entries |
... | ... | |
198 | 238 |
instance.save() |
199 | 239 |
return self.json_response(self.instance_to_dict(instance, |
200 | 240 |
self.exclude_fields)) |
201 |
|
Also available in: Unified diff