Esempio n. 1
0
def upload_page():
    user_id = current_user.id
    user = User.get_or_none(User.id == user_id)  # should never happen
    if user is None:
        return fail(404, 'User not found')
    if request.content_length > MAX_REQUEST_SIZE:
        return fail(
            413,
            f'File is too big. {MAX_REQUEST_SIZE // 1000000}MB allowed',
        )

    file: Optional[FileStorage] = request.files.get('file')
    if file is None:
        return fail(422, 'No file was given')

    try:
        matches, misses = upload.new(user, file)
    except (AlreadyExists, BadUploadFile) as e:
        logger.debug(e)
        return fail(400, str(e))

    return jsonify({
        'exercise_matches': matches,
        'exercise_misses': misses,
    })
Esempio n. 2
0
def upload_page(course_id: int):
    user_id = current_user.id
    user = User.get_or_none(User.id == user_id)  # should never happen
    if user is None:
        return fail(404, 'User not found.')
    if request.content_length > MAX_UPLOAD_SIZE:
        return fail(
            413, f'File is too big. {MAX_UPLOAD_SIZE // 1000000}MB allowed.',
        )

    file: Optional[FileStorage] = request.files.get('file')
    if file is None:
        return fail(422, 'No file was given.')

    try:
        matches, misses = upload.new(user.id, course_id, file)
    except UploadError as e:
        log.debug(e)
        return fail(400, str(e))
    except FileSizeError as e:
        log.debug(e)
        return fail(413, str(e))

    return jsonify({
        'exercise_matches': matches,
        'exercise_misses': misses,
    })