1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
20 from mediagoblin
import messages
, mg_globals
21 from mediagoblin
.db
.models
import (MediaEntry
, MediaTag
, Collection
,
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
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
)
37 from werkzeug
.contrib
.atom
import AtomFeed
38 from werkzeug
.exceptions
import MethodNotAllowed
41 _log
= logging
.getLogger(__name__
)
42 _log
.setLevel(logging
.DEBUG
)
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
51 user
= User
.query
.filter_by(username
=request
.matchdict
['user']).first()
53 return render_404(request
)
54 elif user
.status
!= u
'active':
55 return render_to_response(
57 'mediagoblin/user_pages/user.html',
60 cursor
= MediaEntry
.query
.\
61 filter_by(uploader
= user
.id,
62 state
= u
'processed').order_by(MediaEntry
.created
.desc())
64 pagination
= Pagination(page
, cursor
)
65 media_entries
= pagination()
67 #if no data is available, return NotFound
68 if media_entries
== None:
69 return render_404(request
)
71 user_gallery_url
= request
.urlgen(
72 'mediagoblin.user_pages.user_gallery',
75 return render_to_response(
77 'mediagoblin/user_pages/user.html',
79 'user_gallery_url': user_gallery_url
,
80 'media_entries': media_entries
,
81 'pagination': 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(
91 state
=u
'processed').order_by(MediaEntry
.created
.desc())
93 # Filter potentially by tag too:
95 cursor
= cursor
.filter(
96 MediaEntry
.tags_helper
.any(
97 MediaTag
.slug
== request
.matchdict
['tag']))
100 pagination
= Pagination(page
, cursor
)
101 media_entries
= pagination()
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
)
108 return render_to_response(
110 'mediagoblin/user_pages/gallery.html',
111 {'user': url_user
, 'tag': tag
,
112 'media_entries': media_entries
,
113 'pagination': pagination
})
116 MEDIA_COMMENTS_PER_PAGE
= 50
119 @get_user_media_entry
121 def media_home(request
, media
, page
, **kwargs
):
123 'Homepage' of a MediaEntry()
125 comment_id
= request
.matchdict
.get('comment', None)
128 mark_comment_notification_seen(comment_id
, request
.user
)
130 pagination
= Pagination(
131 page
, media
.get_comments(
132 mg_globals
.app_config
['comments_ascending']),
133 MEDIA_COMMENTS_PER_PAGE
,
136 pagination
= Pagination(
137 page
, media
.get_comments(
138 mg_globals
.app_config
['comments_ascending']),
139 MEDIA_COMMENTS_PER_PAGE
)
141 comments
= pagination()
143 comment_form
= user_forms
.MediaCommentForm(request
.form
)
145 media_template_name
= media
.media_manager
['display_template']
147 return render_to_response(
151 'comments': comments
,
152 'pagination': pagination
,
153 'comment_form': comment_form
,
154 'app_config': mg_globals
.app_config
})
157 @get_media_entry_by_id
158 @require_active_login
159 def media_post_comment(request
, media
):
161 recieves POST from a MediaEntry() comment form, saves the comment.
163 if not request
.method
== 'POST':
164 raise MethodNotAllowed()
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'])
171 # Show error message if commenting is disabled.
172 if not mg_globals
.app_config
['allow_comments']:
173 messages
.add_message(
176 _("Sorry, comments are disabled."))
177 elif not comment
.content
.strip():
178 messages
.add_message(
181 _("Oops, your comment was empty."))
185 messages
.add_message(
186 request
, messages
.SUCCESS
,
187 _('Your comment has been posted!'))
189 trigger_notification(comment
, media
, request
)
191 add_comment_subscription(request
.user
, media
)
193 return redirect_obj(request
, media
)
196 @get_media_entry_by_id
197 @require_active_login
198 def media_collect(request
, media
):
199 """Add media to collection submission"""
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
)
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.'))
212 return render_to_response(
214 'mediagoblin/user_pages/media_collect.html',
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
)
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()
240 # Otherwise, use the collection selected from the drop-down
242 collection
= form
.collection
.data
243 if collection
and collection
.creator
!= request
.user
.id:
246 # Make sure the user actually selected a 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
,
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
)
266 messages
.add_message(request
, messages
.SUCCESS
,
267 _('"%s" added to collection "%s"')
268 % (media
.title
, collection
.title
))
270 return redirect_obj(request
, media
)
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
):
279 form
= user_forms
.ConfirmDeleteForm(request
.form
)
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.
286 messages
.add_message(
287 request
, messages
.SUCCESS
, _('You deleted the media.'))
289 return redirect(request
, "mediagoblin.user_pages.user_home",
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
)
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."))
304 return render_to_response(
306 'mediagoblin/user_pages/media_confirm_delete.html',
311 @active_user_from_url
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()
320 return render_404(request
)
322 cursor
= collection
.get_collection_items()
324 pagination
= Pagination(page
, cursor
)
325 collection_items
= pagination()
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
)
332 return render_to_response(
334 'mediagoblin/user_pages/collection.html',
336 'collection': collection
,
337 'collection_items': collection_items
,
338 'pagination': pagination
})
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
)
347 return render_to_response(
349 'mediagoblin/user_pages/collection_list.html',
351 'collections': collections
})
354 @get_user_collection_item
355 @require_active_login
356 @user_may_alter_collection
357 def collection_item_confirm_remove(request
, collection_item
):
359 form
= user_forms
.ConfirmCollectionItemRemoveForm(request
.form
)
361 if request
.method
== 'POST' and form
.validate():
362 username
= collection_item
.in_collection
.get_creator
.username
363 collection
= collection_item
.in_collection
365 if form
.confirm
.data
is True:
366 entry
= collection_item
.get_media_entry
367 entry
.collected
= entry
.collected
- 1
370 collection_item
.delete()
371 collection
.items
= collection
.items
- 1
374 messages
.add_message(
375 request
, messages
.SUCCESS
, _('You deleted the item from the collection.'))
377 messages
.add_message(
378 request
, messages
.ERROR
,
379 _("The item was not removed because you didn't check that you were sure."))
381 return redirect_obj(request
, collection
)
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."))
390 return render_to_response(
392 'mediagoblin/user_pages/collection_item_confirm_remove.html',
393 {'collection_item': collection_item
,
398 @require_active_login
399 @user_may_alter_collection
400 def collection_confirm_delete(request
, collection
):
402 form
= user_forms
.ConfirmDeleteForm(request
.form
)
404 if request
.method
== 'POST' and form
.validate():
406 username
= collection
.get_creator
.username
408 if form
.confirm
.data
is True:
409 collection_title
= collection
.title
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
419 messages
.add_message(request
, messages
.SUCCESS
,
420 _('You deleted the collection "%s"') % collection_title
)
422 return redirect(request
, "mediagoblin.user_pages.user_home",
425 messages
.add_message(
426 request
, messages
.ERROR
,
427 _("The collection was not deleted because you didn't check that you were sure."))
429 return redirect_obj(request
, collection
)
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."))
438 return render_to_response(
440 'mediagoblin/user_pages/collection_confirm_delete.html',
441 {'collection': collection
,
445 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
= 15
448 def atom_feed(request
):
450 generates the atom feed with the newest images
452 user
= User
.query
.filter_by(
453 username
= request
.matchdict
['user'],
454 status
= u
'active').first()
456 return render_404(request
)
458 cursor
= MediaEntry
.query
.filter_by(
460 state
= u
'processed').\
461 order_by(MediaEntry
.created
.desc()).\
462 limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
)
465 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
468 'href': request
.urlgen(
469 'mediagoblin.user_pages.user_home',
470 qualified
=True, user
=request
.matchdict
['user']),
475 if mg_globals
.app_config
["push_urls"]:
476 for push_url
in mg_globals
.app_config
["push_urls"]:
482 "MediaGoblin: Feed for user '%s'" % request
.matchdict
['user'],
483 feed_url
=request
.url
,
484 id='tag:{host},{year}:gallery.user-{user}'.format(
486 year
=datetime
.datetime
.today().strftime('%Y'),
487 user
=request
.matchdict
['user']),
491 feed
.add(entry
.get('title'),
492 entry
.description_html
,
493 id=entry
.url_for_self(request
.urlgen
, qualified
=True),
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'),
502 'href': entry
.url_for_self(
506 'type': 'text/html'}])
508 return feed
.get_response()
511 def collection_atom_feed(request
):
513 generates the atom feed with the newest images from a collection
515 user
= User
.query
.filter_by(
516 username
= request
.matchdict
['user'],
517 status
= u
'active').first()
519 return render_404(request
)
521 collection
= Collection
.query
.filter_by(
523 slug
=request
.matchdict
['collection']).first()
525 return render_404(request
)
527 cursor
= CollectionItem
.query
.filter_by(
528 collection
=collection
.id) \
529 .order_by(CollectionItem
.added
.desc()) \
530 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
)
533 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
536 'href': collection
.url_for_self(request
.urlgen
, qualified
=True),
541 if mg_globals
.app_config
["push_urls"]:
542 for push_url
in mg_globals
.app_config
["push_urls"]:
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}'\
554 year
=collection
.created
.strftime('%Y'),
555 user
=request
.matchdict
['user'],
556 slug
=collection
.slug
),
560 entry
= item
.get_media_entry
561 feed
.add(entry
.get('title'),
563 id=entry
.url_for_self(request
.urlgen
, qualified
=True),
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'),
572 'href': entry
.url_for_self(
576 'type': 'text/html'}])
578 return feed
.get_response()
581 @require_active_login
582 def processing_panel(request
):
584 Show to the user what media is still in conversion/processing...
585 and what failed, and why!
587 user
= User
.query
.filter_by(username
=request
.matchdict
['user']).first()
588 # TODO: XXX: Should this be a decorator?
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.
595 request
, 'mediagoblin.user_pages.user_home',
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())
604 # Get media entries which have failed to process
605 failed_entries
= MediaEntry
.query
.\
606 filter_by(uploader
= user
.id,
608 order_by(MediaEntry
.created
.desc())
610 processed_entries
= MediaEntry
.query
.\
611 filter_by(uploader
= user
.id,
612 state
= u
'processed').\
613 order_by(MediaEntry
.created
.desc()).\
617 return render_to_response(
619 'mediagoblin/user_pages/processing_panel.html',
621 'processing_entries': processing_entries
,
622 'failed_entries': failed_entries
,
623 'processed_entries': processed_entries
})