def test_VersionedNode_init_without_args(self): self.printLine() node = VersionedNode() node.save() print node.uuid, node.timestamp node2 = VersionedNode.pull(node.uuid) assert node2.uuid == node.uuid self.printLine()
def test_VersionedNode_update_field_dict_append(self): self.printLine() setattr(VersionedNode, "foo", CaliopeJSONProperty()) node = VersionedNode() node.save() print node.uuid, node.timestamp, node.foo node.update_field("foo", "bar", field_id="foo") assert node.foo["foo"] == "bar" self.printLine()
def test_VersionedNode_init_with_args(self): self.printLine() props = {'foo': 'bar', 'other': 2} node = VersionedNode(**props) node.save() print node.uuid, node.timestamp node.refresh() assert node.foo == 'bar' and node.other == 2 self.printLine()
def get_all(cls, context=None, recursive=False): user_node = CaliopeUser.index.get(username=LoginManager().get_user()) if context: node_context = VersionedNode.pull(context).__node__.id else: node_context = "*" results, metadata = user_node.cypher( """ START n=node(*) MATCH path=(n)-[:TARGET]-(task)-[:__CONTEXT__]-(context), p_none=(task)<-[?:PARENT*]-() WHERE has(n.uuid) and p_none=null RETURN n """.format( context=node_context ) ) rv = list() instances = list() entities = list() models = list() for row in results: form = dict() uuid = row[0]["uuid"] entity_name = VersionedNode.pull(uuid).__class__.__name__ # TODO: fix workaround if entity_name not in current_app.caliope_forms: continue if entity_name not in entities: entities.append(entity_name) data = super(FormManager, cls).get_data(uuid=uuid) form["uuid"] = uuid form["classname"] = entity_name form["data"] = data form["browsable"] = current_app.caliope_forms[entity_name]["browsable"] if recursive: cls.get_related_data(instances, data) instances.append(form) rv.append({"instances": instances}) for entity_name in entities: models.append(cls.get_empty_model(entity_name, thumbnails=True)) rv.append({"models": models}) return rv
def get_all(cls, context=None, recursive=False): user_node = CaliopeUser.index.get(username=LoginManager().get_user()) if context: node_context = VersionedNode.pull(context).__node__.id else: node_context = '*' results, metadata = user_node.cypher(""" START n=node(*) MATCH path=(n)-[:TARGET]-(task)-[:__CONTEXT__]-(context), p_none=(task)<-[?:PARENT*]-() WHERE has(n.uuid) and p_none=null RETURN n """.format(context=node_context)) rv = list() instances = list() entities = list() models = list() for row in results: form = dict() uuid = row[0]['uuid'] entity_name = VersionedNode.pull(uuid).__class__.__name__ #TODO: fix workaround if entity_name not in current_app.caliope_forms: continue if entity_name not in entities: entities.append(entity_name) data = super(FormManager, cls).get_data(uuid=uuid) form['uuid'] = uuid form['classname'] = entity_name form['data'] = data form['browsable'] = current_app.caliope_forms[entity_name]['browsable'] if (recursive): cls.get_related_data(instances, data) instances.append(form) rv.append({'instances': instances}) for entity_name in entities: models.append(cls.get_empty_model(entity_name, thumbnails=True)) rv.append({'models': models}) return rv
def get_related_data(cls, instances, data): uuid4hex = re.compile("[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}") for field_name in data: field = data[field_name] type(field) == dict for key in field: related_uuid = uuid4hex.match(key) if related_uuid: entity_name = VersionedNode.pull(related_uuid.string).__class__.__name__ # TODO: fix workaround if entity_name not in current_app.caliope_forms: continue form = dict() node = super(FormManager, cls).get_data(uuid=related_uuid.string) form["uuid"] = related_uuid.string form["classname"] = entity_name form["data"] = node form["browsable"] = False instances.append(form) cls.get_related_data(instances, node)
def test_VersionedNode_push(self): self.printLine() props = {'foo': 'bar', 'other': 2} node = VersionedNode.push(**props) print node.uuid, node.timestamp assert node.foo == 'bar' and node.other == 2 self.printLine()
def get_related_data(cls, instances, data): uuid4hex = re.compile('[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}') for field_name in data: field = data[field_name] type(field) == dict for key in field: related_uuid = uuid4hex.match(key) if related_uuid: entity_name = VersionedNode.pull(related_uuid.string).__class__.__name__ #TODO: fix workaround if entity_name not in current_app.caliope_forms: continue form = dict() node = super(FormManager, cls).get_data(uuid=related_uuid.string) form['uuid'] = related_uuid.string form['classname'] = entity_name form['data'] = node form['browsable'] = False instances.append(form) cls.get_related_data(instances, node)
def commit(cls, uuid, loopback_notification=False): hkey_name = uuid hkey_name_rels = uuid + "_rels" #Create form if is associated to a form if cls.r.hexists(hkey_name, "formtask") and not cls.r.hexists( hkey_name_rels, "target"): from cid.core.forms.services import FormManager form_name = cls.r.hget(hkey_name, "formtask") form = FormManager.create_form_from_id(form_name, {}) cls.update_relationship(uuid, "target", form["uuid"]) #notify the other users of the change holders_to_add = [] holders_to_remove = [] if cls.r.hexists(hkey_name_rels, "holders"): holders_to_add = [ h for h, v in json.loads( cls.r.hget(hkey_name_rels, "holders"), object_hook=DatetimeDecoder.json_date_parser).items() if "__changed__" in v ] holders_to_remove = [ h for h, v in json.loads( cls.r.hget(hkey_name_rels, "holders"), object_hook=DatetimeDecoder.json_date_parser).items() if "__delete__" in v ] rv = super(TaskServices, cls).commit(uuid) for holder in holders_to_add: PubSub().publish_command("", holder, "createTask", VersionedNode.pull(uuid).serialize(), loopback=loopback_notification) PubSub().subscribe_uuid_with_user_uuid(holder, uuid) for holder in holders_to_remove: PubSub().publish_command("", holder, "removeTask", VersionedNode.pull(uuid).serialize(), loopback=loopback_notification) return rv
def test_VersionedNode_change(self): self.printLine() props = {'foo': 'bar', 'other': 2} node = VersionedNode.push(**props) print node.uuid, node.timestamp setattr(node, 'foo', 'no_bar') setattr(node, 'other', 4) node.save() assert node.foo == 'no_bar' and node.other == 4 self.printLine()
def test_versionednode_update_field(self): self.printLine() node = VersionedNode() node.foo = 'bar' node.save() print node.uuid, node.timestamp, node.foo node.update_field("foo", "new_bar") assert node.foo == "new_bar" self.printLine()
def commit(cls, uuid, loopback_notification=False): hkey_name = uuid hkey_name_rels = uuid + "_rels" # Create form if is associated to a form if cls.r.hexists(hkey_name, "formtask") and not cls.r.hexists(hkey_name_rels, "target"): from cid.core.forms.services import FormManager form_name = cls.r.hget(hkey_name, "formtask") form = FormManager.create_form_from_id(form_name, {}) cls.update_relationship(uuid, "target", form["uuid"]) # notify the other users of the change holders_to_add = [] holders_to_remove = [] if cls.r.hexists(hkey_name_rels, "holders"): holders_to_add = [ h for h, v in json.loads( cls.r.hget(hkey_name_rels, "holders"), object_hook=DatetimeDecoder.json_date_parser ).items() if "__changed__" in v ] holders_to_remove = [ h for h, v in json.loads( cls.r.hget(hkey_name_rels, "holders"), object_hook=DatetimeDecoder.json_date_parser ).items() if "__delete__" in v ] rv = super(TaskServices, cls).commit(uuid) for holder in holders_to_add: PubSub().publish_command( "", holder, "createTask", VersionedNode.pull(uuid).serialize(), loopback=loopback_notification ) PubSub().subscribe_uuid_with_user_uuid(holder, uuid) for holder in holders_to_remove: PubSub().publish_command( "", holder, "removeTask", VersionedNode.pull(uuid).serialize(), loopback=loopback_notification ) return rv
def test_VersionedNode_parent(self): self.printLine() props = {'foo': 'bar', 'other': 2} node = VersionedNode.push(**props) print node.uuid, node.timestamp setattr(node, 'foo', 'no_bar') setattr(node, 'other', 3) node.save() node_prev = node.parent.single() assert node_prev.foo == props['foo'] assert node_prev.other == props['other'] self.printLine()
def test_VersionedNode_update_field_list_append(self): self.printLine() node = VersionedNode() node.foo = ['item0', "item1"] node.save() print node.uuid, node.timestamp, node.foo node.update_field("foo", "bar", 0) assert node.foo[0] == "bar" assert node.foo[1] == "item1" self.printLine()
def test_VersionedNode_update_field_dict_update(self): self.printLine() setattr(VersionedNode, "foo", CaliopeJSONProperty()) node = VersionedNode() node.foo = {"foo": "bar", "a": 1} node.save() print node.uuid, node.timestamp, node.foo node.update_field("foo", "new_bar", "foo") assert node.foo["foo"] == "new_bar" self.printLine()
def get_current_user_kanban(cls, category=None, context=None): user_node = CaliopeUser.index.get(username=LoginManager().get_user()) #: Starting from current user, match all nodes which are connected througth a HOLDER #: relationship and that node is connected with a CURRENT relationship to a task. #: From the task find the FIRST node if context: node_context = VersionedNode.pull(context).__node__.id else: node_context = "*" if category is None: results, metadata = user_node.cypher( """ START user=node({{self}}), context_node=node({context}) MATCH (user)-[r:HOLDER]-(t)-[TASK]-(), (t)-[:__CONTEXT__]-(context) WHERE has(r.category) and (r.category='ToDo' or r.category='Doing' or r.category='Done') and id(context_node)=id(context) RETURN distinct t, r.category """.format( context=node_context ) ) else: results, metadata = user_node.cypher( """ START user=node({{self}}), context_arg=node({context}) MATCH (user)-[r:HOLDER]-(t)-[TASK]-(), (t)-[:__CONTEXT__]-(context) WHERE has(r.category) and (r.category='{category}') RETURN distinct t, r.category """.format( category=category, context=node_context ) ) tasks_list = { "ToDo": {"pos": 0, "category": {"value": "ToDo"}, "tasks": []}, "Doing": {"pos": 1, "category": {"value": "Doing"}, "tasks": []}, "Done": {"pos": 2, "category": {"value": "Done"}, "tasks": []}, } for row in results: tl = tasks_list[row[1]]["tasks"] task = cls.get_data(row[0]._properties["uuid"]) tl.append(task) return [list for list in sorted(tasks_list.values(), key=lambda pos: pos["pos"])]
def test_VersionedNode_history(self): self.printLine() props = {'foo': 'bar', 'other': 2} node = VersionedNode.push(**props) print node.uuid, node.timestamp setattr(node, 'foo', 'no_bar') setattr(node, 'other', 3) node.save() node_prev = node.parent.single() history = node._get_change_history() #:TODO Asserts. setattr(node, 'x', 'new') setattr(node, 'other', 3) delattr(node,'foo') node.save() history = node._get_change_history() self.printLine()
def test_VersionedNode_history(self): self.printLine() props = {'foo': 'bar', 'other': 2} node = VersionedNode.push(**props) print node.uuid, node.timestamp setattr(node, 'foo', 'no_bar') setattr(node, 'other', 3) node.save() node_prev = node.parent.single() history = node._get_change_history() #:TODO Asserts. setattr(node, 'x', 'new') setattr(node, 'other', 3) delattr(node, 'foo') node.save() history = node._get_change_history() self.printLine()
def commit(cls, uuid, loopback_notification=False): rv = dict() related = cls._get_related(uuid) if related > 0: for target_uuid in related.keys(): #Avoid related locks if theres is a back-relationship: if cls._is_related(target_uuid, uuid): continue #: This is to save nodes when no data added but there are # in a relationships, we're saving "blank" connected nodes. if VersionedNode.pull(target_uuid) is None: cls.update_field(target_uuid, "uuid", target_uuid) rv.update(cls.commit(target_uuid, loopback_notification)) rv.update(super(FormManager, cls).commit(uuid, loopback_notification)) PubSub().publish_command("", uuid, 'message', {'msg': 'Formulario actualizado.', 'type': 'success'}, loopback=True) return rv
def commit(cls, uuid, loopback_notification=False): rv = dict() related = cls._get_related(uuid) if related > 0: for target_uuid in related.keys(): # Avoid related locks if theres is a back-relationship: if cls._is_related(target_uuid, uuid): continue #: This is to save nodes when no data added but there are # in a relationships, we're saving "blank" connected nodes. if VersionedNode.pull(target_uuid) is None: cls.update_field(target_uuid, "uuid", target_uuid) rv.update(cls.commit(target_uuid, loopback_notification)) rv.update(super(FormManager, cls).commit(uuid, loopback_notification)) PubSub().publish_command( "", uuid, "message", {"msg": "Formulario actualizado.", "type": "success"}, loopback=True ) return rv
def get_current_user_kanban(cls, category=None, context=None): user_node = CaliopeUser.index.get(username=LoginManager().get_user()) #: Starting from current user, match all nodes which are connected througth a HOLDER #: relationship and that node is connected with a CURRENT relationship to a task. #: From the task find the FIRST node if context: node_context = VersionedNode.pull(context).__node__.id else: node_context = '*' if category is None: results, metadata = user_node.cypher(""" START user=node({{self}}), context_node=node({context}) MATCH (user)-[r:HOLDER]-(t)-[TASK]-(), (t)-[:__CONTEXT__]-(context) WHERE has(r.category) and (r.category='ToDo' or r.category='Doing' or r.category='Done') and id(context_node)=id(context) RETURN distinct t, r.category """.format(context=node_context)) else: results, metadata = user_node.cypher(""" START user=node({{self}}), context_arg=node({context}) MATCH (user)-[r:HOLDER]-(t)-[TASK]-(), (t)-[:__CONTEXT__]-(context) WHERE has(r.category) and (r.category='{category}') RETURN distinct t, r.category """.format(category=category, context=node_context)) tasks_list = { 'ToDo': { 'pos': 0, 'category': { 'value': 'ToDo' }, 'tasks': [] }, 'Doing': { 'pos': 1, 'category': { 'value': 'Doing' }, 'tasks': [] }, 'Done': { 'pos': 2, 'category': { 'value': 'Done' }, 'tasks': [] } } for row in results: tl = tasks_list[row[1]]['tasks'] task = cls.get_data(row[0]._properties['uuid']) tl.append(task) return [ list for list in sorted(tasks_list.values(), key=lambda pos: pos['pos']) ]