Let Main Server emit startup notice including version
[mediagoblin.git] / mediagoblin / user_pages / views.py
CommitLineData
7f4ebeed 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
9a16e16f
SS
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
1c63ad5d 17from webob import exc
52359e91 18
3a8c3a38 19from mediagoblin import messages, mg_globals
9074ee7c 20from mediagoblin.db.util import DESCENDING, ObjectId
152a3bfa 21from mediagoblin.tools.response import render_to_response, render_404, redirect
ae3bc7fa 22from mediagoblin.tools.translate import pass_to_ugettext as _
152a3bfa
AW
23from mediagoblin.tools.pagination import Pagination
24from mediagoblin.tools.files import delete_media_files
9074ee7c 25from mediagoblin.user_pages import forms as user_forms
f6249408 26
50854db0 27from mediagoblin.decorators import (uses_pagination, get_user_media_entry,
502073f2 28 require_active_login, user_may_delete_media)
9a16e16f 29
00c39256 30from werkzeug.contrib.atom import AtomFeed
1301a8ad 31
93bdab9d
JW
32from mediagoblin.media_types import get_media_manager
33
9074ee7c 34
3eb6fc4f 35@uses_pagination
1301a8ad 36def user_home(request, page):
9a16e16f 37 """'Homepage' of a User()"""
7acdbfd3 38 user = request.db.User.find_one({
990d3b69 39 'username': request.matchdict['user']})
7acdbfd3 40 if not user:
de12b4e7 41 return render_404(request)
7a3d00ec 42 elif user.status != u'active':
990d3b69
CAW
43 return render_to_response(
44 request,
45 'mediagoblin/user_pages/user.html',
46 {'user': user})
9a16e16f 47
434b3221 48 cursor = request.db.MediaEntry.find(
eabe6b67 49 {'uploader': user._id,
434b3221 50 'state': 'processed'}).sort('created', DESCENDING)
9a16e16f 51
1301a8ad 52 pagination = Pagination(page, cursor)
ca3ca51c 53 media_entries = pagination()
44e3e917 54
ae85ed0f
BK
55 #if no data is available, return NotFound
56 if media_entries == None:
de12b4e7 57 return render_404(request)
243c3843 58
5949be9a
CAW
59 user_gallery_url = request.urlgen(
60 'mediagoblin.user_pages.user_gallery',
5a4e3ff1 61 user=user.username)
5949be9a 62
9038c9f9
CAW
63 return render_to_response(
64 request,
c9c24934
E
65 'mediagoblin/user_pages/user.html',
66 {'user': user,
5949be9a 67 'user_gallery_url': user_gallery_url,
c9c24934
E
68 'media_entries': media_entries,
69 'pagination': pagination})
f6249408 70
243c3843 71
184f2240 72@uses_pagination
73def user_gallery(request, page):
74 """'Gallery' of a User()"""
75 user = request.db.User.find_one({
76 'username': request.matchdict['user'],
77 'status': 'active'})
78 if not user:
de12b4e7 79 return render_404(request)
184f2240 80
81 cursor = request.db.MediaEntry.find(
eabe6b67 82 {'uploader': user._id,
184f2240 83 'state': 'processed'}).sort('created', DESCENDING)
84
85 pagination = Pagination(page, cursor)
86 media_entries = pagination()
87
88 #if no data is available, return NotFound
89 if media_entries == None:
de12b4e7 90 return render_404(request)
243c3843 91
4b5f5a08 92 return render_to_response(
93 request,
94 'mediagoblin/user_pages/gallery.html',
95 {'user': user,
96 'media_entries': media_entries,
97 'pagination': pagination})
184f2240 98
6f59a3a3 99MEDIA_COMMENTS_PER_PAGE = 50
434b3221 100
243c3843 101
01674e10 102@get_user_media_entry
9074ee7c 103@uses_pagination
6f59a3a3 104def media_home(request, media, page, **kwargs):
9074ee7c
JW
105 """
106 'Homepage' of a MediaEntry()
107 """
af2fcba5
JW
108 if ObjectId(request.matchdict.get('comment')):
109 pagination = Pagination(
7c378f2c
CAW
110 page, media.get_comments(
111 mg_globals.app_config['comments_ascending']),
112 MEDIA_COMMENTS_PER_PAGE,
af2fcba5
JW
113 ObjectId(request.matchdict.get('comment')))
114 else:
115 pagination = Pagination(
7c378f2c
CAW
116 page, media.get_comments(
117 mg_globals.app_config['comments_ascending']),
118 MEDIA_COMMENTS_PER_PAGE)
9074ee7c 119
6f59a3a3 120 comments = pagination()
9074ee7c 121
6f59a3a3 122 comment_form = user_forms.MediaCommentForm(request.POST)
9074ee7c 123
f4ee8399 124 media_template_name = get_media_manager(media.media_type)['display_template']
93bdab9d 125
9038c9f9
CAW
126 return render_to_response(
127 request,
93bdab9d 128 media_template_name,
9074ee7c
JW
129 {'media': media,
130 'comments': comments,
131 'pagination': pagination,
3a8c3a38
JW
132 'comment_form': comment_form,
133 'app_config': mg_globals.app_config})
9074ee7c 134
95e6da02 135
95e12bf2 136@get_user_media_entry
9074ee7c 137@require_active_login
95e12bf2 138def media_post_comment(request, media):
9074ee7c
JW
139 """
140 recieves POST from a MediaEntry() comment form, saves the comment.
141 """
95e12bf2
CAW
142 assert request.method == 'POST'
143
9074ee7c 144 comment = request.db.MediaComment()
eabe6b67
E
145 comment['media_entry'] = media._id
146 comment['author'] = request.user._id
08750772 147 comment['content'] = unicode(request.POST['comment_content'])
9074ee7c 148
7298ffa1
AW
149 if not comment['content'].strip():
150 messages.add_message(
151 request,
152 messages.ERROR,
eae7d058 153 _("Oops, your comment was empty."))
7298ffa1
AW
154 else:
155 comment.save()
b5d3aec6 156
7298ffa1
AW
157 messages.add_message(
158 request, messages.SUCCESS,
eae7d058 159 _('Your comment has been posted!'))
52359e91 160
95e12bf2
CAW
161 return exc.HTTPFound(
162 location=media.url_for_self(request.urlgen))
00c39256 163
95e6da02 164
502073f2
JW
165@get_user_media_entry
166@require_active_login
167@user_may_delete_media
168def media_confirm_delete(request, media):
169
170 form = user_forms.ConfirmDeleteForm(request.POST)
171
172 if request.method == 'POST' and form.validate():
8daef28d 173 if form.confirm.data is True:
05751758 174 username = media.get_uploader.username
502073f2
JW
175
176 # Delete all files on the public storage
177 delete_media_files(media)
178
179 media.delete()
ea33f636
E
180 messages.add_message(
181 request, messages.SUCCESS, _('You deleted the media.'))
502073f2
JW
182
183 return redirect(request, "mediagoblin.user_pages.user_home",
184 user=username)
185 else:
d0ba62e2
PUS
186 messages.add_message(
187 request, messages.ERROR,
56bfd91a 188 _("The media was not deleted because you didn't check that you were sure."))
8d7b549b
E
189 return exc.HTTPFound(
190 location=media.url_for_self(request.urlgen))
502073f2 191
bec591d8 192 if ((request.user.is_admin and
4deda94a 193 request.user._id != media.uploader)):
7a4c0126
CAW
194 messages.add_message(
195 request, messages.WARNING,
196 _("You are about to delete another user's media. "
197 "Proceed with caution."))
198
502073f2
JW
199 return render_to_response(
200 request,
201 'mediagoblin/user_pages/media_confirm_delete.html',
202 {'media': media,
203 'form': form})
204
205
a5303e47 206ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
00c39256 207
243c3843 208
00c39256
BK
209def atom_feed(request):
210 """
211 generates the atom feed with the newest images
212 """
213
214 user = request.db.User.find_one({
215 'username': request.matchdict['user'],
216 'status': 'active'})
217 if not user:
de12b4e7 218 return render_404(request)
00c39256
BK
219
220 cursor = request.db.MediaEntry.find({
eabe6b67 221 'uploader': user._id,
00c39256
BK
222 'state': 'processed'}) \
223 .sort('created', DESCENDING) \
224 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
225
1df68a35
MA
226 """
227 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
228 """
5b1a7bae
MA
229 atomlinks = [{
230 'href': request.urlgen(
231 'mediagoblin.user_pages.user_home',
232 qualified=True,user=request.matchdict['user']),
233 'rel': 'alternate',
234 'type': 'text/html'
235 }];
bb025ebd
MA
236 if mg_globals.app_config["push_urls"]:
237 for push_url in mg_globals.app_config["push_urls"]:
238 atomlinks.append({
239 'rel': 'hub',
240 'href': push_url})
5b1a7bae 241
1df68a35
MA
242 feed = AtomFeed(
243 "MediaGoblin: Feed for user '%s'" % request.matchdict['user'],
00c39256 244 feed_url=request.url,
1df68a35 245 id='tag:'+request.host+',2011:gallery.user-'+request.matchdict['user'],
5b1a7bae
MA
246 links=atomlinks)
247
243c3843 248
00c39256
BK
249 for entry in cursor:
250 feed.add(entry.get('title'),
1e72e075 251 entry.description_html,
1df68a35 252 id=entry.url_for_self(request.urlgen,qualified=True),
00c39256 253 content_type='html',
1df68a35
MA
254 author={
255 'name': entry.get_uploader.username,
256 'uri': request.urlgen(
257 'mediagoblin.user_pages.user_home',
258 qualified=True, user=entry.get_uploader.username)},
00c39256 259 updated=entry.get('created'),
1df68a35
MA
260 links=[{
261 'href': entry.url_for_self(
262 request.urlgen,
263 qualified=True),
264 'rel': 'alternate',
265 'type': 'text/html'}])
00c39256 266
9074ee7c 267 return feed.get_response()
01c75c7e
CAW
268
269
270@require_active_login
271def processing_panel(request):
272 """
273 Show to the user what media is still in conversion/processing...
274 and what failed, and why!
275 """
276 # Get the user
277 user = request.db.User.find_one(
278 {'username': request.matchdict['user'],
279 'status': 'active'})
280
281 # Make sure the user exists and is active
282 if not user:
de12b4e7 283 return render_404(request)
7a3d00ec 284 elif user.status != u'active':
01c75c7e
CAW
285 return render_to_response(
286 request,
287 'mediagoblin/user_pages/user.html',
288 {'user': user})
289
290 # XXX: Should this be a decorator?
291 #
292 # Make sure we have permission to access this user's panel. Only
293 # admins and this user herself should be able to do so.
eabe6b67 294 if not (user._id == request.user._id
01c75c7e
CAW
295 or request.user.is_admin):
296 # No? Let's simply redirect to this user's homepage then.
297 return redirect(
298 request, 'mediagoblin.user_pages.user_home',
299 user=request.matchdict['user'])
300
301 # Get media entries which are in-processing
302 processing_entries = request.db.MediaEntry.find(
eabe6b67 303 {'uploader': user._id,
01c75c7e
CAW
304 'state': 'processing'}).sort('created', DESCENDING)
305
306 # Get media entries which have failed to process
307 failed_entries = request.db.MediaEntry.find(
eabe6b67 308 {'uploader': user._id,
01c75c7e
CAW
309 'state': 'failed'}).sort('created', DESCENDING)
310
311 # Render to response
312 return render_to_response(
313 request,
314 'mediagoblin/user_pages/processing_panel.html',
315 {'user': user,
316 'processing_entries': processing_entries,
317 'failed_entries': failed_entries})