def list_all_category():
    token = request.headers.get('Token')
    ErrorResponse = isErrorToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    ErrorResponse = checkUserErrorByToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    response = models.category.list_all_category(get_user_id_by_token(token))
    return response.response_message, response.response_code
def list_all_category():
    token = request.headers.get('Token')
    ErrorResponse = isErrorToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    ErrorResponse = checkUserErrorByToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    response = models.category.list_all_category(get_user_id_by_token(token))
    return response.response_message, response.response_code
def get_book_by_id(book_id):
    token = request.headers.get('Token')
    ErrorResponse = isErrorToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    ErrorResponse = checkUserErrorByToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    response = models.books.get_book_by_id(get_user_id_by_token(token), book_id)
    return response.response_message, response.response_code
Beispiel #4
0
def upload_user_head():
    token = request.headers.get('Token')
    if token is None:
        return makeResponse(JSONResponseProvideToken)

    user_id = get_user_id_by_token(token)
    if user_id == 0:
        return makeResponse(JSONResponseLoginFirst)
    if 'head_image' not in request.files:
        return makeResponse(JSONResponse(dumps({'message': 'No head_image provided.'}), 400))
    f = request.files['head_image']
    saveresult = save_user_head_image(f, user_id)
    return saveresult.response_message, saveresult.response_code
def add_book():
    token = request.headers.get('Token')
    ErrorResponse = isErrorToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    ErrorResponse = checkUserErrorByToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    jsondata = request.get_json()
    if not jsondata:
        return makeResponse(JSONResponseInvalidJSON)
    if 'bookname' not in jsondata:
        return makeResponse(JSONResponseProvideAtLeastBookName)

    bookname = ""
    author = ""
    publisher = ""
    publish_date = ""
    price = 0
    ISBN = ""
    tags = []
    cover_image_url = ""
    category_id = []

    if 'bookname' in jsondata:
        bookname = jsondata['bookname']
    if 'author' in jsondata:
        author = jsondata['author']
    if 'publisher' in jsondata:
        publisher = jsondata['publisher']
    if 'publish_date' in jsondata:
        publish_date = jsondata['publish_date']
    if 'price' in jsondata:
        price = jsondata['price']
    if 'ISBN' in jsondata:
        ISBN = jsondata['ISBN']
    if 'tags' in jsondata:
        tags = jsondata['tags']
    if 'cover_image_url' in jsondata:
        cover_image_url = jsondata['cover_image_url']
    if 'category_id' in jsondata:
        category_id = jsondata['category_id']

    if not isfloat(price):
        return makeResponse(JSONResponsePriceNotNumber)

    user_id = get_user_id_by_token(request.headers.get('Token'))

    response = models.books.add_book(user_id, bookname, author, publisher, publish_date, price, ISBN, tags, cover_image_url, category_id)
    return response.response_message, response.response_code
Beispiel #6
0
def getTokenInfoByToken(token):
    if token == "ALL":
        tmptoken = tokensdb.find()
    else:
        tmptoken = tokensdb.find_one({'token': token})
        if len(tmptoken) == 0:
            return makeResponse(JSONResponseTokenNotFound)
    return JSONResponse(dumps(tmptoken))
def login():
    jsondata = request.get_json()
    if not jsondata:
        return makeResponse(JSONResponseInvalidJSON)

    email = None
    password = None
    if 'email' in jsondata:
        email = jsondata['email']
    if 'password' in jsondata:
        password = jsondata['password']
    token = request.headers.get('Token')
    if email is None or password is None or token is None:
        return makeResponse(JSONResponseProvideNecessaryInfo)

    response = models.users.login(email, password, token)
    return response.response_message, response.response_code
Beispiel #8
0
def login():
    jsondata = request.get_json()
    if not jsondata:
        return makeResponse(JSONResponseInvalidJSON)

    email = None
    password = None
    if 'email' in jsondata:
        email = jsondata['email']
    if 'password' in jsondata:
        password = jsondata['password']
    token = request.headers.get('Token')
    if email is None or password is None or token is None:
        return makeResponse(JSONResponseProvideNecessaryInfo)

    response = models.users.login(email, password, token)
    return response.response_message, response.response_code
Beispiel #9
0
def getTokenInfoByToken(token):
    if token == "ALL":
        tmptoken = tokensdb.find()
    else:
        tmptoken = tokensdb.find_one({'token': token})
        if len(tmptoken) == 0:
            return makeResponse(JSONResponseTokenNotFound)
    return JSONResponse(dumps(tmptoken))
def add_books_to_category(category_id):
    token = request.headers.get('Token')
    ErrorResponse = isErrorToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    ErrorResponse = checkUserErrorByToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    jsondata = request.get_json()
    if not jsondata:
        return makeResponse(JSONResponseInvalidJSON)
    if 'books' not in jsondata:
        return makeResponse(JSONResponseProvideNecessaryInfo)

    user_id = get_user_id_by_token(request.headers.get('Token'))
    books = jsondata['books']
    response = models.category.add_books_to_category(user_id, category_id, books)
    return response.response_message, response.response_code
def update_category(category_id):
    token = request.headers.get('Token')
    ErrorResponse = isErrorToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    ErrorResponse = checkUserErrorByToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    jsondata = request.get_json()
    if not jsondata:
        return makeResponse(JSONResponseInvalidJSON)
    if 'category_name' not in jsondata:
        makeResponse(JSONResponseProvideNecessaryInfo)
    category_name = jsondata['category_name']

    user_id = get_user_id_by_token(request.headers.get('Token'))

    response = models.category.update_category(user_id, category_id, category_name)
    return response.response_message, response.response_code
def del_category():
    token = request.headers.get('Token')
    ErrorResponse = isErrorToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    ErrorResponse = checkUserErrorByToken(token)
    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)

    user_id = get_user_id_by_token(request.headers.get('Token'))
    jsondata = request.get_json()
    if not jsondata:
        return makeResponse(JSONResponseInvalidJSON)
    if 'category_id' not in jsondata:
        return makeResponse(JSONResponseProvideNecessaryInfo)
    category_id = jsondata['category_id']

    response = models.category.del_category(user_id, category_id)
    return response.response_message, response.response_code
Beispiel #13
0
def add_user():
    token = request.headers.get('Token')
    ErrorResponse = isErrorToken(token)

    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    jsondata = request.get_json()
    if not jsondata:
        return makeResponse(JSONResponseInvalidJSON)
    if 'username' not in jsondata or 'password' not in jsondata or 'email' not in jsondata:
        return makeResponse(JSONResponseProvideNecessaryInfo)
    username = jsondata['username']
    password = jsondata['password']
    email = jsondata['email']
    head_image_url = ""
    if 'head_image_url' in jsondata:
        head_image_url = jsondata['head_image_url']
    response = models.users.add_user(username, password, email, head_image_url, token)
    return response.response_message, response.response_code
Beispiel #14
0
def update_user(user_id):
    tmpuser = usersdb.find_one({'user_id': user_id})
    jsondata = request.get_json()
    if tmpuser is None:
        return makeResponse(JSONResponseUserNotFound)
    if not jsondata:
        return makeResponse(JSONResponseInvalidJSON)

    username = ""
    password = ""
    head_image_url = ""

    if 'username' in jsondata:
        username = jsondata['username']
    if 'password' in jsondata:
        password = jsondata['password']
    if 'head_image_url' in jsondata:
        head_image_url = jsondata['head_image_url']
    response = models.users.update_user(user_id, username, password, head_image_url)
    return response.response_message, response.response_code
Beispiel #15
0
def add_user():
    token = request.headers.get('Token')
    ErrorResponse = isErrorToken(token)

    if ErrorResponse is not None:
        return makeResponse(ErrorResponse)
    jsondata = request.get_json()
    if not jsondata:
        return makeResponse(JSONResponseInvalidJSON)
    if 'username' not in jsondata or 'password' not in jsondata or 'email' not in jsondata:
        return makeResponse(JSONResponseProvideNecessaryInfo)
    username = jsondata['username']
    password = jsondata['password']
    email = jsondata['email']
    head_image_url = ""
    if 'head_image_url' in jsondata:
        head_image_url = jsondata['head_image_url']
    response = models.users.add_user(username, password, email, head_image_url,
                                     token)
    return response.response_message, response.response_code
Beispiel #16
0
def update_user(user_id):
    tmpuser = usersdb.find_one({'user_id': user_id})
    jsondata = request.get_json()
    if tmpuser is None:
        return makeResponse(JSONResponseUserNotFound)
    if not jsondata:
        return makeResponse(JSONResponseInvalidJSON)

    username = ""
    password = ""
    head_image_url = ""

    if 'username' in jsondata:
        username = jsondata['username']
    if 'password' in jsondata:
        password = jsondata['password']
    if 'head_image_url' in jsondata:
        head_image_url = jsondata['head_image_url']
    response = models.users.update_user(user_id, username, password,
                                        head_image_url)
    return response.response_message, response.response_code
Beispiel #17
0
def logout():
    token = request.headers.get('Token')
    if not token:
        return makeResponse(JSONResponseProvideNecessaryInfo)
    response = models.users.logout(token)
    return response.response_message, response.response_code
Beispiel #18
0
def logout():
    token = request.headers.get('Token')
    if not token:
        return makeResponse(JSONResponseProvideNecessaryInfo)
    response = models.users.logout(token)
    return response.response_message, response.response_code