+ def filter_state(self, **kwargs):\r
+ return self.all().filter_state(**kwargs)\r
+\r
+\r
+class NodeStateDict(object):\r
+ def __init__(self, node):\r
+ self.__dict__['_node'] = node\r
+\r
+ def __getattr__(self, name):\r
+ if self.__dict__.get(name, None):\r
+ return self.__dict__[name]\r
+\r
+ try:\r
+ node = self.__dict__['_node']\r
+ action = NodeState.objects.get(node=node, state_type=name).action\r
+ self.__dict__[name] = action\r
+ return action\r
+ except:\r
+ return None\r
+\r
+ def __setattr__(self, name, value):\r
+ current = self.__getattr__(name)\r
+\r
+ if value:\r
+ if current:\r
+ current.action = value\r
+ current.save()\r
+ else:\r
+ node = self.__dict__['_node']\r
+ state = NodeState(node=node, action=value, state_type=name)\r
+ state.save()\r
+ self.__dict__[name] = value\r
+\r
+ if not "(%s)" % name in node.state_string:\r
+ node.state_string = "%s(%s)" % (node.state_string, name)\r
+ node.save()\r
+ else:\r
+ if current:\r
+ node = self.__dict__['_node']\r
+ node.state_string = "".join("(%s)" % s for s in re.findall('\w+', node.state_string) if s != name)\r
+ node.save()\r
+ current.node_state.delete()\r
+ del self.__dict__[name]\r
+\r
+\r
+class NodeStateQuery(object):\r
+ def __init__(self, node):\r
+ self.__dict__['_node'] = node\r
+\r
+ def __getattr__(self, name):\r
+ node = self.__dict__['_node']\r
+ return "(%s)" % name in node.state_string\r
+\r
+\r