Ejemplo n.º 1
0
def post_entry(request):
    _log.debug('Posting entry')

    if request.method == 'OPTIONS':
        return json_response({'status': 200})

    if request.method != 'POST':
        _log.debug('Must POST against post_entry')
        raise BadRequest()

    if not check_file_field(request, 'file'):
        _log.debug('File field not found')
        raise BadRequest()

    upload_limit, max_file_size = get_upload_file_limits(request.user)

    callback_url = request.form.get('callback_url')
    if callback_url:
        callback_url = unicode(callback_url)
    try:
        entry = submit_media(mg_app=request.app,
                             user=request.user,
                             submitted_file=request.files['file'],
                             filename=request.files['file'].filename,
                             title=unicode(request.form.get('title')),
                             description=unicode(
                                 request.form.get('description')),
                             license=unicode(request.form.get('license', '')),
                             tags_string=unicode(request.form.get('tags', '')),
                             upload_limit=upload_limit,
                             max_file_size=max_file_size,
                             callback_url=callback_url)

        return json_response(get_entry_serializable(entry, request.urlgen))

    # 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.'))

    except Exception as e:
        '''
        This section is intended to catch exceptions raised in
        mediagoblin.media_types
        '''
        if isinstance(e, InvalidFileType) or \
                isinstance(e, FileTypeNotSupported):
            raise BadRequest(unicode(e))
        else:
            raise
Ejemplo n.º 2
0
def register_podcast(request):
	
	upload_limit, max_file_size = get_upload_file_limits(request.user)
	
	podcast_form = forms.podcast_create_form(
		request.form,
		license=request.user.license_preference,
		max_file_size = max_file_size,
		upload_limit = upload_limit,
		uploaded = request.user.uploaded)
	return render_to_response(request, '/podcast/makeapodcast.html', {'podcast_form':podcast_form})
Ejemplo n.º 3
0
def post_entry(request):
    _log.debug('Posting entry')

    if request.method == 'OPTIONS':
        return json_response({'status': 200})

    if request.method != 'POST':
        _log.debug('Must POST against post_entry')
        raise BadRequest()

    if not check_file_field(request, 'file'):
        _log.debug('File field not found')
        raise BadRequest()

    upload_limit, max_file_size = get_upload_file_limits(request.user)

    callback_url = request.form.get('callback_url')
    if callback_url:
        callback_url = unicode(callback_url)
    try:
        entry = submit_media(
            mg_app=request.app, user=request.user,
            submitted_file=request.files['file'],
            filename=request.files['file'].filename,
            title=unicode(request.form.get('title')),
            description=unicode(request.form.get('description')),
            license=unicode(request.form.get('license', '')),
            upload_limit=upload_limit, max_file_size=max_file_size,
            callback_url=callback_url)

        return json_response(get_entry_serializable(entry, request.urlgen))

    # 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.'))

    except Exception as e:
        '''
        This section is intended to catch exceptions raised in
        mediagoblin.media_types
        '''
        if isinstance(e, InvalidFileType) or \
                isinstance(e, FileTypeNotSupported):
            raise BadRequest(unicode(e))
        else:
            raise
Ejemplo n.º 4
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.º 5
0
def addmedia(args):
    # Run eagerly unless explicetly set not to
    if not args.celery:
        os.environ['CELERY_ALWAYS_EAGER'] = 'true'

    app = commands_util.setup_app(args)

    # get the user
    user = app.db.LocalUser.query.filter(
        LocalUser.username==args.username.lower()
    ).first()
    if user is None:
        print("Sorry, no user by username '%s'" % args.username)
        return

    # check for the file, if it exists...
    filename = os.path.split(args.filename)[-1]
    abs_filename = os.path.abspath(args.filename)
    if not os.path.exists(abs_filename):
        print("Can't find a file with filename '%s'" % args.filename)
        return

    upload_limit, max_file_size = get_upload_file_limits(user)

    def maybe_unicodeify(some_string):
        # this is kinda terrible
        if some_string is None:
            return None
        if six.PY2:
            return six.text_type(some_string, 'utf-8')
        return some_string

    try:
        submit_media(
            mg_app=app,
            user=user,
            submitted_file=open(abs_filename, 'rb'), filename=filename,
            title=maybe_unicodeify(args.title),
            description=maybe_unicodeify(args.description),
            license=maybe_unicodeify(args.license),
            tags_string=maybe_unicodeify(args.tags) or u"",
            upload_limit=upload_limit, max_file_size=max_file_size)
    except FileUploadLimit:
        print("This file is larger than the upload limits for this site.")
    except UserUploadLimit:
        print("This file will put this user past their upload limits.")
    except UserPastUploadLimit:
        print("This user is already past their upload limits.")
Ejemplo n.º 6
0
def addmedia(args):
    # Run eagerly unless explicetly set not to
    if not args.celery:
        os.environ['CELERY_ALWAYS_EAGER'] = 'true'

    app = commands_util.setup_app(args)

    # get the user
    user = app.db.LocalUser.query.filter(
        LocalUser.username == args.username.lower()).first()
    if user is None:
        print("Sorry, no user by username '%s'" % args.username)
        return

    # check for the file, if it exists...
    filename = os.path.split(args.filename)[-1]
    abs_filename = os.path.abspath(args.filename)
    if not os.path.exists(abs_filename):
        print("Can't find a file with filename '%s'" % args.filename)
        return

    upload_limit, max_file_size = get_upload_file_limits(user)

    def maybe_unicodeify(some_string):
        # this is kinda terrible
        if some_string is None:
            return None
        if six.PY2:
            return six.text_type(some_string, 'utf-8')
        return some_string

    try:
        submit_media(mg_app=app,
                     user=user,
                     submitted_file=open(abs_filename, 'rb'),
                     filename=filename,
                     title=maybe_unicodeify(args.title),
                     description=maybe_unicodeify(args.description),
                     license=maybe_unicodeify(args.license),
                     tags_string=maybe_unicodeify(args.tags) or u"",
                     upload_limit=upload_limit,
                     max_file_size=max_file_size)
    except FileUploadLimit:
        print("This file is larger than the upload limits for this site.")
    except UserUploadLimit:
        print("This file will put this user past their upload limits.")
    except UserPastUploadLimit:
        print("This user is already past their upload limits.")
Ejemplo n.º 7
0
def upload_handler(request):
	if request.GET.get('url') and request.GET.get('title') and request.user:
		upload_limit, max_file_size = get_upload_file_limits(request.user)
		try:
			f = urlopen(request.GET.get('url'))
			fname = request.GET.get('url')[request.GET.get('url').rfind('/')+1:]
			tmpfile = tempfile.NamedTemporaryFile()
			tmpfile.write(f.read)
			tmpfile.flush()
			local_file = open(tmpfile.name, "r")
			try:
				entry = submit_media(
					mg_app = request.app, user=request.user,
					submitted_file=local_file, filename=fname,
					title=request.GET.get('title'))
				entryinfo = get_entry_serializable(entry, request.urlgen)
				os.unlink(f.name)
				return json_response({'status':200, 'permalink':entryinfo['permalink']})
			except FileUploadLimit:
				return json_reponse({'status':400, 'error':'Past File size Upload Limit'})
			except UserUploadLimit:
				return json_response({'status':400, 'error':'Past User upload limit'})
			except UserPastUploadLimit:
				return json_response({'status':400, 'error':'Past upload limit'})
			except Exception as e:
				return json_response({'status':400, 'error':'Unspecified error'})

		except HTTPError as e:
			print("HTTP Error:", e.code, url)
			return json_response({'status':400, 'error':'unspecifice httperror'})
		except URLError as e:
			print("URL Error:", e.reason, url)
			return json_response({'status':400, 'error':'unspecified url error'})
	else:
		if not request.GET.get('url'):
			return json_response({'status':400, 'error':'No URL specified [GET, url]'})
		elif not request.GET.get('title'):
			return json_response({'status':400, 'error':'No title specified [GET, title]'})
		elif not request.user:
			return json_response({'status':401, 'error':'No user found'});
		else:
			return json_reponse({'status':400, 'error':'Unknown Error Occured'})
Ejemplo n.º 8
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)

    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:
                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)

                add_message(request, 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.º 9
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.º 10
0
def batchaddmedia(args):
    # Run eagerly unless explicetly set not to
    if not args.celery:
        os.environ['CELERY_ALWAYS_EAGER'] = 'true'

    app = commands_util.setup_app(args)

    files_uploaded, files_attempted = 0, 0

    # get the user
    user = app.db.User.query.filter_by(username=args.username.lower()).first()
    if user is None:
        print _(u"Sorry, no user by username '{username}' exists".format(
                    username=args.username))
        return

    upload_limit, max_file_size = get_upload_file_limits(user)
    temp_files = []

    if os.path.isfile(args.metadata_path):
        metadata_path = args.metadata_path

    else:
        error = _(u'File at {path} not found, use -h flag for help'.format(
                    path=args.metadata_path))
        print error
        return

    abs_metadata_filename = os.path.abspath(metadata_path)
    abs_metadata_dir = os.path.dirname(abs_metadata_filename)
    upload_limit, max_file_size = get_upload_file_limits(user)

    def maybe_unicodeify(some_string):
        # this is kinda terrible
        if some_string is None:
            return None
        else:
            return unicode(some_string)

    with codecs.open(
            abs_metadata_filename, 'r', encoding='utf-8') as all_metadata:
        contents = all_metadata.read()
        media_metadata = parse_csv_file(contents)

    for media_id, file_metadata in media_metadata.iteritems():
        files_attempted += 1
        # In case the metadata was not uploaded initialize an empty dictionary.
        json_ld_metadata = compact_and_validate({})

        # Get all metadata entries starting with 'media' as variables and then
        # delete them because those are for internal use only.
        original_location = file_metadata['location']

        ### Pull the important media information for mediagoblin from the
        ### metadata, if it is provided.
        title = file_metadata.get('title') or file_metadata.get('dc:title')
        description = (file_metadata.get('description') or
            file_metadata.get('dc:description'))

        license = file_metadata.get('license')
        try:
            json_ld_metadata = compact_and_validate(file_metadata)
        except ValidationError, exc:
            error = _(u"""Error with media '{media_id}' value '{error_path}': {error_msg}
Metadata was not uploaded.""".format(
                media_id=media_id,
                error_path=exc.path[0],
                error_msg=exc.message))
            print error
            continue

        url = urlparse(original_location)
        filename = url.path.split()[-1]

        if url.scheme == 'http':
            res = requests.get(url.geturl(), stream=True)
            media_file = res.raw

        elif url.scheme == '':
            path = url.path
            if os.path.isabs(path):
                file_abs_path = os.path.abspath(path)
            else:
                file_path = os.path.join(abs_metadata_dir, path)
                file_abs_path = os.path.abspath(file_path)
            try:
                media_file = file(file_abs_path, 'r')
            except IOError:
                print _(u"""\
FAIL: Local file {filename} could not be accessed.
{filename} will not be uploaded.""".format(filename=filename))
                continue
        try:
            submit_media(
                mg_app=app,
                user=user,
                submitted_file=media_file,
                filename=filename,
                title=maybe_unicodeify(title),
                description=maybe_unicodeify(description),
                license=maybe_unicodeify(license),
                metadata=json_ld_metadata,
                tags_string=u"",
                upload_limit=upload_limit, max_file_size=max_file_size)
            print _(u"""Successfully submitted {filename}!
Be sure to look at the Media Processing Panel on your website to be sure it
uploaded successfully.""".format(filename=filename))
            files_uploaded += 1
        except FileUploadLimit:
            print _(
u"FAIL: This file is larger than the upload limits for this site.")
        except UserUploadLimit:
            print _(
"FAIL: This file will put this user past their upload limits.")
        except UserPastUploadLimit:
            print _("FAIL: This user is already past their upload limits.")
Ejemplo n.º 11
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)

    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:
                submit_media(mg_app=request.app,
                             user=request.user,
                             submitted_file=request.files['file'],
                             filename=request.files['file'].filename,
                             title=unicode(submit_form.title.data),
                             description=unicode(submit_form.description.data),
                             license=unicode(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)

                add_message(request, 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 Exception as e:
                '''
                This section is intended to catch exceptions raised in
                mediagoblin.media_types
                '''
                if isinstance(e, InvalidFileType) or \
                        isinstance(e, FileTypeNotSupported):
                    submit_form.file.errors.append(e)
                else:
                    raise

    return render_to_response(request, 'mediagoblin/submit/start.html', {
        'submit_form': submit_form,
        'app_config': mg_globals.app_config
    })
Ejemplo n.º 12
0
def upload_handler(request):
    if request.GET.get('url') and request.GET.get('title') and request.user:
        upload_limit, max_file_size = get_upload_file_limits(request.user)
        try:
            f = urlopen(request.GET.get('url'))
            fname = request.GET.get('url')[request.GET.get('url').rfind('/') +
                                           1:]
            tmpfile = tempfile.NamedTemporaryFile()
            tmpfile.write(f.read)
            tmpfile.flush()
            local_file = open(tmpfile.name, "r")
            try:
                entry = submit_media(mg_app=request.app,
                                     user=request.user,
                                     submitted_file=local_file,
                                     filename=fname,
                                     title=request.GET.get('title'))
                entryinfo = get_entry_serializable(entry, request.urlgen)
                os.unlink(f.name)
                return json_response({
                    'status': 200,
                    'permalink': entryinfo['permalink']
                })
            except FileUploadLimit:
                return json_reponse({
                    'status': 400,
                    'error': 'Past File size Upload Limit'
                })
            except UserUploadLimit:
                return json_response({
                    'status': 400,
                    'error': 'Past User upload limit'
                })
            except UserPastUploadLimit:
                return json_response({
                    'status': 400,
                    'error': 'Past upload limit'
                })
            except Exception as e:
                return json_response({
                    'status': 400,
                    'error': 'Unspecified error'
                })

        except HTTPError as e:
            print("HTTP Error:", e.code, url)
            return json_response({
                'status': 400,
                'error': 'unspecifice httperror'
            })
        except URLError as e:
            print("URL Error:", e.reason, url)
            return json_response({
                'status': 400,
                'error': 'unspecified url error'
            })
    else:
        if not request.GET.get('url'):
            return json_response({
                'status': 400,
                'error': 'No URL specified [GET, url]'
            })
        elif not request.GET.get('title'):
            return json_response({
                'status': 400,
                'error': 'No title specified [GET, title]'
            })
        elif not request.user:
            return json_response({
                'status': 401,
                'error': 'No user found'
            })
        else:
            return json_reponse({
                'status': 400,
                'error': 'Unknown Error Occured'
            })
Ejemplo n.º 13
0
def batchaddmedia(args):
    # Run eagerly unless explicetly set not to
    if not args.celery:
        os.environ['CELERY_ALWAYS_EAGER'] = 'true'

    app = commands_util.setup_app(args)

    files_uploaded, files_attempted = 0, 0

    # get the user
    user = app.db.User.query.filter_by(username=args.username.lower()).first()
    if user is None:
        print _(u"Sorry, no user by username '{username}' exists".format(
            username=args.username))
        return

    upload_limit, max_file_size = get_upload_file_limits(user)
    temp_files = []

    if os.path.isfile(args.metadata_path):
        metadata_path = args.metadata_path

    else:
        error = _(u'File at {path} not found, use -h flag for help'.format(
            path=args.metadata_path))
        print error
        return

    abs_metadata_filename = os.path.abspath(metadata_path)
    abs_metadata_dir = os.path.dirname(abs_metadata_filename)
    upload_limit, max_file_size = get_upload_file_limits(user)

    def maybe_unicodeify(some_string):
        # this is kinda terrible
        if some_string is None:
            return None
        else:
            return unicode(some_string)

    with codecs.open(abs_metadata_filename, 'r',
                     encoding='utf-8') as all_metadata:
        contents = all_metadata.read()
        media_metadata = parse_csv_file(contents)

    for media_id, file_metadata in media_metadata.iteritems():
        files_attempted += 1
        # In case the metadata was not uploaded initialize an empty dictionary.
        json_ld_metadata = compact_and_validate({})

        # Get all metadata entries starting with 'media' as variables and then
        # delete them because those are for internal use only.
        original_location = file_metadata['location']

        ### Pull the important media information for mediagoblin from the
        ### metadata, if it is provided.
        title = file_metadata.get('title') or file_metadata.get('dc:title')
        description = (file_metadata.get('description')
                       or file_metadata.get('dc:description'))

        license = file_metadata.get('license')
        try:
            json_ld_metadata = compact_and_validate(file_metadata)
        except ValidationError, exc:
            error = _(
                u"""Error with media '{media_id}' value '{error_path}': {error_msg}
Metadata was not uploaded.""".format(media_id=media_id,
                                     error_path=exc.path[0],
                                     error_msg=exc.message))
            print error
            continue

        url = urlparse(original_location)
        filename = url.path.split()[-1]

        if url.scheme == 'http':
            res = requests.get(url.geturl(), stream=True)
            media_file = res.raw

        elif url.scheme == '':
            path = url.path
            if os.path.isabs(path):
                file_abs_path = os.path.abspath(path)
            else:
                file_path = os.path.join(abs_metadata_dir, path)
                file_abs_path = os.path.abspath(file_path)
            try:
                media_file = file(file_abs_path, 'r')
            except IOError:
                print _(u"""\
FAIL: Local file {filename} could not be accessed.
{filename} will not be uploaded.""".format(filename=filename))
                continue
        try:
            submit_media(mg_app=app,
                         user=user,
                         submitted_file=media_file,
                         filename=filename,
                         title=maybe_unicodeify(title),
                         description=maybe_unicodeify(description),
                         license=maybe_unicodeify(license),
                         metadata=json_ld_metadata,
                         tags_string=u"",
                         upload_limit=upload_limit,
                         max_file_size=max_file_size)
            print _(u"""Successfully submitted {filename}!
Be sure to look at the Media Processing Panel on your website to be sure it
uploaded successfully.""".format(filename=filename))
            files_uploaded += 1
        except FileUploadLimit:
            print _(
                u"FAIL: This file is larger than the upload limits for this site."
            )
        except UserUploadLimit:
            print _(
                "FAIL: This file will put this user past their upload limits.")
        except UserPastUploadLimit:
            print _("FAIL: This user is already past their upload limits.")
Ejemplo n.º 14
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})