322118a004620cfaed84c6c91c08d3c62deb2687
[redakcja.git] / apps / api / response.py
1 # -*- encoding: utf-8 -*-
2
3 __author__= "Ɓukasz Rekucki"
4 __date__ = "$2009-09-26 00:32:18$"
5 __doc__ = "Extensible HTTP Responses."
6
7 from django.http import HttpResponse
8 from django.utils import simplejson as json
9
10 MIME_PLAIN = 'text/plain'
11 MIME_JSON = 'application/json'
12
13 class ResponseObject(object):
14
15     def __init__(self, code, mimetype=MIME_JSON):
16         self._code = code
17         self._mime = mimetype        
18
19     def django_response(self, body=None):
20         if body is None:
21             data = ''
22         elif self._mime == MIME_JSON:
23             data = json.dumps(body, default=lambda o: repr(o) )
24         else:
25             data = u"%s\n%s" % (self.MESSAGE, unicode(self._info))
26             data = data.encode('utf-8')
27             
28         return HttpResponse(content=data, status=self._code, \
29                 content_type=self._mime+'; charset=utf-8' )        
30     
31 class SuccessAllOk(ResponseObject):
32     def __init__(self, **kwargs):
33         ResponseObject.__init__(self, 200, **kwargs)
34         
35 class EntityCreated(ResponseObject):
36
37     def __init__(self, **kwargs):
38         ResponseObject.__init__(self, 201, **kwargs)
39
40     def django_response(self, url, body):        
41         response = ResponseObject.django_response(self, body)
42         response['Location'] = url
43         return response
44
45 class RequestAccepted(ResponseObject):
46
47     def __init__(self, **kwargs):
48         ResponseObject.__init__(self, 202, **kwargs)
49
50     def django_response(self, ticket_status, ticket_uri):
51         return ResponseObject.django_response(self, {
52             'status': ticket_status,
53             'refer_to': ticket_uri })     
54         
55 class SuccessNoContent(ResponseObject):
56
57     def __init__(self, **kwargs):
58         ResponseObject.__init__(self, 204, **kwargs)
59
60     def django_response(self):
61         return ResponseObject.django_response(self, body=None)
62
63
64 #
65 # Client errors
66 #
67
68 class BadRequest(ResponseObject):
69
70     def __init__(self, **kwargs):
71         ResponseObject.__init__(self, 400, **kwargs)
72     
73 class AccessDenied(ResponseObject):
74
75     def __init__(self, **kwargs):
76         ResponseObject.__init__(self, 403, **kwargs)
77
78     def django_response(self, reason):
79         return ResponseObject.django_response(self, \
80             body={'reason': reason})
81
82 class EntityNotFound(ResponseObject):
83
84     def __init__(self, **kwargs):
85         ResponseObject.__init__(self, 404, **kwargs)
86
87 class EntityGone(ResponseObject):
88
89     def __init__(self, **kwargs):
90         ResponseObject.__init__(self, 410, **kwargs)
91
92
93 class EntityConflict(ResponseObject):
94
95     def __init__(self, **kwargs):
96         ResponseObject.__init__(self, 409, **kwargs)
97
98
99 def validate_form(formclass, source='GET'):
100     from functools import wraps
101
102     def decorator(func):
103         @wraps(func)
104         def decorated(self, request, *args, **kwargs):
105             form = formclass(getattr(request,  source), request.FILES)
106
107             if not form.is_valid():
108                 errorlist = [{'field': k, 'errors': e} for k,e in form.errors]
109                 return BadRequest().django_response(errorlist)
110             
111             return func(self, request, form, *args, **kwargs)
112         return decorated
113     return decorator
114        
115
116
117     
118
119