Changed how the comment was encoded/read.
[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 comment = unicode(request.form['comment_content'])
203 cleancomment = { "content":cleaned_markdown_conversion(comment)}
204
205 return Response(json.dumps(cleancomment))
206
207 @get_media_entry_by_id
208 @require_active_login
209 def media_collect(request, media):
210 """Add media to collection submission"""
211
212 form = user_forms.MediaCollectForm(request.form)
213 # A user's own collections:
214 form.collection.query = Collection.query.filter_by(
215 creator = request.user.id).order_by(Collection.title)
216
217 if request.method != 'POST' or not form.validate():
218 # No POST submission, or invalid form
219 if not form.validate():
220 messages.add_message(request, messages.ERROR,
221 _('Please check your entries and try again.'))
222
223 return render_to_response(
224 request,
225 'mediagoblin/user_pages/media_collect.html',
226 {'media': media,
227 'form': form})
228
229 # If we are here, method=POST and the form is valid, submit things.
230 # If the user is adding a new collection, use that:
231 if form.collection_title.data:
232 # Make sure this user isn't duplicating an existing collection
233 existing_collection = Collection.query.filter_by(
234 creator=request.user.id,
235 title=form.collection_title.data).first()
236 if existing_collection:
237 messages.add_message(request, messages.ERROR,
238 _('You already have a collection called "%s"!')
239 % existing_collection.title)
240 return redirect(request, "mediagoblin.user_pages.media_home",
241 user=media.get_uploader.username,
242 media=media.slug_or_id)
243
244 collection = Collection()
245 collection.title = form.collection_title.data
246 collection.description = form.collection_description.data
247 collection.creator = request.user.id
248 collection.generate_slug()
249 collection.save()
250
251 # Otherwise, use the collection selected from the drop-down
252 else:
253 collection = form.collection.data
254 if collection and collection.creator != request.user.id:
255 collection = None
256
257 # Make sure the user actually selected a collection
258 if not collection:
259 messages.add_message(
260 request, messages.ERROR,
261 _('You have to select or add a collection'))
262 return redirect(request, "mediagoblin.user_pages.media_collect",
263 user=media.get_uploader.username,
264 media_id=media.id)
265
266
267 # Check whether media already exists in collection
268 elif CollectionItem.query.filter_by(
269 media_entry=media.id,
270 collection=collection.id).first():
271 messages.add_message(request, messages.ERROR,
272 _('"%s" already in collection "%s"')
273 % (media.title, collection.title))
274 else: # Add item to collection
275 add_media_to_collection(collection, media, form.note.data)
276
277 messages.add_message(request, messages.SUCCESS,
278 _('"%s" added to collection "%s"')
279 % (media.title, collection.title))
280
281 return redirect_obj(request, media)
282
283
284 #TODO: Why does @user_may_delete_media not implicate @require_active_login?
285 @get_media_entry_by_id
286 @require_active_login
287 @user_may_delete_media
288 def media_confirm_delete(request, media):
289
290 form = user_forms.ConfirmDeleteForm(request.form)
291
292 if request.method == 'POST' and form.validate():
293 if form.confirm.data is True:
294 username = media.get_uploader.username
295 # Delete MediaEntry and all related files, comments etc.
296 media.delete()
297 messages.add_message(
298 request, messages.SUCCESS, _('You deleted the media.'))
299
300 return redirect(request, "mediagoblin.user_pages.user_home",
301 user=username)
302 else:
303 messages.add_message(
304 request, messages.ERROR,
305 _("The media was not deleted because you didn't check that you were sure."))
306 return redirect_obj(request, media)
307
308 if ((request.user.is_admin and
309 request.user.id != media.uploader)):
310 messages.add_message(
311 request, messages.WARNING,
312 _("You are about to delete another user's media. "
313 "Proceed with caution."))
314
315 return render_to_response(
316 request,
317 'mediagoblin/user_pages/media_confirm_delete.html',
318 {'media': media,
319 'form': form})
320
321
322 @active_user_from_url
323 @uses_pagination
324 def user_collection(request, page, url_user=None):
325 """A User-defined Collection"""
326 collection = Collection.query.filter_by(
327 get_creator=url_user,
328 slug=request.matchdict['collection']).first()
329
330 if not collection:
331 return render_404(request)
332
333 cursor = collection.get_collection_items()
334
335 pagination = Pagination(page, cursor)
336 collection_items = pagination()
337
338 # if no data is available, return NotFound
339 # TODO: Should an empty collection really also return 404?
340 if collection_items == None:
341 return render_404(request)
342
343 return render_to_response(
344 request,
345 'mediagoblin/user_pages/collection.html',
346 {'user': url_user,
347 'collection': collection,
348 'collection_items': collection_items,
349 'pagination': pagination})
350
351
352 @active_user_from_url
353 def collection_list(request, url_user=None):
354 """A User-defined Collection"""
355 collections = Collection.query.filter_by(
356 get_creator=url_user)
357
358 return render_to_response(
359 request,
360 'mediagoblin/user_pages/collection_list.html',
361 {'user': url_user,
362 'collections': collections})
363
364
365 @get_user_collection_item
366 @require_active_login
367 @user_may_alter_collection
368 def collection_item_confirm_remove(request, collection_item):
369
370 form = user_forms.ConfirmCollectionItemRemoveForm(request.form)
371
372 if request.method == 'POST' and form.validate():
373 username = collection_item.in_collection.get_creator.username
374 collection = collection_item.in_collection
375
376 if form.confirm.data is True:
377 entry = collection_item.get_media_entry
378 entry.collected = entry.collected - 1
379 entry.save()
380
381 collection_item.delete()
382 collection.items = collection.items - 1
383 collection.save()
384
385 messages.add_message(
386 request, messages.SUCCESS, _('You deleted the item from the collection.'))
387 else:
388 messages.add_message(
389 request, messages.ERROR,
390 _("The item was not removed because you didn't check that you were sure."))
391
392 return redirect_obj(request, collection)
393
394 if ((request.user.is_admin and
395 request.user.id != collection_item.in_collection.creator)):
396 messages.add_message(
397 request, messages.WARNING,
398 _("You are about to delete an item from another user's collection. "
399 "Proceed with caution."))
400
401 return render_to_response(
402 request,
403 'mediagoblin/user_pages/collection_item_confirm_remove.html',
404 {'collection_item': collection_item,
405 'form': form})
406
407
408 @get_user_collection
409 @require_active_login
410 @user_may_alter_collection
411 def collection_confirm_delete(request, collection):
412
413 form = user_forms.ConfirmDeleteForm(request.form)
414
415 if request.method == 'POST' and form.validate():
416
417 username = collection.get_creator.username
418
419 if form.confirm.data is True:
420 collection_title = collection.title
421
422 # Delete all the associated collection items
423 for item in collection.get_collection_items():
424 entry = item.get_media_entry
425 entry.collected = entry.collected - 1
426 entry.save()
427 item.delete()
428
429 collection.delete()
430 messages.add_message(request, messages.SUCCESS,
431 _('You deleted the collection "%s"') % collection_title)
432
433 return redirect(request, "mediagoblin.user_pages.user_home",
434 user=username)
435 else:
436 messages.add_message(
437 request, messages.ERROR,
438 _("The collection was not deleted because you didn't check that you were sure."))
439
440 return redirect_obj(request, collection)
441
442 if ((request.user.is_admin and
443 request.user.id != collection.creator)):
444 messages.add_message(
445 request, messages.WARNING,
446 _("You are about to delete another user's collection. "
447 "Proceed with caution."))
448
449 return render_to_response(
450 request,
451 'mediagoblin/user_pages/collection_confirm_delete.html',
452 {'collection': collection,
453 'form': form})
454
455
456 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS = 15
457
458
459 def atom_feed(request):
460 """
461 generates the atom feed with the newest images
462 """
463 user = User.query.filter_by(
464 username = request.matchdict['user'],
465 status = u'active').first()
466 if not user:
467 return render_404(request)
468
469 cursor = MediaEntry.query.filter_by(
470 uploader = user.id,
471 state = u'processed').\
472 order_by(MediaEntry.created.desc()).\
473 limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
474
475 """
476 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
477 """
478 atomlinks = [{
479 'href': request.urlgen(
480 'mediagoblin.user_pages.user_home',
481 qualified=True, user=request.matchdict['user']),
482 'rel': 'alternate',
483 'type': 'text/html'
484 }]
485
486 if mg_globals.app_config["push_urls"]:
487 for push_url in mg_globals.app_config["push_urls"]:
488 atomlinks.append({
489 'rel': 'hub',
490 'href': push_url})
491
492 feed = AtomFeed(
493 "MediaGoblin: Feed for user '%s'" % request.matchdict['user'],
494 feed_url=request.url,
495 id='tag:{host},{year}:gallery.user-{user}'.format(
496 host=request.host,
497 year=datetime.datetime.today().strftime('%Y'),
498 user=request.matchdict['user']),
499 links=atomlinks)
500
501 for entry in cursor:
502 feed.add(entry.get('title'),
503 entry.description_html,
504 id=entry.url_for_self(request.urlgen, qualified=True),
505 content_type='html',
506 author={
507 'name': entry.get_uploader.username,
508 'uri': request.urlgen(
509 'mediagoblin.user_pages.user_home',
510 qualified=True, user=entry.get_uploader.username)},
511 updated=entry.get('created'),
512 links=[{
513 'href': entry.url_for_self(
514 request.urlgen,
515 qualified=True),
516 'rel': 'alternate',
517 'type': 'text/html'}])
518
519 return feed.get_response()
520
521
522 def collection_atom_feed(request):
523 """
524 generates the atom feed with the newest images from a collection
525 """
526 user = User.query.filter_by(
527 username = request.matchdict['user'],
528 status = u'active').first()
529 if not user:
530 return render_404(request)
531
532 collection = Collection.query.filter_by(
533 creator=user.id,
534 slug=request.matchdict['collection']).first()
535 if not collection:
536 return render_404(request)
537
538 cursor = CollectionItem.query.filter_by(
539 collection=collection.id) \
540 .order_by(CollectionItem.added.desc()) \
541 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
542
543 """
544 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
545 """
546 atomlinks = [{
547 'href': collection.url_for_self(request.urlgen, qualified=True),
548 'rel': 'alternate',
549 'type': 'text/html'
550 }]
551
552 if mg_globals.app_config["push_urls"]:
553 for push_url in mg_globals.app_config["push_urls"]:
554 atomlinks.append({
555 'rel': 'hub',
556 'href': push_url})
557
558 feed = AtomFeed(
559 "MediaGoblin: Feed for %s's collection %s" %
560 (request.matchdict['user'], collection.title),
561 feed_url=request.url,
562 id=u'tag:{host},{year}:gnu-mediagoblin.{user}.collection.{slug}'\
563 .format(
564 host=request.host,
565 year=collection.created.strftime('%Y'),
566 user=request.matchdict['user'],
567 slug=collection.slug),
568 links=atomlinks)
569
570 for item in cursor:
571 entry = item.get_media_entry
572 feed.add(entry.get('title'),
573 item.note_html,
574 id=entry.url_for_self(request.urlgen, qualified=True),
575 content_type='html',
576 author={
577 'name': entry.get_uploader.username,
578 'uri': request.urlgen(
579 'mediagoblin.user_pages.user_home',
580 qualified=True, user=entry.get_uploader.username)},
581 updated=item.get('added'),
582 links=[{
583 'href': entry.url_for_self(
584 request.urlgen,
585 qualified=True),
586 'rel': 'alternate',
587 'type': 'text/html'}])
588
589 return feed.get_response()
590
591
592 @require_active_login
593 def processing_panel(request):
594 """
595 Show to the user what media is still in conversion/processing...
596 and what failed, and why!
597 """
598 user = User.query.filter_by(username=request.matchdict['user']).first()
599 # TODO: XXX: Should this be a decorator?
600 #
601 # Make sure we have permission to access this user's panel. Only
602 # admins and this user herself should be able to do so.
603 if not (user.id == request.user.id or request.user.is_admin):
604 # No? Simply redirect to this user's homepage.
605 return redirect(
606 request, 'mediagoblin.user_pages.user_home',
607 user=user.username)
608
609 # Get media entries which are in-processing
610 processing_entries = MediaEntry.query.\
611 filter_by(uploader = user.id,
612 state = u'processing').\
613 order_by(MediaEntry.created.desc())
614
615 # Get media entries which have failed to process
616 failed_entries = MediaEntry.query.\
617 filter_by(uploader = user.id,
618 state = u'failed').\
619 order_by(MediaEntry.created.desc())
620
621 processed_entries = MediaEntry.query.\
622 filter_by(uploader = user.id,
623 state = u'processed').\
624 order_by(MediaEntry.created.desc()).\
625 limit(10)
626
627 # Render to response
628 return render_to_response(
629 request,
630 'mediagoblin/user_pages/processing_panel.html',
631 {'user': user,
632 'processing_entries': processing_entries,
633 'failed_entries': failed_entries,
634 'processed_entries': processed_entries})