def setUpClass(cls): cls.loadOntology() with open( os.path.join( "tests/fixtures/resource_graphs/unique_graph_shape.json"), "rU") as f: json = JSONDeserializer().deserialize(f) cls.unique_graph = Graph(json["graph"][0]) cls.unique_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/ambiguous_graph_shape.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.ambiguous_graph = Graph(json["graph"][0]) cls.ambiguous_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/phase_type_assignment.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.phase_type_assignment_graph = Graph(json["graph"][0]) cls.phase_type_assignment_graph.save()
def setUpClass(cls): geojson_nodeid = "3ebc6785-fa61-11e6-8c85-14109fd34195" cls.loadOntology() with open( os.path.join( "tests/fixtures/resource_graphs/unique_graph_shape.json"), "rU") as f: json = JSONDeserializer().deserialize(f) cls.unique_graph = Graph(json["graph"][0]) cls.unique_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/ambiguous_graph_shape.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.ambiguous_graph = Graph(json["graph"][0]) cls.ambiguous_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/phase_type_assignment.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.phase_type_assignment_graph = Graph(json["graph"][0]) cls.phase_type_assignment_graph.save() # Load the test package to provide resources graph. test_pkg_path = os.path.join(test_settings.TEST_ROOT, "fixtures", "testing_prj", "testing_prj", "pkg") management.call_command("packages", operation="load_package", source=test_pkg_path, yes=True)
def setUpClass(cls): geojson_nodeid = "3ebc6785-fa61-11e6-8c85-14109fd34195" cls.loadOntology() with open( os.path.join( "tests/fixtures/resource_graphs/unique_graph_shape.json"), "rU") as f: json = JSONDeserializer().deserialize(f) cls.unique_graph = Graph(json["graph"][0]) cls.unique_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/ambiguous_graph_shape.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.ambiguous_graph = Graph(json["graph"][0]) cls.ambiguous_graph.save() with open( os.path.join( "tests/fixtures/resource_graphs/phase_type_assignment.json" ), "rU") as f: json = JSONDeserializer().deserialize(f) cls.phase_type_assignment_graph = Graph(json["graph"][0]) cls.phase_type_assignment_graph.save()
def load_resource_graph_file(path_to_file): if isfile(path_to_file) and path_to_file.endswith(suffix): basepath = path_to_file name = basepath.split(os.sep)[-1] with codecs.open(basepath, 'rU', encoding='utf-8') as f: file = json.load(f) resource_graph = Graph(file['graph'][0]) resource_graph.save()
def import_graph(graphs): reporter = GraphImportReporter(graphs) with transaction.atomic(): errors = [] for resource in graphs: reporter.name = resource['name'] reporter.resource_model = resource['isresource'] graph = Graph(resource) if not hasattr(graph, 'cards'): errors.append('{0} graph has no attribute cards'.format( graph.name)) else: if graph.cards == [] or graph.cards == {}: errors.append('{0} graph has no cards'.format(graph.name)) else: graph.save() reporter.update_graphs_saved() if not hasattr(graph, 'cards_x_nodes_x_widgets'): errors.append( '{0} graph has no attribute cards_x_nodes_x_widgets'. format(graph.name)) else: for card_x_node_x_widget in graph.cards_x_nodes_x_widgets: cardxnodexwidget = CardXNodeXWidget.objects.update_or_create( **card_x_node_x_widget) if not hasattr(graph, 'forms'): errors.append('{0} graph has no attribute forms'.format) else: for form in graph.forms: form = Form.objects.update_or_create(**form) reporter.update_forms_saved() if not hasattr(graph, 'forms_x_cards'): errors.append( '{0} graph has no attribute forms_x_cards'.format( graph.name)) else: for form_x_card in graph.forms_x_cards: formxcard = FormXCard.objects.update_or_create( **form_x_card) if not hasattr(graph, 'reports'): errors.append('{0} graph has no attribute reports'.format( graph.name)) else: for report in graph.reports: report = Report.objects.update_or_create(**report) reporter.update_reports_saved() return errors, reporter
def setUpClass(cls): with open( os.path.join( 'tests/fixtures/resource_graphs/unique_graph_shape.json'), 'rU') as f: json = JSONDeserializer().deserialize(f) cls.unique_graph = Graph(json['graph'][0]) cls.unique_graph.save() with open( os.path.join( 'tests/fixtures/resource_graphs/ambiguous_graph_shape.json' ), 'rU') as f: json = JSONDeserializer().deserialize(f) cls.ambiguous_graph = Graph(json['graph'][0]) cls.ambiguous_graph.save() with open( os.path.join( 'tests/fixtures/resource_graphs/phase_type_assignment.json' ), 'rU') as f: json = JSONDeserializer().deserialize(f) cls.phase_type_assignment_graph = Graph(json['graph'][0]) cls.phase_type_assignment_graph.save()
def import_graph(graphs, overwrite_graphs=True): reporter = GraphImportReporter(graphs) def check_default_configs(default_configs, configs): if default_configs is not None: if configs is None: configs = {} else: try: "" in configs # Checking if configs is a dict-like object except AttributeError: configs = JSONDeserializer().deserialize(configs) for default_key in default_configs: if default_key not in configs: configs[default_key] = default_configs[default_key] return configs with transaction.atomic(): errors = [] for resource in graphs: if len(list(OntologyClass.objects.all())) > 0: if resource["ontology_id"] is not None and resource[ "ontology_id"] not in [ str(f["ontologyid"]) for f in Ontology.objects.all().values("ontologyid") ]: errors.append( "The ontologyid of the graph you're trying to load does not exist in Arches." ) else: errors.append( "No ontologies have been loaded. Any GraphModel that depends on an ontology cannot be loaded." ) reporter.name = resource["name"] reporter.resource_model = resource["isresource"] reporter.graph_id = resource["graphid"] try: graph = Graph(resource) ontology_classes = [ str(f["source"]) for f in OntologyClass.objects.all().values("source") ] for node in list(graph.nodes.values()): if resource["ontology_id"] is not None: if node.ontologyclass not in ontology_classes: errors.append( "The ontology class of this node does not exist in the indicated ontology scheme." ) node_config = node.config default_config = DDataType.objects.get( datatype=node.datatype).defaultconfig node.config = check_default_configs( default_config, node_config) if not hasattr(graph, "cards"): errors.append("{0} graph has no attribute cards".format( graph.name)) else: if len(Graph.objects.filter(pk=graph.graphid) ) == 0 or overwrite_graphs is True: if hasattr(graph, "reports"): for report in graph.reports: if report["active"]: report_config = report["config"] default_config = ReportTemplate.objects.get( templateid=report["template_id"] ).defaultconfig graph.config = check_default_configs( default_config, report_config) graph.template_id = report["template_id"] graph.save() reporter.update_graphs_saved() else: overwrite_input = input( "Overwrite {0} (Y/N) ? ".format(graph.name)) if overwrite_input.lower() in ("t", "true", "y", "yes"): graph.save() else: raise GraphImportException( "{0} - already exists. Skipping import.". format(graph.name)) if not hasattr(graph, "cards_x_nodes_x_widgets"): errors.append( "{0} graph has no attribute cards_x_nodes_x_widgets". format(graph.name)) else: for card_x_node_x_widget in graph.cards_x_nodes_x_widgets: card_x_node_x_widget_config = card_x_node_x_widget[ "config"] default_config = Widget.objects.get( widgetid=card_x_node_x_widget["widget_id"] ).defaultconfig card_x_node_x_widget["config"] = check_default_configs( default_config, card_x_node_x_widget_config) cardxnodexwidget = CardXNodeXWidget.objects.update_or_create( **card_x_node_x_widget) except GraphImportException as ge: errors.append(ge) except Exception as e: errors.append( f"Could not define graph. Its resources were not loaded.") errors.append(e) # try/except block here until all graphs have a resource_2_resource_constraints object. try: if not hasattr(graph, "resource_2_resource_constraints"): errors.append( "{0} graph has no attribute resource_2_resource_constraints" .format(graph.resource_2_resource_constraints)) else: for resource_2_resource_constraint in graph.resource_2_resource_constraints: resource2resourceconstraint = Resource2ResourceConstraint.objects.update_or_create( **resource_2_resource_constraint) except: pass return errors, reporter
def import_graph(graphs, overwrite_graphs=True): reporter = GraphImportReporter(graphs) def check_default_configs(default_configs, configs): if default_configs is not None: if configs is None: configs = {} else: try: '' in configs # Checking if configs is a dict-like object except AttributeError: configs = JSONDeserializer().deserialize(configs) for default_key in default_configs: if default_key not in configs: configs[default_key] = default_configs[default_key] return configs with transaction.atomic(): errors = [] for resource in graphs: try: if resource['ontology_id'] is not None: if resource['ontology_id'] not in [ str(f['ontologyid']) for f in Ontology.objects.all().values('ontologyid') ]: errors.append( 'The ontologyid of the graph you\'re trying to load does not exist in Arches.' ) reporter.name = resource['name'] reporter.resource_model = resource['isresource'] reporter.graph_id = resource['graphid'] graph = Graph(resource) ontology_classes = [ str(f['source']) for f in OntologyClass.objects.all().values('source') ] for node in list(graph.nodes.values()): if resource['ontology_id'] is not None: if node.ontologyclass not in ontology_classes: errors.append( 'The ontology class of this node does not exist in the indicated ontology scheme.' ) node_config = node.config default_config = DDataType.objects.get( datatype=node.datatype).defaultconfig node.config = check_default_configs( default_config, node_config) if not hasattr(graph, 'cards'): errors.append('{0} graph has no attribute cards'.format( graph.name)) else: if len(Graph.objects.filter(pk=graph.graphid) ) == 0 or overwrite_graphs is True: if hasattr(graph, 'reports'): for report in graph.reports: if report['active']: report_config = report['config'] default_config = ReportTemplate.objects.get( templateid=report['template_id'] ).defaultconfig graph.config = check_default_configs( default_config, report_config) graph.template_id = report['template_id'] graph.save() reporter.update_graphs_saved() else: overwrite_input = input( 'Overwrite {0} (Y/N) ? '.format(graph.name)) if overwrite_input.lower() in ('t', 'true', 'y', 'yes'): graph.save() else: raise GraphImportException( '{0} - already exists. Skipping import.'. format(graph.name)) if not hasattr(graph, 'cards_x_nodes_x_widgets'): errors.append( '{0} graph has no attribute cards_x_nodes_x_widgets'. format(graph.name)) else: for card_x_node_x_widget in graph.cards_x_nodes_x_widgets: card_x_node_x_widget_config = card_x_node_x_widget[ 'config'] default_config = Widget.objects.get( widgetid=card_x_node_x_widget['widget_id'] ).defaultconfig card_x_node_x_widget['config'] = check_default_configs( default_config, card_x_node_x_widget_config) cardxnodexwidget = CardXNodeXWidget.objects.update_or_create( **card_x_node_x_widget) # try/except block here until all graphs have a resource_2_resource_constraints object. try: if not hasattr(graph, 'resource_2_resource_constraints'): errors.append( '{0} graph has no attribute resource_2_resource_constraints' .format(graph.resource_2_resource_constraints)) else: for resource_2_resource_constraint in graph.resource_2_resource_constraints: resource2resourceconstraint = Resource2ResourceConstraint.objects.update_or_create( **resource_2_resource_constraint) except: pass except Exception as e: print(e) return errors, reporter
def test_graph_doesnt_polute_db(self): """ test that the mere act of creating a Graph instance doesn't save anything to the database """ graph_obj = { "name": "TEST GRAPH", "subtitle": "ARCHES TEST GRAPH", "author": "Arches", "description": "ARCHES TEST GRAPH", "version": "v1.0.0", "isresource": True, "isactive": False, "iconclass": "fa fa-building", 'nodes':[{ "status": None, "description": "", "name": "ROOT_NODE", "istopnode": True, "ontologyclass": "", "nodeid": "55555555-343e-4af3-8857-f7322dc9eb4b", "nodegroup_id": "", "datatype": "semantic" },{ "status": None, "description": "", "name": "NODE_NAME", "istopnode": False, "ontologyclass": "", "nodeid": "66666666-24c9-4226-bde2-2c40ee60a26c", "nodegroup_id": "66666666-24c9-4226-bde2-2c40ee60a26c", "datatype": "string" }], 'edges':[{ "rangenodeid": "66666666-24c9-4226-bde2-2c40ee60a26c", "name": "", "edgeid": "11111111-d50f-11e5-8754-80e6500ee4e4", "domainnodeid": "55555555-343e-4af3-8857-f7322dc9eb4b", "ontologyproperty": "P2", "description": "" }], 'cards':[{ "name": "NODE_NAME", "description": "", "instructions": "", "helptext": "", "cardinality": "n", "nodegroup_id": "66666666-24c9-4226-bde2-2c40ee60a26c" }] } nodes_count_before = models.Node.objects.count() edges_count_before = models.Edge.objects.count() cards_count_before = models.CardModel.objects.count() nodegroups_count_before = models.NodeGroup.objects.count() graph = Graph(graph_obj) self.assertEqual(models.Node.objects.count()-nodes_count_before, 0) self.assertEqual(models.Edge.objects.count()-edges_count_before, 0) self.assertEqual(models.CardModel.objects.count()-cards_count_before, 0) self.assertEqual(models.NodeGroup.objects.count()-nodegroups_count_before, 0) self.assertEqual(graph_obj['name'], graph.name) self.assertEqual(graph_obj['subtitle'], graph.subtitle) self.assertEqual(graph_obj['author'], graph.author) self.assertEqual(graph_obj['description'], graph.description) self.assertEqual(graph_obj['version'], graph.version) self.assertEqual(graph_obj['isresource'], graph.isresource) self.assertEqual(graph_obj['isactive'], graph.isactive) self.assertEqual(graph_obj['iconclass'], graph.iconclass)
def import_graph(graphs, overwrite_graphs=True): reporter = GraphImportReporter(graphs) def check_default_configs(default_configs, configs): if default_configs != None: if configs == None: configs = {} else: try: configs.has_key( '') #Checking if configs is a dict-like object except AttributeError: configs = JSONDeserializer().deserialize(configs) for default_key in default_configs: if default_key not in configs: configs[default_key] = default_configs[default_key] return configs with transaction.atomic(): errors = [] for resource in graphs: try: reporter.name = resource['name'] reporter.resource_model = resource['isresource'] graph = Graph(resource) for node in graph.nodes.values(): node_config = node.config default_config = DDataType.objects.get( datatype=node.datatype).defaultconfig node.config = check_default_configs( default_config, node_config) if not hasattr(graph, 'cards'): errors.append('{0} graph has no attribute cards'.format( graph.name)) else: if graph.cards == [] or graph.cards == {}: errors.append('{0} graph has no cards'.format( graph.name)) else: if len(Graph.objects.filter(pk=graph.graphid) ) == 0 or overwrite_graphs == True: graph.save() reporter.update_graphs_saved() else: overwrite_input = raw_input( 'Overwrite {0} (Y/N) ? '.format(graph.name)) if overwrite_input.lower() in ('t', 'true', 'y', 'yes'): graph.save() else: raise GraphImportException( '{0} - already exists. Skipping import.'. format(graph.name)) if not hasattr(graph, 'cards_x_nodes_x_widgets'): errors.append( '{0} graph has no attribute cards_x_nodes_x_widgets'. format(graph.name)) else: for card_x_node_x_widget in graph.cards_x_nodes_x_widgets: card_x_node_x_widget_config = card_x_node_x_widget[ 'config'] default_config = Widget.objects.get( widgetid=card_x_node_x_widget['widget_id'] ).defaultconfig card_x_node_x_widget['config'] = check_default_configs( default_config, card_x_node_x_widget_config) cardxnodexwidget = CardXNodeXWidget.objects.update_or_create( **card_x_node_x_widget) if not hasattr(graph, 'forms'): errors.append('{0} graph has no attribute forms'.format) else: for form in graph.forms: form = Form.objects.update_or_create(**form) reporter.update_forms_saved() if not hasattr(graph, 'forms_x_cards'): errors.append( '{0} graph has no attribute forms_x_cards'.format( graph.name)) else: for form_x_card in graph.forms_x_cards: formxcard = FormXCard.objects.update_or_create( **form_x_card) if not hasattr(graph, 'reports'): errors.append('{0} graph has no attribute reports'.format( graph.name)) else: for report in graph.reports: report_config = report['config'] default_config = ReportTemplate.objects.get( templateid=report['template_id']).defaultconfig report['config'] = check_default_configs( default_config, report_config) report = Report.objects.update_or_create(**report) reporter.update_reports_saved() # try/except block here until all graphs have a resource_2_resource_constraints object. try: if not hasattr(graph, 'resource_2_resource_constraints'): errors.append( '{0} graph has no attribute resource_2_resource_constraints' .format(graph.resource_2_resource_constraints)) else: for resource_2_resource_constraint in graph.resource_2_resource_constraints: resource2resourceconstraint = Resource2ResourceConstraint.objects.update_or_create( **resource_2_resource_constraint) except: pass except Exception as e: print e return errors, reporter
def import_graph(graphs): graph = Graph(graphs) graph.save()