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 tofay, 8 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  
    1616
    1717
    1818import wtforms
    19 
     19from wtforms.ext.sqlalchemy.fields import QuerySelectField
    2020from mediagoblin import mg_globals
    2121from mediagoblin.tools.text import tag_length_validator
    2222from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
    def get_submit_start_form(form, **kwargs):  
    5050            _('License'),
    5151            [wtforms.validators.Optional(),],
    5252            choices=licenses_as_choices())
     53        collection = QuerySelectField(
     54            _('Collection'),
     55            allow_blank=True, blank_text=_('-- Select --'), get_label='title',)
    5356        max_file_size = wtforms.HiddenField('')
    5457        upload_limit = wtforms.HiddenField('')
    5558        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  
    2323
    2424_log = logging.getLogger(__name__)
    2525
    26 
     26from mediagoblin.db.models import Collection
     27from mediagoblin.tools.federation import create_activity
    2728from mediagoblin.tools.translate import pass_to_ugettext as _
    2829from mediagoblin.tools.response import render_to_response, redirect
    2930from mediagoblin.decorators import require_active_login, user_has_privilege
    from mediagoblin.media_types import FileTypeNotSupported  
    3334from mediagoblin.submit.lib import \
    3435    check_file_field, submit_media, get_upload_file_limits, \
    3536    FileUploadLimit, UserUploadLimit, UserPastUploadLimit
     37from mediagoblin.user_pages.lib import add_media_to_collection
    3638
    3739
    3840@require_active_login
    def submit_start(request):  
    4951        max_file_size=max_file_size,
    5052        upload_limit=upload_limit,
    5153        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
    5263
    5364    if request.method == 'POST' and submit_form.validate():
    5465        if not check_file_field(request, 'file'):
    def submit_start(request):  
    5667                _(u'You must provide a file.'))
    5768        else:
    5869            try:
    59                 submit_media(
     70                media = submit_media(
    6071                    mg_app=request.app, user=request.user,
    6172                    submitted_file=request.files['file'],
    6273                    filename=request.files['file'].filename,
    def submit_start(request):  
    6778                    upload_limit=upload_limit, max_file_size=max_file_size,
    6879                    urlgen=request.urlgen)
    6980
     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
    7088                add_message(request, SUCCESS, _('Woohoo! Submitted!'))
    7189
    7290                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  
    2323
    2424import os
    2525import pytest
     26import webtest.forms
    2627
    2728import six.moves.urllib.parse as urlparse
    2829
    gi.require_version('Gst', '1.0')  
    3233from gi.repository import Gst
    3334Gst.init(None)
    3435
    35 from mediagoblin.tests.tools import fixture_add_user
     36from mediagoblin.tests.tools import fixture_add_user, fixture_add_collection
    3637from .media_tools import create_av
    3738from mediagoblin import mg_globals
    3839from mediagoblin.db.models import MediaEntry, User, LocalUser
    class TestSubmission:  
    421422            size = os.stat(filename).st_size
    422423            assert last_size > size
    423424            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