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