Пример #1
0
def upload_file():
    # check if the post request has the file part
    if 'file' not in request.files or request.files['file'] is None:
        return json_response_with_status({
            'error': 'NO_FILE_UPLOADED'
        }, 500)

    uploaded_file = request.files['file']

    if uploaded_file.filename == '':
        return json_response_with_status({
            'error': 'EMPTY_FILE_NAME'
        }, 500)

    if allowed_file(uploaded_file.filename):
        now = datetime.datetime.now()
        appended_file_name = os.path.join(str(now.year) + str(now.month), str(random.randint(100000, 999999)) + "__" + uploaded_file.filename)
        path = os.path.abspath(os.path.join(settings.FILE_UPLOAD_FOLDER, appended_file_name))

        parent_dir = os.path.dirname(path)
        if not os.path.exists(parent_dir):
            os.makedirs(parent_dir)

        uploaded_file.save(path)

        return json_response({"status": "OK", "url": appended_file_name})
    else:
        return json_response_with_status({
            'error': 'NOT_ALLOWED_EXTENSIONS'
        }, 500)
Пример #2
0
def login():
    login_info = request.get_json(True)

    user = models.User.get_by_email(login_info["email"])
    if user and not user.is_disabled and user.verify_password(
            login_info['password']):
        login_user(user,
                   remember=("remember" in login_info
                             and login_info["remember"]))
        return json_response({'status': 'OK'})
    else:
        return json_response_with_status({'error': 'LOGIN_FAILED'}, 401)
Пример #3
0
def setup():
    # local proxy needs to use != to check None
    if current_org != None:
        return json_response_with_status({'error': 'SETUP_NOT_ALLOWED'}, 403)

    setup_info = request.get_json(True)
    default_org, user = create_org(setup_info["org_name"], setup_info["name"],
                                   setup_info["email"], setup_info["password"])
    g.org = default_org
    login_user(user)

    return json_response({'status': 'OK'})
def upload_image():
    # check if the post request has the file part
    if 'file' not in request.files or request.files['file'] is None:
        return json_response_with_status({'error': 'NO_FILE_UPLOADED'}, 500)

    uploaded_file = request.files['file']

    if uploaded_file.filename == '':
        return json_response_with_status({'error': 'EMPTY_FILE_NAME'}, 500)

    if allowed_file(uploaded_file.filename):
        now = datetime.datetime.now()
        appended_file_name = os.path.join(
            str(now.year) + str(now.month),
            str(random.randint(100000, 999999)) + "__" +
            uploaded_file.filename)
        path = os.path.abspath(
            os.path.join(settings.IMAGE_UPLOAD_FOLDER, appended_file_name))

        parent_dir = os.path.dirname(path)
        print(parent_dir)
        if not os.path.exists(parent_dir):
            os.makedirs(parent_dir)

        uploaded_file.save(path)
        print(path)

        # print("1111111111")
        # with open(r'///opt/redash/image_uploads/{}'.format(appended_file_name),'rb') as f:
        #     image=f.read()
        #     resp = Response(image, mimetype="image/jpg")
        # print(image)
        # print(resp)
        # return resp

        return json_response({"status": "OK", "url": appended_file_name})
    else:
        return json_response_with_status({'error': 'NOT_ALLOWED_EXTENSIONS'},
                                         500)
Пример #5
0
def handle_unauthorized():
    return json_response_with_status({'error': 'UNAUTHORIZED'}, 401)