Fix up tests
[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
fb2fbe2c 18import logging
64712915 19import datetime
52359e91 20
3a8c3a38 21from mediagoblin import messages, mg_globals
9074ee7c 22from mediagoblin.db.util import DESCENDING, ObjectId
64c2a400 23from mediagoblin.db.sql.models import MediaEntry, Collection, CollectionItem
152a3bfa 24from mediagoblin.tools.response import render_to_response, render_404, redirect
ae3bc7fa 25from mediagoblin.tools.translate import pass_to_ugettext as _
152a3bfa
AW
26from mediagoblin.tools.pagination import Pagination
27from mediagoblin.tools.files import delete_media_files
9074ee7c 28from mediagoblin.user_pages import forms as user_forms
252eaf21 29from mediagoblin.user_pages.lib import send_comment_email
f6249408 30
50854db0 31from mediagoblin.decorators import (uses_pagination, get_user_media_entry,
6d1e55b2 32 require_active_login, user_may_delete_media, user_may_alter_collection,
64c2a400 33 get_user_collection, get_user_collection_item, active_user_from_url)
9a16e16f 34
00c39256 35from werkzeug.contrib.atom import AtomFeed
1301a8ad 36
9074ee7c 37
fb2fbe2c
JAN
38_log = logging.getLogger(__name__)
39_log.setLevel(logging.DEBUG)
40
64712915 41
3eb6fc4f 42@uses_pagination
1301a8ad 43def user_home(request, page):
9a16e16f 44 """'Homepage' of a User()"""
7acdbfd3 45 user = request.db.User.find_one({
990d3b69 46 'username': request.matchdict['user']})
7acdbfd3 47 if not user:
de12b4e7 48 return render_404(request)
7a3d00ec 49 elif user.status != u'active':
990d3b69
CAW
50 return render_to_response(
51 request,
52 'mediagoblin/user_pages/user.html',
53 {'user': user})
9a16e16f 54
434b3221 55 cursor = request.db.MediaEntry.find(
eabe6b67 56 {'uploader': user._id,
5bd0adeb 57 'state': u'processed'}).sort('created', DESCENDING)
9a16e16f 58
1301a8ad 59 pagination = Pagination(page, cursor)
ca3ca51c 60 media_entries = pagination()
44e3e917 61
ae85ed0f
BK
62 #if no data is available, return NotFound
63 if media_entries == None:
de12b4e7 64 return render_404(request)
243c3843 65
5949be9a
CAW
66 user_gallery_url = request.urlgen(
67 'mediagoblin.user_pages.user_gallery',
5a4e3ff1 68 user=user.username)
5949be9a 69
9038c9f9
CAW
70 return render_to_response(
71 request,
c9c24934
E
72 'mediagoblin/user_pages/user.html',
73 {'user': user,
5949be9a 74 'user_gallery_url': user_gallery_url,
c9c24934
E
75 'media_entries': media_entries,
76 'pagination': pagination})
f6249408 77
243c3843 78
64c2a400 79@active_user_from_url
184f2240 80@uses_pagination
64c2a400 81def user_gallery(request, page, url_user=None):
184f2240 82 """'Gallery' of a User()"""
64c2a400
SS
83 cursor = MediaEntry.query.filter_by(
84 uploader=url_user.id,
85 state=u'processed').order_by(MediaEntry.created.desc())
86 # Paginate gallery
184f2240 87 pagination = Pagination(page, cursor)
88 media_entries = pagination()
89
90 #if no data is available, return NotFound
64c2a400 91 # TODO: Should we really also return 404 for empty galleries?
184f2240 92 if media_entries == None:
de12b4e7 93 return render_404(request)
243c3843 94
4b5f5a08 95 return render_to_response(
96 request,
97 'mediagoblin/user_pages/gallery.html',
64c2a400 98 {'user': url_user,
4b5f5a08 99 'media_entries': media_entries,
100 'pagination': pagination})
184f2240 101
6f59a3a3 102MEDIA_COMMENTS_PER_PAGE = 50
434b3221 103
243c3843 104
01674e10 105@get_user_media_entry
9074ee7c 106@uses_pagination
6f59a3a3 107def media_home(request, media, page, **kwargs):
9074ee7c
JW
108 """
109 'Homepage' of a MediaEntry()
110 """
af2fcba5
JW
111 if ObjectId(request.matchdict.get('comment')):
112 pagination = Pagination(
7c378f2c
CAW
113 page, media.get_comments(
114 mg_globals.app_config['comments_ascending']),
115 MEDIA_COMMENTS_PER_PAGE,
af2fcba5
JW
116 ObjectId(request.matchdict.get('comment')))
117 else:
118 pagination = Pagination(
7c378f2c
CAW
119 page, media.get_comments(
120 mg_globals.app_config['comments_ascending']),
121 MEDIA_COMMENTS_PER_PAGE)
9074ee7c 122
6f59a3a3 123 comments = pagination()
9074ee7c 124
111a609d 125 comment_form = user_forms.MediaCommentForm(request.form)
9074ee7c 126
5f8b4ae8 127 media_template_name = media.media_manager['display_template']
93bdab9d 128
9038c9f9
CAW
129 return render_to_response(
130 request,
93bdab9d 131 media_template_name,
9074ee7c
JW
132 {'media': media,
133 'comments': comments,
134 'pagination': pagination,
3a8c3a38
JW
135 'comment_form': comment_form,
136 'app_config': mg_globals.app_config})
9074ee7c 137
95e6da02 138
95e12bf2 139@get_user_media_entry
9074ee7c 140@require_active_login
95e12bf2 141def media_post_comment(request, media):
9074ee7c
JW
142 """
143 recieves POST from a MediaEntry() comment form, saves the comment.
144 """
95e12bf2
CAW
145 assert request.method == 'POST'
146
9074ee7c 147 comment = request.db.MediaComment()
8efcd405
E
148 comment.media_entry = media.id
149 comment.author = request.user.id
111a609d 150 comment.content = unicode(request.form['comment_content'])
9074ee7c 151
8efcd405 152 if not comment.content.strip():
7298ffa1
AW
153 messages.add_message(
154 request,
155 messages.ERROR,
eae7d058 156 _("Oops, your comment was empty."))
7298ffa1
AW
157 else:
158 comment.save()
b5d3aec6 159
7298ffa1
AW
160 messages.add_message(
161 request, messages.SUCCESS,
eae7d058 162 _('Your comment has been posted!'))
52359e91 163
252eaf21
DM
164 media_uploader = media.get_uploader
165 #don't send email if you comment on your own post
166 if (comment.author != media_uploader and
00722c99 167 media_uploader.wants_comment_notification):
252eaf21
DM
168 send_comment_email(media_uploader, comment, media, request)
169
95e12bf2
CAW
170 return exc.HTTPFound(
171 location=media.url_for_self(request.urlgen))
00c39256 172
95e6da02 173
be5be115
AW
174@get_user_media_entry
175@require_active_login
176def media_collect(request, media):
177
111a609d 178 form = user_forms.MediaCollectForm(request.form)
be5be115 179 filt = (request.db.Collection.creator == request.user.id)
6d1e55b2
JW
180 form.collection.query = request.db.Collection.query.filter(
181 filt).order_by(request.db.Collection.title)
be5be115
AW
182
183 if request.method == 'POST':
184 if form.validate():
185
186 collection = None
187 collection_item = request.db.CollectionItem()
188
189 # If the user is adding a new collection, use that
111a609d 190 if request.form['collection_title']:
be5be115
AW
191 collection = request.db.Collection()
192 collection.id = ObjectId()
193
194 collection.title = (
111a609d 195 unicode(request.form['collection_title']))
be5be115 196
6d1e55b2 197 collection.description = unicode(
111a609d 198 request.form.get('collection_description'))
be5be115
AW
199 collection.creator = request.user._id
200 collection.generate_slug()
201
202 # Make sure this user isn't duplicating an existing collection
203 existing_collection = request.db.Collection.find_one({
204 'creator': request.user._id,
6d1e55b2
JW
205 'title': collection.title})
206
be5be115
AW
207 if existing_collection:
208 messages.add_message(
6d1e55b2
JW
209 request, messages.ERROR,
210 _('You already have a collection called "%s"!'
211 % collection.title))
212
be5be115
AW
213 return redirect(request, "mediagoblin.user_pages.media_home",
214 user=request.user.username,
215 media=media.id)
216
217 collection.save(validate=True)
218
219 collection_item.collection = collection.id
220 # Otherwise, use the collection selected from the drop-down
221 else:
6d1e55b2 222 collection = request.db.Collection.find_one({
111a609d 223 '_id': request.form.get('collection')})
be5be115
AW
224 collection_item.collection = collection.id
225
226 # Make sure the user actually selected a collection
227 if not collection:
228 messages.add_message(
6d1e55b2
JW
229 request, messages.ERROR,
230 _('You have to select or add a collection'))
be5be115 231 # Check whether media already exists in collection
6d1e55b2
JW
232 elif request.db.CollectionItem.find_one({
233 'media_entry': media.id,
234 'collection': collection_item.collection}):
be5be115 235 messages.add_message(
6d1e55b2
JW
236 request, messages.ERROR,
237 _('"%s" already in collection "%s"'
238 % (media.title, collection.title)))
be5be115
AW
239 else:
240 collection_item.media_entry = media.id
241 collection_item.author = request.user.id
111a609d 242 collection_item.note = unicode(request.form['note'])
be5be115
AW
243 collection_item.save(validate=True)
244
245 collection.items = collection.items + 1
246 collection.save(validate=True)
6d1e55b2 247
be5be115
AW
248 media.collected = media.collected + 1
249 media.save()
250
251 messages.add_message(
6d1e55b2
JW
252 request, messages.SUCCESS, _('"%s" added to collection "%s"'
253 % (media.title, collection.title)))
be5be115
AW
254
255 return redirect(request, "mediagoblin.user_pages.media_home",
c9ac6841 256 user=media.get_uploader.username,
be5be115
AW
257 media=media.id)
258 else:
259 messages.add_message(
6d1e55b2
JW
260 request, messages.ERROR,
261 _('Please check your entries and try again.'))
be5be115
AW
262
263 return render_to_response(
264 request,
265 'mediagoblin/user_pages/media_collect.html',
266 {'media': media,
267 'form': form})
268
269
502073f2
JW
270@get_user_media_entry
271@require_active_login
272@user_may_delete_media
273def media_confirm_delete(request, media):
274
111a609d 275 form = user_forms.ConfirmDeleteForm(request.form)
502073f2
JW
276
277 if request.method == 'POST' and form.validate():
8daef28d 278 if form.confirm.data is True:
05751758 279 username = media.get_uploader.username
502073f2 280
a0a7f87f
BS
281 # Delete all the associated comments
282 for comment in media.get_comments():
283 comment.delete()
284
502073f2 285 # Delete all files on the public storage
fb2fbe2c
JAN
286 try:
287 delete_media_files(media)
288 except OSError, error:
289 _log.error('No such files from the user "{1}"'
290 ' to delete: {0}'.format(str(error), username))
291 messages.add_message(request, messages.ERROR,
292 _('Some of the files with this entry seem'
293 ' to be missing. Deleting anyway.'))
502073f2
JW
294
295 media.delete()
ea33f636
E
296 messages.add_message(
297 request, messages.SUCCESS, _('You deleted the media.'))
502073f2
JW
298
299 return redirect(request, "mediagoblin.user_pages.user_home",
300 user=username)
301 else:
d0ba62e2
PUS
302 messages.add_message(
303 request, messages.ERROR,
56bfd91a 304 _("The media was not deleted because you didn't check that you were sure."))
8d7b549b
E
305 return exc.HTTPFound(
306 location=media.url_for_self(request.urlgen))
502073f2 307
bec591d8 308 if ((request.user.is_admin and
4deda94a 309 request.user._id != media.uploader)):
7a4c0126
CAW
310 messages.add_message(
311 request, messages.WARNING,
312 _("You are about to delete another user's media. "
313 "Proceed with caution."))
314
502073f2
JW
315 return render_to_response(
316 request,
317 'mediagoblin/user_pages/media_confirm_delete.html',
318 {'media': media,
319 'form': form})
320
321
e2ae0f59 322@active_user_from_url
be5be115 323@uses_pagination
e2ae0f59 324def user_collection(request, page, url_user=None):
be5be115 325 """A User-defined Collection"""
e2ae0f59
SS
326 collection = Collection.query.filter_by(
327 get_creator=url_user,
328 slug=request.matchdict['collection']).first()
be5be115 329
e2ae0f59 330 cursor = collection.get_collection_items()
be5be115
AW
331
332 pagination = Pagination(page, cursor)
333 collection_items = pagination()
334
e2ae0f59
SS
335 # if no data is available, return NotFound
336 # TODO: Should an empty collection really also return 404?
be5be115
AW
337 if collection_items == None:
338 return render_404(request)
339
340 return render_to_response(
341 request,
342 'mediagoblin/user_pages/collection.html',
e2ae0f59 343 {'user': url_user,
be5be115
AW
344 'collection': collection,
345 'collection_items': collection_items,
346 'pagination': pagination})
347
348
349@get_user_collection_item
350@require_active_login
351@user_may_alter_collection
352def collection_item_confirm_remove(request, collection_item):
353
111a609d 354 form = user_forms.ConfirmCollectionItemRemoveForm(request.form)
be5be115
AW
355
356 if request.method == 'POST' and form.validate():
357 username = collection_item.in_collection.get_creator.username
358 collection = collection_item.in_collection
359
360 if form.confirm.data is True:
361 entry = collection_item.get_media_entry
362 entry.collected = entry.collected - 1
363 entry.save()
364
365 collection_item.delete()
6d1e55b2 366 collection.items = collection.items - 1
be5be115
AW
367 collection.save()
368
369 messages.add_message(
370 request, messages.SUCCESS, _('You deleted the item from the collection.'))
371 else:
372 messages.add_message(
373 request, messages.ERROR,
374 _("The item was not removed because you didn't check that you were sure."))
375
376 return redirect(request, "mediagoblin.user_pages.user_collection",
377 user=username,
378 collection=collection.slug)
379
380 if ((request.user.is_admin and
cdb35b97 381 request.user._id != collection_item.in_collection.creator)):
be5be115
AW
382 messages.add_message(
383 request, messages.WARNING,
384 _("You are about to delete an item from another user's collection. "
385 "Proceed with caution."))
386
387 return render_to_response(
388 request,
389 'mediagoblin/user_pages/collection_item_confirm_remove.html',
390 {'collection_item': collection_item,
391 'form': form})
392
393
394@get_user_collection
395@require_active_login
396@user_may_alter_collection
397def collection_confirm_delete(request, collection):
398
111a609d 399 form = user_forms.ConfirmDeleteForm(request.form)
be5be115
AW
400
401 if request.method == 'POST' and form.validate():
402
403 username = collection.get_creator.username
404
405 if form.confirm.data is True:
406 collection_title = collection.title
407
408 # Delete all the associated collection items
409 for item in collection.get_collection_items():
410 entry = item.get_media_entry
411 entry.collected = entry.collected - 1
412 entry.save()
413 item.delete()
414
415 collection.delete()
416 messages.add_message(
417 request, messages.SUCCESS, _('You deleted the collection "%s"' % collection_title))
418
419 return redirect(request, "mediagoblin.user_pages.user_home",
420 user=username)
421 else:
422 messages.add_message(
423 request, messages.ERROR,
424 _("The collection was not deleted because you didn't check that you were sure."))
425
426 return redirect(request, "mediagoblin.user_pages.user_collection",
427 user=username,
428 collection=collection.slug)
429
430 if ((request.user.is_admin and
431 request.user._id != collection.creator)):
432 messages.add_message(
433 request, messages.WARNING,
434 _("You are about to delete another user's collection. "
435 "Proceed with caution."))
436
437 return render_to_response(
438 request,
439 'mediagoblin/user_pages/collection_confirm_delete.html',
440 {'collection': collection,
441 'form': form})
442
443
a5303e47 444ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
00c39256 445
243c3843 446
00c39256
BK
447def atom_feed(request):
448 """
449 generates the atom feed with the newest images
450 """
451
452 user = request.db.User.find_one({
453 'username': request.matchdict['user'],
5bd0adeb 454 'status': u'active'})
00c39256 455 if not user:
de12b4e7 456 return render_404(request)
00c39256
BK
457
458 cursor = request.db.MediaEntry.find({
eabe6b67 459 'uploader': user._id,
5bd0adeb 460 'state': u'processed'}) \
00c39256
BK
461 .sort('created', DESCENDING) \
462 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
463
1df68a35
MA
464 """
465 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
466 """
5b1a7bae
MA
467 atomlinks = [{
468 'href': request.urlgen(
469 'mediagoblin.user_pages.user_home',
64712915 470 qualified=True, user=request.matchdict['user']),
5b1a7bae
MA
471 'rel': 'alternate',
472 'type': 'text/html'
64712915
JW
473 }]
474
bb025ebd
MA
475 if mg_globals.app_config["push_urls"]:
476 for push_url in mg_globals.app_config["push_urls"]:
477 atomlinks.append({
478 'rel': 'hub',
479 'href': push_url})
5b1a7bae 480
1df68a35
MA
481 feed = AtomFeed(
482 "MediaGoblin: Feed for user '%s'" % request.matchdict['user'],
00c39256 483 feed_url=request.url,
64712915
JW
484 id='tag:{host},{year}:gallery.user-{user}'.format(
485 host=request.host,
486 year=datetime.datetime.today().strftime('%Y'),
487 user=request.matchdict['user']),
5b1a7bae
MA
488 links=atomlinks)
489
00c39256
BK
490 for entry in cursor:
491 feed.add(entry.get('title'),
1e72e075 492 entry.description_html,
64712915 493 id=entry.url_for_self(request.urlgen, qualified=True),
00c39256 494 content_type='html',
1df68a35
MA
495 author={
496 'name': entry.get_uploader.username,
497 'uri': request.urlgen(
498 'mediagoblin.user_pages.user_home',
499 qualified=True, user=entry.get_uploader.username)},
00c39256 500 updated=entry.get('created'),
1df68a35
MA
501 links=[{
502 'href': entry.url_for_self(
503 request.urlgen,
be5be115
AW
504 qualified=True),
505 'rel': 'alternate',
506 'type': 'text/html'}])
507
508 return feed.get_response()
509
6d1e55b2 510
be5be115
AW
511def collection_atom_feed(request):
512 """
513 generates the atom feed with the newest images from a collection
514 """
515
516 user = request.db.User.find_one({
517 'username': request.matchdict['user'],
518 'status': u'active'})
519 if not user:
520 return render_404(request)
521
522 collection = request.db.Collection.find_one({
523 'creator': user.id,
524 'slug': request.matchdict['collection']})
525
526 cursor = request.db.CollectionItem.find({
527 'collection': collection._id}) \
528 .sort('added', DESCENDING) \
529 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
530
531 """
532 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
533 """
534 atomlinks = [{
535 'href': request.urlgen(
536 'mediagoblin.user_pages.user_collection',
537 qualified=True, user=request.matchdict['user'], collection=collection.slug),
538 'rel': 'alternate',
539 'type': 'text/html'
540 }]
541
542 if mg_globals.app_config["push_urls"]:
543 for push_url in mg_globals.app_config["push_urls"]:
544 atomlinks.append({
545 'rel': 'hub',
546 'href': push_url})
547
548 feed = AtomFeed(
549 "MediaGoblin: Feed for %s's collection %s" % (request.matchdict['user'], collection.title),
550 feed_url=request.url,
551 id='tag:{host},{year}:collection.user-{user}.title-{title}'.format(
552 host=request.host,
553 year=datetime.datetime.today().strftime('%Y'),
554 user=request.matchdict['user'],
555 title=collection.title),
556 links=atomlinks)
557
558 for item in cursor:
559 entry = item.get_media_entry
560 feed.add(entry.get('title'),
561 item.note_html,
562 id=entry.url_for_self(request.urlgen, qualified=True),
563 content_type='html',
564 author={
565 'name': entry.get_uploader.username,
566 'uri': request.urlgen(
567 'mediagoblin.user_pages.user_home',
568 qualified=True, user=entry.get_uploader.username)},
569 updated=item.get('added'),
570 links=[{
571 'href': entry.url_for_self(
572 request.urlgen,
1df68a35
MA
573 qualified=True),
574 'rel': 'alternate',
575 'type': 'text/html'}])
00c39256 576
9074ee7c 577 return feed.get_response()
01c75c7e
CAW
578
579
580@require_active_login
581def processing_panel(request):
582 """
583 Show to the user what media is still in conversion/processing...
584 and what failed, and why!
585 """
586 # Get the user
587 user = request.db.User.find_one(
588 {'username': request.matchdict['user'],
5bd0adeb 589 'status': u'active'})
01c75c7e
CAW
590
591 # Make sure the user exists and is active
592 if not user:
de12b4e7 593 return render_404(request)
7a3d00ec 594 elif user.status != u'active':
01c75c7e
CAW
595 return render_to_response(
596 request,
597 'mediagoblin/user_pages/user.html',
598 {'user': user})
599
600 # XXX: Should this be a decorator?
601 #
602 # Make sure we have permission to access this user's panel. Only
603 # admins and this user herself should be able to do so.
eabe6b67 604 if not (user._id == request.user._id
01c75c7e
CAW
605 or request.user.is_admin):
606 # No? Let's simply redirect to this user's homepage then.
607 return redirect(
608 request, 'mediagoblin.user_pages.user_home',
609 user=request.matchdict['user'])
610
611 # Get media entries which are in-processing
612 processing_entries = request.db.MediaEntry.find(
eabe6b67 613 {'uploader': user._id,
64712915 614 'state': u'processing'}).sort('created', DESCENDING)
01c75c7e
CAW
615
616 # Get media entries which have failed to process
617 failed_entries = request.db.MediaEntry.find(
eabe6b67 618 {'uploader': user._id,
5bd0adeb 619 'state': u'failed'}).sort('created', DESCENDING)
01c75c7e 620
64712915
JW
621 processed_entries = request.db.MediaEntry.find(
622 {'uploader': user._id,
623 'state': u'processed'}).sort('created', DESCENDING).limit(10)
624
01c75c7e
CAW
625 # Render to response
626 return render_to_response(
627 request,
628 'mediagoblin/user_pages/processing_panel.html',
629 {'user': user,
630 'processing_entries': processing_entries,
64712915
JW
631 'failed_entries': failed_entries,
632 'processed_entries': processed_entries})