Example #1
0
def login():
    name = request.args.get('name')
    if not name:
        return 'name is required', 400

    password = request.args.get('password')
    if not password:
        return 'password is required', 400

    # 从数据库获取用户数据
    user = UserDB.get_user(name)
    if user.get('password') == password:
        return 'OK', 200
    else:
        return 'password is wrong', 400
Example #2
0
    def on_post(self, req, res, userId):
        '''
            - check if user exists;
            - check if book exists;
            - check if book number reach maximum;
        '''
        data = req.stream.read()
        data = json.loads(data)

        sche = schema.Schema("book")
        sche.validate(data)

        # Check if user exists
        user = UserDB()
        user_checking = user.get_user(userId)
        if not user_checking:
            res.status = httplib.INTERNAL_SERVER_ERROR
            res.body = "User Id %s not exists. Please create user first.\n" % userId
            return

        book = BookDB()
        # check if book already exists
        book_existing = book.get_book_by_name(userId, data['name'])
        if len(book_existing) != 0:
            res.status = httplib.INTERNAL_SERVER_ERROR
            res.body = "Book %s has already existed." % data['name']
            return

        # check if book amount reach maximum
        counts = book.count_book(userId)
        if counts == MAX_BOOKS_PER_UER:
            res.status = httplib.INTERNAL_SERVER_ERROR
            res.body = "Only %s books can be added. It reaches maximum." % MAX_BOOKS_PER_UER
            return

        # Add book to DB
        if counts == 0:
            bookId = 1
        else:
            bookId = 15
            books = book.list_book(userId)
            for ibook in books:
                bookId = bookId - ibook['ID'] % 10000

            for i in [1, 2, 4, 8]:
                if bookId & i != 0:
                    bookId = i
                    break

        LOG.info(("Book relative ID: [{0}]").format(bookId))

        status = 0
        if 'status' in data:
            status = data['status']

        desc = data['name']
        if desc in data:
            desc = data['description']

        bookId = 10000 * int(userId) + bookId
        book.add_book(userId, bookId, data['name'], status, desc)

        res.status = httplib.OK
        res.body = "Book %s is added successfully" % data['name']