import datetime
from mediagoblin import messages, mg_globals
-from mediagoblin.db.models import (MediaEntry, Collection, CollectionItem,
- User)
+from mediagoblin.db.models import (MediaEntry, MediaTag, Collection,
+ CollectionItem, User)
from mediagoblin.tools.response import render_to_response, render_404, redirect
from mediagoblin.tools.translate import pass_to_ugettext as _
from mediagoblin.tools.pagination import Pagination
-from mediagoblin.tools.files import delete_media_files
from mediagoblin.user_pages import forms as user_forms
-from mediagoblin.user_pages.lib import send_comment_email
+from mediagoblin.user_pages.lib import (send_comment_email,
+ add_media_to_collection)
from mediagoblin.decorators import (uses_pagination, get_user_media_entry,
+ get_media_entry_by_id,
require_active_login, user_may_delete_media, user_may_alter_collection,
get_user_collection, get_user_collection_item, active_user_from_url)
@uses_pagination
def user_gallery(request, page, url_user=None):
"""'Gallery' of a User()"""
+ tag = request.matchdict.get('tag', None)
cursor = MediaEntry.query.filter_by(
uploader=url_user.id,
state=u'processed').order_by(MediaEntry.created.desc())
+ # Filter potentially by tag too:
+ if tag:
+ cursor = cursor.filter(
+ MediaEntry.tags_helper.any(
+ MediaTag.slug == request.matchdict['tag']))
+
# Paginate gallery
pagination = Pagination(page, cursor)
media_entries = pagination()
return render_to_response(
request,
'mediagoblin/user_pages/gallery.html',
- {'user': url_user,
+ {'user': url_user, 'tag': tag,
'media_entries': media_entries,
'pagination': pagination})
"""
'Homepage' of a MediaEntry()
"""
- if request.matchdict.get('comment', None):
+ comment_id = request.matchdict.get('comment', None)
+ if comment_id:
pagination = Pagination(
page, media.get_comments(
mg_globals.app_config['comments_ascending']),
MEDIA_COMMENTS_PER_PAGE,
- request.matchdict.get('comment'))
+ comment_id)
else:
pagination = Pagination(
page, media.get_comments(
'app_config': mg_globals.app_config})
-@get_user_media_entry
+@get_media_entry_by_id
@require_active_login
def media_post_comment(request, media):
"""
return redirect(request, location=media.url_for_self(request.urlgen))
-@get_user_media_entry
+@get_media_entry_by_id
@require_active_login
def media_collect(request, media):
"""Add media to collection submission"""
# If we are here, method=POST and the form is valid, submit things.
# If the user is adding a new collection, use that:
- if request.form['collection_title']:
+ if form.collection_title.data:
# Make sure this user isn't duplicating an existing collection
existing_collection = Collection.query.filter_by(
creator=request.user.id,
- title=request.form['collection_title']).first()
+ title=form.collection_title.data).first()
if existing_collection:
messages.add_message(request, messages.ERROR,
- _('You already have a collection called "%s"!'
- % collection.title))
+ _('You already have a collection called "%s"!')
+ % existing_collection.title)
return redirect(request, "mediagoblin.user_pages.media_home",
- user=request.user.username,
- media=media.id)
+ user=media.get_uploader.username,
+ media=media.slug_or_id)
collection = Collection()
- collection.title = request.form['collection_title']
- collection.description = request.form.get('collection_description')
+ collection.title = form.collection_title.data
+ collection.description = form.collection_description.data
collection.creator = request.user.id
collection.generate_slug()
collection.save()
# Otherwise, use the collection selected from the drop-down
else:
- collection = Collection.query.filter_by(
- id=request.form.get('collection')).first()
+ collection = form.collection.data
+ if collection and collection.creator != request.user.id:
+ collection = None
# Make sure the user actually selected a collection
if not collection:
messages.add_message(
request, messages.ERROR,
_('You have to select or add a collection'))
+ return redirect(request, "mediagoblin.user_pages.media_collect",
+ user=media.get_uploader.username,
+ media_id=media.id)
+
# Check whether media already exists in collection
elif CollectionItem.query.filter_by(
media_entry=media.id,
collection=collection.id).first():
messages.add_message(request, messages.ERROR,
- _('"%s" already in collection "%s"'
- % (media.title, collection.title)))
+ _('"%s" already in collection "%s"')
+ % (media.title, collection.title))
else: # Add item to collection
- collection_item = request.db.CollectionItem()
- collection_item.collection = collection.id
- collection_item.media_entry = media.id
- collection_item.author = request.user.id
- collection_item.note = request.form['note']
- collection_item.save()
-
- collection.items = collection.items + 1
- collection.save()
-
- media.collected = media.collected + 1
- media.save()
+ add_media_to_collection(collection, media, form.note.data)
messages.add_message(request, messages.SUCCESS,
- _('"%s" added to collection "%s"'
- % (media.title, collection.title)))
+ _('"%s" added to collection "%s"')
+ % (media.title, collection.title))
return redirect(request, "mediagoblin.user_pages.media_home",
user=media.get_uploader.username,
- media=media.id)
+ media=media.slug_or_id)
#TODO: Why does @user_may_delete_media not implicate @require_active_login?
-@get_user_media_entry
+@get_media_entry_by_id
@require_active_login
@user_may_delete_media
def media_confirm_delete(request, media):
if request.method == 'POST' and form.validate():
if form.confirm.data is True:
username = media.get_uploader.username
-
- # Delete all the associated comments
- for comment in media.get_comments():
- comment.delete()
-
- # Delete all files on the public storage
- try:
- delete_media_files(media)
- except OSError, error:
- _log.error('No such files from the user "{1}"'
- ' to delete: {0}'.format(str(error), username))
- messages.add_message(request, messages.ERROR,
- _('Some of the files with this entry seem'
- ' to be missing. Deleting anyway.'))
-
+ # Delete MediaEntry and all related files, comments etc.
media.delete()
messages.add_message(
request, messages.SUCCESS, _('You deleted the media.'))
get_creator=url_user,
slug=request.matchdict['collection']).first()
+ if not collection:
+ return render_404(request)
+
cursor = collection.get_collection_items()
pagination = Pagination(page, cursor)
'pagination': pagination})
+@active_user_from_url
+def collection_list(request, url_user=None):
+ """A User-defined Collection"""
+ collections = Collection.query.filter_by(
+ get_creator=url_user)
+
+ return render_to_response(
+ request,
+ 'mediagoblin/user_pages/collection_list.html',
+ {'user': url_user,
+ 'collections': collections})
+
+
@get_user_collection_item
@require_active_login
@user_may_alter_collection
collection = Collection.query.filter_by(
creator=user.id,
slug=request.matchdict['collection']).first()
+ if not collection:
+ return render_404(request)
cursor = CollectionItem.query.filter_by(
collection=collection.id) \
- .sort(CollectionItem.added.desc()) \
+ .order_by(CollectionItem.added.desc()) \
.limit(ATOM_DEFAULT_NR_OF_UPDATED_ITEMS)
"""
'href': push_url})
feed = AtomFeed(
- "MediaGoblin: Feed for %s's collection %s" % (request.matchdict['user'], collection.title),
- feed_url=request.url,
- id='tag:{host},{year}:collection.user-{user}.title-{title}'.format(
- host=request.host,
- year=datetime.datetime.today().strftime('%Y'),
- user=request.matchdict['user'],
- title=collection.title),
- links=atomlinks)
+ "MediaGoblin: Feed for %s's collection %s" %
+ (request.matchdict['user'], collection.title),
+ feed_url=request.url,
+ id=u'tag:{host},{year}:gnu-mediagoblin.{user}.collection.{slug}'\
+ .format(
+ host=request.host,
+ year=collection.created.strftime('%Y'),
+ user=request.matchdict['user'],
+ slug=collection.slug),
+ links=atomlinks)
for item in cursor:
entry = item.get_media_entry