-
-
Save phpdude/f98b377b613b4ffdcb99 to your computer and use it in GitHub Desktop.
state example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class History(LimitedOrderedSet): | |
def get_products(self): | |
return get_products_list(products=",".join(map(str, self))) | |
class ShopState(object): | |
""" | |
:type history: History | |
""" | |
def __init__(self): | |
self.history = None | |
self.init() | |
def init(self): | |
if not self.history: | |
setattr(self, 'history', History(limit=5)) | |
def __repr__(self): | |
return pformat(self.__dict__) | |
class ShopMiddleware(object): | |
session_key = '__state' | |
def process_request(self, request): | |
request.state = request.session.get(self.session_key, ShopState()) | |
def process_response(self, request, response): | |
request.session[self.session_key] = request.state | |
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.request.state.history.add(product.id) | |
context.update({ | |
'product': product, | |
'viewed': self.request.state.history.get_products() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment