return Model(self, model)
+ def save_action(self, action):
+ """
+ This method store an action object in the session and returns an integer
+ identifying that action. The method get_action() can be used to get
+ back the action.
+
+ :param the_action: The action to save in the session.
+ :type the_action: anything
+ :return: A key identifying the saved action.
+ :rtype: integer
+ """
+ saved_actions = self.setdefault('saved_actions', {"next": 1, "actions": {}})
+ # we don't allow more than 10 stored actions
+ if len(saved_actions["actions"]) >= 10:
+ del saved_actions["actions"][min(saved_actions["actions"])]
+ key = saved_actions["next"]
+ saved_actions["actions"][key] = action
+ saved_actions["next"] = key + 1
+ self.modified = True
+ return key
+
+ def get_action(self, key):
+ """
+ Gets back a previously saved action. This method can return None if the action
+ was saved since too much time (this case should be handled in a smart way).
+
+ :param key: The key given by save_action()
+ :type key: integer
+ :return: The saved action or None.
+ :rtype: anything
+ """
+ saved_actions = self.get('saved_actions', {})
+ return saved_actions.get("actions", {}).get(key)
+
def session_gc(session_store):
if random.random() < 0.001:
# we keep session one week