Exemplo n.º 1
0
 def wrap(*args, **kwargs):
     retval = fn(*args, **kwargs)
     # try to find self, and from there, request
     request = None
     if args:
         self = args[0]
         request = getattr(self, 'request', None)
     
     return _instantiate_viewmodel(cls, retval, request=request)
Exemplo n.º 2
0
        except APIError, apie:
            resp = HttpResponse(simplejson.dumps(apie.errors),
                                mimetype="application/json")
            resp.status_code = apie.code
            return resp


        # paginate before you translate viewmodels
        if self.json:
            if self.paginate and context.get('paginate', False):
                new_context = self.paginate_objects(request, context.get(context['paginate']))
                new_context.update(context)
                del context[context['paginate']]
                del context['paginate']
                context = new_context
            for name in self.remove_from_context:
                del context[name]

        # translate viewmodels
        if getattr(self, 'viewmodels'):
            for name, cls in self.viewmodels.items():
                if name in context:
                    context[name] = utils._instantiate_viewmodel(cls, context[name], request=request)

        # convert context to a response
        if self.template_name:
            return self.render_template(self.template_name, context, request)

        if self.json:
            return self.render_json(context, request)