1 # -*- encoding: utf-8 -*-
3 __author__ = "Ćukasz Rekucki"
4 __date__ = "$2009-09-20 21:48:03$"
5 __doc__ = "Module documentation."
8 from functools import wraps
9 from piston.emitters import Emitter
10 from piston.utils import rc
17 class TextEmitter(Emitter):
18 def render(self, request):
19 return unicode(self.construct())
21 Emitter.register('raw', TextEmitter, 'text/plain; charset=utf-8')
22 Emitter.register('rawhtml', TextEmitter, 'text/html; charset=utf-8')
23 Emitter.register('rawxml', TextEmitter, 'application/xml; charset=utf-8')
25 class DjangoAuth(object):
27 def is_authenticated(self, request):
28 return request.user.is_authenticated()
34 def validate_form(formclass, source='GET'):
38 def decorated(self, request, * args, ** kwargs):
39 form = formclass(getattr(request, source), request.FILES)
41 if not form.is_valid():
42 errorlist = [{'field': k, 'errors': str(e)} for k, e in form.errors.items()]
43 return api.response.BadRequest().django_response(errorlist)
46 return func(self, request, * args, ** kwargs)
52 def decorated(self, *args, **kwargs):
53 l = wlrepo.open_library(settings.REPOSITORY_PATH, 'hg')
55 return func(self, *args, **kwargs)
63 NAT_EXPR = re.compile(r'(\d+)', re.LOCALE | re.UNICODE)
64 def natural_order(get_key=lambda x: x):
68 if not isinstance(nkey, unicode):
69 ukey = nkey.decode('utf-8')
73 parts = enumerate( NAT_EXPR.split(ukey))
74 return [int(x) if n%2 else locale.strxfrm(x.encode('utf-8')) for (n,x) in parts ]