Ejemplo n.º 1
0
from api.graph import Node

tenant='myblog'

jo4neo = Node.new(tenant, name="jo4neo")
welcome = Node.new(tenant, name="welcome")

summer = Node.new(tenant, name="Summer")
mark = Node.new(tenant, name="Mark")

post1 = Node.new(tenant, content="jo4neo makes it easy to provision a graph with data")
post1.relationships.outgoing.create("author", summer)
post1.relationships.outgoing.create("hasTag", jo4neo)

post2 = Node.new(tenant, content="jo4neo provides simple yet useful features")
post2.relationships.outgoing.create("author", mark)
post2.relationships.outgoing.create("hasTag", welcome)
post2.relationships.outgoing.create("hasTag", jo4neo)

c = 0
for r in post2.relationships.outgoing.hasTag:
    c+=1

assert c == 2
Ejemplo n.º 2
0
    def post(self, node_id):

        logging.info("**thread " + threading.current_thread().name)
        logging.info("**post data: " + str(self.request.POST))

        current = self.node_id_to_node(node_id)
        yaml_data = None

        if self.request.headers['Content-Type'] == "application/yaml":
            yaml_data = self.convert_to_yaml(self.request.body)
        else:
            if 'yaml' in  self.request.POST:
                yaml_data = self.convert_to_yaml(self.request.POST['yaml'])
            elif 'delete' in self.request.POST:
                return self.delete(node_id)
            else:
                self.abort(400, "Missing input (either submit a form field named yaml or post content-type application/yaml)")

        pl = {}
        rl = []

        n = current

        if 'node' in yaml_data and (yaml_data['node'] is not None) and 'properties' in yaml_data['node']:
            pl = yaml_data['node']['properties']
            for pli in pl:
                if type(pl[pli]) != "str":
                    pl[pli] = str(pl[pli])
            n = Node.new(auth.get_tenant(), **pl)
            if 'relations' in yaml_data['node']:
                rl = yaml_data['node']['relations']
        elif 'relations' in yaml_data:
            rl = yaml_data['relations']
        elif 'properties' in yaml_data:
            n.add_properties(yaml_data['properties'])
        else:
            self.abort(400, "Expecting node/properties or relations or properties in the POST payload")

        for r in rl:

            if not isinstance(r, dict):
                self.abort(400, "Could not parse the relations!")

            logging.info("**** Adding relation: " + r['type'])

            rpl = r['properties'] if 'properties' in r else {}
            logging.info("  ** relation properties: ")
            for rpli in rpl:
                if type(rpl[rpli]) != "str":
                    rpl[rpli] = str(rpl[rpli])

            n1 = None
            n2 = None
            message = ""

            if 'from' in r:
                s = r['from']
                n1, msg =  self.spec_to_node(current, s)
                message += msg
                n2 = n

            elif 'to' in r:
                s = r['to']
                n2, msg =  self.spec_to_node(current, s)
                message += msg
                n1 = n
        
            if n1 is not None and n2 is not None:
                logging.info("n2 = " + str(n2))
                n1.relationships.create(r['type'], n2, **rpl)
            else:
                self.abort(404, str(message))

        self.response.headers['Content-Type']  = "application/json"
        self.response.headers['Location'] = "/graphdb/" + str(n.id)

        if self.request.headers['Content-Type'] == "application/yaml":
            self.response.status = "201 Created"
        else:
            return self.get(n.id)