X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/0e87ae0739ed3e72301b7b718098f97a7f06a5d8..139af81833f2b6727618d8e2c30cc7157c42e8a1:/apps/piston/resource.py diff --git a/apps/piston/resource.py b/apps/piston/resource.py index 40f065d05..ee78b0e77 100644 --- a/apps/piston/resource.py +++ b/apps/piston/resource.py @@ -26,22 +26,22 @@ class Resource(object): is an authentication handler. If not specified, `NoAuthentication` will be used by default. """ - callmap = { 'GET': 'read', 'POST': 'create', + callmap = { 'GET': 'read', 'POST': 'create', 'PUT': 'update', 'DELETE': 'delete' } - + def __init__(self, handler, authentication=None): if not callable(handler): raise AttributeError, "Handler not callable." - + self.handler = handler() - + if not authentication: self.authentication = (NoAuthentication(),) elif isinstance(authentication, (list, tuple)): self.authentication = authentication else: self.authentication = (authentication,) - + # Erroring self.email_errors = getattr(settings, 'PISTON_EMAIL_ERRORS', True) self.display_errors = getattr(settings, 'PISTON_DISPLAY_ERRORS', True) @@ -58,12 +58,12 @@ class Resource(object): that as well. """ em = kwargs.pop('emitter_format', None) - + if not em: em = request.GET.get('format', 'json') return em - + @property def anonymous(self): """ @@ -74,16 +74,16 @@ class Resource(object): """ if hasattr(self.handler, 'anonymous'): anon = self.handler.anonymous - + if callable(anon): return anon for klass in typemapper.keys(): if anon == klass.__name__: return klass - + return None - + def authenticate(self, request, rm): actor, anonymous = False, True @@ -97,9 +97,9 @@ class Resource(object): actor, anonymous = authenticator.challenge, CHALLENGE else: return self.handler, self.handler.is_anonymous - + return actor, anonymous - + @vary_on_headers('Authorization') def __call__(self, request, *args, **kwargs): """ @@ -119,19 +119,19 @@ class Resource(object): return actor() else: handler = actor - + # Translate nested datastructs into `request.data` here. if rm in ('POST', 'PUT'): try: translate_mime(request) except MimerDataException: return rc.BAD_REQUEST - + if not rm in handler.allowed_methods: return HttpResponseNotAllowed(handler.allowed_methods) - + meth = getattr(handler, self.callmap.get(rm), None) - + if not meth: raise Http404 @@ -139,18 +139,18 @@ class Resource(object): em_format = self.determine_emitter(request, *args, **kwargs) kwargs.pop('emitter_format', None) - + # Clean up the request object a bit, since we might # very well have `oauth_`-headers in there, and we # don't want to pass these along to the handler. request = self.cleanup_request(request) - + try: result = meth(request, *args, **kwargs) except FormValidationError, e: resp = rc.BAD_REQUEST resp.write(' '+str(e.form.errors)) - + return resp except TypeError, e: result = rc.BAD_REQUEST @@ -158,15 +158,15 @@ class Resource(object): sig = hm.signature msg = 'Method signature does not match.\n\n' - + if sig: msg += 'Signature should be: %s' % sig else: msg += 'Resource does not expect any parameters.' - if self.display_errors: + if self.display_errors: msg += '\n\nException was: %s' % str(e) - + result.content = format_error(msg) except Http404: return rc.NOT_FOUND @@ -177,13 +177,13 @@ class Resource(object): On errors (like code errors), we'd like to be able to give crash reports to both admins and also the calling user. There's two setting parameters for this: - + Parameters:: - `PISTON_EMAIL_ERRORS`: Will send a Django formatted error email to people in `settings.ADMINS`. - `PISTON_DISPLAY_ERRORS`: Will return a simple traceback to the caller, so he can tell you what error they got. - + If `PISTON_DISPLAY_ERRORS` is not enabled, the caller will receive a basic "500 Internal Server Error" message. """ @@ -237,17 +237,17 @@ class Resource(object): if True in [ k.startswith("oauth_") for k in block.keys() ]: sanitized = block.copy() - + for k in sanitized.keys(): if k.startswith("oauth_"): sanitized.pop(k) - + setattr(request, method_type, sanitized) return request - - # -- - + + # -- + def email_exception(self, reporter): subject = "Piston crash report" html = reporter.get_traceback_html() @@ -255,6 +255,6 @@ class Resource(object): message = EmailMessage(settings.EMAIL_SUBJECT_PREFIX+subject, html, settings.SERVER_EMAIL, [ admin[1] for admin in settings.ADMINS ]) - + message.content_subtype = 'html' message.send(fail_silently=True)