Commit | Line | Data |
---|---|---|
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 | ||
01674e10 | 18 | from bson.errors import InvalidId |
bb3eaf20 CAW |
19 | from webob import exc |
20 | ||
724933b1 CAW |
21 | from mediagoblin.db.util import ObjectId |
22 | ||
bb3eaf20 CAW |
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): | |
bcec749b AM |
39 | if request.user and request.user.get('status') == u'needs_email_verification': |
40 | return exc.HTTPFound( | |
41 | location = request.urlgen('mediagoblin.auth.verify_email_notice')) | |
42 | elif not request.user or request.user.get('status') != u'active': | |
bb3eaf20 | 43 | return exc.HTTPFound( |
7eba0306 | 44 | location="%s?next=%s" % ( |
bcec749b | 45 | request.urlgen("mediagoblin.auth.login"), |
7eba0306 | 46 | request.path_info)) |
bb3eaf20 CAW |
47 | |
48 | return controller(request, *args, **kwargs) | |
49 | ||
50 | return _make_safe(new_controller_func, controller) | |
3eb6fc4f BK |
51 | |
52 | ||
53 | def uses_pagination(controller): | |
54 | """ | |
55 | Check request GET 'page' key for wrong values | |
56 | """ | |
57 | def wrapper(request, *args, **kwargs): | |
58 | try: | |
1301a8ad | 59 | page = int(request.GET.get('page', 1)) |
3eb6fc4f BK |
60 | if page < 0: |
61 | return exc.HTTPNotFound() | |
62 | except ValueError: | |
63 | return exc.HTTPNotFound() | |
3eb6fc4f | 64 | |
439e37f7 | 65 | return controller(request, page=page, *args, **kwargs) |
3eb6fc4f | 66 | |
3c2567ac | 67 | return _make_safe(wrapper, controller) |
724933b1 CAW |
68 | |
69 | ||
01674e10 | 70 | def get_user_media_entry(controller): |
724933b1 CAW |
71 | """ |
72 | Pass in a MediaEntry based off of a url component | |
73 | """ | |
74 | def wrapper(request, *args, **kwargs): | |
01674e10 CAW |
75 | user = request.db.User.find_one( |
76 | {'username': request.matchdict['user']}) | |
77 | ||
78 | if not user: | |
79 | return exc.HTTPNotFound() | |
80 | ||
724933b1 CAW |
81 | media = request.db.MediaEntry.find_one( |
82 | {'slug': request.matchdict['media'], | |
01674e10 | 83 | 'state': 'processed', |
16509be1 | 84 | 'uploader': user['_id']}) |
724933b1 CAW |
85 | |
86 | # no media via slug? Grab it via ObjectId | |
87 | if not media: | |
01674e10 CAW |
88 | try: |
89 | media = request.db.MediaEntry.find_one( | |
90 | {'_id': ObjectId(request.matchdict['media']), | |
91 | 'state': 'processed', | |
16509be1 | 92 | 'uploader': user['_id']}) |
01674e10 CAW |
93 | except InvalidId: |
94 | return exc.HTTPNotFound() | |
724933b1 CAW |
95 | |
96 | # Still no media? Okay, 404. | |
97 | if not media: | |
98 | return exc.HTTPNotFound() | |
99 | ||
100 | return controller(request, media=media, *args, **kwargs) | |
101 | ||
102 | return _make_safe(wrapper, controller) |