Example #1
0
def homepage():
    global username
    username = request.args.get("user")
    # accToken = jwt.decode(token, algorithms=['RS256'])
    print(username)

    resp, status = db.read(id=None, username=username)
    # if status != 200:
    #     return jsonify({"status": "get all failed", "error": resp}), status
    print(resp, status)
    file_list = []

    for r in resp:
        filename = r[9]
        filename = filename[filename.rindex("/") + 1:]
        file_list.append({
            "id": r[0],
            "username": r[1],
            "filename": filename,
            "firstname": r[2],
            "lastname": r[3],
            "uploadTime": r[4],
            "createdAt": r[5],
            "description": r[7],
            "filekey": r[9]
        })
    return render_template('./sample/home.html', file_list=file_list)
Example #2
0
def fetchone(_id):
    result = db.read("SELECT id,name FROM folder WHERE id={0}".format(_id))

    folder = None

    for r in result:
        folder=_entity_from_resultset(r)

        break

    return folder
Example #3
0
def fetchone_by_name(name):
    result = db.read(
        "SELECT id,name FROM language WHERE name=\'{0}\'".format(name))

    language = None

    for r in result:
        language = _entity_from_resultset(r)
        break

    return language
Example #4
0
def fetchone_by_name(name):
    result = db.read("SELECT id,name FROM folder WHERE name=\'{0}\'".format(name))

    folder = None

    for r in result:
        folder=_entity_from_resultset(r)

        break

    return folder
Example #5
0
def fetchone(_id):
    result = db.read("SELECT id,name FROM language WHERE id={0}".format(_id))

    language = None

    for r in result:
        language = _entity_from_resultset(r)

        break

    return language
Example #6
0
def fetchone(id):
    result = db.read("SELECT id,username,password,email FROM user WHERE id=" +
                     str(id))

    user = None

    for r in result:
        user = _entity_from_resultset(r)

        break

    return user
Example #7
0
def fetchone_by_username(username):
    result = db.read(
        "SELECT id,username,password,email FROM user WHERE username=\'{0}\'".
        format(username))

    user = None

    for r in result:
        user = _entity_from_resultset(r)

        break

    return user
Example #8
0
def fetchone(_id):
    result = db.read(
        "SELECT id,user_id,folder_id,language_1_id,language_2_id,phrase_1,phrase_2 WHERE id={0}"
        .format(_id))

    translation = None

    for r in result:
        translation = _entity_from_resultset(r)

        break

    return translation
Example #9
0
def fetchall():
    result = db.read(
        "SELECT id,user_id,folder_id,language_1_id,language_2_id,phrase_1,phrase_2"
    )

    translations = []

    for r in result:
        translation = _entity_from_resultset(r)

        translations.append(translation)

    return translations
Example #10
0
def fetchall_by_user(user_id):
    result = db.read(
        "SELECT id,user_id,folder_id,language_1_id,language_2_id,phrase_1,phrase_2 WHERE user_id={0}"
        .format(_id))

    translations = []

    for r in result:
        translation = _entity_from_resultset(r)

        translations.append(translation)

    return translations
Example #11
0
def fetchall(page=None, items=None):
    sql = "SELECT id,name FROM language"

    if (page is not None and items is not None):
        sql += " LIMIT {0},{1}".format(str((int(page) - 1) * int(items)),
                                       str(items))

    result = db.read(sql)

    languages = []

    for r in result:
        languages.append(_entity_from_resultset(r))

    return languages
Example #12
0
def fetch_one_by_user_id(userId):

    user_settings = None

    sql = "SELECT id,user_id,role_id,language_1_id,language_2_id FROM user_settings WHERE user_id={0}".format(
        userId)

    result = db.read(sql)
    print(result)
    for r in result:
        user_settings = _entity_from_resultset(r)

        break

    return user_settings
Example #13
0
def fetchone_by_username_and_password(username, password):

    sql = "SELECT id,username,password,email FROM user WHERE username=\'{0}\' and (password=\'{1}\' or password=\'{2}\')".format(
        username, password, _md5_password(password))

    result = db.read(sql)

    user = None

    for r in result:
        user = _entity_from_resultset(r)

        break

    return user
Example #14
0
def fetchall(page=None, items=None):
    sql = " SELECT id,name FROM folder "

    if (page is not None and items is not None):
        sql += " LIMIT {0},{1}".format(str((int(page)-1)*int(items)), str(items))

    result = db.read(sql)

    folders = []

    for r in result:
        folder=_entity_from_resultset(r)

        folders.append(folder)

    return folders
Example #15
0
def fetchall(page=None, items=None):
    sql = "SELECT id,username,password,email FROM user"

    if (page is not None and items is not None):
        sql += " limit {0}, {1}".format(str((int(page) - 1) * int(items)),
                                        str(items))

    result = db.read(sql)

    users = []

    for r in result:
        user = _entity_from_resultset(r)

        users.append(user)

    return users
Example #16
0
def onedata(id):

    # GET a specific data by id
    if request.method == 'GET':
        r, status = db.read(id)
        if status != 200:
            return jsonify({"status": "get failed", "error": r}), status
            # return Response({"status": "get failed", "error": err}, 500, mimetype='application/json')

        print(r)
        if len(r) > 0:
            r = r[0]
            result = {
                "id": r[0],
                "username": r[1],
                "firstname": r[2],
                "lastname": r[3],
                "uploadTime": r[4],
                "createdAt": r[5],
                "updatedAt": r[6],
                "description": r[7],
                "isDeleted": r[8]
            }

            return jsonify({"status": "get success", "data": result}), status
        else:
            return jsonify({
                "status": "not found",
                "data": f"No record found for id: {id}"
            }), 404
        # return Response({"status": "get success", "data": result}, 200, mimetype='application/json')

    # DELETE a data
    if request.method == 'DELETE':
        resp, status = db.delete(id)
        if status != 204:
            return jsonify({"status": "delete failed", "error": resp}), status
            # return Response({"status": "delete failed", "error": err}, 500, mimetype='application/json')
        if resp > 0:
            return jsonify({"status": "delete success", "data": resp}), 200
        else:
            return jsonify({
                "status": "not found",
                "data": f"No record found to delete for id: {id}"
            }), 404

        # return Response({"status": "delete success", "data": resp}, 200, mimetype='application/json')

    # UPDATE a data by id
    if request.method == 'PUT':
        if id is None:
            return jsonify({
                "status": "failed",
                "msg": "invalid id to update data"
            }), 400

        body = request.json
        if body:
            if "username" not in body:
                return jsonify({
                    "status": "failed",
                    "msg": "username required"
                }), 400
            elif "firstname" not in body:
                return jsonify({
                    "status": "failed",
                    "msg": "firstname required"
                }), 400
            elif "lastname" not in body:
                return jsonify({
                    "status": "failed",
                    "msg": "lastname required"
                }), 400
            elif "uploadTime" not in body:
                return jsonify({
                    "status": "failed",
                    "msg": "uploadTime required"
                }), 400
            elif "updatedAt" not in body:
                return jsonify({
                    "status": "failed",
                    "msg": "updatedAt required"
                }), 400
            elif "description" not in body:
                return jsonify({
                    "status": "failed",
                    "msg": "description required"
                }), 400

            # username = body['username']
            # firstname = body["firstname"]
            # lastname = body["lastname"]
            # uploadTime = body["uploadTime"]
            # description = body["description"]

            resp, status = db.update(id, body)
            if status != 201:
                return jsonify({
                    "status": "update failed",
                    "error": resp
                }), status
                # return Response({"status": "update failed", "error": err}, 500, mimetype='application/json')

            return jsonify({"status": "update success", "data": resp}), status
            # return Response({"status": "update success", "data": resp}, 200, mimetype='application/json')

        else:
            return jsonify({
                "status": "failed",
                "msg": "invalid request body"
            }), 400
Example #17
0
def data():

    # POST a data to database
    if request.method == 'POST':
        body = request.json
        print(body)
        if body:
            if "username" not in body:
                return jsonify({
                    "status": "failed",
                    "msg": "username required"
                }), 400
            elif "firstname" not in body:
                return jsonify({
                    "status": "failed",
                    "msg": "firstname required"
                }), 400
            elif "lastname" not in body:
                return jsonify({
                    "status": "failed",
                    "msg": "lastname required"
                }), 400
            elif "uploadTime" not in body:
                return jsonify({
                    "status": "failed",
                    "msg": "uploadTime required"
                }), 400
            elif "description" not in body:
                return jsonify({
                    "status": "failed",
                    "msg": "description required"
                }), 400

            # username = body['username']
            # firstname = body["firstname"]
            # lastname = body["lastname"]
            # uploadTime = body["uploadTime"]
            # description = body["description"]
            body["filekey"] = "pbox-storage/"
            resp, status = db.insert(body)
            if status != 200:
                return jsonify({
                    "status": "insert failed",
                    "error": resp
                }), status
                # return Response({"status": "insert failed", "error": err}, 500, mimetype='application/json')

            return jsonify({"status": "insert success", "data": resp}), status
            # return Response({"status": "insert success", "data": resp}, 200, mimetype='application/json')

        else:
            return Response(
                {
                    "status": "failed, ",
                    "msg": "invalid request body"
                },
                400,
                mimetype='application/json')

        # resp, err, status = db.insert(body)
        # if not err:
        #     return jsonify({"status": "post failed", "error": err}), 500
        #     # return Response({"status": "post failed", "error": resp}, 500, mimetype='application/json')

        # return jsonify({"status": "post success", "data": resp}), 200
        # return Response({"status": "post success", "data": resp}, 200, mimetype='application/json')

    # GET all data from database
    if request.method == 'GET':
        resp, status = db.read(None)
        if status != 200:
            return jsonify({"status": "get all failed", "error": resp}), status
            # return Response({"status": "get all failed", "error": err}, 404, mimetype='application/json')

        result = []

        for r in resp:
            result.append({
                "id": r[0],
                "username": r[1],
                "firstname": r[2],
                "lastname": r[3],
                "uploadTime": r[4],
                "createdAt": r[5],
                "updatedAt": r[6],
                "description": r[7],
                "isDeleted": r[8]
            })
        print(resp, result)
        return jsonify({"status": "get all success", "data": result}), status
Example #18
0
def take_action(action, msg, state, intent):
    if debug is True:
        print('action', action, state, intent)

    if state == CREATE_START:
        action['type'] = CREATE_ACTION
    elif state == CREATE_DESCRIPTION:
        action['description'] = msg
    elif state == CREATE_DETAIL:
        action['detail'] = msg

        msg_to_parse = action['description'] + '. ' + action['detail']
        action = parse_for_entities(action, msg_to_parse)
    elif state == INIT and intent == 'read_all':
        entities = parse_for_entities({}, msg, quiet=True)
        print(entities)

        deadline = None
        tasks = []

        if entities is None or 'deadline' not in entities:
            tasks = db.read_all()
        else:
            deadline = entities['deadline']
            tasks = db.read(deadline)

        if len(tasks) == 0:
            if deadline is None:
                bot_say(
                    'You have no tasks. Use <create new> to create a new task.'
                )
            else:
                bot_say(
                    f'You have no tasks due {entities["times"]}. Would you like to see your upcoming due tasks?'
                )
                return action, READ_BY_DEADLINE_ACTION
        else:
            res = prettify_tasks_summary(tasks)

            bot_say(str(res))
            action['type'] = READ_ACTION
            action['tasks'] = tasks
    elif state == READ_BY_DEADLINE_ACTION and intent == 'affirm':
        tasks = db.read_all(True)

        if (len(tasks) == 0):
            bot_say(
                'You have no tasks. Use <create new> to create a new task.')
        else:
            res = prettify_tasks_summary(tasks)

            bot_say(str(res))
            action['type'] = READ_ACTION
            action['tasks'] = tasks
    elif state == READ_BY_DEADLINE_ACTION and intent != 'affirm':
        bot_say("I will take that as a [no] 🈲️")
    elif state == INIT and intent == "read_more":
        tasks = action['tasks']
        index = int(re.search(r"[1-9][0-9]*", msg).group(0))

        if action['type'] == READ_ACTION and index <= len(tasks):
            task = tasks[index - 1]
            bot_say(prettify_task(task))
        else:
            bot_say('Invalid Index. Use [list all] to show all tasks')
    elif state == INIT and intent == 'delete':
        index = int(re.search(r"[1-9][0-9]*", msg).group(0))
        action['type'] = DELETE_ACTION
        action['index'] = index
        tasks = action['tasks']

        if index <= len(tasks):
            task = tasks[index - 1]
            bot_say(prettify_task(task))
        else:
            bot_say('Invalid Index. Use [list all] to show all tasks')
    elif state == DELETE_CONFIRM and intent == 'affirm':
        tasks = action['tasks']
        index = action['index']

        if action['type'] == DELETE_ACTION and index <= len(tasks):
            task = tasks[index - 1]
            bot_say(f'Deleting {index}: {task["description"]}')
            db.delete(task['id'])
        else:
            bot_say('Invalid Index. Use [list all] to show all tasks')
    elif state == CREATE_CONFIRM and intent == 'affirm':
        print('save', action)
        db.create(action)

        del_key_arr = [
            'description', 'detail', 'times', 'locations', 'people', 'deadline'
        ]
        for key in del_key_arr:
            if key in action:
                del action[key]

        print('db action done')
    elif state == INIT and intent == 'update':
        parsed_index = int(re.search(r"[1-9][0-9]*", msg).group(0))

        if parsed_index is None and 'index' not in action:
            bot_say("Invalid Index. Use [list all] to show all tasks")
            return

        if parsed_index is not None:
            action['index'] = parsed_index

        tasks = action['tasks']
        if tasks is None or action['index'] > len(tasks):
            bot_say("Invalid Index. Use [list all] to show all tasks")
            return
    elif state == UPDATE_DETAIL:
        bot_say(msg)
        print('before >> ', action)
        index = action['index']
        task = action['tasks'][index - 1]
        task['detail'] = '\n[update]\n'.join([task['detail'], msg])

        msg_to_parse = task['description'] + '. ' + task['detail']
        task = parse_for_entities(task, msg_to_parse)
        action['tasks'][index - 1] = task
        print('after >> ', action)
        db.update(action)

    return action, None