def create(username): index = r.hgetall(SESSION_PREFIX + username) # 检查库存 for k, v in index.items(): stock = Book.get_stock(k) if int(stock) < int(v): return False r.rename(SESSION_PREFIX + username, ORDER_PREFIX + username) for k, v in index.items(): stock = Book.get_stock(k) Book.set_stock(k, int(stock) - int(v)) db.orders.insert({'username': username, 'books': index}) return True
def merge_cart(self): """ 合并两个购物车(会话) :param username: :param session_id: :return: """ cart1 = r.hgetall(SESSION_PREFIX + self.session_id) cart2 = r.hgetall(SESSION_PREFIX + self.username) r.hmset(SESSION_PREFIX + self.username, dict(cart1, **cart2)) r.delete(SESSION_PREFIX + self.session_id)
def get(isbn=None): book = r.hgetall(BOOK_PREFIX + isbn) if not book: book = db.books.find_one({"isbn": isbn}) del book['_id'] r.hmset(BOOK_PREFIX + isbn, book) return book
def get(username): index = r.hgetall(ORDER_PREFIX + username) pipeline = r.pipeline() for isbn in index.keys(): pipeline.hgetall(BOOK_PREFIX + isbn) books = pipeline.execute() for book in books: book['count'] = index[book['isbn']] return books
def get(query_id): index = r.hgetall(SESSION_PREFIX + query_id) pipeline = r.pipeline() for isbn in index.keys(): pipeline.hgetall(BOOK_PREFIX + isbn) books = pipeline.execute() for book in books: book['count'] = index[book['isbn']] return books
def get_user(self, username): if not username: return None r.hgetall(USER_PREFIX + username)