def spec_to_node(self, current, spec): n = None message = "" if spec == "ref": n = reference() elif spec == "current": n = current elif self.is_uuid4(spec): n = Node.findById(spec) if n is None: message += "Node with id: " + spec + " does not exist" else: try: nq = dict([ [nv[0].strip(), nv[1].strip() ] for nv in [nv.split("=") for nv in spec[spec.index("(")+1:spec.index(")")].strip().split(",")]]) except: self.abort(400, "Unable to parse node defn") n = Node.find(auth.get_tenant(), **nq) if len(n) == 1: n = n[0] elif len(n) > 1: n = None message += "Could not find a unique node nodes with props: " + str(nq) else: n = None message += "Could not find a node nodes with props: " + str(nq) return (n, message)
def delete(self): nodes = Node.findn(auth.get_tenant()) for n in nodes: logging.info( "Deleting..." + str(n)) n.delete() self.response.status = "204 OK"
def get(self): dump_indexes() nodes = Node.findn(auth.get_tenant()) nodes_hash = { 'nodes': [] } for n in nodes: tref = { 'properties' : {} , 'node_link' : '/graphdb/' + n.id , 'node_id' : n.id} for an,av in n.items(): tref['properties'][an] = av tref['relationships'] = dict(outgoing=[], incoming=[]) for r in n.relationships.outgoing: x = {} for ran,rav in r.items(): x[ran] = rav tref['relationships']['outgoing'].append( { 'link' : '/graphdb/' + r.end().id, 'type_name' : r.type.name(), 'properties' : x } ) for r in n.relationships.incoming: x = {} for ran,rav in r.items(): x[ran] = rav tref['relationships']['incoming'].append( { 'link' : '/graphdb/' + r.start().id, 'type_name' : r.type.name(), 'properties' : x } ) nodes_hash['nodes'].append(tref) self.response.status = "200 OK" if self.request.headers['Accept'] == "application/json": self.response.headers['Content-Type'] = "application/json" self.response.out.write(json.dumps(nodes_hash)) else: self.response.headers['Content-Type'] = "text/html" template_values = nodes_hash path = os.path.join(os.path.dirname(__file__), 'nodelist.html') self.response.out.write(template.render(path, template_values))
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)
def reference(): return Node.ref(auth.get_tenant())