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
,
22 CollectionItem
, User
, MediaComment
,
23 CommentReport
, MediaReport
)
24 from mediagoblin
.tools
.response
import render_to_response
, render_404
, \
25 redirect
, redirect_obj
26 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
27 from mediagoblin
.tools
.pagination
import Pagination
28 from mediagoblin
.user_pages
import forms
as user_forms
29 from mediagoblin
.user_pages
.lib
import (send_comment_email
, build_report_form
,
30 add_media_to_collection
)
32 from mediagoblin
.decorators
import (uses_pagination
, get_user_media_entry
,
33 get_media_entry_by_id
, user_has_privilege
,
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 get_media_comment_by_id
)
38 from werkzeug
.contrib
.atom
import AtomFeed
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
})
115 MEDIA_COMMENTS_PER_PAGE
= 50
118 @get_user_media_entry
120 def media_home(request
, media
, page
, **kwargs
):
122 'Homepage' of a MediaEntry()
124 comment_id
= request
.matchdict
.get('comment', None)
126 pagination
= Pagination(
127 page
, media
.get_comments(
128 mg_globals
.app_config
['comments_ascending']),
129 MEDIA_COMMENTS_PER_PAGE
,
132 pagination
= Pagination(
133 page
, media
.get_comments(
134 mg_globals
.app_config
['comments_ascending']),
135 MEDIA_COMMENTS_PER_PAGE
)
137 comments
= pagination()
139 comment_form
= user_forms
.MediaCommentForm(request
.form
)
141 media_template_name
= media
.media_manager
['display_template']
143 return render_to_response(
147 'comments': comments
,
148 'pagination': pagination
,
149 'comment_form': comment_form
,
150 'app_config': mg_globals
.app_config
})
153 @get_media_entry_by_id
154 @require_active_login
155 @user_has_privilege(u
'commenter')
156 def media_post_comment(request
, media
):
158 recieves POST from a MediaEntry() comment form, saves the comment.
160 assert request
.method
== 'POST'
162 comment
= request
.db
.MediaComment()
163 comment
.media_entry
= media
.id
164 comment
.author
= request
.user
.id
165 comment
.content
= unicode(request
.form
['comment_content'])
167 # Show error message if commenting is disabled.
168 if not mg_globals
.app_config
['allow_comments']:
169 messages
.add_message(
172 _("Sorry, comments are disabled."))
173 elif not comment
.content
.strip():
174 messages
.add_message(
177 _("Oops, your comment was empty."))
181 messages
.add_message(
182 request
, messages
.SUCCESS
,
183 _('Your comment has been posted!'))
185 media_uploader
= media
.get_uploader
186 #don't send email if you comment on your own post
187 if (comment
.author
!= media_uploader
and
188 media_uploader
.wants_comment_notification
):
189 send_comment_email(media_uploader
, comment
, media
, request
)
191 return redirect_obj(request
, media
)
194 @get_media_entry_by_id
195 @require_active_login
196 def media_collect(request
, media
):
197 """Add media to collection submission"""
199 form
= user_forms
.MediaCollectForm(request
.form
)
200 # A user's own collections:
201 form
.collection
.query
= Collection
.query
.filter_by(
202 creator
= request
.user
.id).order_by(Collection
.title
)
204 if request
.method
!= 'POST' or not form
.validate():
205 # No POST submission, or invalid form
206 if not form
.validate():
207 messages
.add_message(request
, messages
.ERROR
,
208 _('Please check your entries and try again.'))
210 return render_to_response(
212 'mediagoblin/user_pages/media_collect.html',
216 # If we are here, method=POST and the form is valid, submit things.
217 # If the user is adding a new collection, use that:
218 if form
.collection_title
.data
:
219 # Make sure this user isn't duplicating an existing collection
220 existing_collection
= Collection
.query
.filter_by(
221 creator
=request
.user
.id,
222 title
=form
.collection_title
.data
).first()
223 if existing_collection
:
224 messages
.add_message(request
, messages
.ERROR
,
225 _('You already have a collection called "%s"!')
226 % existing_collection
.title
)
227 return redirect(request
, "mediagoblin.user_pages.media_home",
228 user
=media
.get_uploader
.username
,
229 media
=media
.slug_or_id
)
231 collection
= Collection()
232 collection
.title
= form
.collection_title
.data
233 collection
.description
= form
.collection_description
.data
234 collection
.creator
= request
.user
.id
235 collection
.generate_slug()
238 # Otherwise, use the collection selected from the drop-down
240 collection
= form
.collection
.data
241 if collection
and collection
.creator
!= request
.user
.id:
244 # Make sure the user actually selected a collection
246 messages
.add_message(
247 request
, messages
.ERROR
,
248 _('You have to select or add a collection'))
249 return redirect(request
, "mediagoblin.user_pages.media_collect",
250 user
=media
.get_uploader
.username
,
254 # Check whether media already exists in collection
255 elif CollectionItem
.query
.filter_by(
256 media_entry
=media
.id,
257 collection
=collection
.id).first():
258 messages
.add_message(request
, messages
.ERROR
,
259 _('"%s" already in collection "%s"')
260 % (media
.title
, collection
.title
))
261 else: # Add item to collection
262 add_media_to_collection(collection
, media
, form
.note
.data
)
264 messages
.add_message(request
, messages
.SUCCESS
,
265 _('"%s" added to collection "%s"')
266 % (media
.title
, collection
.title
))
268 return redirect_obj(request
, media
)
271 #TODO: Why does @user_may_delete_media not implicate @require_active_login?
272 @get_media_entry_by_id
273 @require_active_login
274 @user_may_delete_media
275 def media_confirm_delete(request
, media
):
277 form
= user_forms
.ConfirmDeleteForm(request
.form
)
279 if request
.method
== 'POST' and form
.validate():
280 if form
.confirm
.data
is True:
281 username
= media
.get_uploader
.username
282 # Delete MediaEntry and all related files, comments etc.
284 messages
.add_message(
285 request
, messages
.SUCCESS
, _('You deleted the media.'))
287 return redirect(request
, "mediagoblin.user_pages.user_home",
290 messages
.add_message(
291 request
, messages
.ERROR
,
292 _("The media was not deleted because you didn't check that you were sure."))
293 return redirect_obj(request
, media
)
295 if ((request
.user
.is_admin
and
296 request
.user
.id != media
.uploader
)):
297 messages
.add_message(
298 request
, messages
.WARNING
,
299 _("You are about to delete another user's media. "
300 "Proceed with caution."))
302 return render_to_response(
304 'mediagoblin/user_pages/media_confirm_delete.html',
309 @active_user_from_url
311 def user_collection(request
, page
, url_user
=None):
312 """A User-defined Collection"""
313 collection
= Collection
.query
.filter_by(
314 get_creator
=url_user
,
315 slug
=request
.matchdict
['collection']).first()
318 return render_404(request
)
320 cursor
= collection
.get_collection_items()
322 pagination
= Pagination(page
, cursor
)
323 collection_items
= pagination()
325 # if no data is available, return NotFound
326 # TODO: Should an empty collection really also return 404?
327 if collection_items
== None:
328 return render_404(request
)
330 return render_to_response(
332 'mediagoblin/user_pages/collection.html',
334 'collection': collection
,
335 'collection_items': collection_items
,
336 'pagination': pagination
})
339 @active_user_from_url
340 def collection_list(request
, url_user
=None):
341 """A User-defined Collection"""
342 collections
= Collection
.query
.filter_by(
343 get_creator
=url_user
)
345 return render_to_response(
347 'mediagoblin/user_pages/collection_list.html',
349 'collections': collections
})
352 @get_user_collection_item
353 @require_active_login
354 @user_may_alter_collection
355 def collection_item_confirm_remove(request
, collection_item
):
357 form
= user_forms
.ConfirmCollectionItemRemoveForm(request
.form
)
359 if request
.method
== 'POST' and form
.validate():
360 username
= collection_item
.in_collection
.get_creator
.username
361 collection
= collection_item
.in_collection
363 if form
.confirm
.data
is True:
364 entry
= collection_item
.get_media_entry
365 entry
.collected
= entry
.collected
- 1
368 collection_item
.delete()
369 collection
.items
= collection
.items
- 1
372 messages
.add_message(
373 request
, messages
.SUCCESS
, _('You deleted the item from the collection.'))
375 messages
.add_message(
376 request
, messages
.ERROR
,
377 _("The item was not removed because you didn't check that you were sure."))
379 return redirect_obj(request
, collection
)
381 if ((request
.user
.is_admin
and
382 request
.user
.id != collection_item
.in_collection
.creator
)):
383 messages
.add_message(
384 request
, messages
.WARNING
,
385 _("You are about to delete an item from another user's collection. "
386 "Proceed with caution."))
388 return render_to_response(
390 'mediagoblin/user_pages/collection_item_confirm_remove.html',
391 {'collection_item': collection_item
,
396 @require_active_login
397 @user_may_alter_collection
398 def collection_confirm_delete(request
, collection
):
400 form
= user_forms
.ConfirmDeleteForm(request
.form
)
402 if request
.method
== 'POST' and form
.validate():
404 username
= collection
.get_creator
.username
406 if form
.confirm
.data
is True:
407 collection_title
= collection
.title
409 # Delete all the associated collection items
410 for item
in collection
.get_collection_items():
411 entry
= item
.get_media_entry
412 entry
.collected
= entry
.collected
- 1
417 messages
.add_message(request
, messages
.SUCCESS
,
418 _('You deleted the collection "%s"') % collection_title
)
420 return redirect(request
, "mediagoblin.user_pages.user_home",
423 messages
.add_message(
424 request
, messages
.ERROR
,
425 _("The collection was not deleted because you didn't check that you were sure."))
427 return redirect_obj(request
, collection
)
429 if ((request
.user
.is_admin
and
430 request
.user
.id != collection
.creator
)):
431 messages
.add_message(
432 request
, messages
.WARNING
,
433 _("You are about to delete another user's collection. "
434 "Proceed with caution."))
436 return render_to_response(
438 'mediagoblin/user_pages/collection_confirm_delete.html',
439 {'collection': collection
,
443 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
= 15
446 def atom_feed(request
):
448 generates the atom feed with the newest images
450 user
= User
.query
.filter_by(
451 username
= request
.matchdict
['user'],
452 status
= u
'active').first()
454 return render_404(request
)
456 cursor
= MediaEntry
.query
.filter_by(
458 state
= u
'processed').\
459 order_by(MediaEntry
.created
.desc()).\
460 limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
)
463 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
466 'href': request
.urlgen(
467 'mediagoblin.user_pages.user_home',
468 qualified
=True, user
=request
.matchdict
['user']),
473 if mg_globals
.app_config
["push_urls"]:
474 for push_url
in mg_globals
.app_config
["push_urls"]:
480 "MediaGoblin: Feed for user '%s'" % request
.matchdict
['user'],
481 feed_url
=request
.url
,
482 id='tag:{host},{year}:gallery.user-{user}'.format(
484 year
=datetime
.datetime
.today().strftime('%Y'),
485 user
=request
.matchdict
['user']),
489 feed
.add(entry
.get('title'),
490 entry
.description_html
,
491 id=entry
.url_for_self(request
.urlgen
, qualified
=True),
494 'name': entry
.get_uploader
.username
,
495 'uri': request
.urlgen(
496 'mediagoblin.user_pages.user_home',
497 qualified
=True, user
=entry
.get_uploader
.username
)},
498 updated
=entry
.get('created'),
500 'href': entry
.url_for_self(
504 'type': 'text/html'}])
506 return feed
.get_response()
509 def collection_atom_feed(request
):
511 generates the atom feed with the newest images from a collection
513 user
= User
.query
.filter_by(
514 username
= request
.matchdict
['user'],
515 status
= u
'active').first()
517 return render_404(request
)
519 collection
= Collection
.query
.filter_by(
521 slug
=request
.matchdict
['collection']).first()
523 return render_404(request
)
525 cursor
= CollectionItem
.query
.filter_by(
526 collection
=collection
.id) \
527 .order_by(CollectionItem
.added
.desc()) \
528 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
)
531 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
534 'href': collection
.url_for_self(request
.urlgen
, qualified
=True),
539 if mg_globals
.app_config
["push_urls"]:
540 for push_url
in mg_globals
.app_config
["push_urls"]:
546 "MediaGoblin: Feed for %s's collection %s" %
547 (request
.matchdict
['user'], collection
.title
),
548 feed_url
=request
.url
,
549 id=u
'tag:{host},{year}:gnu-mediagoblin.{user}.collection.{slug}'\
552 year
=collection
.created
.strftime('%Y'),
553 user
=request
.matchdict
['user'],
554 slug
=collection
.slug
),
558 entry
= item
.get_media_entry
559 feed
.add(entry
.get('title'),
561 id=entry
.url_for_self(request
.urlgen
, qualified
=True),
564 'name': entry
.get_uploader
.username
,
565 'uri': request
.urlgen(
566 'mediagoblin.user_pages.user_home',
567 qualified
=True, user
=entry
.get_uploader
.username
)},
568 updated
=item
.get('added'),
570 'href': entry
.url_for_self(
574 'type': 'text/html'}])
576 return feed
.get_response()
579 @require_active_login
580 def processing_panel(request
):
582 Show to the user what media is still in conversion/processing...
583 and what failed, and why!
585 user
= User
.query
.filter_by(username
=request
.matchdict
['user']).first()
586 # TODO: XXX: Should this be a decorator?
588 # Make sure we have permission to access this user's panel. Only
589 # admins and this user herself should be able to do so.
590 if not (user
.id == request
.user
.id or request
.user
.is_admin
):
591 # No? Simply redirect to this user's homepage.
593 request
, 'mediagoblin.user_pages.user_home',
596 # Get media entries which are in-processing
597 processing_entries
= MediaEntry
.query
.\
598 filter_by(uploader
= user
.id,
599 state
= u
'processing').\
600 order_by(MediaEntry
.created
.desc())
602 # Get media entries which have failed to process
603 failed_entries
= MediaEntry
.query
.\
604 filter_by(uploader
= user
.id,
606 order_by(MediaEntry
.created
.desc())
608 processed_entries
= MediaEntry
.query
.\
609 filter_by(uploader
= user
.id,
610 state
= u
'processed').\
611 order_by(MediaEntry
.created
.desc()).\
615 return render_to_response(
617 'mediagoblin/user_pages/processing_panel.html',
619 'processing_entries': processing_entries
,
620 'failed_entries': failed_entries
,
621 'processed_entries': processed_entries
})
623 @require_active_login
624 @get_user_media_entry
625 @user_has_privilege(u
'reporter')
626 def file_a_report(request
, media
, comment
=None):
627 if request
.method
== "POST":
628 report_form
= build_report_form(request
.form
)
635 if comment
is not None:
636 context
= {'media': media
,
639 context
= {'media': media
}
641 return render_to_response(
643 'mediagoblin/user_pages/report.html',
646 @require_active_login
647 @get_user_media_entry
648 @get_media_comment_by_id
649 def file_a_comment_report(request
, media
, comment
):
650 return file_a_report(request
, comment
=comment
)