| 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 webob import exc |
| 19 | |
| 20 | from mediagoblin.util import redirect |
| 21 | from mediagoblin.db.util import ObjectId, InvalidId |
| 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 request.user and \ |
| 40 | request.user.get('status') == u'needs_email_verification': |
| 41 | return redirect(request, |
| 42 | 'mediagoblin.auth.verify_email_notice') |
| 43 | elif not request.user or request.user.get('status') != u'active': |
| 44 | return exc.HTTPFound( |
| 45 | location="%s?next=%s" % ( |
| 46 | request.urlgen("mediagoblin.auth.login"), |
| 47 | request.path_info)) |
| 48 | |
| 49 | return controller(request, *args, **kwargs) |
| 50 | |
| 51 | return _make_safe(new_controller_func, controller) |
| 52 | |
| 53 | |
| 54 | def uses_pagination(controller): |
| 55 | """ |
| 56 | Check request GET 'page' key for wrong values |
| 57 | """ |
| 58 | def wrapper(request, *args, **kwargs): |
| 59 | try: |
| 60 | page = int(request.GET.get('page', 1)) |
| 61 | if page < 0: |
| 62 | return exc.HTTPNotFound() |
| 63 | except ValueError: |
| 64 | return exc.HTTPNotFound() |
| 65 | |
| 66 | return controller(request, page=page, *args, **kwargs) |
| 67 | |
| 68 | return _make_safe(wrapper, controller) |
| 69 | |
| 70 | |
| 71 | def get_user_media_entry(controller): |
| 72 | """ |
| 73 | Pass in a MediaEntry based off of a url component |
| 74 | """ |
| 75 | def wrapper(request, *args, **kwargs): |
| 76 | user = request.db.User.find_one( |
| 77 | {'username': request.matchdict['user']}) |
| 78 | |
| 79 | if not user: |
| 80 | return exc.HTTPNotFound() |
| 81 | |
| 82 | media = request.db.MediaEntry.find_one( |
| 83 | {'slug': request.matchdict['media'], |
| 84 | 'state': 'processed', |
| 85 | 'uploader': user['_id']}) |
| 86 | |
| 87 | # no media via slug? Grab it via ObjectId |
| 88 | if not media: |
| 89 | try: |
| 90 | media = request.db.MediaEntry.find_one( |
| 91 | {'_id': ObjectId(request.matchdict['media']), |
| 92 | 'state': 'processed', |
| 93 | 'uploader': user['_id']}) |
| 94 | except InvalidId: |
| 95 | return exc.HTTPNotFound() |
| 96 | |
| 97 | # Still no media? Okay, 404. |
| 98 | if not media: |
| 99 | return exc.HTTPNotFound() |
| 100 | |
| 101 | return controller(request, media=media, *args, **kwargs) |
| 102 | |
| 103 | return _make_safe(wrapper, controller) |
| 104 | |
| 105 | def get_media_entry_by_id(controller): |
| 106 | """ |
| 107 | Pass in a MediaEntry based off of a url component |
| 108 | """ |
| 109 | def wrapper(request, *args, **kwargs): |
| 110 | try: |
| 111 | media = request.db.MediaEntry.find_one( |
| 112 | {'_id': ObjectId(request.matchdict['media']), |
| 113 | 'state': 'processed'}) |
| 114 | except InvalidId: |
| 115 | return exc.HTTPNotFound() |
| 116 | |
| 117 | # Still no media? Okay, 404. |
| 118 | if not media: |
| 119 | return exc.HTTPNotFound() |
| 120 | |
| 121 | return controller(request, media=media, *args, **kwargs) |
| 122 | |
| 123 | return _make_safe(wrapper, controller) |