Ejemplo n.º 1
0
def change_chard_status():
    card_id = request.form["cardId"]
    new_status = request.form["status"]
    database_handler("UPDATE cards\
                    SET status = '{0}'\
                    WHERE id={1};".format(new_status, card_id), "write")
    return "ok"
Ejemplo n.º 2
0
def add_new_card():
    card_title = request.form['title']
    card_status = "new"
    card_board_id = request.form['board_id']
    database_handler("INSERT INTO cards (title, status, boards_id) VALUES \
                                        ('{0}', '{1}', {2});".format(card_title, card_status, card_board_id), 'write')
    card_id = database_handler("SELECT id FROM cards ORDER BY id DESC LIMIT 1;")
    return jsonify(data=card_id)
Ejemplo n.º 3
0
def change_board_title():
    new_board_title = request.form['title']
    actual_board_id = request.form['boardId']
    database_handler("UPDATE boards\
                    SET title = '{0}'\
                    WHERE id={1};".format(new_board_title, actual_board_id), "write")

    return "ok"
Ejemplo n.º 4
0
 def __connect_db(self):
     """creates instace of db class"""
     self.__db = database_handler(database_path=self.__database_path,
                                  check_connection=True,
                                  create_table=True)
     self.__TEST_db = database_handler(
         database_path=self.__TEST_database_path,
         check_connection=True,
         create_table=True)
Ejemplo n.º 5
0
def get_boards():
    if 'userid' in session:
        board_data = database_handler("SELECT * FROM boards\
                                        WHERE user_id={0};".format(session['userid']))
        list_board_data = [list(element) for element in board_data]
        for element in list_board_data:
            x = element[0]
            card_data = database_handler("SELECT * FROM cards WHERE boards_id = {0};".format(x))
            element.append(card_data)
        return jsonify(data=list_board_data)
    else:
        return ""
    def test_make_prediction_on_thread(self):

        # create predictor
        p = predictor(self.predictor_cfg)

        # connect to db
        db = database_handler(self.db_path)

        # get predictor info
        model_info, model_id = p.get_model_info()
        self.assertEqual(model_id, self.model_id)
        self.assertEqual(model_info["predictor_backend"],
                         self.predictor_backend)

        # create prediction
        db.create_prediction(self.prediction_id, model_info, model_id)

        # start prediction on thread
        pred_thread = prediction_thread(self.db_path,
                                        self.image_path,
                                        self.prediction_id,
                                        predictor=p)
        pred_thread.start()

        # wait for prediction
        time.sleep(self.result_wait_time)

        # get prediction result
        pred_json = db.get_prediction_json(self.prediction_id)
        self.assertEqual(pred_json["prediction_status"], 200)
        self.assertNotEqual(pred_json["predictions"], "")
Ejemplo n.º 7
0
 def __init__(self, database_path, temp_image_path, prediction_id, predictor):
     super().__init__()
     self.db = database_handler(database_path)
     self.logger = logger_creator().prediction_thread_logger()
     self.predictor = predictor
     self.prediction_id = prediction_id
     self.temp_image_path = temp_image_path
Ejemplo n.º 8
0
def store_vote():
    planet_id = None
    username = None
    if request.method == "POST":
        planet_id = request.json['planetId']
        username = request.json['userName']
        get_user_id = database_handler("SELECT id FROM user_table\
                                            WHERE username='******';".format(
            username))
        user_id_flatten = get_user_id[0][0]
        date_now = str(datetime.now())[:19]
        write_to_database = database_handler(
            "INSERT INTO planet_votes_table (planet_id, user_id, submission_time)\
                            VALUES ({0}, {1}, '{2}');".format(
                planet_id, user_id_flatten, date_now), "write")
        if write_to_database == "error":
            return '<a href="/"><button href= class="btn btn-default">Back</button></a><p>An error occurred :(<p>'
    return redirect(url_for('index'))
Ejemplo n.º 9
0
def registration():
    username = request.form['username']
    password = request.form['password']
    hashed_password = generate_password_hash(password, "pbkdf2:sha224", 1)
    print("registration in work")
    write_to_database = database_handler("INSERT INTO users (username, password)\
                    VALUES ($${0}$$, $${1}$$);".format(username, hashed_password), "write")

    if write_to_database == "error":
        flash("Username already exists!")

    return redirect(url_for('index'))
Ejemplo n.º 10
0
def registration():
    username = request.form['username']
    password = request.form['password']
    hashed_password = generate_password_hash(password, "pbkdf2:sha224", 1)

    write_to_database = database_handler(
        "INSERT INTO user_table (username, password)\
                    VALUES ($${0}$$, $${1}$$);".format(username,
                                                       hashed_password),
        "write")

    if write_to_database == "error":
        return '<a href="/"><button href= class="btn btn-default">Back</button></a><p>Username already exists or too long<p>'

    return redirect(url_for('index'))
Ejemplo n.º 11
0
def report():
    votes_by_planet_ids = database_handler(
        "SELECT planet_id, COUNT(planet_id) from planet_votes_table\
                                            GROUP BY planet_id;")

    planet_with_votes = []
    for elements in votes_by_planet_ids:
        actual_planet = []
        response = requests.get("http://swapi.co/api/planets/" +
                                str(elements[0])).json()
        planet_name = response['name']
        actual_planet.append(planet_name)
        actual_planet.append(elements[1])
        planet_with_votes.append(actual_planet)
    votes_by_planet_ids_json = json.dumps(planet_with_votes)
    return Response(votes_by_planet_ids_json)
Ejemplo n.º 12
0
    def test_delete_prediction(self):
        db = database_handler(self.db_path)
        self.assertIsNotNone(db)
        db.create_table_if_not_exists()

        status = db.is_prediction_exists(self.prediction_id)
        self.assertFalse(status)

        db.create_prediction(self.prediction_id, self.model_info,
                             self.model_id)

        status = db.is_prediction_exists(self.prediction_id)
        self.assertTrue(status)

        db.delete_prediction(self.prediction_id)

        status = db.is_prediction_exists(self.prediction_id)
        self.assertFalse(status)
Ejemplo n.º 13
0
def login():
    username = request.form['username']
    password = request.form['password']
    user_db_data = database_handler("SELECT username, password, id FROM users\
                        WHERE username='******';".format(username))
    try:
        username_from_db = user_db_data[0][0]
    except IndexError:
        flash("User name does not exist!")
    else:
        user_pw_from_db = user_db_data[0][1]
        remove_white_spaces_from_pw = user_pw_from_db.strip()
        password_check = check_password_hash(remove_white_spaces_from_pw, password)
        if password_check:
            session['username'] = username_from_db
            session['userid'] = user_db_data[0][2]
        else:
            flash('Invalid Password')
    return redirect(url_for('index'))
Ejemplo n.º 14
0
def login():
    username = request.form['username']
    password = request.form['password']
    user_db_data = database_handler("SELECT username, password FROM user_table\
                        WHERE username='******';".format(username))
    try:
        username_from_db = user_db_data[0][0]
    except IndexError:
        return '<a href="/"><button href= class="btn btn-default">Back</button></a><p>Username not found<p>'
    else:
        user_pw_from_db = user_db_data[0][1]
        remove_white_spaces_from_pw = user_pw_from_db.strip()
        password_check = check_password_hash(remove_white_spaces_from_pw,
                                             password)
        if password_check:
            session['username'] = username_from_db
        else:
            return '<a href="/"><button href= class="btn btn-default">Back</button></a><p>Invalid password<p>'

    return redirect(url_for('index'))
Ejemplo n.º 15
0
    def test_successful_prediction(self):
        db = database_handler(self.db_path)
        self.assertIsNotNone(db)
        db.create_table_if_not_exists()

        status = db.is_prediction_exists(self.prediction_id)
        self.assertFalse(status)

        db.create_prediction(self.prediction_id, self.model_info,
                             self.model_id)

        status = db.is_prediction_exists(self.prediction_id)
        self.assertTrue(status)

        db.update_successful_prediction(self.prediction_id, self.prediction,
                                        self.image_path, self.prediction_time)

        pred_json = db.get_prediction_json(self.prediction_id)
        self.assertEqual(pred_json["prediction_status"], 200)
        self.assertNotEqual(pred_json["predictions"], "")
        self.assertEqual(pred_json["prediction_time"], 0.555)
Ejemplo n.º 16
0
def create_app(deep_predictor_cfg_path):
    # create logger
    logger = logger_creator().flask_logger()
    logger.info("creating flask app with cfg file on path:{0}".format(deep_predictor_cfg_path))

    # set config options
    cfg = file_folder_operations.read_json_file(deep_predictor_cfg_path)

    # flask options
    secret_key = cfg["deep_predictor_options"]["flask_options"]["secret_key"]
    max_content_length = eval(cfg["deep_predictor_options"]["flask_options"]["max_content_length"])
    debug = cfg["deep_predictor_options"]["flask_options"]["debug"]


    # ---------- production options ----------

    # recaptcha_options 
    recaptcha_sitekey = cfg["deep_predictor_options"]["production"]["recaptcha_options"]["recaptcha_sitekey"]
    recaptcha_secret_key = cfg["deep_predictor_options"]["production"]["recaptcha_options"]["recaptcha_secret_key"]

    # upload options
    supported_extensions = cfg["deep_predictor_options"]["production"]["upload_options"]["supported_extensions"]

    # api options
    api_key = cfg["deep_predictor_options"]["production"]["api_options"]["api_key"]
    prediction_id_length = cfg["deep_predictor_options"]["production"]["api_options"]["prediction_id_length"]
    default_api_response = cfg["deep_predictor_options"]["production"]["api_options"]["default_api_response"]
    get_prediction_endpoint = cfg["deep_predictor_options"]["production"]["api_options"]["get_prediction_endpoint"]
    get_predictors_endpoint = cfg["deep_predictor_options"]["production"]["api_options"]["get_predictors_endpoint"]

    # path options
    temp_save_path = cfg["deep_predictor_options"]["production"]["path_options"]["temp_save_path"]
    database_path = cfg["deep_predictor_options"]["production"]["path_options"]["database_path"]

    # prediction options
    default_predictor_name = cfg["deep_predictor_options"]["production"]["prediction_options"]["default_predictor_name"]

    # create instace of db class
    db = database_handler(database_path = database_path, check_connection = True, create_table = True)

    # create predictors
    predictors = {}
    for predictor in cfg["deep_predictor_options"]["production"]["prediction_options"]["predictors"]:
        predictors.update({
            predictor : deep_predictor(cfg["deep_predictor_options"]["production"]["prediction_options"]["predictors"][predictor])
        })
    # --------------------------------------------------

    # ---------- test options ----------

    # upload options
    TEST_supported_extensions = cfg["deep_predictor_options"]["test"]["upload_options"]["supported_extensions"]

    # api options
    TEST_key = cfg["deep_predictor_options"]["test"]["api_options"]["api_key"]
    TEST_prediction_id_length = cfg["deep_predictor_options"]["test"]["api_options"]["prediction_id_length"]
    TEST_default_api_response = cfg["deep_predictor_options"]["test"]["api_options"]["default_api_response"]
    TEST_get_prediction_endpoint = cfg["deep_predictor_options"]["test"]["api_options"]["get_prediction_endpoint"]
    TEST_get_predictors_endpoint = cfg["deep_predictor_options"]["test"]["api_options"]["get_predictors_endpoint"]

    # path options
    TEST_temp_save_path = cfg["deep_predictor_options"]["test"]["path_options"]["temp_save_path"]
    TEST_database_path = cfg["deep_predictor_options"]["test"]["path_options"]["database_path"]

    # create instace of db class
    TEST_db = database_handler(database_path = TEST_database_path, check_connection = True, create_table = True)
    
    # create predictors
    TEST_predictors = {}
    for predictor in cfg["deep_predictor_options"]["test"]["prediction_options"]["predictors"]:
        TEST_predictors.update({
            predictor : deep_predictor(cfg["deep_predictor_options"]["test"]["prediction_options"]["predictors"][predictor])
        })
    # --------------------------------------------------


    # flask app
    app = Flask(__name__)
    app.config["DEBUG"] = debug
    app.config['MAX_CONTENT_LENGTH'] = max_content_length
    app.secret_key = secret_key


    # ---------- regular routes ----------
    @app.route('/', methods=['GET'])
    def home():
        logger.debug("function: {0} method: {1}".format("home", request.method))
        return render_template("index.html"), 200

    @app.route('/result', methods=['GET'])
    def result():
        logger.debug("function: {0} method: {1}".format("result", request.method))
        return redirect(url_for("home")), 308

    @app.route('/upload', methods=['POST', 'GET'])
    def upload():
        logger.debug("function: {0} method: {1}".format("upload", request.method))
        if(request.method == 'POST'):
 
            # ---------- check key errors from form ----------
            try:
                uploaded_file = request.files['image']
                filename = secure_filename(uploaded_file.filename)
                model_name = request.form['model_name']
                prediction_id = request.form['prediction_id']
            except KeyError:
                logger.warning("KeyError", exc_info=True)
                abort(400, description="Unknown key received from form")

            recaptcha_response = request.form.get('g-recaptcha-response')
            form_api_key = request.form.get('api_key')
            # --------------------------------------------------

            # ---------- check recaptcha or api_key ----------
            if(form_api_key):
                # check api key
                if(api_key != form_api_key):
                    logger.warning("api key is incorrect")
                    abort(400, description="Api key is incorrect")
            elif(recaptcha_response):
                # check recaptcha
                if(not validate_recaptcha(recaptcha_response, recaptcha_secret_key)):
                    logger.warning("recaptcha is not verified")
                    return render_template("upload.html", models=predictors.keys(), recaptcha_sitekey=recaptcha_sitekey), 400
            else:
                logger.warning("recaptcha or api key is not provided")
                abort(400, description="Recaptcha or api key is not provided")
            # --------------------------------------------------

            # ---------- check form elements ----------
            if(model_name == ""):
                model_name = default_predictor_name

            if(filename == "" or prediction_id == ""):
                logger.warning("required form elements are empty")
                return render_template("upload.html", models=predictors.keys(), recaptcha_sitekey=recaptcha_sitekey), 400

            # prediction_id length
            if(len(prediction_id) != prediction_id_length):
                logger.warning("prediction_id length is wrong")
                abort(400, description="Something went wrong please try again")

            # model name
            if(model_name not in predictors):
                logger.warning("model name is not exists")
                abort(400, description="This predictor is not exists")

            # image extension
            if(not image_operations.validate_extension(supported_extensions, filename, is_case_sensitive=False)):
                logger.warning("image extension is not supported")
                abort(400, description="Your image extension is not supported")
            # --------------------------------------------------


            # check for prediction_id collision
            if(db.is_prediction_exists(prediction_id)):
                logger.warning("prediction id exists")
                abort(500, description="Something went wrong please try again")

            # generate unique id
            # unique_full_filename = file_folder_operations.create_unique_file_name(os.path.join(temp_save_path, filename))
            unique_filename = str(uuid.uuid4())
            _, file_extension = os.path.splitext(filename)
            unique_full_filename = os.path.join(temp_save_path, unique_filename + file_extension)
            uploaded_file.save(unique_full_filename)


            # check image resizability
            status, unique_full_filename = image_operations.validate_image(unique_full_filename, try_to_convert = True, delete_unresizable = True)
            if(not status):
                logger.warning("image is not supported")
                abort(400, description="Your image is not supported")

            # prepare preediction
            model_info, model_id = predictors[model_name].get_model_info()
            db.create_prediction(prediction_id, model_info, model_id)

            # start prediction on thread
            pred_thread = prediction_thread(
                database_path, 
                unique_full_filename, 
                prediction_id, 
                predictor=predictors[model_name]
                )

            pred_thread.start()

            return render_template("result.html", prediction_id=prediction_id), 201
        else:
            return render_template("upload.html", models=predictors.keys(), recaptcha_sitekey=recaptcha_sitekey), 200

    @app.route('/api', methods=['GET'])
    def api():
        logger.debug("function: {0} method: {1}".format("api", request.method))

        # get args
        arguments = request.args
        prediction_id = arguments.get(get_prediction_endpoint)
        test_prediction_id = arguments.get(TEST_get_prediction_endpoint)

        if(prediction_id):
            prediction = db.get_prediction_json(prediction_id)
            return prediction, 200

        elif(get_predictors_endpoint in arguments):
            return {get_predictors_endpoint : list(predictors.keys())}, 200

        else:
            logger.warning("prediction_id is not provided")
            return default_api_response, 400
    # --------------------------------------------------


    # ---------- error routes ---------- 
    @app.route('/', defaults={'path': ''})
    @app.route('/<path:path>')
    def catch_all(path):
        # all to 404
        # e.code
        # e.name
        abort(404)

    @app.errorhandler(404)   
    @app.errorhandler(400)
    @app.errorhandler(413)
    @app.errorhandler(500)
    def generic_error(e):
        logger.debug("function: {0} method: {1}".format("error", request.method))

        if(e.code == 404):
            return render_template('error.html', description="The page you are looking for is not found", code=e.code), e.code
        elif(e.code == 413):
            return render_template('error.html', description="Your image is too large", code=e.code), e.code
        else:
            return render_template('error.html', description=e.description, code=e.code), e.code

    # --------------------------------------------------


    # ---------- test routes ----------
    @app.route('/test-upload', methods=['POST', 'GET'])
    def test_upload():
        if(request.method == 'POST'):
            logger.debug("function: {0} method: {1}".format("test_upload", request.method))

            # ---------- check key errors from form ----------
            try:
                uploaded_file = request.files['image']
                filename = secure_filename(uploaded_file.filename)
                model_name = request.form['model_name']
                prediction_id = request.form['prediction_id']
                form_api_key = request.form['api_key']
            except KeyError:
                logger.warning("KeyError", exc_info=True)
                abort(400, description="Unknown key received from form")
            # --------------------------------------------------

            # ---------- check test api key ----------
            if(TEST_key != form_api_key):
                logger.warning("test api key is incorrect")
                abort(400, description="test api key is incorrect")
            # --------------------------------------------------

            # ---------- check form elements ----------
            if(filename == "" or prediction_id == "" or model_name == ""):
                logger.warning("required form elements are empty")
                abort(400, description="Required form elements are empty")

            # prediction_id length
            if(len(prediction_id) != TEST_prediction_id_length):
                logger.warning("prediction_id length is wrong")
                abort(400, description="Something went wrong please try again")

            # model name
            if(model_name not in TEST_predictors):
                logger.warning("model name is not exists")
                abort(400, description="This predictor is not exists")

            # image extension
            if(not image_operations.validate_extension(TEST_supported_extensions, filename, is_case_sensitive=False)):
                logger.warning("image extension is not supported")
                abort(400, description="Your image extension is not supported")
            # --------------------------------------------------

            # check for prediction_id collision
            if(TEST_db.is_prediction_exists(prediction_id)):
                logger.warning("prediction id exists")
                abort(500, description="Something went wrong please try again")

            # generate unique id
            # unique_full_filename = file_folder_operations.create_unique_file_name(os.path.join(temp_save_path, filename))
            unique_filename = str(uuid.uuid4())
            _, file_extension = os.path.splitext(filename)
            unique_full_filename = os.path.join(TEST_temp_save_path, unique_filename + file_extension)
            uploaded_file.save(unique_full_filename)

            # check image resizability
            status, unique_full_filename = image_operations.validate_image(unique_full_filename, try_to_convert = True, delete_unresizable = True)
            if(not status):
                logger.warning("image is not supported")
                abort(400, description="Your image is not supported")

            # prepare preediction
            model_info, model_id = TEST_predictors[model_name].get_model_info()
            TEST_db.create_prediction(prediction_id, model_info, model_id)

            # start prediction on thread
            pred_thread = prediction_thread(
                TEST_database_path, 
                unique_full_filename, 
                prediction_id, 
                predictor=TEST_predictors[model_name]
                )
            
            pred_thread.start()

            return "success", 201
        else:
            return redirect(url_for("home")), 308

    @app.route('/test-api', methods=['GET'])
    def test_api():
        logger.debug("function: {0} method: {1}".format("test_api", request.method))

        # get args
        arguments = request.args
        prediction_id = arguments.get(TEST_get_prediction_endpoint)

        if(prediction_id):
            prediction = TEST_db.get_prediction_json(prediction_id)
            return prediction, 200

        elif(TEST_get_predictors_endpoint in arguments):
            return {TEST_get_predictors_endpoint : list(TEST_predictors.keys())}, 200

        else:
            logger.warning("prediction_id is not provided")
            return TEST_default_api_response, 400
    # --------------------------------------------------


    @app.route('/admin', methods=['GET'])
    def roll():
        return redirect("https://www.youtube.com/watch?v=dQw4w9WgXcQ&ab_channel=RickAstleyVEVO")


    # return flask app for serving
    return app
Ejemplo n.º 17
0
def new_board():
    new_board_name = request.form["title"]
    database_handler("INSERT INTO boards (title, user_id) VALUES ('{0}', {1});".format(new_board_name, session['userid']), "write")
    return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}