1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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.
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.
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/>.
20 from mediagoblin
.db
.util
import ObjectId
23 def _make_safe(decorator
, original
):
25 Copy the function data from the old function to the decorator.
27 decorator
.__name
__ = original
.__name
__
28 decorator
.__dict
__ = original
.__dict
__
29 decorator
.__doc
__ = original
.__doc
__
33 def require_active_login(controller
):
35 Require an active login from the user.
37 def new_controller_func(request
, *args
, **kwargs
):
38 if not request
.user
or not request
.user
.get('status') == u
'active':
39 # TODO: Indicate to the user that they were redirected
40 # here because an *active* user is required.
42 location
="%s?next=%s" % (
43 request
.urlgen("mediagoblin.auth.login"),
46 return controller(request
, *args
, **kwargs
)
48 return _make_safe(new_controller_func
, controller
)
51 def uses_pagination(controller
):
53 Check request GET 'page' key for wrong values
55 def wrapper(request
, *args
, **kwargs
):
57 page
= int(request
.GET
.get('page', 1))
59 return exc
.HTTPNotFound()
61 return exc
.HTTPNotFound()
63 return controller(request
, page
=page
, *args
, **kwargs
)
65 return _make_safe(wrapper
, controller
)
68 def get_media_entry(controller
):
70 Pass in a MediaEntry based off of a url component
72 def wrapper(request
, *args
, **kwargs
):
73 media
= request
.db
.MediaEntry
.find_one(
74 {'slug': request
.matchdict
['media'],
75 'state': 'processed'})
77 # no media via slug? Grab it via ObjectId
79 media
= request
.db
.MediaEntry
.find_one(
80 {'_id': ObjectId(request
.matchdict
['media']),
81 'state': 'processed'})
83 # Still no media? Okay, 404.
85 return exc
.HTTPNotFound()
87 return controller(request
, media
=media
, *args
, **kwargs
)
89 return _make_safe(wrapper
, controller
)