Fixed validation in API post_entry.
[mediagoblin.git] / mediagoblin / decorators.py
CommitLineData
bb3eaf20 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
bb3eaf20
CAW
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
1e03504e 17from functools import wraps
bb3eaf20 18
3a199328
JW
19from urlparse import urljoin
20from urllib import urlencode
21
bb3eaf20
CAW
22from webob import exc
23
3efdd97c 24from mediagoblin.db.util import ObjectId, InvalidId
1e03504e 25from mediagoblin.tools.response import redirect, render_404
bb3eaf20
CAW
26
27
28def require_active_login(controller):
29 """
30 Require an active login from the user.
31 """
1e03504e 32 @wraps(controller)
bb3eaf20 33 def new_controller_func(request, *args, **kwargs):
a72c504b
CAW
34 if request.user and \
35 request.user.get('status') == u'needs_email_verification':
d43b472a
CAW
36 return redirect(
37 request, 'mediagoblin.user_pages.user_home',
5a4e3ff1 38 user=request.user.username)
bcec749b 39 elif not request.user or request.user.get('status') != u'active':
3a199328
JW
40 next_url = urljoin(
41 request.urlgen('mediagoblin.auth.login',
42 qualified=True),
43 request.url)
44
bb3eaf20 45 return exc.HTTPFound(
3a199328
JW
46 location='?'.join([
47 request.urlgen('mediagoblin.auth.login'),
48 urlencode({
49 'next': next_url})]))
bb3eaf20
CAW
50
51 return controller(request, *args, **kwargs)
52
1e03504e 53 return new_controller_func
3eb6fc4f 54
53c5e0b0 55
502073f2
JW
56def user_may_delete_media(controller):
57 """
53c5e0b0 58 Require user ownership of the MediaEntry to delete.
502073f2 59 """
1e03504e 60 @wraps(controller)
502073f2 61 def wrapper(request, *args, **kwargs):
4deda94a
E
62 uploader_id = request.db.MediaEntry.find_one(
63 {'_id': ObjectId(request.matchdict['media'])}).uploader
bec591d8 64 if not (request.user.is_admin or
4deda94a 65 request.user._id == uploader_id):
502073f2
JW
66 return exc.HTTPForbidden()
67
68 return controller(request, *args, **kwargs)
69
1e03504e 70 return wrapper
502073f2 71
3eb6fc4f
BK
72
73def uses_pagination(controller):
74 """
75 Check request GET 'page' key for wrong values
76 """
1e03504e 77 @wraps(controller)
3eb6fc4f
BK
78 def wrapper(request, *args, **kwargs):
79 try:
1301a8ad 80 page = int(request.GET.get('page', 1))
3eb6fc4f 81 if page < 0:
de12b4e7 82 return render_404(request)
3eb6fc4f 83 except ValueError:
de12b4e7 84 return render_404(request)
3eb6fc4f 85
439e37f7 86 return controller(request, page=page, *args, **kwargs)
3eb6fc4f 87
1e03504e 88 return wrapper
724933b1
CAW
89
90
01674e10 91def get_user_media_entry(controller):
724933b1
CAW
92 """
93 Pass in a MediaEntry based off of a url component
94 """
1e03504e 95 @wraps(controller)
724933b1 96 def wrapper(request, *args, **kwargs):
01674e10
CAW
97 user = request.db.User.find_one(
98 {'username': request.matchdict['user']})
99
100 if not user:
de12b4e7 101 return render_404(request)
724933b1
CAW
102 media = request.db.MediaEntry.find_one(
103 {'slug': request.matchdict['media'],
5bd0adeb 104 'state': u'processed',
eabe6b67 105 'uploader': user._id})
724933b1
CAW
106
107 # no media via slug? Grab it via ObjectId
108 if not media:
01674e10
CAW
109 try:
110 media = request.db.MediaEntry.find_one(
111 {'_id': ObjectId(request.matchdict['media']),
5bd0adeb 112 'state': u'processed',
eabe6b67 113 'uploader': user._id})
01674e10 114 except InvalidId:
de12b4e7 115 return render_404(request)
724933b1
CAW
116
117 # Still no media? Okay, 404.
118 if not media:
de12b4e7 119 return render_404(request)
724933b1
CAW
120
121 return controller(request, media=media, *args, **kwargs)
122
1e03504e 123 return wrapper
aba81c9f 124
243c3843 125
aba81c9f
E
126def get_media_entry_by_id(controller):
127 """
128 Pass in a MediaEntry based off of a url component
129 """
1e03504e 130 @wraps(controller)
aba81c9f
E
131 def wrapper(request, *args, **kwargs):
132 try:
133 media = request.db.MediaEntry.find_one(
134 {'_id': ObjectId(request.matchdict['media']),
5bd0adeb 135 'state': u'processed'})
aba81c9f 136 except InvalidId:
de12b4e7 137 return render_404(request)
aba81c9f
E
138
139 # Still no media? Okay, 404.
140 if not media:
de12b4e7 141 return render_404(request)
aba81c9f
E
142
143 return controller(request, media=media, *args, **kwargs)
144
1e03504e 145 return wrapper