Ticket #795: 0001-Add-collection-drop-down-to-submit-page.patch
File 0001-Add-collection-drop-down-to-submit-page.patch, 7.2 KB (added by , 9 years ago) |
---|
-
mediagoblin/submit/forms.py
From 0c4c13742596c050aba938f919a884c6521f17a5 Mon Sep 17 00:00:00 2001 From: tom <tom@teamfay.co.uk> Date: Sat, 12 Dec 2015 11:02:39 +0000 Subject: [PATCH] Add collection drop down to submit page. --- mediagoblin/submit/forms.py | 5 ++- mediagoblin/submit/views.py | 22 +++++++++++-- mediagoblin/tests/test_submission.py | 60 +++++++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 4 deletions(-) diff --git a/mediagoblin/submit/forms.py b/mediagoblin/submit/forms.py index c5bacc4..69d211e 100644
a b 16 16 17 17 18 18 import wtforms 19 19 from wtforms.ext.sqlalchemy.fields import QuerySelectField 20 20 from mediagoblin import mg_globals 21 21 from mediagoblin.tools.text import tag_length_validator 22 22 from mediagoblin.tools.translate import lazy_pass_to_ugettext as _ … … def get_submit_start_form(form, **kwargs): 50 50 _('License'), 51 51 [wtforms.validators.Optional(),], 52 52 choices=licenses_as_choices()) 53 collection = QuerySelectField( 54 _('Collection'), 55 allow_blank=True, blank_text=_('-- Select --'), get_label='title',) 53 56 max_file_size = wtforms.HiddenField('') 54 57 upload_limit = wtforms.HiddenField('') 55 58 uploaded = wtforms.HiddenField('') -
mediagoblin/submit/views.py
diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py index eae4a1e..c2eecdb 100644
a b import logging 23 23 24 24 _log = logging.getLogger(__name__) 25 25 26 26 from mediagoblin.db.models import Collection 27 from mediagoblin.tools.federation import create_activity 27 28 from mediagoblin.tools.translate import pass_to_ugettext as _ 28 29 from mediagoblin.tools.response import render_to_response, redirect 29 30 from mediagoblin.decorators import require_active_login, user_has_privilege … … from mediagoblin.media_types import FileTypeNotSupported 33 34 from mediagoblin.submit.lib import \ 34 35 check_file_field, submit_media, get_upload_file_limits, \ 35 36 FileUploadLimit, UserUploadLimit, UserPastUploadLimit 37 from mediagoblin.user_pages.lib import add_media_to_collection 36 38 37 39 38 40 @require_active_login … … def submit_start(request): 49 51 max_file_size=max_file_size, 50 52 upload_limit=upload_limit, 51 53 uploaded=request.user.uploaded) 54 users_collections = Collection.query.filter_by( 55 actor=request.user.id, 56 type=Collection.USER_DEFINED_TYPE 57 ).order_by(Collection.title) 58 59 if users_collections.count() > 0: 60 submit_form.collection.query = users_collections 61 else: 62 del submit_form.collection 52 63 53 64 if request.method == 'POST' and submit_form.validate(): 54 65 if not check_file_field(request, 'file'): … … def submit_start(request): 56 67 _(u'You must provide a file.')) 57 68 else: 58 69 try: 59 submit_media(70 media = submit_media( 60 71 mg_app=request.app, user=request.user, 61 72 submitted_file=request.files['file'], 62 73 filename=request.files['file'].filename, … … def submit_start(request): 67 78 upload_limit=upload_limit, max_file_size=max_file_size, 68 79 urlgen=request.urlgen) 69 80 81 if submit_form.collection and submit_form.collection.data: 82 add_media_to_collection( 83 submit_form.collection.data, media) 84 create_activity( 85 "add", media, request.user, 86 target=submit_form.collection.data) 87 70 88 add_message(request, SUCCESS, _('Woohoo! Submitted!')) 71 89 72 90 return redirect(request, "mediagoblin.user_pages.user_home", -
mediagoblin/tests/test_submission.py
diff --git a/mediagoblin/tests/test_submission.py b/mediagoblin/tests/test_submission.py index f9031d3..eed7afa 100644
a b if six.PY2: # this hack only work in Python 2 23 23 24 24 import os 25 25 import pytest 26 import webtest.forms 26 27 27 28 import six.moves.urllib.parse as urlparse 28 29 … … gi.require_version('Gst', '1.0') 32 33 from gi.repository import Gst 33 34 Gst.init(None) 34 35 35 from mediagoblin.tests.tools import fixture_add_user 36 from mediagoblin.tests.tools import fixture_add_user, fixture_add_collection 36 37 from .media_tools import create_av 37 38 from mediagoblin import mg_globals 38 39 from mediagoblin.db.models import MediaEntry, User, LocalUser … … class TestSubmission: 421 422 size = os.stat(filename).st_size 422 423 assert last_size > size 423 424 last_size = size 425 426 def test_collection_selection(self): 427 """Test the ability to choose a collection when submitting media 428 """ 429 # Collection option shouldn't be present if the user has no collections 430 response = self.test_app.get('/submit/') 431 assert 'collection' not in response.form.fields 432 433 upload = webtest.forms.Upload(os.path.join( 434 'mediagoblin', 'static', 'images', 'media_thumbs', 'image.png')) 435 436 # Check that upload of an image when a user has no collections 437 response.form['file'] = upload 438 no_collection_title = 'no collection' 439 response.form['title'] = no_collection_title 440 response.form.submit() 441 assert MediaEntry.query.filter_by( 442 actor=self.our_user().id 443 ).first().title == no_collection_title 444 445 # Collection option should be present if the user has collections. It 446 # shouldn't allow other users' collections to be selected. 447 col = fixture_add_collection(user=self.our_user()) 448 user = fixture_add_user(username=u'different') 449 fixture_add_collection(user=user, name=u'different') 450 response = self.test_app.get('/submit/') 451 form = response.form 452 assert 'collection' in form.fields 453 # Option length is 2, because of the default "--Select--" option 454 assert len(form['collection'].options) == 2 455 assert form['collection'].options[1][2] == col.title 456 457 # Test that if we specify a collection then the media entry is added to 458 # the specified collection. 459 form['file'] = upload 460 title = 'new picture' 461 form['title'] = title 462 form['collection'] = form['collection'].options[1][0] 463 form.submit() 464 # The title of the first item in our user's first collection should 465 # match the title of the picture that was just added. 466 col = self.our_user().collections[0] 467 assert col.collection_items[0].get_object().title == title 468 469 # Test upload succeeds if the user has collection and no collection is 470 # chosen. 471 form['file'] = webtest.forms.Upload(os.path.join( 472 'mediagoblin', 'static', 'images', 'media_thumbs', 'image.png')) 473 title = 'no collection 2' 474 form['title'] = title 475 form['collection'] = form['collection'].options[0][0] 476 form.submit() 477 # The title of the first item in our user's first collection should 478 # match the title of the picture that was just added. 479 assert MediaEntry.query.filter_by( 480 actor=self.our_user().id 481 ).count() == 3