示例#1
0
def borrow_book():
    if not current_user.is_authenticated or current_user.type != 'book_admin':
        return get_response(status='fail: have no permission.')
    rdata = {
        key: value[0]
        for key, value in request.form.to_dict(flat=False).items()
        if key in ['user_id', 'book_id']
    }
    if 'user_id' not in rdata.keys():
        return get_response(status='fail: user_id is must.')
    if 'book_id' not in rdata.keys():
        return get_response(status='fail: book_id is must.')
    user = User.get(rdata['user_id'])
    book = Book.get(rdata['book_id'])
    if not user or user.type != 'user':
        return get_response(status='fail: user is not valuable.')
    if user.role is None or user.role.borrow_amount is None or user.role.borrow_limit is None or user.role.borrow_amount >= user.role.borrow_limit:
        return get_response(
            status='fail: the user\'s borrow number exceed the limit.')
    print(rdata['book_id'], book, file=sys.stderr)
    if not book:
        return get_response(status='fail: the book is not valuable.')
    if book.status == 0:
        return get_response(status='fail: the book was borrowed.')
    user.alter(borrow_amount=user.role.borrow_amount + 1)
    book.alter(status=0)
    now = datetime.datetime.now()
    BorrowRecord(
        user_id=rdata['user_id'],
        book_id=rdata['book_id'],
        borrow_time=now,
        should_time=now + datetime.timedelta(days=user.role.borrow_period),
        status=0,
    ).commit()
    return get_response(status='success')
示例#2
0
    def get(self, book_id):
        """
        Get book from id
        """
        book = Book.get(book_id, ignore=404)

        if book:
            return book.to_dict(include_id=True, include_offers=True)

        else:
            abort(404, error='Book not found')
示例#3
0
    def post(self):
        """
        Add offer
        """
        data = request.json
        book = Book.get(data['book_id'], ignore=404)

        if book is None:
            abort(404, error='Book not found')

        offer = book.add_offer(g.user.client_id, data['price'])

        return offer.to_dict(include_id=True)
示例#4
0
def get_book():
    if not current_user.is_authenticated or current_user.type != 'book_admin':
        return get_response(status='fail: have no permission.')
    rdata = {
        key: value[0]
        for key, value in request.form.to_dict(flat=False).items()
        if key in ['id']
    }
    if 'id' not in rdata.keys():
        return get_response(status='fail: id is must.')
    book = Book.get(rdata['id'])
    if not book:
        return get_response(
            status='fail: the book({}) not exists.'.format(rdata['id']))
    return get_response(status='success', data=book.to_dict())
示例#5
0
def add_book():
    if not current_user.is_authenticated or current_user.type != 'book_admin':
        return get_response(status='fail: have no permission.')
    rdata = {
        key: value[0]
        for key, value in request.form.to_dict(flat=False).items()
        if key in Book.get_book_keys()
    }
    if 'id' not in rdata.keys():
        return get_response(status='fail: id is must.')
    if 'name' not in rdata.keys():
        return get_response(status='fail: name is must.')
    if Book.get(rdata['id']):
        return get_response(
            status='fail: the book({}) already exists.'.format(rdata['id']))
    if 'entering_date' not in rdata.keys() or rdata['entering_date'] == '':
        rdata.update({'entering_date': datetime.datetime.now()})
    rdata['entering_men'] = current_user.name
    Book(**rdata).commit()
    return get_response(status='success')
示例#6
0
def return_book():
    if not current_user.is_authenticated or current_user.type != 'book_admin':
        return get_response(status='fail: have no permission.')
    rdata = {
        key: value[0]
        for key, value in request.form.to_dict(flat=False).items()
        if key in ['book_id']
    }
    if 'book_id' not in rdata.keys():
        return get_response(status='fail: book_id is must.')
    borrow_list = BorrowRecord.query.filter(
        BorrowRecord.book_id == rdata['book_id'],
        BorrowRecord.status == 0).all()
    if len(borrow_list) == 0:
        return get_response(status='fail: the book is not out.')
    borrow_record = borrow_list[0]
    user = User.get(borrow_record.user_id)
    book = Book.get(borrow_record.book_id)
    user.alter(borrow_amount=user.role.borrow_amount - 1)
    book.alter(status=1)
    now = datetime.datetime.now()
    borrow_record.alter(status=1, return_time=now)
    return get_response(status='success')
示例#7
0
    def put(self, offer_id):
        """
        Update offer
        """
        data = request.json

        offer = BookOffer.get(id=offer_id, ignore=404)
        if offer is None:
            abort(404, error='Offer not found')

        if offer.client_id != g.user.client_id:
            abort(404, error='Offer not found')

        if offer.purchased:
            abort(400, error='Offer purchased')

        offer.price = data['price']
        offer.save(refresh=True)

        book = Book.get(id=offer.book_id, ignore=404)
        if book is not None:
            book.update_offers_summary()

        return offer.to_dict(include_id=True)