1 from pecan import request, response
2 from base64 import b64decode
3 from functools import wraps
10 # Handle authorization
13 def decorated(*args, **kwargs):
14 if not request.authorization:
16 response.headers['WWW-Authenticate'] = 'Basic realm="Login Required"'
17 return {'message': 'auth: No HTTP username/password'}
19 username, password = b64decode(request.authorization[1]).split(':')
21 # Check that the username exists
22 if username not in module.instance.keys:
24 response.headers['WWW-Authenticate'] = 'Basic realm="Login Required"'
25 return {'message': 'auth: No such user'}
28 if module.instance.keys[username] != password:
30 response.headers['WWW-Authenticate'] = 'Basic realm="Login Required"'
31 return {'message': 'auth: Incorrect password'}
33 return f(*args, **kwargs)
37 # Helper function to lock the function
40 def decorated(*args, **kwargs):
41 with module.instance.requests_lock:
42 return f(*args, **kwargs)
46 # Support ?page=N argument
49 def decorated(*args, **kwargs):
50 _out = f(*args, **kwargs)
52 # Do not modify anything without a specific request
53 if not 'page' in kwargs:
56 # A pass-through for errors, etc
57 if not isinstance(_out, list):
60 # Parse the page argument
61 _page = kwargs['page']
66 return {'message': 'The requested page is not an integer'}
68 # Raise _page so that 0 is the first page and -1 is the last
74 _page = len(_out) - (_page*100)
76 return _out[_page - 100: _page]