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