Ejemplo n.º 1
0
def pwg_images_addSimple(request):
    form = AddSimpleForm(request.form)
    if not form.validate():
        _log.error("addSimple: form failed")
        raise BadRequest()
    dump = []
    for f in form:
        dump.append("%s=%r" % (f.name, f.data))
    _log.info("addSimple: %r %s %r", request.form, " ".join(dump), request.files)

    if not check_file_field(request, "image"):
        raise BadRequest()

    filename = request.files["image"].filename

    # Sniff the submitted media to determine which
    # media plugin should handle processing
    media_type, media_manager = sniff_media(request.files["image"])

    # create entry and save in database
    entry = new_upload_entry(request.user)
    entry.media_type = unicode(media_type)
    entry.title = unicode(form.name.data) or unicode(splitext(filename)[0])

    entry.description = unicode(form.comment.data)

    """
    # Process the user's folksonomy "tags"
    entry.tags = convert_to_tag_list_of_dicts(
        form.tags.data)
    """

    # Generate a slug from the title
    entry.generate_slug()

    queue_file = prepare_queue_task(request.app, entry, filename)

    with queue_file:
        shutil.copyfileobj(request.files["image"].stream, queue_file, length=4 * 1048576)

    # Save now so we have this data before kicking off processing
    entry.save()

    # Pass off to processing
    #
    # (... don't change entry after this point to avoid race
    # conditions with changes to the document via processing code)
    feed_url = request.urlgen("mediagoblin.user_pages.atom_feed", qualified=True, user=request.user.username)
    run_process_media(entry, feed_url)

    collection_id = form.category.data
    if collection_id > 0:
        collection = Collection.query.get(collection_id)
        if collection is not None and collection.creator == request.user.id:
            add_media_to_collection(collection, entry, "")

    return {"image_id": entry.id, "url": entry.url_for_self(request.urlgen, qualified=True)}
Ejemplo n.º 2
0
def pwg_images_addSimple(request):
    form = AddSimpleForm(request.form)
    if not form.validate():
        _log.error("addSimple: form failed")
        raise BadRequest()
    dump = []
    for f in form:
        dump.append("%s=%r" % (f.name, f.data))
    _log.info("addSimple: %r %s %r", request.form, " ".join(dump),
              request.files)

    if not check_file_field(request, 'image'):
        raise BadRequest()

    upload_limit, max_file_size = get_upload_file_limits(request.user)

    try:
        entry = submit_media(
            mg_app=request.app, user=request.user,
            submitted_file=request.files['image'],
            filename=request.files['image'].filename,
            title=six.text_type(form.name.data),
            description=six.text_type(form.comment.data),
            upload_limit=upload_limit, max_file_size=max_file_size)

        collection_id = form.category.data
        if collection_id > 0:
            collection = Collection.query.get(collection_id)
            if collection is not None and collection.actor == request.user.id:
                add_media_to_collection(collection, entry, "")

        return {
            'image_id': entry.id,
            'url': entry.url_for_self(
                request.urlgen,
                qualified=True)}

    # Handle upload limit issues
    except FileUploadLimit:
        raise BadRequest(
            _(u'Sorry, the file size is too big.'))
    except UserUploadLimit:
        raise BadRequest(
            _('Sorry, uploading this file will put you over your'
              ' upload limit.'))
    except UserPastUploadLimit:
        raise BadRequest(
            _('Sorry, you have reached your upload limit.'))
Ejemplo n.º 3
0
 def add_to_collection(self, collection_title, entries):
     if not entries:
         return
     collection = (self.db.Collection.query
                   .filter_by(creator=1, title=collection_title)
                   .first())
     if not collection:
         collection = self.db.Collection()
         collection.title = collection_title
         collection.creator = 1
         collection.generate_slug()
         collection.save()
         Session.commit()
     for entry in entries:
         add_media_to_collection(collection, entry, commit=False)
     try:
         Session.commit()
     except Exception as exc:
         print (u"Could add media to collection {}: {}"
                "".format(collection_title, exc))
         Session.rollback()
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)
Ejemplo n.º 5
0
def submit_start(request):
    """
    First view for submitting a file.
    """
    upload_limit, max_file_size = get_upload_file_limits(request.user)

    submit_form = submit_forms.get_submit_start_form(
        request.form,
        license=request.user.license_preference,
        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)

    # Only show the Collections dropdown if the user has some
    # collections set up
    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'):
            submit_form.file.errors.append(
                _(u'You must provide a file.'))
        else:
            try:
                media = submit_media(
                    mg_app=request.app, user=request.user,
                    submitted_file=request.files['file'],
                    filename=request.files['file'].filename,
                    title=six.text_type(submit_form.title.data),
                    description=six.text_type(submit_form.description.data),
                    license=six.text_type(submit_form.license.data) or None,
                    tags_string=submit_form.tags.data,
                    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)

                messages.add_message(
                    request,
                    messages.SUCCESS,
                    _('Woohoo! Submitted!'))

                return redirect(request, "mediagoblin.user_pages.user_home",
                            user=request.user.username)


            # Handle upload limit issues
            except FileUploadLimit:
                submit_form.file.errors.append(
                    _(u'Sorry, the file size is too big.'))
            except UserUploadLimit:
                submit_form.file.errors.append(
                    _('Sorry, uploading this file will put you over your'
                      ' upload limit.'))
            except UserPastUploadLimit:
                messages.add_message(
                    request,
                    messages.WARNING,
                    _('Sorry, you have reached your upload limit.'))
                return redirect(request, "mediagoblin.user_pages.user_home",
                                user=request.user.username)
            except FileTypeNotSupported as e:
                submit_form.file.errors.append(e)
            except Exception as e:
                raise

    return render_to_response(
        request,
        'mediagoblin/submit/start.html',
        {'submit_form': submit_form,
         'app_config': mg_globals.app_config})
Ejemplo n.º 6
0
def submit_media(
    mg_app,
    user,
    submitted_file,
    filename,
    title=None,
    description=None,
    collection_slug=None,
    license=None,
    metadata=None,
    tags_string=u"",
    callback_url=None,
    urlgen=None,
):
    """
    Args:
     - mg_app: The MediaGoblinApp instantiated for this process
     - user: the user object this media entry should be associated with
     - submitted_file: the file-like object that has the
       being-submitted file data in it (this object should really have
       a .name attribute which is the filename on disk!)
     - filename: the *original* filename of this.  Not necessarily the
       one on disk being referenced by submitted_file.
     - title: title for this media entry
     - description: description for this media entry
     - collection_slug: collection for this media entry
     - license: license for this media entry
     - tags_string: comma separated string of tags to be associated
       with this entry
     - callback_url: possible post-hook to call after submission
     - urlgen: if provided, used to do the feed_url update and assign a public
               ID used in the API (very important).
    """
    upload_limit, max_file_size = get_upload_file_limits(user)
    if upload_limit and user.uploaded >= upload_limit:
        raise UserPastUploadLimit()

    # If the filename contains non ascii generate a unique name
    if not all(ord(c) < 128 for c in filename):
        filename = six.text_type(uuid.uuid4()) + splitext(filename)[-1]

    # Sniff the submitted media to determine which
    # media plugin should handle processing
    media_type, media_manager = sniff_media(submitted_file, filename)

    # create entry and save in database
    entry = new_upload_entry(user)
    entry.media_type = media_type
    entry.title = (title or six.text_type(splitext(filename)[0]))

    entry.description = description or u""

    entry.license = license or None

    entry.media_metadata = metadata or {}

    # Process the user's folksonomy "tags"
    entry.tags = convert_to_tag_list_of_dicts(tags_string)

    # Generate a slug from the title
    entry.generate_slug()

    queue_file = prepare_queue_task(mg_app, entry, filename)

    with queue_file:
        queue_file.write(submitted_file)

    # Get file size and round to 2 decimal places
    file_size = mg_app.queue_store.get_file_size(
        entry.queued_media_file) / (1024.0 * 1024)
    file_size = float('{0:.2f}'.format(file_size))

    # Check if file size is over the limit
    if max_file_size and file_size >= max_file_size:
        raise FileUploadLimit()

    # Check if user is over upload limit
    if upload_limit and (user.uploaded + file_size) >= upload_limit:
        raise UserUploadLimit()

    user.uploaded = user.uploaded + file_size
    user.save()

    entry.file_size = file_size

    # Save now so we have this data before kicking off processing
    entry.save()

    # Various "submit to stuff" things, callbackurl and this silly urlgen
    # thing
    if callback_url:
        metadata = ProcessingMetaData()
        metadata.media_entry = entry
        metadata.callback_url = callback_url
        metadata.save()

    if urlgen:
        # Generate the public_id, this is very importent, especially relating
        # to deletion, it allows the shell to be accessable post-delete!
        entry.get_public_id(urlgen)

        # Generate the feed URL
        feed_url = urlgen('mediagoblin.user_pages.atom_feed',
                          qualified=True,
                          user=user.username)
    else:
        feed_url = None

    add_comment_subscription(user, entry)

    # Create activity
    create_activity("post", entry, entry.actor)
    entry.save()

    # add to collection
    if collection_slug:
        collection = Collection.query.filter_by(slug=collection_slug,
                                                actor=user.id).first()
        if collection:
            add_media_to_collection(collection, entry)

    # Pass off to processing
    #
    # (... don't change entry after this point to avoid race
    # conditions with changes to the document via processing code)
    run_process_media(entry, feed_url)

    return entry
def multi_submit_start(request):
    """
  First view for submitting a file.
  """
    submit_form = submit_forms.get_submit_start_form(
        request.form, license=request.user.license_preference)
    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


#  Below is what was used for mediagoblin 0.5.0-dev. Above is the new way.
#  submit_form = submit_forms.SubmitStartForm(request.form, license=request.user.license_preference)
    filecount = 0
    if request.method == 'POST' and submit_form.validate():
        if not check_file_field(request, 'file'):
            submit_form.file.errors.append(
                _(u'You must provide at least one file.'))
        else:
            for submitted_file in request.files.getlist('file'):
                try:
                    if not submitted_file.filename:
                        # MOST likely an invalid file
                        continue  # Skip the rest of the loop for this file
                    else:
                        filename = submitted_file.filename
                        _log.info("html5-multi-upload: Got filename: %s" %
                                  filename)

                        # If the filename contains non ascii generate a unique name
                        if not all(ord(c) < 128 for c in filename):
                            filename = str(
                                uuid.uuid4()) + splitext(filename)[-1]

                        # Sniff the submitted media to determine which
                        # media plugin should handle processing
                        media_type, media_manager = sniff_media(
                            submitted_file, filename)

                        # create entry and save in database
                        entry = new_upload_entry(request.user)
                        entry.media_type = str(media_type)
                        entry.title = (str(submit_form.title.data) or str(
                            splitext(submitted_file.filename)[0]))

                        entry.description = str(submit_form.description.data)

                        entry.license = str(submit_form.license.data) or None

                        # Process the user's folksonomy "tags"
                        entry.tags = convert_to_tag_list_of_dicts(
                            submit_form.tags.data)

                        # Generate a slug from the title
                        entry.generate_slug()

                        queue_file = prepare_queue_task(
                            request.app, entry, filename)

                        with queue_file:
                            queue_file.write(submitted_file.stream.read())

                        # Save now so we have this data before kicking off processing
                        entry.save()

                        # Pass off to async processing
                        #
                        # (... don't change entry after this point to avoid race
                        # conditions with changes to the document via processing code)
                        feed_url = request.urlgen(
                            'mediagoblin.user_pages.atom_feed',
                            qualified=True,
                            user=request.user.username)
                        run_process_media(entry, feed_url)
                        if submit_form.collection and submit_form.collection.data:
                            add_media_to_collection(
                                submit_form.collection.data, entry)
                            create_activity("add",
                                            entry,
                                            request.user,
                                            target=submit_form.collection.data)

                        add_comment_subscription(request.user, entry)
                        filecount = filecount + 1

                except Exception as e:
                    '''
          This section is intended to catch exceptions raised in
          mediagoblin.media_types
          '''
                    if isinstance(e, TypeNotFound) or isinstance(
                            e, FileTypeNotSupported):
                        submit_form.file.errors.append(e)
                    else:
                        raise

            add_message(request, SUCCESS,
                        _('Woohoo! Submitted %d Files!' % filecount))
            return redirect(request,
                            "mediagoblin.user_pages.user_home",
                            user=request.user.username)

    return render_to_response(request, 'start.html',
                              {'multi_submit_form': submit_form})
Ejemplo n.º 8
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)
def multi_submit_start(request):
  """
  First view for submitting a file.
  """
  submit_form = submit_forms.get_submit_start_form(request.form, license=request.user.license_preference)
  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

#  Below is what was used for mediagoblin 0.5.0-dev. Above is the new way.
#  submit_form = submit_forms.SubmitStartForm(request.form, license=request.user.license_preference)
  filecount = 0
  if request.method == 'POST' and submit_form.validate():
    if not check_file_field(request, 'file'):
      submit_form.file.errors.append(_(u'You must provide at least one file.'))
    else:
      for submitted_file in request.files.getlist('file'):
        try:
          if not submitted_file.filename:
            # MOST likely an invalid file
            continue # Skip the rest of the loop for this file
          else:
            filename = submitted_file.filename
            _log.info("html5-multi-upload: Got filename: %s" % filename)

            # If the filename contains non ascii generate a unique name
            if not all(ord(c) < 128 for c in filename):
              filename = unicode(uuid.uuid4()) + splitext(filename)[-1]

            # Sniff the submitted media to determine which
            # media plugin should handle processing
            media_type, media_manager = sniff_media(
              submitted_file, filename)

            # create entry and save in database
            entry = new_upload_entry(request.user)
            entry.media_type = unicode(media_type)
            entry.title = (
              unicode(submit_form.title.data)
              or unicode(splitext(submitted_file.filename)[0]))

            entry.description = unicode(submit_form.description.data)

            entry.license = unicode(submit_form.license.data) or None

            # Process the user's folksonomy "tags"
            entry.tags = convert_to_tag_list_of_dicts(
              submit_form.tags.data)

            # Generate a slug from the title
            entry.generate_slug()

            queue_file = prepare_queue_task(request.app, entry, filename)

            with queue_file:
              queue_file.write(submitted_file.stream.read())

            # Save now so we have this data before kicking off processing
            entry.save()

            # Pass off to async processing
            #
            # (... don't change entry after this point to avoid race
            # conditions with changes to the document via processing code)
            feed_url = request.urlgen(
              'mediagoblin.user_pages.atom_feed',
              qualified=True, user=request.user.username)
            run_process_media(entry, feed_url)
            if submit_form.collection and submit_form.collection.data:
              add_media_to_collection(
                submit_form.collection.data, entry)
              create_activity(
                "add", entry, request.user,
                target=submit_form.collection.data)

            add_comment_subscription(request.user, entry)
            filecount = filecount + 1

        except Exception as e:
          '''
          This section is intended to catch exceptions raised in
          mediagoblin.media_types
          '''
          if isinstance(e, TypeNotFound) or isinstance(e, FileTypeNotSupported):
            submit_form.file.errors.append(e)
          else:
            raise

      add_message(request, SUCCESS, _('Woohoo! Submitted %d Files!' % filecount))
      return redirect(request, "mediagoblin.user_pages.user_home",
              user=request.user.username)

  return render_to_response(
    request,
    'start.html',
    {'multi_submit_form': submit_form})
Ejemplo n.º 10
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)
Ejemplo n.º 11
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)
Ejemplo n.º 12
0
def submit_start(request):
    """
    First view for submitting a file.
    """
    upload_limit, max_file_size = get_upload_file_limits(request.user)

    submit_form = submit_forms.get_submit_start_form(
        request.form,
        license=request.user.license_preference,
        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)

    # Only show the Collections dropdown if the user has some
    # collections set up
    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'):
            submit_form.file.errors.append(
                _('You must provide a file.'))
        else:
            try:
                media = submit_media(
                    mg_app=request.app, user=request.user,
                    submitted_file=request.files['file'],
                    filename=request.files['file'].filename,
                    title=str(submit_form.title.data),
                    description=str(submit_form.description.data),
                    license=str(submit_form.license.data) or None,
                    tags_string=submit_form.tags.data,
                    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)

                messages.add_message(
                    request,
                    messages.SUCCESS,
                    _('Woohoo! Submitted!'))

                return redirect(request, "mediagoblin.user_pages.user_home",
                            user=request.user.username)


            # Handle upload limit issues
            except FileUploadLimit:
                submit_form.file.errors.append(
                    _('Sorry, the file size is too big.'))
            except UserUploadLimit:
                submit_form.file.errors.append(
                    _('Sorry, uploading this file will put you over your'
                      ' upload limit.'))
            except UserPastUploadLimit:
                messages.add_message(
                    request,
                    messages.WARNING,
                    _('Sorry, you have reached your upload limit.'))
                return redirect(request, "mediagoblin.user_pages.user_home",
                                user=request.user.username)
            except FileTypeNotSupported as e:
                submit_form.file.errors.append(e)
            except Exception as e:
                raise

    return render_to_response(
        request,
        'mediagoblin/submit/start.html',
        {'submit_form': submit_form,
         'app_config': mg_globals.app_config})