Beispiel #1
0
def db_delete_favorite():
    if (request.data == b''):
        return Response(status=400)
    dataDict = json.loads(request.data)
    payload = is_authorized()
    if (payload != None):
        dictionary = find_user_by_email(payload)
        if (dictionary['status'] == 200):
            user_id = dictionary['id']
            book_id = dataDict['bookId']
            conn = get_database_connection()
            print("Encoding for this connection is", conn.encoding)
            curs = conn.cursor()
            response = Response("true", status=200)
            try:
                curs.execute(
                    "delete from favorites where book_id={book_id} and user_id={user_id} returning *"
                    .format(book_id=book_id, user_id=user_id))
                for row in curs.fetchall():
                    print('delete: ', row)
            except psycopg2.Error as e:
                print('delete error', e)
                response = Response("false", status=200)
                pass

            curs.close()
            conn.commit()
            conn.close()
            return response
        else:
            return build_response(dictionary, None)
    else:
        return Response(status=401)
Beispiel #2
0
def check_token():
    token = request.cookies.get('token')
    if (token != None):
        payload = decode_auth_token(token)
        dictionary = find_user_by_email(payload)
        if (dictionary['status'] == 200):
            return Response("true", status=200, mimetype='application/json')
    return Response("false", status=200, mimetype='application/json')
Beispiel #3
0
def db_add_favorite():
    if (request.data == b''):
        return Response(status=400)
    dataDict = json.loads(request.data)
    payload = is_authorized()
    if (payload != None):
        dictionary = find_user_by_email(payload)
        if (dictionary['status'] == 200):
            user_id = dictionary['id']
            book_id = dataDict['bookId']
            print('user_id: ', user_id)
            print('book_id: ', book_id)
            conn = get_database_connection()
            print("Encoding for this connection is", conn.encoding)
            curs = conn.cursor()
            response = Response("true", status=200)
            try:
                sqlString = "insert into favorites (book_id, user_id) values({book_id}, {user_id}) returning *".format(
                    book_id=book_id, user_id=user_id)
                print('sqlString', sqlString)
                curs.execute(sqlString)
                if (curs.rowcount > 0):
                    for row in curs.fetchall():
                        print('insert: ', row)
                        dictionary = {}
                        dictionary['status'] = 200
                        dictionary['id'] = row[0]
                        dictionary['bookId'] = row[1]
                        dictionary['userId'] = row[2]
                    print('dictionary: ', dictionary)
                    response = build_response(dictionary, None)
                else:
                    print('error - unable to insert')
                    response = Response("false", status=200)
            except psycopg2.Error as e:
                print('error', e)
                response = Response("false", status=200)
                pass

            curs.close()
            conn.commit()
            conn.close()
            return response
        else:
            return build_response(dictionary, None)
    else:
        return Response(status=401)
Beispiel #4
0
def db_check_favorite():
    payload = is_authorized()
    if (payload != None):
        dictionary = find_user_by_email(payload)
        if (dictionary['status'] == 200):
            user_id = dictionary['id']
            book_id = request.args.get('bookId')
            conn = get_database_connection()
            print("Encoding for this connection is ", conn.encoding)
            curs = conn.cursor()
            try:
                curs.execute(
                    "select * from favorites where book_id={book_id} and user_id={user_id}"
                    .format(book_id=book_id, user_id=user_id))
                if (curs.rowcount > 0):
                    for row in curs.fetchall():
                        print('check: ', row)
                        dictionary = {}
                        dictionary['status'] = 200
                        dictionary['id'] = row[0]
                        dictionary['bookId'] = row[1]
                        dictionary['userId'] = row[2]
                    print('dictionary: ', dictionary)
                    response = build_response(dictionary, None)
                else:
                    response = Response("false", status=200)
            except psycopg2.Error as e:
                print('not foun: ', e)
                response = Response("false", status=200)
                pass

            curs.close()
            conn.commit()
            conn.close()
            return response
        else:
            return build_array_response(dictionary, None)
    else:
        return Response(status=401)
Beispiel #5
0
def db_list_favorites():
    payload = is_authorized()
    if (payload != None):
        dictionary = find_user_by_email(payload)
        if (dictionary['status'] == 200):
            conn = get_database_connection()
            print("Encoding for this connection is", conn.encoding)
            user_id = dictionary['id']
            curs = conn.cursor()
            print("Extracting the rows ...")
            sqlString = db_get_favorite_sql_string(user_id)
            curs.execute(sqlString)
            response = Response(json.dumps({}), status=200)
            if (curs.rowcount > 0):
                data = list()
                for row in curs.fetchall():
                    # print(row)
                    dictionary = {}
                    dictionary['author'] = row[0]
                    dictionary['coverUrl'] = row[1]
                    dictionary['description'] = row[2]
                    dictionary['genre'] = row[3]
                    dictionary['title'] = row[4]
                    dictionary['bookId'] = row[5]
                    dictionary['userId'] = row[6]
                    dictionary['id'] = row[7]
                    data.insert(0, dictionary)
                    # dict(id=row[0], title=row[1], author=row[2], genre=row[3]))
                    # data.insert(0, row)
                response = build_array_response(data)

            curs.close()
            conn.commit()
            conn.close()
            return response
        else:
            return build_response(dictionary, None)
    else:
        return Response(status=401)
Beispiel #6
0
def create_token():
    print(request)
    if (request.data == b''):
        return Response(status=400)
    dataDict = json.loads(request.data)
    email = dataDict['email']
    password = dataDict['password']
    if (email == None):
        abort(401)
    if (password == None):
        abort(401)
    dictionary = find_user_by_email(email)
    print(dictionary)
    if (dictionary['status'] == 200):
        if (not match_password(dictionary['hashed_password'], password)):
            dictionary = {}
            dictionary['status'] = 401
            dictionary['message'] = 'Unable to authenticate user'
            return build_response(dictionary, None)
        else:
            return build_response(dictionary, email)
    else:
        return build_response(dictionary, None)