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 login(self, username, password): self.user = db.users.find_one({'username': username, 'password': password}) if self.user: del self.user["_id"] del self.user["password"] r.hmset(USER_PREFIX + username, self.user) return self.user
def add(isbn, book): import time book['date_added'] = int(time.time()) r.hmset(BOOK_PREFIX + isbn, book) b = db.books.find_one({'isbn': isbn}) if b: book['_id'] = b['_id'] db.books.save(book) else: db.books.insert(book)
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)