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
, redirect
24 from mediagoblin
.tools
.translate
import pass_to_ugettext
as _
25 from mediagoblin
.tools
.pagination
import Pagination
26 from mediagoblin
.user_pages
import forms
as user_forms
27 from mediagoblin
.user_pages
.lib
import send_comment_email
29 from mediagoblin
.decorators
import (uses_pagination
, get_user_media_entry
,
30 get_media_entry_by_id
,
31 require_active_login
, user_may_delete_media
, user_may_alter_collection
,
32 get_user_collection
, get_user_collection_item
, active_user_from_url
)
34 from werkzeug
.contrib
.atom
import AtomFeed
37 _log
= logging
.getLogger(__name__
)
38 _log
.setLevel(logging
.DEBUG
)
42 def user_home(request
, page
):
43 """'Homepage' of a User()"""
44 # TODO: decide if we only want homepages for active users, we can
45 # then use the @get_active_user decorator and also simplify the
47 user
= User
.query
.filter_by(username
=request
.matchdict
['user']).first()
49 return render_404(request
)
50 elif user
.status
!= u
'active':
51 return render_to_response(
53 'mediagoblin/user_pages/user.html',
56 cursor
= MediaEntry
.query
.\
57 filter_by(uploader
= user
.id,
58 state
= u
'processed').order_by(MediaEntry
.created
.desc())
60 pagination
= Pagination(page
, cursor
)
61 media_entries
= pagination()
63 #if no data is available, return NotFound
64 if media_entries
== None:
65 return render_404(request
)
67 user_gallery_url
= request
.urlgen(
68 'mediagoblin.user_pages.user_gallery',
71 return render_to_response(
73 'mediagoblin/user_pages/user.html',
75 'user_gallery_url': user_gallery_url
,
76 'media_entries': media_entries
,
77 'pagination': pagination
})
82 def user_gallery(request
, page
, url_user
=None):
83 """'Gallery' of a User()"""
84 tag
= request
.matchdict
.get('tag', None)
85 cursor
= MediaEntry
.query
.filter_by(
87 state
=u
'processed').order_by(MediaEntry
.created
.desc())
89 # Filter potentially by tag too:
91 cursor
= cursor
.filter(
92 MediaEntry
.tags_helper
.any(
93 MediaTag
.slug
== request
.matchdict
['tag']))
96 pagination
= Pagination(page
, cursor
)
97 media_entries
= pagination()
99 #if no data is available, return NotFound
100 # TODO: Should we really also return 404 for empty galleries?
101 if media_entries
== None:
102 return render_404(request
)
104 return render_to_response(
106 'mediagoblin/user_pages/gallery.html',
107 {'user': url_user
, 'tag': tag
,
108 'media_entries': media_entries
,
109 'pagination': pagination
})
111 MEDIA_COMMENTS_PER_PAGE
= 50
114 @get_user_media_entry
116 def media_home(request
, media
, page
, **kwargs
):
118 'Homepage' of a MediaEntry()
120 comment_id
= request
.matchdict
.get('comment', None)
122 pagination
= Pagination(
123 page
, media
.get_comments(
124 mg_globals
.app_config
['comments_ascending']),
125 MEDIA_COMMENTS_PER_PAGE
,
128 pagination
= Pagination(
129 page
, media
.get_comments(
130 mg_globals
.app_config
['comments_ascending']),
131 MEDIA_COMMENTS_PER_PAGE
)
133 comments
= pagination()
135 comment_form
= user_forms
.MediaCommentForm(request
.form
)
137 media_template_name
= media
.media_manager
['display_template']
139 return render_to_response(
143 'comments': comments
,
144 'pagination': pagination
,
145 'comment_form': comment_form
,
146 'app_config': mg_globals
.app_config
})
149 @get_media_entry_by_id
150 @require_active_login
151 def media_post_comment(request
, media
):
153 recieves POST from a MediaEntry() comment form, saves the comment.
155 assert request
.method
== 'POST'
157 comment
= request
.db
.MediaComment()
158 comment
.media_entry
= media
.id
159 comment
.author
= request
.user
.id
160 comment
.content
= unicode(request
.form
['comment_content'])
162 if not comment
.content
.strip():
163 messages
.add_message(
166 _("Oops, your comment was empty."))
170 messages
.add_message(
171 request
, messages
.SUCCESS
,
172 _('Your comment has been posted!'))
174 media_uploader
= media
.get_uploader
175 #don't send email if you comment on your own post
176 if (comment
.author
!= media_uploader
and
177 media_uploader
.wants_comment_notification
):
178 send_comment_email(media_uploader
, comment
, media
, request
)
180 return redirect(request
, location
=media
.url_for_self(request
.urlgen
))
183 @get_media_entry_by_id
184 @require_active_login
185 def media_collect(request
, media
):
186 """Add media to collection submission"""
188 form
= user_forms
.MediaCollectForm(request
.form
)
189 # A user's own collections:
190 form
.collection
.query
= Collection
.query
.filter_by(
191 creator
= request
.user
.id).order_by(Collection
.title
)
193 if request
.method
!= 'POST' or not form
.validate():
194 # No POST submission, or invalid form
195 if not form
.validate():
196 messages
.add_message(request
, messages
.ERROR
,
197 _('Please check your entries and try again.'))
199 return render_to_response(
201 'mediagoblin/user_pages/media_collect.html',
205 # If we are here, method=POST and the form is valid, submit things.
206 # If the user is adding a new collection, use that:
207 if request
.form
['collection_title']:
208 # Make sure this user isn't duplicating an existing collection
209 existing_collection
= Collection
.query
.filter_by(
210 creator
=request
.user
.id,
211 title
=request
.form
['collection_title']).first()
212 if existing_collection
:
213 messages
.add_message(request
, messages
.ERROR
,
214 _('You already have a collection called "%s"!')
215 % existing_collection
.title
)
216 return redirect(request
, "mediagoblin.user_pages.media_home",
217 user
=media
.get_uploader
.username
,
218 media
=media
.slug_or_id
)
220 collection
= Collection()
221 collection
.title
= request
.form
['collection_title']
222 collection
.description
= request
.form
.get('collection_description')
223 collection
.creator
= request
.user
.id
224 collection
.generate_slug()
227 # Otherwise, use the collection selected from the drop-down
229 collection
= Collection
.query
.filter_by(
230 id=request
.form
.get('collection'),
231 creator
=request
.user
.id).first()
233 # Make sure the user actually selected a collection
235 messages
.add_message(
236 request
, messages
.ERROR
,
237 _('You have to select or add a collection'))
238 return redirect(request
, "mediagoblin.user_pages.media_collect",
239 user
=media
.get_uploader
.username
,
243 # Check whether media already exists in collection
244 elif CollectionItem
.query
.filter_by(
245 media_entry
=media
.id,
246 collection
=collection
.id).first():
247 messages
.add_message(request
, messages
.ERROR
,
248 _('"%s" already in collection "%s"')
249 % (media
.title
, collection
.title
))
250 else: # Add item to collection
251 collection_item
= request
.db
.CollectionItem()
252 collection_item
.collection
= collection
.id
253 collection_item
.media_entry
= media
.id
254 collection_item
.note
= request
.form
['note']
255 collection_item
.save()
257 collection
.items
= collection
.items
+ 1
260 media
.collected
= media
.collected
+ 1
263 messages
.add_message(request
, messages
.SUCCESS
,
264 _('"%s" added to collection "%s"')
265 % (media
.title
, collection
.title
))
267 return redirect(request
, "mediagoblin.user_pages.media_home",
268 user
=media
.get_uploader
.username
,
269 media
=media
.slug_or_id
)
272 #TODO: Why does @user_may_delete_media not implicate @require_active_login?
273 @get_media_entry_by_id
274 @require_active_login
275 @user_may_delete_media
276 def media_confirm_delete(request
, media
):
278 form
= user_forms
.ConfirmDeleteForm(request
.form
)
280 if request
.method
== 'POST' and form
.validate():
281 if form
.confirm
.data
is True:
282 username
= media
.get_uploader
.username
283 # Delete MediaEntry and all related files, comments etc.
285 messages
.add_message(
286 request
, messages
.SUCCESS
, _('You deleted the media.'))
288 return redirect(request
, "mediagoblin.user_pages.user_home",
291 messages
.add_message(
292 request
, messages
.ERROR
,
293 _("The media was not deleted because you didn't check that you were sure."))
294 return redirect(request
,
295 location
=media
.url_for_self(request
.urlgen
))
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(request
, "mediagoblin.user_pages.user_collection",
383 collection
=collection
.slug
)
385 if ((request
.user
.is_admin
and
386 request
.user
.id != collection_item
.in_collection
.creator
)):
387 messages
.add_message(
388 request
, messages
.WARNING
,
389 _("You are about to delete an item from another user's collection. "
390 "Proceed with caution."))
392 return render_to_response(
394 'mediagoblin/user_pages/collection_item_confirm_remove.html',
395 {'collection_item': collection_item
,
400 @require_active_login
401 @user_may_alter_collection
402 def collection_confirm_delete(request
, collection
):
404 form
= user_forms
.ConfirmDeleteForm(request
.form
)
406 if request
.method
== 'POST' and form
.validate():
408 username
= collection
.get_creator
.username
410 if form
.confirm
.data
is True:
411 collection_title
= collection
.title
413 # Delete all the associated collection items
414 for item
in collection
.get_collection_items():
415 entry
= item
.get_media_entry
416 entry
.collected
= entry
.collected
- 1
421 messages
.add_message(
422 request
, messages
.SUCCESS
, _('You deleted the collection "%s"' % collection_title
))
424 return redirect(request
, "mediagoblin.user_pages.user_home",
427 messages
.add_message(
428 request
, messages
.ERROR
,
429 _("The collection was not deleted because you didn't check that you were sure."))
431 return redirect(request
, "mediagoblin.user_pages.user_collection",
433 collection
=collection
.slug
)
435 if ((request
.user
.is_admin
and
436 request
.user
.id != collection
.creator
)):
437 messages
.add_message(
438 request
, messages
.WARNING
,
439 _("You are about to delete another user's collection. "
440 "Proceed with caution."))
442 return render_to_response(
444 'mediagoblin/user_pages/collection_confirm_delete.html',
445 {'collection': collection
,
449 ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
= 15
452 def atom_feed(request
):
454 generates the atom feed with the newest images
456 user
= User
.query
.filter_by(
457 username
= request
.matchdict
['user'],
458 status
= u
'active').first()
460 return render_404(request
)
462 cursor
= MediaEntry
.query
.filter_by(
464 state
= u
'processed').\
465 order_by(MediaEntry
.created
.desc()).\
466 limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
)
469 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
472 'href': request
.urlgen(
473 'mediagoblin.user_pages.user_home',
474 qualified
=True, user
=request
.matchdict
['user']),
479 if mg_globals
.app_config
["push_urls"]:
480 for push_url
in mg_globals
.app_config
["push_urls"]:
486 "MediaGoblin: Feed for user '%s'" % request
.matchdict
['user'],
487 feed_url
=request
.url
,
488 id='tag:{host},{year}:gallery.user-{user}'.format(
490 year
=datetime
.datetime
.today().strftime('%Y'),
491 user
=request
.matchdict
['user']),
495 feed
.add(entry
.get('title'),
496 entry
.description_html
,
497 id=entry
.url_for_self(request
.urlgen
, qualified
=True),
500 'name': entry
.get_uploader
.username
,
501 'uri': request
.urlgen(
502 'mediagoblin.user_pages.user_home',
503 qualified
=True, user
=entry
.get_uploader
.username
)},
504 updated
=entry
.get('created'),
506 'href': entry
.url_for_self(
510 'type': 'text/html'}])
512 return feed
.get_response()
515 def collection_atom_feed(request
):
517 generates the atom feed with the newest images from a collection
519 user
= User
.query
.filter_by(
520 username
= request
.matchdict
['user'],
521 status
= u
'active').first()
523 return render_404(request
)
525 collection
= Collection
.query
.filter_by(
527 slug
=request
.matchdict
['collection']).first()
529 return render_404(request
)
531 cursor
= CollectionItem
.query
.filter_by(
532 collection
=collection
.id) \
533 .order_by(CollectionItem
.added
.desc()) \
534 .limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS
)
537 ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI)
540 'href': request
.urlgen(
541 'mediagoblin.user_pages.user_collection',
542 qualified
=True, user
=request
.matchdict
['user'], collection
=collection
.slug
),
547 if mg_globals
.app_config
["push_urls"]:
548 for push_url
in mg_globals
.app_config
["push_urls"]:
554 "MediaGoblin: Feed for %s's collection %s" %
555 (request
.matchdict
['user'], collection
.title
),
556 feed_url
=request
.url
,
557 id=u
'tag:{host},{year}:gnu-mediagoblin.{user}.collection.{slug}'\
560 year
=collection
.created
.strftime('%Y'),
561 user
=request
.matchdict
['user'],
562 slug
=collection
.slug
),
566 entry
= item
.get_media_entry
567 feed
.add(entry
.get('title'),
569 id=entry
.url_for_self(request
.urlgen
, qualified
=True),
572 'name': entry
.get_uploader
.username
,
573 'uri': request
.urlgen(
574 'mediagoblin.user_pages.user_home',
575 qualified
=True, user
=entry
.get_uploader
.username
)},
576 updated
=item
.get('added'),
578 'href': entry
.url_for_self(
582 'type': 'text/html'}])
584 return feed
.get_response()
587 @require_active_login
588 def processing_panel(request
):
590 Show to the user what media is still in conversion/processing...
591 and what failed, and why!
593 user
= User
.query
.filter_by(username
=request
.matchdict
['user']).first()
594 # TODO: XXX: Should this be a decorator?
596 # Make sure we have permission to access this user's panel. Only
597 # admins and this user herself should be able to do so.
598 if not (user
.id == request
.user
.id or request
.user
.is_admin
):
599 # No? Simply redirect to this user's homepage.
601 request
, 'mediagoblin.user_pages.user_home',
604 # Get media entries which are in-processing
605 processing_entries
= MediaEntry
.query
.\
606 filter_by(uploader
= user
.id,
607 state
= u
'processing').\
608 order_by(MediaEntry
.created
.desc())
610 # Get media entries which have failed to process
611 failed_entries
= MediaEntry
.query
.\
612 filter_by(uploader
= user
.id,
614 order_by(MediaEntry
.created
.desc())
616 processed_entries
= MediaEntry
.query
.\
617 filter_by(uploader
= user
.id,
618 state
= u
'processed').\
619 order_by(MediaEntry
.created
.desc()).\
623 return render_to_response(
625 'mediagoblin/user_pages/processing_panel.html',
627 'processing_entries': processing_entries
,
628 'failed_entries': failed_entries
,
629 'processed_entries': processed_entries
})