Permalink
Browse files

Various small improvements. Improved utf-8 handling.

  • Loading branch information...
1 parent a524e5e commit f141c1ff0c102d78c17f47c848c17e7112ea2dea @debrouwere debrouwere committed Jan 11, 2011
Showing with 32 additions and 4 deletions.
  1. +2 −1 apiserver/authentication.py
  2. +1 −1 apiserver/controllers.py
  3. +2 −0 apiserver/dispatch.py
  4. +2 −2 apiserver/serializers.py
  5. +25 −0 apiserver/utils.py
@@ -78,9 +78,10 @@ def __init__(self, realm, level):
self.level = level
def __call__(self, view):
+ @wraps(view)
def decorated(*vargs, **kwargs):
if request.environ["user"].may_see(self.realm, self.level):
return view(*vargs, **kwargs)
else:
abort(403)
- return decorated
+ return decorated
View
@@ -15,7 +15,7 @@ def methods(self):
'DELETE': getattr(self, 'destroy', None),
}
- def __init__(self, app):
+ def __init__(self, app):
route = self.route.rstrip('/') + '.<format>'
available_methods = [method for method in self.methods if self.methods[method] is not None]
View
@@ -3,6 +3,7 @@
from flask import request, abort
import types
from functools import wraps
+import utils
MIMETYPES = {
'json': 'application/json',
@@ -29,6 +30,7 @@
"""
def format_dispatcher(app, view):
+ @utils.timed()
def format_aware_view(**kwargs):
format = kwargs['format']
request.format = format
View
@@ -88,7 +88,7 @@ def __init__(self, obj):
# that completely mirrors the JSON output
# (no attributes or other fancy stuff)
def to_xml(self, parent='item', depth=0):
- out = ''
+ out = u''
simple = True
if isinstance(self.obj, list):
@@ -116,7 +116,7 @@ def to_xml(self, parent='item', depth=0):
start_tabs += ' '
if not simple:
end_tabs = start_tabs
- return '{0}<{1}>{2}{3}</{1}>\n'.format(start_tabs, parent, out, end_tabs)
+ return u'{0}<{1}>{2}{3}</{1}>\n'.format(start_tabs, parent, out, end_tabs)
def to_json(self):
return simplejson.dumps(self.obj, indent=4)
View
@@ -0,0 +1,25 @@
+# encoding: utf-8
+
+import time
+from functools import wraps
+
+class timed(object):
+ def __init__(self, callback=None):
+ self.callback = callback
+
+ def __call__(self, fn):
+ @wraps(fn)
+ def timed_fn(*vargs, **kwargs):
+ start = time.time()
+ result = fn(*vargs, **kwargs)
+ finish = time.time()
+ duration = round(finish-start, 2)
+ msg = "Executed {module}.{name} in {time} seconds.".format(
+ module=fn.__module__, name=fn.__name__, time=duration)
+ if self.callback:
+ self.callback(msg)
+ else:
+ print msg
+ return result
+
+ return timed_fn

0 comments on commit f141c1f

Please sign in to comment.