Revision 479c3051 snf-cyclades-app/synnefo/ui/userdata/rest.py

b/snf-cyclades-app/synnefo/ui/userdata/rest.py
33 33
# or implied, of GRNET S.A.
34 34

  
35 35
from django import http
36
from django.template import RequestContext, loader
37 36
from django.utils import simplejson as json
38
from django.core import serializers
39 37
from django.core.urlresolvers import reverse
40 38
from django.http import HttpResponse
41 39

  
......
44 42
from snf_django.lib.astakos import get_user
45 43
from django.conf import settings
46 44

  
47
# base view class
48
# https://github.com/bfirsh/django-class-based-views/blob/master/class_based_views/base.py
49

  
50 45

  
51 46
class View(object):
52 47
    """
......
82 77
        Main entry point for a request-response process.
83 78
        """
84 79
        def view(request, *args, **kwargs):
85
            user = get_user(request, settings.ASTAKOS_URL)
80
            get_user(request, settings.ASTAKOS_URL)
86 81
            if not request.user_uniq:
87 82
                return HttpResponse(status=401)
88 83
            self = cls(*initargs, **initkwargs)
......
100 95

  
101 96
            if request.method.upper() in ['POST', 'PUT']:
102 97
                # Expect json data
103
                if request.META.get('CONTENT_TYPE').startswith('application/json'):
98
                if request.META.get('CONTENT_TYPE').startswith(
99
                        'application/json'):
104 100
                    try:
105 101
                        data = json.loads(data)
106 102
                    except ValueError:
107
                        return http.HttpResponseServerError('Invalid JSON data.')
103
                        return \
104
                            http.HttpResponseServerError('Invalid JSON data.')
108 105
                else:
109
                    return http.HttpResponseServerError('Unsupported Content-Type.')
106
                    return http.HttpResponseServerError(
107
                        'Unsupported Content-Type.')
110 108
            try:
111
                return getattr(self, request.method.upper())(request, data, *args, **kwargs)
109
                return getattr(self, request.method.upper())(
110
                    request, data, *args, **kwargs)
112 111
            except ValidationError, e:
113 112
                # specific response for validation errors
114
                return http.HttpResponseServerError(json.dumps({'errors':
115
                    e.message_dict, 'non_field_key':
116
                    NON_FIELD_ERRORS }))
113
                return http.HttpResponseServerError(
114
                    json.dumps({'errors': e.message_dict,
115
                                'non_field_key': NON_FIELD_ERRORS}))
117 116

  
118 117
        else:
119 118
            allowed_methods = [m for m in self.method_names if hasattr(self, m)]
......
126 125
    """
127 126

  
128 127
    url_name = None
128

  
129 129
    def __init__(self, url_name, *args, **kwargs):
130 130
        self.url_name = url_name
131 131
        return super(JSONRestView, self).__init__(*args, **kwargs)
......
184 184
            raise http.Http404
185 185

  
186 186
    def GET(self, request, data, *args, **kwargs):
187
        return self.json_response(self.instance_to_dict(self.instance(),
188
            self.exclude_fields))
187
        return self.json_response(
188
            self.instance_to_dict(self.instance(), self.exclude_fields))
189 189

  
190 190
    def PUT(self, request, data, *args, **kwargs):
191 191
        instance = self.instance()
......
209 209
        return self.model.objects.all()
210 210

  
211 211
    def GET(self, request, data, *args, **kwargs):
212
        return self.json_response(list(self.qs_to_dict_iter(self.queryset(),
213
            self.exclude_fields)))
212
        return self.json_response(
213
            list(self.qs_to_dict_iter(self.queryset(), self.exclude_fields)))
214 214

  
215 215
    def POST(self, request, data, *args, **kwargs):
216 216
        instance = self.model()
217 217
        self.update_instance(instance, data, self.exclude_fields)
218 218
        instance.full_clean()
219 219
        instance.save()
220
        return self.json_response(self.instance_to_dict(instance,
221
            self.exclude_fields))
220
        return self.json_response(
221
            self.instance_to_dict(instance, self.exclude_fields))
222 222

  
223 223

  
224 224
class UserResourceView(ResourceView):
......
227 227
    """
228 228
    def queryset(self):
229 229
        return super(UserResourceView,
230
                self).queryset().filter(user=self.request.user_uniq)
230
                     self).queryset().filter(user=self.request.user_uniq)
231 231

  
232 232

  
233 233
class UserCollectionView(CollectionView):
......
235 235
    Filter collection queryset for request user entries
236 236
    """
237 237
    def queryset(self):
238
        return super(UserCollectionView, self).queryset().filter(user=self.request.user_uniq)
238
        return super(UserCollectionView,
239
                     self).queryset().filter(user=self.request.user_uniq)
239 240

  
240 241
    def POST(self, request, data, *args, **kwargs):
241 242
        instance = self.model()
......
243 244
        instance.user = request.user_uniq
244 245
        instance.full_clean()
245 246
        instance.save()
246
        return self.json_response(self.instance_to_dict(instance,
247
            self.exclude_fields))
247
        return self.json_response(
248
            self.instance_to_dict(instance, self.exclude_fields))

Also available in: Unified diff