예제 #1
1
 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
예제 #2
0
 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)
예제 #3
0
 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
예제 #4
0
 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
예제 #5
0
    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
예제 #6
0
 def get_user(self, username):
     if not username:
         return None
     r.hgetall(USER_PREFIX + username)