Commit | Line | Data |
---|---|---|
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 | 17 | from functools import wraps |
bb3eaf20 | 18 | |
3a199328 | 19 | from urlparse import urljoin |
7f4e42b0 | 20 | from werkzeug.exceptions import Forbidden, NotFound |
059eaee4 | 21 | from werkzeug.urls import url_quote |
bb3eaf20 | 22 | |
f91dcc9d | 23 | from mediagoblin import mg_globals as mgg |
b0c8328e | 24 | from mediagoblin.db.models import MediaEntry, User |
1e03504e | 25 | from mediagoblin.tools.response import redirect, render_404 |
bb3eaf20 CAW |
26 | |
27 | ||
28 | def 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 | 34 | if request.user and \ |
066d49b2 | 35 | request.user.status == u'needs_email_verification': |
d43b472a CAW |
36 | return redirect( |
37 | request, 'mediagoblin.user_pages.user_home', | |
5a4e3ff1 | 38 | user=request.user.username) |
066d49b2 | 39 | elif not request.user or request.user.status != u'active': |
3a199328 JW |
40 | next_url = urljoin( |
41 | request.urlgen('mediagoblin.auth.login', | |
42 | qualified=True), | |
43 | request.url) | |
44 | ||
059eaee4 SS |
45 | return redirect(request, 'mediagoblin.auth.login', |
46 | next=url_quote(next_url)) | |
bb3eaf20 CAW |
47 | |
48 | return controller(request, *args, **kwargs) | |
49 | ||
1e03504e | 50 | return new_controller_func |
3eb6fc4f | 51 | |
ad742028 SS |
52 | def active_user_from_url(controller): |
53 | """Retrieve User() from <user> URL pattern and pass in as url_user=... | |
54 | ||
55 | Returns a 404 if no such active user has been found""" | |
56 | @wraps(controller) | |
57 | def wrapper(request, *args, **kwargs): | |
58 | user = User.query.filter_by(username=request.matchdict['user']).first() | |
59 | if user is None: | |
60 | return render_404(request) | |
61 | ||
62 | return controller(request, *args, url_user=user, **kwargs) | |
63 | ||
64 | return wrapper | |
65 | ||
53c5e0b0 | 66 | |
502073f2 JW |
67 | def user_may_delete_media(controller): |
68 | """ | |
53c5e0b0 | 69 | Require user ownership of the MediaEntry to delete. |
502073f2 | 70 | """ |
1e03504e | 71 | @wraps(controller) |
502073f2 | 72 | def wrapper(request, *args, **kwargs): |
461dd971 | 73 | uploader_id = kwargs['media'].uploader |
bec591d8 | 74 | if not (request.user.is_admin or |
5c2b8486 | 75 | request.user.id == uploader_id): |
cfa92229 | 76 | raise Forbidden() |
502073f2 JW |
77 | |
78 | return controller(request, *args, **kwargs) | |
79 | ||
1e03504e | 80 | return wrapper |
502073f2 | 81 | |
3eb6fc4f | 82 | |
be5be115 AW |
83 | def user_may_alter_collection(controller): |
84 | """ | |
85 | Require user ownership of the Collection to modify. | |
86 | """ | |
87 | @wraps(controller) | |
88 | def wrapper(request, *args, **kwargs): | |
89 | creator_id = request.db.User.find_one( | |
90 | {'username': request.matchdict['user']}).id | |
91 | if not (request.user.is_admin or | |
5c2b8486 | 92 | request.user.id == creator_id): |
cfa92229 | 93 | raise Forbidden() |
be5be115 AW |
94 | |
95 | return controller(request, *args, **kwargs) | |
96 | ||
97 | return wrapper | |
98 | ||
99 | ||
3eb6fc4f BK |
100 | def uses_pagination(controller): |
101 | """ | |
102 | Check request GET 'page' key for wrong values | |
103 | """ | |
1e03504e | 104 | @wraps(controller) |
3eb6fc4f BK |
105 | def wrapper(request, *args, **kwargs): |
106 | try: | |
1301a8ad | 107 | page = int(request.GET.get('page', 1)) |
3eb6fc4f | 108 | if page < 0: |
de12b4e7 | 109 | return render_404(request) |
3eb6fc4f | 110 | except ValueError: |
de12b4e7 | 111 | return render_404(request) |
3eb6fc4f | 112 | |
439e37f7 | 113 | return controller(request, page=page, *args, **kwargs) |
3eb6fc4f | 114 | |
1e03504e | 115 | return wrapper |
724933b1 CAW |
116 | |
117 | ||
01674e10 | 118 | def get_user_media_entry(controller): |
724933b1 CAW |
119 | """ |
120 | Pass in a MediaEntry based off of a url component | |
121 | """ | |
1e03504e | 122 | @wraps(controller) |
724933b1 | 123 | def wrapper(request, *args, **kwargs): |
7f4e42b0 | 124 | user = User.query.filter_by(username=request.matchdict['user']).first() |
01674e10 | 125 | if not user: |
7f4e42b0 SS |
126 | raise NotFound() |
127 | ||
128 | media = MediaEntry.query.filter_by( | |
67c7c811 CAW |
129 | slug=request.matchdict['media'], |
130 | state=u'processed', | |
131 | uploader=user.id).first() | |
724933b1 | 132 | |
724933b1 | 133 | if not media: |
7f4e42b0 SS |
134 | # no media via slug? Grab it via object id |
135 | try: | |
136 | media = MediaEntry.query.filter_by( | |
137 | id = int(request.matchdict['media']), | |
138 | state = u'processed', | |
139 | uploader = user.id).first() | |
140 | except ValueError: | |
141 | # media "id" was no int | |
142 | raise NotFound() | |
143 | ||
144 | if not media: | |
145 | # no media by that id? Okay, 404. | |
146 | raise NotFound() | |
724933b1 CAW |
147 | |
148 | return controller(request, media=media, *args, **kwargs) | |
149 | ||
1e03504e | 150 | return wrapper |
aba81c9f | 151 | |
243c3843 | 152 | |
be5be115 AW |
153 | def get_user_collection(controller): |
154 | """ | |
155 | Pass in a Collection based off of a url component | |
156 | """ | |
157 | @wraps(controller) | |
158 | def wrapper(request, *args, **kwargs): | |
159 | user = request.db.User.find_one( | |
160 | {'username': request.matchdict['user']}) | |
161 | ||
162 | if not user: | |
163 | return render_404(request) | |
164 | ||
165 | collection = request.db.Collection.find_one( | |
166 | {'slug': request.matchdict['collection'], | |
5c2b8486 | 167 | 'creator': user.id}) |
be5be115 AW |
168 | |
169 | # Still no collection? Okay, 404. | |
170 | if not collection: | |
171 | return render_404(request) | |
172 | ||
173 | return controller(request, collection=collection, *args, **kwargs) | |
174 | ||
175 | return wrapper | |
176 | ||
177 | ||
178 | def get_user_collection_item(controller): | |
179 | """ | |
180 | Pass in a CollectionItem based off of a url component | |
181 | """ | |
182 | @wraps(controller) | |
183 | def wrapper(request, *args, **kwargs): | |
184 | user = request.db.User.find_one( | |
185 | {'username': request.matchdict['user']}) | |
186 | ||
187 | if not user: | |
188 | return render_404(request) | |
189 | ||
be5be115 | 190 | collection_item = request.db.CollectionItem.find_one( |
5c2b8486 | 191 | {'id': request.matchdict['collection_item'] }) |
be5be115 AW |
192 | |
193 | # Still no collection item? Okay, 404. | |
194 | if not collection_item: | |
195 | return render_404(request) | |
196 | ||
197 | return controller(request, collection_item=collection_item, *args, **kwargs) | |
198 | ||
199 | return wrapper | |
200 | ||
201 | ||
aba81c9f E |
202 | def get_media_entry_by_id(controller): |
203 | """ | |
204 | Pass in a MediaEntry based off of a url component | |
205 | """ | |
1e03504e | 206 | @wraps(controller) |
aba81c9f | 207 | def wrapper(request, *args, **kwargs): |
71717fd5 | 208 | media = MediaEntry.query.filter_by( |
461dd971 | 209 | id=request.matchdict['media_id'], |
71717fd5 | 210 | state=u'processed').first() |
aba81c9f E |
211 | # Still no media? Okay, 404. |
212 | if not media: | |
de12b4e7 | 213 | return render_404(request) |
aba81c9f | 214 | |
461dd971 E |
215 | given_username = request.matchdict.get('user') |
216 | if given_username and (given_username != media.get_uploader.username): | |
217 | return render_404(request) | |
218 | ||
aba81c9f E |
219 | return controller(request, media=media, *args, **kwargs) |
220 | ||
1e03504e | 221 | return wrapper |
f91dcc9d SS |
222 | |
223 | ||
224 | def get_workbench(func): | |
225 | """Decorator, passing in a workbench as kwarg which is cleaned up afterwards""" | |
226 | ||
227 | @wraps(func) | |
228 | def new_func(*args, **kwargs): | |
bd6fe977 | 229 | with mgg.workbench_manager.create() as workbench: |
f91dcc9d SS |
230 | return func(*args, workbench=workbench, **kwargs) |
231 | ||
232 | return new_func |