# along with this program. If not, see <http://www.gnu.org/licenses/>.
+from bson.errors import InvalidId
from webob import exc
+from mediagoblin.util import redirect
+from mediagoblin.db.util import ObjectId
+
def _make_safe(decorator, original):
"""
Require an active login from the user.
"""
def new_controller_func(request, *args, **kwargs):
- if not request.user or not request.user.get('status') == u'active':
- # TODO: Indicate to the user that they were redirected
- # here because an *active* user is required.
+ if request.user and \
+ request.user.get('status') == u'needs_email_verification':
+ return redirect(request,
+ 'mediagoblin.auth.verify_email_notice')
+ elif not request.user or request.user.get('status') != u'active':
return exc.HTTPFound(
location="%s?next=%s" % (
request.urlgen("mediagoblin.auth.login"),
"""
def wrapper(request, *args, **kwargs):
try:
- page = int(request.str_GET['page'])
+ page = int(request.GET.get('page', 1))
if page < 0:
return exc.HTTPNotFound()
except ValueError:
return exc.HTTPNotFound()
- except KeyError:
- request.str_GET['page'] = 1
- return controller(request, *args, **kwargs)
+ return controller(request, page=page, *args, **kwargs)
+
+ return _make_safe(wrapper, controller)
+
+
+def get_user_media_entry(controller):
+ """
+ Pass in a MediaEntry based off of a url component
+ """
+ def wrapper(request, *args, **kwargs):
+ user = request.db.User.find_one(
+ {'username': request.matchdict['user']})
+
+ if not user:
+ return exc.HTTPNotFound()
+
+ media = request.db.MediaEntry.find_one(
+ {'slug': request.matchdict['media'],
+ 'state': 'processed',
+ 'uploader': user['_id']})
+
+ # no media via slug? Grab it via ObjectId
+ if not media:
+ try:
+ media = request.db.MediaEntry.find_one(
+ {'_id': ObjectId(request.matchdict['media']),
+ 'state': 'processed',
+ 'uploader': user['_id']})
+ except InvalidId:
+ return exc.HTTPNotFound()
+
+ # Still no media? Okay, 404.
+ if not media:
+ return exc.HTTPNotFound()
+
+ return controller(request, media=media, *args, **kwargs)
+
+ return _make_safe(wrapper, controller)
+
+def get_media_entry_by_id(controller):
+ """
+ Pass in a MediaEntry based off of a url component
+ """
+ def wrapper(request, *args, **kwargs):
+ try:
+ media = request.db.MediaEntry.find_one(
+ {'_id': ObjectId(request.matchdict['media']),
+ 'state': 'processed'})
+ except InvalidId:
+ return exc.HTTPNotFound()
+
+ # Still no media? Okay, 404.
+ if not media:
+ return exc.HTTPNotFound()
+
+ return controller(request, media=media, *args, **kwargs)
- return _make_safe(wrapper,controller)
+ return _make_safe(wrapper, controller)