X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/dba809feb44cd1c4d155f3e3254a1cca5323f95f..22aac0da99594406b261f14c135812c855c196ef:/apps/api/response.py diff --git a/apps/api/response.py b/apps/api/response.py deleted file mode 100644 index 6b685624..00000000 --- a/apps/api/response.py +++ /dev/null @@ -1,126 +0,0 @@ -# -*- encoding: utf-8 -*- - -__author__= "Łukasz Rekucki" -__date__ = "$2009-09-26 00:32:18$" -__doc__ = "Extensible HTTP Responses." - -from django.http import HttpResponse -from django.utils import simplejson as json - -MIME_PLAIN = 'text/plain' -MIME_JSON = 'application/json' - -class ResponseObject(object): - - def __init__(self, code, mimetype=MIME_JSON): - self._code = code - self._mime = mimetype - - def django_response(self, body=None): - if body is None: - data = '' - elif self._mime == MIME_JSON: - data = json.dumps(body, default=lambda o: repr(o) ) - else: - data = u"%s\n%s" % (self.MESSAGE, unicode(body)) - data = data.encode('utf-8') - - return HttpResponse(content=data, status=self._code, \ - content_type=self._mime+'; charset=utf-8' ) - -class SuccessAllOk(ResponseObject): - def __init__(self, **kwargs): - ResponseObject.__init__(self, 200, **kwargs) - -class EntityCreated(ResponseObject): - - def __init__(self, **kwargs): - ResponseObject.__init__(self, 201, **kwargs) - - def django_response(self, url, body): - response = ResponseObject.django_response(self, body) - response['Location'] = url - return response - -class RequestAccepted(ResponseObject): - - def __init__(self, **kwargs): - ResponseObject.__init__(self, 202, **kwargs) - - def django_response(self, ticket_status, ticket_uri): - return ResponseObject.django_response(self, { - 'status': ticket_status, - 'refer_to': ticket_uri }) - -class SuccessNoContent(ResponseObject): - - def __init__(self, **kwargs): - ResponseObject.__init__(self, 204, **kwargs) - - def django_response(self): - return ResponseObject.django_response(self, body=None) - -# -# Client errors -# - -class BadRequest(ResponseObject): - - def __init__(self, **kwargs): - ResponseObject.__init__(self, 400, **kwargs) - -class AccessDenied(ResponseObject): - - def __init__(self, **kwargs): - ResponseObject.__init__(self, 403, **kwargs) - - def django_response(self, reason): - return ResponseObject.django_response(self, \ - body={'reason': reason}) - -class EntityNotFound(ResponseObject): - - def __init__(self, **kwargs): - ResponseObject.__init__(self, 404, **kwargs) - -class EntityGone(ResponseObject): - - def __init__(self, **kwargs): - ResponseObject.__init__(self, 410, **kwargs) - - -class EntityConflict(ResponseObject): - - def __init__(self, **kwargs): - ResponseObject.__init__(self, 409, **kwargs) - - -# -# Server side errors -# -class NotImplemented(ResponseObject): - - def __init__(self, **kwargs): - ResponseObject.__init__(self, 501, **kwargs) - -def validate_form(formclass, source='GET'): - from functools import wraps - - def decorator(func): - @wraps(func) - def decorated(self, request, *args, **kwargs): - form = formclass(getattr(request, source), request.FILES) - - if not form.is_valid(): - errorlist = [{'field': k, 'errors': e} for k,e in form.errors] - return BadRequest().django_response(errorlist) - - return func(self, request, form, *args, **kwargs) - return decorated - return decorator - - - - - -