~fgallina/python-timeline-django/no-query-fix

« back to all changes in this revision

Viewing changes to timeline_django/hooks.py

  • Committer: Tarmac
  • Author(s): James Westby
  • Date: 2012-02-24 20:08:48 UTC
  • mfrom: (8.1.1 stack-of-actions)
  • Revision ID: tarmac@server-5390-20120224200848-ykg07j7o92pa1a63
[r=james-w] Handle nested actions in the hooks.

Because saving one model may save another we have to handle nested actions.
We therefore use a stack of actions for each action type.

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
            return
59
59
        action = timeline.start(category, self._model_name(sender),
60
60
                allow_nested=True)
61
 
        setattr(self._actions, self._action_name(category, sender), action)
 
61
        attr = self._action_name(category, sender)
 
62
        actions = getattr(self._actions, attr, [])
 
63
        actions.append(action)
 
64
        setattr(self._actions, attr, actions)
62
65
 
63
66
    def _handle_post(self, category, sender, **kwargs):
64
67
        action_name = self._action_name(category, sender)
65
 
        action = getattr(self._actions, action_name, None)
66
 
        if action is None:
 
68
        actions = getattr(self._actions, action_name, [])
 
69
        if not actions:
67
70
            raise AssertionError("post action called without pre action.")
68
 
        delattr(self._actions, action_name)
 
71
        action = actions.pop()
 
72
        setattr(self._actions, action_name, actions)
69
73
        action.finish()
70
74
 
71
75
    def pre_init(self, sender, **kwargs):