Esempio n. 1
0
def media_confirm_delete(request, media):

    form = user_forms.ConfirmDeleteForm(request.form)

    if request.method == "POST" and form.validate():
        if form.confirm.data is True:
            username = media.get_uploader.username
            # Delete MediaEntry and all related files, comments etc.
            media.delete()
            messages.add_message(request, messages.SUCCESS, _("You deleted the media."))

            location = media.url_to_next(request.urlgen)
            if not location:
                location = media.url_to_prev(request.urlgen)
            if not location:
                location = request.urlgen("mediagoblin.user_pages.user_home", user=username)
            return redirect(request, location=location)
        else:
            messages.add_message(
                request, messages.ERROR, _("The media was not deleted because you didn't check that you were sure.")
            )
            return redirect_obj(request, media)

    if request.user.is_admin and request.user.id != media.uploader:
        messages.add_message(
            request, messages.WARNING, _("You are about to delete another user's media. " "Proceed with caution.")
        )

    return render_to_response(
        request, "mediagoblin/user_pages/media_confirm_delete.html", {"media": media, "form": form}
    )
Esempio n. 2
0
def media_post_comment(request, media):
    """
    recieves POST from a MediaEntry() comment form, saves the comment.
    """
    if not request.method == "POST":
        raise MethodNotAllowed()

    comment = request.db.MediaComment()
    comment.media_entry = media.id
    comment.author = request.user.id
    print request.form["comment_content"]
    comment.content = unicode(request.form["comment_content"])

    # Show error message if commenting is disabled.
    if not mg_globals.app_config["allow_comments"]:
        messages.add_message(request, messages.ERROR, _("Sorry, comments are disabled."))
    elif not comment.content.strip():
        messages.add_message(request, messages.ERROR, _("Oops, your comment was empty."))
    else:
        comment.save()

        messages.add_message(request, messages.SUCCESS, _("Your comment has been posted!"))

        trigger_notification(comment, media, request)

        add_comment_subscription(request.user, media)

    return redirect_obj(request, media)
Esempio n. 3
0
def media_post_comment(request, media):
    """
    recieves POST from a MediaEntry() comment form, saves the comment.
    """
    if not request.method == 'POST':
        raise MethodNotAllowed()

    comment = request.db.MediaComment()
    comment.media_entry = media.id
    comment.author = request.user.id
    comment.content = unicode(request.form['comment_content'])

    # Show error message if commenting is disabled.
    if not mg_globals.app_config['allow_comments']:
        messages.add_message(
            request,
            messages.ERROR,
            _("Sorry, comments are disabled."))
    elif not comment.content.strip():
        messages.add_message(
            request,
            messages.ERROR,
            _("Oops, your comment was empty."))
    else:
        comment.save()

        messages.add_message(
            request, messages.SUCCESS,
            _('Your comment has been posted!'))

        trigger_notification(comment, media, request)

        add_comment_subscription(request.user, media)

    return redirect_obj(request, media)
Esempio n. 4
0
def media_confirm_delete(request, media):

    form = user_forms.ConfirmDeleteForm(request.form)

    if request.method == 'POST' and form.validate():
        if form.confirm.data is True:
            username = media.get_uploader.username
            # Delete MediaEntry and all related files, comments etc.
            media.delete()
            messages.add_message(
                request, messages.SUCCESS, _('You deleted the media.'))

            return redirect(request, "mediagoblin.user_pages.user_home",
                user=username)
        else:
            messages.add_message(
                request, messages.ERROR,
                _("The media was not deleted because you didn't check that you were sure."))
            return redirect_obj(request, media)

    if ((request.user.is_admin and
         request.user.id != media.uploader)):
        messages.add_message(
            request, messages.WARNING,
            _("You are about to delete another user's media. "
              "Proceed with caution."))

    return render_to_response(
        request,
        'mediagoblin/user_pages/media_confirm_delete.html',
        {'media': media,
         'form': form})
def media_post_comment(request, media):
    """
    recieves POST from a MediaEntry() comment form, saves the comment.
    """
    if not request.method == 'POST':
        raise MethodNotAllowed()

    comment = request.db.MediaComment()
    comment.media_entry = media.id
    comment.author = request.user.id
    comment.content = six.text_type(request.form['comment_content'])

    # Show error message if commenting is disabled.
    if not mg_globals.app_config['allow_comments']:
        messages.add_message(
            request,
            messages.ERROR,
            _("Sorry, comments are disabled."))
    elif not comment.content.strip():
        messages.add_message(
            request,
            messages.ERROR,
            _("Oops, your comment was empty."))
    else:
        create_activity("post", comment, comment.author, target=media)
        add_comment_subscription(request.user, media)
        comment.save()

        messages.add_message(
            request, messages.SUCCESS,
            _('Your comment has been posted!'))
        trigger_notification(comment, media, request)

    return redirect_obj(request, media)
Esempio n. 6
0
def edit_metadata(request, media):
    # If media is not processed, return NotFound.
    if not media.state == 'processed':
        return render_404(request)

    form = forms.EditMetaDataForm(request.method == 'POST' and request.form
                                  or None)
    if request.method == "POST" and form.validate():
        metadata_dict = {
            row['identifier']: row['value']
            for row in form.media_metadata.data
        }
        json_ld_metadata = None
        json_ld_metadata = compact_and_validate(metadata_dict)
        media.media_metadata = json_ld_metadata
        media.save()
        return redirect_obj(request, media)

    if len(form.media_metadata) == 0:
        for identifier, value in media.media_metadata.items():
            if identifier == "@context": continue
            form.media_metadata.append_entry({
                'identifier': identifier,
                'value': value
            })

    return render_to_response(request, 'mediagoblin/edit/metadata.html', {
        'form': form,
        'media': media
    })
Esempio n. 7
0
def media_post_comment(request, media):
    """
    recieves POST from a MediaEntry() comment form, saves the comment.
    """
    assert request.method == 'POST'

    comment = request.db.MediaComment()
    comment.media_entry = media.id
    comment.author = request.user.id
    comment.content = unicode(request.form['comment_content'])

    if not comment.content.strip():
        messages.add_message(
            request,
            messages.ERROR,
            _("Oops, your comment was empty."))
    else:
        comment.save()

        messages.add_message(
            request, messages.SUCCESS,
            _('Your comment has been posted!'))

        media_uploader = media.get_uploader
        #don't send email if you comment on your own post
        if (comment.author != media_uploader and
            media_uploader.wants_comment_notification):
            send_comment_email(media_uploader, comment, media, request)

    return redirect_obj(request, media)
Esempio n. 8
0
def media_post_comment(request, media):
    """
    recieves POST from a MediaEntry() comment form, saves the comment.
    """
    if not request.method == 'POST':
        raise MethodNotAllowed()

    comment = request.db.TextComment()
    comment.actor = request.user.id
    comment.content = six.text_type(request.form['comment_content'])

    # Show error message if commenting is disabled.
    if not mg_globals.app_config['allow_comments']:
        messages.add_message(request, messages.ERROR,
                             _("Sorry, comments are disabled."))
    elif not comment.content.strip():
        messages.add_message(request, messages.ERROR,
                             _("Oops, your comment was empty."))
    else:
        create_activity("post", comment, comment.actor, target=media)
        add_comment_subscription(request.user, media)
        comment.save()

        link = request.db.Comment()
        link.target = media
        link.comment = comment
        link.save()

        messages.add_message(request, messages.SUCCESS,
                             _('Your comment has been posted!'))
        trigger_notification(link, media, request)

    return redirect_obj(request, media)
Esempio n. 9
0
def collection_item_confirm_remove(request, collection_item):

    form = user_forms.ConfirmCollectionItemRemoveForm(request.form)

    if request.method == 'POST' and form.validate():
        collection = collection_item.in_collection

        if form.confirm.data is True:
            remove_collection_item(collection_item)

            messages.add_message(
                request, messages.SUCCESS, _('You deleted the item from the collection.'))
        else:
            messages.add_message(
                request, messages.ERROR,
                _("The item was not removed because you didn't check that you were sure."))

        return redirect_obj(request, collection)

    if ((request.user.has_privilege(u'admin') and
         request.user.id != collection_item.in_collection.creator)):
        messages.add_message(
            request, messages.WARNING,
            _("You are about to delete an item from another user's collection. "
              "Proceed with caution."))

    return render_to_response(
        request,
        'mediagoblin/user_pages/collection_item_confirm_remove.html',
        {'collection_item': collection_item,
         'form': form})
Esempio n. 10
0
def media_confirm_delete(request):
    
    allowed_state = [u'failed', u'processed']
    media = None
    for media_state in allowed_state:
        media = request.db.MediaEntry.query.filter_by(id=request.matchdict['media_id'], state=media_state).first()
        if media:
            break
    
    if not media:
        return render_404(request)
    
    given_username = request.matchdict.get('user')
    if given_username and (given_username != media.get_uploader.username):
        return render_404(request)
    
    uploader_id = media.uploader
    if not (request.user.is_admin or
            request.user.id == uploader_id):
        raise Forbidden()
    
    form = user_forms.ConfirmDeleteForm(request.form)

    if request.method == 'POST' and form.validate():
        if form.confirm.data is True:
            username = media.get_uploader.username
            # Delete MediaEntry and all related files, comments etc.
            media.delete()
            messages.add_message(
                request, messages.SUCCESS, _('You deleted the media.'))

            location = media.url_to_next(request.urlgen)
            if not location:
                location=media.url_to_prev(request.urlgen)
            if not location:
                location=request.urlgen("mediagoblin.user_pages.user_home",
                                        user=username)
            return redirect(request, location=location)
        else:
            messages.add_message(
                request, messages.ERROR,
                _("The media was not deleted because you didn't check that you were sure."))
            return redirect_obj(request, media)

    if ((request.user.is_admin and
         request.user.id != media.uploader)):
        messages.add_message(
            request, messages.WARNING,
            _("You are about to delete another user's media. "
              "Proceed with caution."))

    return render_to_response(
        request,
        'mediagoblin/user_pages/media_confirm_delete.html',
        {'media': media,
         'form': form})
Esempio n. 11
0
def media_confirm_delete(request, media):

    form = user_forms.ConfirmDeleteForm(request.form)

    if request.method == 'POST' and form.validate():
        if form.confirm.data is True:
            username = media.get_actor.username

            # This probably is already filled but just in case it has slipped
            # through the net somehow, we need to try and make sure the
            # MediaEntry has a public ID so it gets properly soft-deleted.
            media.get_public_id(request.urlgen)

            # Decrement the users uploaded quota.
            media.get_actor.uploaded = media.get_actor.uploaded - \
                media.file_size
            media.get_actor.save()

            # Delete MediaEntry and all related files, comments etc.
            media.delete()
            messages.add_message(
                request,
                messages.SUCCESS,
                _('You deleted the media.'))

            location = media.url_to_next(request.urlgen)
            if not location:
                location=media.url_to_prev(request.urlgen)
            if not location:
                location=request.urlgen("mediagoblin.user_pages.user_home",
                                        user=username)
            return redirect(request, location=location)
        else:
            messages.add_message(
                request,
                messages.ERROR,
                _("The media was not deleted because you didn't check "
                  "that you were sure."))
            return redirect_obj(request, media)

    if ((request.user.has_privilege(u'admin') and
         request.user.id != media.actor)):
        messages.add_message(
            request,
            messages.WARNING,
            _("You are about to delete another user's media. "
              "Proceed with caution."))

    return render_to_response(
        request,
        'mediagoblin/user_pages/media_confirm_delete.html',
        {'media': media,
         'form': form})
Esempio n. 12
0
def collection_confirm_delete(request, collection):

    form = user_forms.ConfirmDeleteForm(request.form)

    if request.method == 'POST' and form.validate():

        username = collection.get_actor.username

        if form.confirm.data is True:
            collection_title = collection.title

            # Firstly like with the MediaEntry delete, lets ensure the
            # public_id is populated as this is really important!
            collection.get_public_id(request.urlgen)

            # Delete all the associated collection items
            for item in collection.get_collection_items():
                obj = item.get_object()
                obj.save()
                item.delete()

            collection.delete()
            messages.add_message(
                request,
                messages.SUCCESS,
                _('You deleted the collection "%s"') %
                    collection_title)

            return redirect(request, "mediagoblin.user_pages.user_home",
                user=username)
        else:
            messages.add_message(
                request,
                messages.ERROR,
                _("The collection was not deleted because you didn't "
                  "check that you were sure."))

            return redirect_obj(request, collection)

    if ((request.user.has_privilege(u'admin') and
         request.user.id != collection.actor)):
        messages.add_message(
            request, messages.WARNING,
            _("You are about to delete another user's collection. "
              "Proceed with caution."))

    return render_to_response(
        request,
        'mediagoblin/user_pages/collection_confirm_delete.html',
        {'collection': collection,
         'form': form})
Esempio n. 13
0
def edit_collection(request, collection):
    defaults = dict(
        title=collection.title,
        slug=collection.slug,
        description=collection.description)

    form = forms.EditCollectionForm(
        request.form,
        **defaults)

    if request.method == 'POST' and form.validate():
        # Make sure there isn't already a Collection with such a slug
        # and userid.
        slug_used = check_collection_slug_used(collection.creator,
                form.slug.data, collection.id)

        # Make sure there isn't already a Collection with this title
        existing_collection = request.db.Collection.query.filter_by(
                creator=request.user.id,
                title=form.title.data).first()

        if existing_collection and existing_collection.id != collection.id:
            messages.add_message(
                request, messages.ERROR,
                _('You already have a collection called "%s"!') % \
                    form.title.data)
        elif slug_used:
            form.slug.errors.append(
                _(u'A collection with that slug already exists for this user.'))
        else:
            collection.title = six.text_type(form.title.data)
            collection.description = six.text_type(form.description.data)
            collection.slug = six.text_type(form.slug.data)

            collection.save()

            return redirect_obj(request, collection)

    if request.user.has_privilege(u'admin') \
            and collection.creator != request.user.id \
            and request.method != 'POST':
        messages.add_message(
            request, messages.WARNING,
            _("You are editing another user's collection. Proceed with caution."))

    return render_to_response(
        request,
        'mediagoblin/edit/edit_collection.html',
        {'collection': collection,
         'form': form})
Esempio n. 14
0
def edit_collection(request, collection):
    defaults = dict(
        title=collection.title,
        slug=collection.slug,
        description=collection.description)

    form = forms.EditCollectionForm(
        request.form,
        **defaults)

    if request.method == 'POST' and form.validate():
        # Make sure there isn't already a Collection with such a slug
        # and userid.
        slug_used = check_collection_slug_used(collection.creator,
                form.slug.data, collection.id)

        # Make sure there isn't already a Collection with this title
        existing_collection = request.db.Collection.query.filter_by(
                creator=request.user.id,
                title=form.title.data).first()

        if existing_collection and existing_collection.id != collection.id:
            messages.add_message(
                request, messages.ERROR,
                _('You already have a collection called "%s"!') % \
                    form.title.data)
        elif slug_used:
            form.slug.errors.append(
                _(u'A collection with that slug already exists for this user.'))
        else:
            collection.title = unicode(form.title.data)
            collection.description = unicode(form.description.data)
            collection.slug = unicode(form.slug.data)

            collection.save()

            return redirect_obj(request, collection)

    if request.user.has_privilege(u'admin') \
            and collection.creator != request.user.id \
            and request.method != 'POST':
        messages.add_message(
            request, messages.WARNING,
            _("You are editing another user's collection. Proceed with caution."))

    return render_to_response(
        request,
        'mediagoblin/edit/edit_collection.html',
        {'collection': collection,
         'form': form})
Esempio n. 15
0
def collection_confirm_delete(request, collection):

    form = user_forms.ConfirmDeleteForm(request.form)

    if request.method == 'POST' and form.validate():

        username = collection.get_actor.username

        if form.confirm.data is True:
            collection_title = collection.title

            # Firstly like with the MediaEntry delete, lets ensure the
            # public_id is populated as this is really important!
            collection.get_public_id(request.urlgen)

            # Delete all the associated collection items
            for item in collection.get_collection_items():
                obj = item.get_object()
                obj.save()
                item.delete()

            collection.delete()
            messages.add_message(
                request, messages.SUCCESS,
                _('You deleted the collection "%s"') % collection_title)

            return redirect(request,
                            "mediagoblin.user_pages.user_home",
                            user=username)
        else:
            messages.add_message(
                request, messages.ERROR,
                _("The collection was not deleted because you didn't "
                  "check that you were sure."))

            return redirect_obj(request, collection)

    if (request.user.has_privilege('admin')
            and request.user.id != collection.actor):
        messages.add_message(
            request, messages.WARNING,
            _("You are about to delete another user's collection. "
              "Proceed with caution."))

    return render_to_response(
        request, 'mediagoblin/user_pages/collection_confirm_delete.html', {
            'collection': collection,
            'form': form
        })
Esempio n. 16
0
def media_confirm_delete(request, media):

    form = user_forms.ConfirmDeleteForm(request.form)

    if request.method == 'POST' and form.validate():
        if form.confirm.data is True:
            username = media.get_actor.username

            # This probably is already filled but just in case it has slipped
            # through the net somehow, we need to try and make sure the
            # MediaEntry has a public ID so it gets properly soft-deleted.
            media.get_public_id(request.urlgen)

            # Decrement the users uploaded quota.
            media.get_actor.uploaded = media.get_actor.uploaded - \
                media.file_size
            media.get_actor.save()

            # Delete MediaEntry and all related files, comments etc.
            media.delete()
            messages.add_message(request, messages.SUCCESS,
                                 _('You deleted the media.'))

            location = media.url_to_next(request.urlgen)
            if not location:
                location = media.url_to_prev(request.urlgen)
            if not location:
                location = request.urlgen("mediagoblin.user_pages.user_home",
                                          user=username)
            return redirect(request, location=location)
        else:
            messages.add_message(
                request, messages.ERROR,
                _("The media was not deleted because you didn't check "
                  "that you were sure."))
            return redirect_obj(request, media)

    if (request.user.has_privilege('admin')
            and request.user.id != media.actor):
        messages.add_message(
            request, messages.WARNING,
            _("You are about to delete another user's media. "
              "Proceed with caution."))

    return render_to_response(
        request, 'mediagoblin/user_pages/media_confirm_delete.html', {
            'media': media,
            'form': form
        })
Esempio n. 17
0
def edit_media(request, media):
    # If media is not processed, return NotFound.
    if not media.state == 'processed':
        return render_404(request)

    if not may_edit_media(request, media):
        raise Forbidden("User may not edit this media")

    defaults = dict(title=media.title,
                    slug=media.slug,
                    description=media.description,
                    tags=media_tags_as_string(media.tags),
                    license=media.license)

    form = forms.EditForm(request.method == 'POST' and request.form or None,
                          **defaults)

    if request.method == 'POST' and form.validate():
        # Make sure there isn't already a MediaEntry with such a slug
        # and userid.
        slug = slugify(form.slug.data)
        slug_used = check_media_slug_used(media.actor, slug, media.id)

        if slug_used:
            form.slug.errors.append(
                _('An entry with that slug already exists for this user.'))
        else:
            media.title = form.title.data
            media.description = form.description.data
            media.tags = convert_to_tag_list_of_dicts(form.tags.data)

            media.license = str(form.license.data) or None
            media.slug = slug
            media.save()

            return redirect_obj(request, media)

    if request.user.has_privilege('admin') \
            and media.actor != request.user.id \
            and request.method != 'POST':
        messages.add_message(
            request, messages.WARNING,
            _("You are editing another user's media. Proceed with caution."))

    return render_to_response(request, 'mediagoblin/edit/edit.html', {
        'media': media,
        'form': form
    })
Esempio n. 18
0
def edit_media(request, media):
    if not may_edit_media(request, media):
        raise Forbidden("User may not edit this media")

    defaults = dict(
        title=media.title,
        slug=media.slug,
        description=media.description,
        tags=media_tags_as_string(media.tags),
        license=media.license)

    form = forms.EditForm(
        request.form,
        **defaults)

    if request.method == 'POST' and form.validate():
        # Make sure there isn't already a MediaEntry with such a slug
        # and userid.
        slug = slugify(form.slug.data)
        slug_used = check_media_slug_used(media.uploader, slug, media.id)

        if slug_used:
            form.slug.errors.append(
                _(u'An entry with that slug already exists for this user.'))
        else:
            media.title = form.title.data
            media.description = form.description.data
            media.tags = convert_to_tag_list_of_dicts(
                                   form.tags.data)

            media.license = unicode(form.license.data) or None
            media.slug = slug
            media.save()

            return redirect_obj(request, media)

    if request.user.has_privilege(u'admin') \
            and media.uploader != request.user.id \
            and request.method != 'POST':
        messages.add_message(
            request, messages.WARNING,
            _("You are editing another user's media. Proceed with caution."))

    return render_to_response(
        request,
        'mediagoblin/edit/edit.html',
        {'media': media,
         'form': form})
Esempio n. 19
0
def collection_confirm_delete(request, collection):

    form = user_forms.ConfirmDeleteForm(request.form)

    if request.method == 'POST' and form.validate():

        username = collection.get_creator.username

        if form.confirm.data is True:
            collection_title = collection.title

            # Delete all the associated collection items
            for item in collection.get_collection_items():
                entry = item.get_media_entry
                entry.collected = entry.collected - 1
                entry.save()
                item.delete()

            collection.delete()
            messages.add_message(
                request, messages.SUCCESS,
                _('You deleted the collection "%s"') % collection_title)

            return redirect(request,
                            "mediagoblin.user_pages.user_home",
                            user=username)
        else:
            messages.add_message(
                request, messages.ERROR,
                _("The collection was not deleted because you didn't check that you were sure."
                  ))

            return redirect_obj(request, collection)

    if ((request.user.is_admin and request.user.id != collection.creator)):
        messages.add_message(
            request, messages.WARNING,
            _("You are about to delete another user's collection. "
              "Proceed with caution."))

    return render_to_response(
        request, 'mediagoblin/user_pages/collection_confirm_delete.html', {
            'collection': collection,
            'form': form
        })
Esempio n. 20
0
def collection_confirm_delete(request, collection):

    form = user_forms.ConfirmDeleteForm(request.form)

    if request.method == 'POST' and form.validate():

        username = collection.get_creator.username

        if form.confirm.data is True:
            collection_title = collection.title

            # Delete all the associated collection items
            for item in collection.get_collection_items():
                entry = item.get_media_entry
                entry.collected = entry.collected - 1
                entry.save()
                item.delete()

            collection.delete()
            messages.add_message(request, messages.SUCCESS,
                _('You deleted the collection "%s"') % collection_title)

            return redirect(request, "mediagoblin.user_pages.user_home",
                user=username)
        else:
            messages.add_message(
                request, messages.ERROR,
                _("The collection was not deleted because you didn't check that you were sure."))

            return redirect_obj(request, collection)

    if ((request.user.is_admin and
         request.user.id != collection.creator)):
        messages.add_message(
            request, messages.WARNING,
            _("You are about to delete another user's collection. "
              "Proceed with caution."))

    return render_to_response(
        request,
        'mediagoblin/user_pages/collection_confirm_delete.html',
        {'collection': collection,
         'form': form})
Esempio n. 21
0
def media_confirm_delete(request, media):

    form = user_forms.ConfirmDeleteForm(request.form)

    if request.method == 'POST' and form.validate():
        if form.confirm.data is True:
            username = media.get_uploader.username

            media.get_uploader.uploaded = media.get_uploader.uploaded - \
                media.file_size
            media.get_uploader.save()

            # Delete MediaEntry and all related files, comments etc.
            media.delete()
            messages.add_message(
                request, messages.SUCCESS, _('You deleted the media.'))

            location = media.url_to_next(request.urlgen)
            if not location:
                location=media.url_to_prev(request.urlgen)
            if not location:
                location=request.urlgen("mediagoblin.user_pages.user_home",
                                        user=username)
            return redirect(request, location=location)
        else:
            messages.add_message(
                request, messages.ERROR,
                _("The media was not deleted because you didn't check that you were sure."))
            return redirect_obj(request, media)

    if ((request.user.has_privilege(u'admin') and
         request.user.id != media.uploader)):
        messages.add_message(
            request, messages.WARNING,
            _("You are about to delete another user's media. "
              "Proceed with caution."))

    return render_to_response(
        request,
        'mediagoblin/user_pages/media_confirm_delete.html',
        {'media': media,
         'form': form})
Esempio n. 22
0
def collection_item_confirm_remove(request, collection_item):

    form = user_forms.ConfirmCollectionItemRemoveForm(request.form)

    if request.method == 'POST' and form.validate():
        username = collection_item.in_collection.get_creator.username
        collection = collection_item.in_collection

        if form.confirm.data is True:
            entry = collection_item.get_media_entry
            entry.collected = entry.collected - 1
            entry.save()

            collection_item.delete()
            collection.items = collection.items - 1
            collection.save()

            messages.add_message(
                request, messages.SUCCESS,
                _('You deleted the item from the collection.'))
        else:
            messages.add_message(
                request, messages.ERROR,
                _("The item was not removed because you didn't check that you were sure."
                  ))

        return redirect_obj(request, collection)

    if ((request.user.is_admin
         and request.user.id != collection_item.in_collection.creator)):
        messages.add_message(
            request, messages.WARNING,
            _("You are about to delete an item from another user's collection. "
              "Proceed with caution."))

    return render_to_response(
        request, 'mediagoblin/user_pages/collection_item_confirm_remove.html',
        {
            'collection_item': collection_item,
            'form': form
        })
Esempio n. 23
0
def edit_metadata(request, media):
    form = forms.EditMetaDataForm(request.form)
    if request.method == "POST" and form.validate():
        metadata_dict = dict([(row['identifier'], row['value'])
                              for row in form.media_metadata.data])
        json_ld_metadata = None
        json_ld_metadata = compact_and_validate(metadata_dict)
        media.media_metadata = json_ld_metadata
        media.save()
        return redirect_obj(request, media)

    if len(form.media_metadata) == 0:
        for identifier, value in media.media_metadata.iteritems():
            if identifier == "@context": continue
            form.media_metadata.append_entry({
                'identifier': identifier,
                'value': value
            })

    return render_to_response(request, 'mediagoblin/edit/metadata.html', {
        'form': form,
        'media': media
    })
Esempio n. 24
0
def edit_metadata(request, media):
    form = forms.EditMetaDataForm(request.form)
    if request.method == "POST" and form.validate():
        metadata_dict = dict([(row['identifier'],row['value'])
                            for row in form.media_metadata.data])
        json_ld_metadata = None
        json_ld_metadata = compact_and_validate(metadata_dict)
        media.media_metadata = json_ld_metadata
        media.save()
        return redirect_obj(request, media)      

    if len(form.media_metadata) == 0:
        for identifier, value in media.media_metadata.iteritems():
            if identifier == "@context": continue
            form.media_metadata.append_entry({
                'identifier':identifier,
                'value':value})

    return render_to_response(
        request,
        'mediagoblin/edit/metadata.html',
        {'form':form,
         'media':media})
Esempio n. 25
0
def collection_item_confirm_remove(request, collection_item):

    form = user_forms.ConfirmCollectionItemRemoveForm(request.form)

    if request.method == "POST" and form.validate():
        username = collection_item.in_collection.get_creator.username
        collection = collection_item.in_collection

        if form.confirm.data is True:
            entry = collection_item.get_media_entry
            entry.collected = entry.collected - 1
            entry.save()

            collection_item.delete()
            collection.items = collection.items - 1
            collection.save()

            messages.add_message(request, messages.SUCCESS, _("You deleted the item from the collection."))
        else:
            messages.add_message(
                request, messages.ERROR, _("The item was not removed because you didn't check that you were sure.")
            )

        return redirect_obj(request, collection)

    if request.user.is_admin and request.user.id != collection_item.in_collection.creator:
        messages.add_message(
            request,
            messages.WARNING,
            _("You are about to delete an item from another user's collection. " "Proceed with caution."),
        )

    return render_to_response(
        request,
        "mediagoblin/user_pages/collection_item_confirm_remove.html",
        {"collection_item": collection_item, "form": form},
    )
Esempio n. 26
0
def collection_confirm_delete(request, collection):

    form = user_forms.ConfirmDeleteForm(request.form)

    if request.method == "POST" and form.validate():

        username = collection.get_creator.username

        if form.confirm.data is True:
            collection_title = collection.title

            # Delete all the associated collection items
            for item in collection.get_collection_items():
                remove_collection_item(item)

            collection.delete()
            messages.add_message(request, messages.SUCCESS, _('You deleted the collection "%s"') % collection_title)

            return redirect(request, "mediagoblin.user_pages.user_home", user=username)
        else:
            messages.add_message(
                request,
                messages.ERROR,
                _("The collection was not deleted because you didn't check that you were sure."),
            )

            return redirect_obj(request, collection)

    if request.user.has_privilege(u"admin") and request.user.id != collection.creator:
        messages.add_message(
            request, messages.WARNING, _("You are about to delete another user's collection. " "Proceed with caution.")
        )

    return render_to_response(
        request, "mediagoblin/user_pages/collection_confirm_delete.html", {"collection": collection, "form": form}
    )
Esempio n. 27
0
def edit_collection(request, collection):
    defaults = dict(title=collection.title, slug=collection.slug, description=collection.description)

    form = forms.EditCollectionForm(request.form, **defaults)

    if request.method == "POST" and form.validate():
        # Make sure there isn't already a Collection with such a slug
        # and userid.
        slug_used = check_collection_slug_used(collection.creator, form.slug.data, collection.id)

        # Make sure there isn't already a Collection with this title
        existing_collection = request.db.Collection.find_one({"creator": request.user.id, "title": form.title.data})

        if existing_collection and existing_collection.id != collection.id:
            messages.add_message(
                request, messages.ERROR, _('You already have a collection called "%s"!') % form.title.data
            )
        elif slug_used:
            form.slug.errors.append(_(u"A collection with that slug already exists for this user."))
        else:
            collection.title = unicode(form.title.data)
            collection.description = unicode(form.description.data)
            collection.slug = unicode(form.slug.data)

            collection.save()

            return redirect_obj(request, collection)

    if request.user.is_admin and collection.creator != request.user.id and request.method != "POST":
        messages.add_message(
            request, messages.WARNING, _("You are editing another user's collection. Proceed with caution.")
        )

    return render_to_response(
        request, "mediagoblin/edit/edit_collection.html", {"collection": collection, "form": form}
    )
Esempio n. 28
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(
        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
    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()

    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)

    # Check whether media already exists in collection
    elif 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)
Esempio n. 29
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)
Esempio n. 30
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)
Esempio n. 31
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)