Add collection drop down to submit page.

This commit is contained in:
tom 2015-12-12 11:02:39 +00:00 committed by Christopher Allan Webber
parent cf948352b2
commit 5f60a4550d
3 changed files with 83 additions and 4 deletions

View File

@ -16,7 +16,7 @@
import wtforms
from wtforms.ext.sqlalchemy.fields import QuerySelectField
from mediagoblin import mg_globals
from mediagoblin.tools.text import tag_length_validator
from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
@ -50,6 +50,9 @@ def get_submit_start_form(form, **kwargs):
_('License'),
[wtforms.validators.Optional(),],
choices=licenses_as_choices())
collection = QuerySelectField(
_('Collection'),
allow_blank=True, blank_text=_('-- Select --'), get_label='title',)
max_file_size = wtforms.HiddenField('')
upload_limit = wtforms.HiddenField('')
uploaded = wtforms.HiddenField('')

View File

@ -23,7 +23,8 @@ import logging
_log = logging.getLogger(__name__)
from mediagoblin.db.models import Collection
from mediagoblin.tools.federation import create_activity
from mediagoblin.tools.translate import pass_to_ugettext as _
from mediagoblin.tools.response import render_to_response, redirect
from mediagoblin.decorators import require_active_login, user_has_privilege
@ -33,6 +34,7 @@ from mediagoblin.media_types import FileTypeNotSupported
from mediagoblin.submit.lib import \
check_file_field, submit_media, get_upload_file_limits, \
FileUploadLimit, UserUploadLimit, UserPastUploadLimit
from mediagoblin.user_pages.lib import add_media_to_collection
@require_active_login
@ -49,6 +51,15 @@ def submit_start(request):
max_file_size=max_file_size,
upload_limit=upload_limit,
uploaded=request.user.uploaded)
users_collections = Collection.query.filter_by(
actor=request.user.id,
type=Collection.USER_DEFINED_TYPE
).order_by(Collection.title)
if users_collections.count() > 0:
submit_form.collection.query = users_collections
else:
del submit_form.collection
if request.method == 'POST' and submit_form.validate():
if not check_file_field(request, 'file'):
@ -56,7 +67,7 @@ def submit_start(request):
_(u'You must provide a file.'))
else:
try:
submit_media(
media = submit_media(
mg_app=request.app, user=request.user,
submitted_file=request.files['file'],
filename=request.files['file'].filename,
@ -67,6 +78,13 @@ def submit_start(request):
upload_limit=upload_limit, max_file_size=max_file_size,
urlgen=request.urlgen)
if submit_form.collection and submit_form.collection.data:
add_media_to_collection(
submit_form.collection.data, media)
create_activity(
"add", media, request.user,
target=submit_form.collection.data)
add_message(request, SUCCESS, _('Woohoo! Submitted!'))
return redirect(request, "mediagoblin.user_pages.user_home",

View File

@ -23,6 +23,7 @@ if six.PY2: # this hack only work in Python 2
import os
import pytest
import webtest.forms
import six.moves.urllib.parse as urlparse
@ -32,7 +33,7 @@ gi.require_version('Gst', '1.0')
from gi.repository import Gst
Gst.init(None)
from mediagoblin.tests.tools import fixture_add_user
from mediagoblin.tests.tools import fixture_add_user, fixture_add_collection
from .media_tools import create_av
from mediagoblin import mg_globals
from mediagoblin.db.models import MediaEntry, User, LocalUser
@ -421,3 +422,60 @@ class TestSubmission:
size = os.stat(filename).st_size
assert last_size > size
last_size = size
def test_collection_selection(self):
"""Test the ability to choose a collection when submitting media
"""
# Collection option shouldn't be present if the user has no collections
response = self.test_app.get('/submit/')
assert 'collection' not in response.form.fields
upload = webtest.forms.Upload(os.path.join(
'mediagoblin', 'static', 'images', 'media_thumbs', 'image.png'))
# Check that upload of an image when a user has no collections
response.form['file'] = upload
no_collection_title = 'no collection'
response.form['title'] = no_collection_title
response.form.submit()
assert MediaEntry.query.filter_by(
actor=self.our_user().id
).first().title == no_collection_title
# Collection option should be present if the user has collections. It
# shouldn't allow other users' collections to be selected.
col = fixture_add_collection(user=self.our_user())
user = fixture_add_user(username=u'different')
fixture_add_collection(user=user, name=u'different')
response = self.test_app.get('/submit/')
form = response.form
assert 'collection' in form.fields
# Option length is 2, because of the default "--Select--" option
assert len(form['collection'].options) == 2
assert form['collection'].options[1][2] == col.title
# Test that if we specify a collection then the media entry is added to
# the specified collection.
form['file'] = upload
title = 'new picture'
form['title'] = title
form['collection'] = form['collection'].options[1][0]
form.submit()
# The title of the first item in our user's first collection should
# match the title of the picture that was just added.
col = self.our_user().collections[0]
assert col.collection_items[0].get_object().title == title
# Test upload succeeds if the user has collection and no collection is
# chosen.
form['file'] = webtest.forms.Upload(os.path.join(
'mediagoblin', 'static', 'images', 'media_thumbs', 'image.png'))
title = 'no collection 2'
form['title'] = title
form['collection'] = form['collection'].options[0][0]
form.submit()
# The title of the first item in our user's first collection should
# match the title of the picture that was just added.
assert MediaEntry.query.filter_by(
actor=self.our_user().id
).count() == 3