def build_catalog(site, datasets, format=None): '''Build the DCAT catalog for this site''' site_url = url_for('site.home_redirect', _external=True) catalog_url = url_for('site.rdf_catalog', _external=True) graph = Graph(namespace_manager=namespace_manager) catalog = graph.resource(URIRef(catalog_url)) catalog.set(RDF.type, DCAT.Catalog) catalog.set(DCT.title, Literal(site.title)) catalog.set(DCT.language, Literal(current_app.config['DEFAULT_LANGUAGE'])) catalog.set(FOAF.homepage, URIRef(site_url)) publisher = graph.resource(BNode()) publisher.set(RDF.type, FOAF.Organization) publisher.set(FOAF.name, Literal(current_app.config['SITE_AUTHOR'])) catalog.set(DCT.publisher, publisher) for dataset in datasets: rdf_dataset = dataset_to_rdf(dataset, graph) if dataset.owner: rdf_dataset.add(DCT.publisher, user_to_rdf(dataset.owner, graph)) elif dataset.organization: rdf_dataset.add(DCT.publisher, organization_to_rdf(dataset.organization, graph)) catalog.add(DCAT.dataset, rdf_dataset) if isinstance(datasets, Paginable): paginate_catalog(catalog, graph, datasets, format, 'site.rdf_catalog_format') return catalog
def dataset_to_rdf(dataset, graph=None): ''' Map a dataset domain model to a DCAT/RDF graph ''' # Use the unlocalized permalink to the dataset as URI when available # unless there is already an upstream URI if 'uri' in dataset.extras: id = URIRef(dataset.extras['uri']) elif dataset.id: id = URIRef( url_for('datasets.show_redirect', dataset=dataset.id, _external=True)) else: id = BNode() # Expose upstream identifier if present if 'dct:identifier' in dataset.extras: identifier = dataset.extras['dct:identifier'] else: identifier = dataset.id graph = graph or Graph(namespace_manager=namespace_manager) d = graph.resource(id) d.set(RDF.type, DCAT.Dataset) d.set(DCT.identifier, Literal(identifier)) d.set(DCT.title, Literal(dataset.title)) d.set(DCT.description, Literal(dataset.description)) d.set(DCT.issued, Literal(dataset.created_at)) d.set(DCT.modified, Literal(dataset.last_modified)) if dataset.acronym: d.set(SKOS.altLabel, Literal(dataset.acronym)) for tag in dataset.tags: d.add(DCAT.keyword, Literal(tag)) for resource in dataset.resources: d.add(DCAT.distribution, resource_to_rdf(resource, dataset, graph)) if dataset.owner: d.add(DCT.publisher, user_to_rdf(dataset.owner, graph)) elif dataset.organization: d.add(DCT.publisher, organization_to_rdf(dataset.organization, graph)) if dataset.temporal_coverage: d.set(DCT.temporal, temporal_to_rdf(dataset.temporal_coverage, graph)) frequency = frequency_to_rdf(dataset.frequency) if frequency: d.set(DCT.accrualPeriodicity, frequency) return d
def test_minimal(self): user = UserFactory.build() # Does not have an URL u = user_to_rdf(user) g = u.graph self.assertIsInstance(u, RdfResource) self.assertEqual(len(list(g.subjects(RDF.type, FOAF.Person))), 1) self.assertEqual(u.value(RDF.type).identifier, FOAF.Person) self.assertIsInstance(u.identifier, BNode) self.assertEqual(u.value(FOAF.name), Literal(user.fullname)) self.assertEqual(u.value(RDFS.label), Literal(user.fullname))
def dataset_to_rdf(dataset, graph=None): ''' Map a dataset domain model to a DCAT/RDF graph ''' # Use the unlocalized permalink to the dataset as URI when available # unless there is already an upstream URI if 'uri' in dataset.extras: id = URIRef(dataset.extras['uri']) elif dataset.id: id = URIRef(url_for('datasets.show_redirect', dataset=dataset.id, _external=True)) else: id = BNode() # Expose upstream identifier if present if 'dct:identifier' in dataset.extras: identifier = dataset.extras['dct:identifier'] else: identifier = dataset.id graph = graph or Graph(namespace_manager=namespace_manager) d = graph.resource(id) d.set(RDF.type, DCAT.Dataset) d.set(DCT.identifier, Literal(identifier)) d.set(DCT.title, Literal(dataset.title)) d.set(DCT.description, Literal(dataset.description)) d.set(DCT.issued, Literal(dataset.created_at)) d.set(DCT.modified, Literal(dataset.last_modified)) if dataset.acronym: d.set(SKOS.altLabel, Literal(dataset.acronym)) for tag in dataset.tags: d.add(DCAT.keyword, Literal(tag)) for resource in dataset.resources: d.add(DCAT.distribution, resource_to_rdf(resource, dataset, graph)) if dataset.owner: d.add(DCT.publisher, user_to_rdf(dataset.owner, graph)) elif dataset.organization: d.add(DCT.publisher, organization_to_rdf(dataset.organization, graph)) if dataset.temporal_coverage: d.set(DCT.temporal, temporal_to_rdf(dataset.temporal_coverage, graph)) frequency = frequency_to_rdf(dataset.frequency) if frequency: d.set(DCT.accrualPeriodicity, frequency) return d
def test_all_fields(self): user = UserFactory(website=faker.uri()) user_url = url_for('users.show_redirect', user=user.id, _external=True) u = user_to_rdf(user) g = u.graph self.assertIsInstance(u, RdfResource) self.assertEqual(len(list(g.subjects(RDF.type, FOAF.Person))), 1) self.assertEqual(u.value(RDF.type).identifier, FOAF.Person) self.assertIsInstance(u.identifier, URIRef) self.assertEqual(u.identifier.toPython(), user_url) self.assertEqual(u.value(FOAF.name), Literal(user.fullname)) self.assertEqual(u.value(RDFS.label), Literal(user.fullname)) self.assertEqual( u.value(FOAF.homepage).identifier, URIRef(user.website))
def test_all_fields(self): user = UserFactory(website=faker.uri()) user_url = url_for('users.show_redirect', user=user.id, _external=True) u = user_to_rdf(user) g = u.graph self.assertIsInstance(u, RdfResource) self.assertEqual(len(list(g.subjects(RDF.type, FOAF.Person))), 1) self.assertEqual(u.value(RDF.type).identifier, FOAF.Person) self.assertIsInstance(u.identifier, URIRef) self.assertEqual(u.identifier.toPython(), user_url) self.assertEqual(u.value(FOAF.name), Literal(user.fullname)) self.assertEqual(u.value(RDFS.label), Literal(user.fullname)) self.assertEqual(u.value(FOAF.homepage).identifier, URIRef(user.website))