First changes to media gallery view
[mediagoblin.git] / mediagoblin / decorators.py
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
18 from bson.errors import InvalidId
19 from webob import exc
20
21 from mediagoblin.db.util import ObjectId
22
23
24 def _make_safe(decorator, original):
25 """
26 Copy the function data from the old function to the decorator.
27 """
28 decorator.__name__ = original.__name__
29 decorator.__dict__ = original.__dict__
30 decorator.__doc__ = original.__doc__
31 return decorator
32
33
34 def require_active_login(controller):
35 """
36 Require an active login from the user.
37 """
38 def new_controller_func(request, *args, **kwargs):
39 if not request.user or not request.user.get('status') == u'active':
40 # TODO: Indicate to the user that they were redirected
41 # here because an *active* user is required.
42 return exc.HTTPFound(
43 location="%s?next=%s" % (
44 request.urlgen("mediagoblin.auth.login"),
45 request.path_info))
46
47 return controller(request, *args, **kwargs)
48
49 return _make_safe(new_controller_func, controller)
50
51
52 def uses_pagination(controller):
53 """
54 Check request GET 'page' key for wrong values
55 """
56 def wrapper(request, *args, **kwargs):
57 try:
58 page = int(request.GET.get('page', 1))
59 if page < 0:
60 return exc.HTTPNotFound()
61 except ValueError:
62 return exc.HTTPNotFound()
63
64 return controller(request, page=page, *args, **kwargs)
65
66 return _make_safe(wrapper, controller)
67
68
69 def get_user_media_entry(controller):
70 """
71 Pass in a MediaEntry based off of a url component
72 """
73 def wrapper(request, *args, **kwargs):
74 user = request.db.User.find_one(
75 {'username': request.matchdict['user']})
76
77 if not user:
78 return exc.HTTPNotFound()
79
80 media = request.db.MediaEntry.find_one(
81 {'slug': request.matchdict['media'],
82 'state': 'processed',
83 'uploader': user['_id']})
84
85 # no media via slug? Grab it via ObjectId
86 if not media:
87 try:
88 media = request.db.MediaEntry.find_one(
89 {'_id': ObjectId(request.matchdict['media']),
90 'state': 'processed',
91 'uploader': user['_id']})
92 except InvalidId:
93 return exc.HTTPNotFound()
94
95 # Still no media? Okay, 404.
96 if not media:
97 return exc.HTTPNotFound()
98
99 return controller(request, media=media, *args, **kwargs)
100
101 return _make_safe(wrapper, controller)