Skip to content

Instantly share code, notes, and snippets.

@phpdude
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpdude/f98b377b613b4ffdcb99 to your computer and use it in GitHub Desktop.
Save phpdude/f98b377b613b4ffdcb99 to your computer and use it in GitHub Desktop.
state example
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
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