0e0468a0d3bae18ede8fd1889cd2cd299dff5957
[redakcja.git] / apps / api / utils.py
1 # -*- encoding: utf-8 -*-
2
3 __author__ = "Ɓukasz Rekucki"
4 __date__ = "$2009-09-20 21:48:03$"
5 __doc__ = "Module documentation."
6
7
8 from functools import wraps
9 from piston.emitters import Emitter
10 from piston.utils import rc
11
12 import api.response
13
14 from wlrepo import MercurialLibrary
15 import settings
16
17 class TextEmitter(Emitter):
18     def render(self, request):
19         return unicode(self.construct())
20
21 Emitter.register('text', TextEmitter, 'text/plain; charset=utf-8')
22 Emitter.register('rawxml', TextEmitter, 'application/xml; charset=UTF-8')
23
24 class DjangoAuth(object):
25
26     def is_authenticated(self, request):
27         return request.user.is_authenticated()
28
29     def challenge(self):
30         return rc.FORBIDDEN
31
32
33 def validate_form(formclass, source='GET'):
34   
35     def decorator(func):
36         @wraps(func)
37         def decorated(self, request, * args, ** kwargs):
38             form = formclass(getattr(request, source), request.FILES)
39
40             if not form.is_valid():
41                 errorlist = [{'field': k, 'errors': e} for k, e in form.errors.items()]
42                 return api.response.BadRequest().django_response(errorlist)
43
44             kwargs['form'] = form
45             return func(self, request, * args, ** kwargs)
46         return decorated
47     return decorator
48     
49 def hglibrary(func):
50     @wraps(func)
51     def decorated(self, *args, **kwargs):
52         l = MercurialLibrary(settings.REPOSITORY_PATH)
53         kwargs['lib'] = l
54         return func(self, *args, **kwargs)
55     return decorated
56     
57             
58         
59