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