1 # -*- encoding: utf-8 -*-
3 __author__= "Łukasz Rekucki"
4 __date__ = "$2009-09-26 00:32:18$"
5 __doc__ = "Extensible HTTP Responses."
7 from django.http import HttpResponse
8 from django.utils import simplejson as json
10 MIME_PLAIN = 'text/plain'
11 MIME_JSON = 'application/json'
13 class ResponseObject(object):
15 def __init__(self, code, mimetype=MIME_JSON):
19 def django_response(self, body=None):
22 elif self._mime == MIME_JSON:
23 data = json.dumps(body, default=lambda o: repr(o) )
25 # data = u"%s\n%s" % (self.MESSAGE, unicode(body))
26 data = unicode(body).encode('utf-8')
28 return HttpResponse(content=data, status=self._code, \
29 content_type=self._mime+'; charset=utf-8' )
31 class SuccessAllOk(ResponseObject):
32 def __init__(self, **kwargs):
33 ResponseObject.__init__(self, 200, **kwargs)
35 class EntityCreated(ResponseObject):
37 def __init__(self, **kwargs):
38 ResponseObject.__init__(self, 201, **kwargs)
40 def django_response(self, url, body):
41 response = ResponseObject.django_response(self, body)
42 response['Location'] = url
45 class RequestAccepted(ResponseObject):
47 def __init__(self, **kwargs):
48 ResponseObject.__init__(self, 202, **kwargs)
50 def django_response(self, ticket_status, ticket_uri):
51 return ResponseObject.django_response(self, {
53 'status': ticket_status,
54 'refer_to': ticket_uri })
56 class SuccessNoContent(ResponseObject):
58 def __init__(self, **kwargs):
59 ResponseObject.__init__(self, 204, **kwargs)
61 def django_response(self):
62 return ResponseObject.django_response(self, body=None)
68 class BadRequest(ResponseObject):
70 def __init__(self, **kwargs):
71 ResponseObject.__init__(self, 400, **kwargs)
73 class AccessDenied(ResponseObject):
75 def __init__(self, **kwargs):
76 ResponseObject.__init__(self, 403, **kwargs)
79 class EntityNotFound(ResponseObject):
81 def __init__(self, **kwargs):
82 ResponseObject.__init__(self, 404, **kwargs)
84 class EntityGone(ResponseObject):
86 def __init__(self, **kwargs):
87 ResponseObject.__init__(self, 410, **kwargs)
90 class EntityConflict(ResponseObject):
92 def __init__(self, **kwargs):
93 ResponseObject.__init__(self, 409, **kwargs)
99 class InternalError(ResponseObject):
101 def __init__(self, **kwargs):
102 ResponseObject.__init__(self, 500, **kwargs)
104 class NotImplemented(ResponseObject):
106 def __init__(self, **kwargs):
107 ResponseObject.__init__(self, 501, **kwargs)