Exemplo n.º 1
0
    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
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
    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)
Exemplo n.º 4
0
    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_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()
Exemplo n.º 6
0
 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()
Exemplo n.º 7
0
    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
Exemplo n.º 8
0
    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
Exemplo n.º 9
0
    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"])]
Exemplo n.º 10
0
    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
Exemplo n.º 11
0
    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
Exemplo n.º 12
0
    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'])
        ]