Merge remote branch 'remotes/hanaku/pagination'
[mediagoblin.git] / mediagoblin / decorators.py
CommitLineData
bb3eaf20
CAW
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011 Free Software Foundation, Inc
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18from webob import exc
19
20
21def _make_safe(decorator, original):
22 """
23 Copy the function data from the old function to the decorator.
24 """
25 decorator.__name__ = original.__name__
26 decorator.__dict__ = original.__dict__
27 decorator.__doc__ = original.__doc__
28 return decorator
29
30
31def require_active_login(controller):
32 """
33 Require an active login from the user.
34 """
35 def new_controller_func(request, *args, **kwargs):
e745ce10 36 if not request.user or not request.user.get('status') == u'active':
bb3eaf20
CAW
37 # TODO: Indicate to the user that they were redirected
38 # here because an *active* user is required.
39 return exc.HTTPFound(
7eba0306
CAW
40 location="%s?next=%s" % (
41 request.urlgen("mediagoblin.auth.login"),
42 request.path_info))
bb3eaf20
CAW
43
44 return controller(request, *args, **kwargs)
45
46 return _make_safe(new_controller_func, controller)
3eb6fc4f
BK
47
48
49def uses_pagination(controller):
50 """
51 Check request GET 'page' key for wrong values
52 """
53 def wrapper(request, *args, **kwargs):
54 try:
55 page = int(request.str_GET['page'])
56 if page < 0:
57 return exc.HTTPNotFound()
58 except ValueError:
59 return exc.HTTPNotFound()
60 except KeyError:
61 request.str_GET['page'] = 1
62
63 return controller(request, *args, **kwargs)
64
65 return _make_safe(wrapper,controller)