Nic ciekawego.
[redakcja.git] / apps / api / utils.py
index 4b004ee..19309ff 100644 (file)
@@ -1,19 +1,26 @@
 # -*- encoding: utf-8 -*-
 
-__author__= "Łukasz Rekucki"
+__author__ = "Łukasz Rekucki"
 __date__ = "$2009-09-20 21:48:03$"
 __doc__ = "Module documentation."
 
 
+from functools import wraps
 from piston.emitters import Emitter
 from piston.utils import rc
 
+import api.response
+
+import wlrepo
+import settings
+
 class TextEmitter(Emitter):
     def render(self, request):
         return unicode(self.construct())
 
-Emitter.register('text', TextEmitter, 'text/plain; charset=utf-8')
-Emitter.register('rawxml', TextEmitter, 'application/xml; charset=UTF-8')
+Emitter.register('raw', TextEmitter, 'text/plain; charset=utf-8')
+Emitter.register('rawhtml', TextEmitter, 'text/html; charset=utf-8')
+Emitter.register('rawxml', TextEmitter, 'application/xml; charset=utf-8')
 
 class DjangoAuth(object):
 
@@ -21,4 +28,50 @@ class DjangoAuth(object):
         return request.user.is_authenticated()
 
     def challenge(self):
-        return rc.FORBIDDEN
\ No newline at end of file
+        return rc.FORBIDDEN
+
+
+def validate_form(formclass, source='GET'):
+  
+    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': str(e)} for k, e in form.errors.items()]
+                return api.response.BadRequest().django_response(errorlist)
+
+            kwargs['form'] = form
+            return func(self, request, * args, ** kwargs)
+        return decorated
+    return decorator
+    
+def hglibrary(func):
+    @wraps(func)
+    def decorated(self, *args, **kwargs):
+        l = wlrepo.open_library(settings.REPOSITORY_PATH, 'hg')
+        kwargs['lib'] = l
+        return func(self, *args, **kwargs)
+    return decorated
+
+
+
+import re
+import locale
+
+NAT_EXPR = re.compile(r'(\d+)', re.LOCALE | re.UNICODE)
+def natural_order(get_key=lambda x: x):
+
+    def getter(key):
+        nkey = get_key(key)
+        if not isinstance(nkey, unicode):
+            ukey = nkey.decode('utf-8')
+        else:
+            ukey = nkey
+
+        parts = enumerate( NAT_EXPR.split(ukey))            
+        return [int(x) if n%2 else locale.strxfrm(x.encode('utf-8')) for (n,x) in parts ]
+        
+    return getter
+