Ejemplo n.º 1
0
def fixture_add_collection(name=u"My first Collection", user=None):
    if user is None:
        user = fixture_add_user()
    coll = Collection.query.filter_by(creator=user.id, title=name).first()
    if coll is not None:
        return coll
    coll = Collection()
    coll.creator = user.id
    coll.title = name
    coll.generate_slug()
    coll.save()

    # Reload
    Session.refresh(coll)

    # ... and detach from session:
    Session.expunge(coll)

    return coll
Ejemplo n.º 2
0
def fixture_add_collection(name="My first Collection",
                           user=None,
                           collection_type=Collection.USER_DEFINED_TYPE):
    if user is None:
        user = fixture_add_user()
    coll = Collection.query.filter_by(actor=user.id,
                                      title=name,
                                      type=collection_type).first()
    if coll is not None:
        return coll
    coll = Collection()
    coll.actor = user.id
    coll.title = name
    coll.type = collection_type
    coll.generate_slug()
    coll.save()

    # Reload
    Session.refresh(coll)

    # ... and detach from session:
    Session.expunge(coll)

    return coll
Ejemplo n.º 3
0
def media_collect(request, media):
    """Add media to collection submission"""

    # If media is not processed, return NotFound.
    if not media.state == 'processed':
        return render_404(request)

    form = user_forms.MediaCollectForm(request.form)
    # A user's own collections:
    form.collection.query = Collection.query.filter_by(
        actor=request.user.id,
        type=Collection.USER_DEFINED_TYPE).order_by(Collection.title)

    if request.method != 'POST' or not form.validate():
        # No POST submission, or invalid form
        if not form.validate():
            messages.add_message(request, messages.ERROR,
                                 _('Please check your entries and try again.'))

        return render_to_response(request,
                                  'mediagoblin/user_pages/media_collect.html',
                                  {
                                      'media': media,
                                      'form': form
                                  })

    # If we are here, method=POST and the form is valid, submit things.
    # If the user is adding a new collection, use that:
    if form.collection_title.data:
        # Make sure this user isn't duplicating an existing collection
        existing_collection = Collection.query.filter_by(
            actor=request.user.id,
            title=form.collection_title.data,
            type=Collection.USER_DEFINED_TYPE).first()
        if existing_collection:
            messages.add_message(
                request, messages.ERROR,
                _('You already have a collection called "%s"!') %
                existing_collection.title)
            return redirect(request,
                            "mediagoblin.user_pages.media_home",
                            user=media.get_actor.username,
                            media=media.slug_or_id)

        collection = Collection()
        collection.title = form.collection_title.data
        collection.description = form.collection_description.data
        collection.actor = request.user.id
        collection.type = Collection.USER_DEFINED_TYPE
        collection.generate_slug()
        collection.get_public_id(request.urlgen)
        create_activity("create", collection, collection.actor)
        collection.save()

    # Otherwise, use the collection selected from the drop-down
    else:
        collection = form.collection.data
        if collection and collection.actor != 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_actor.username,
                        media_id=media.id)

    item = CollectionItem.query.filter_by(collection=collection.id)
    item = item.join(CollectionItem.object_helper).filter_by(
        model_type=media.__tablename__, obj_pk=media.id).first()

    # Check whether media already exists in collection
    if item is not None:
        messages.add_message(
            request, messages.ERROR,
            _('"%s" already in collection "%s"') %
            (media.title, collection.title))
    else:  # Add item to collection
        add_media_to_collection(collection, media, form.note.data)
        create_activity("add", media, request.user, target=collection)
        messages.add_message(
            request, messages.SUCCESS,
            _('"%s" added to collection "%s"') %
            (media.title, collection.title))

    return redirect_obj(request, media)
Ejemplo n.º 4
0
def media_collect(request, media):
    """Add media to collection submission"""

    form = user_forms.MediaCollectForm(request.form)
    # A user's own collections:
    form.collection.query = Collection.query.filter_by(
        creator = request.user.id).order_by(Collection.title)

    if request.method != 'POST' or not form.validate():
        # No POST submission, or invalid form
        if not form.validate():
            messages.add_message(request, messages.ERROR,
                _('Please check your entries and try again.'))

        return render_to_response(
            request,
            'mediagoblin/user_pages/media_collect.html',
            {'media': media,
             'form': form})

    # If we are here, method=POST and the form is valid, submit things.
    # If the user is adding a new collection, use that:
    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=form.collection_title.data).first()
        if existing_collection:
            messages.add_message(request, messages.ERROR,
                _('You already have a collection called "%s"!')
                % existing_collection.title)
            return redirect(request, "mediagoblin.user_pages.media_home",
                            user=media.get_uploader.username,
                            media=media.slug_or_id)

        collection = Collection()
        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 = 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))
    else: # Add item to collection
        add_media_to_collection(collection, media, form.note.data)

        messages.add_message(request, messages.SUCCESS,
                             _('"%s" added to collection "%s"')
                             % (media.title, collection.title))

    return redirect_obj(request, media)