Example #1
0
    def json_ld(self):
        result = {
            '@context': 'http://schema.org',
            '@type': 'CreativeWork',
            'alternateName': self.slug,
            'dateCreated': self.created_at.isoformat(),
            'dateModified': self.last_modified.isoformat(),
            'url': endpoint_for('reuses.show', 'api.reuse', reuse=self, _external=True),
            'name': self.title,
            'isBasedOnUrl': self.url,
        }

        if self.description:
            result['description'] = mdstrip(self.description)

        if self.organization:
            author = self.organization.json_ld
        elif self.owner:
            author = self.owner.json_ld
        else:
            author = None

        if author:
            result['author'] = author

        return result
Example #2
0
    def json_ld(self):
        type_ = 'GovernmentOrganization' if self.public_service \
                else 'Organization'

        result = {
            '@context':
            'http://schema.org',
            '@type':
            type_,
            '@id':
            str(self.id),
            'alternateName':
            self.slug,
            'url':
            endpoint_for('organizations.show',
                         'api.organization',
                         org=self,
                         _external=True),
            'name':
            self.name,
            'dateCreated':
            self.created_at.isoformat(),
            'dateModified':
            self.last_modified.isoformat()
        }

        if self.description:
            result['description'] = mdstrip(self.description)

        logo = self.logo(external=True)
        if logo:
            result['logo'] = logo

        return result
Example #3
0
def resource_to_rdf(resource, dataset=None, graph=None):
    '''
    Map a Resource domain model to a DCAT/RDF graph
    '''
    graph = graph or Graph(namespace_manager=namespace_manager)
    if dataset and dataset.id:
        id = URIRef(
            endpoint_for('datasets.show_redirect',
                         'api.dataset',
                         dataset=dataset.id,
                         _external=True,
                         _anchor='resource-{0}'.format(resource.id)))
    else:
        id = BNode(resource.id)
    permalink = endpoint_for('datasets.resource',
                             'api.resource_redirect',
                             id=resource.id,
                             _external=True)
    r = graph.resource(id)
    r.set(RDF.type, DCAT.Distribution)
    r.set(DCT.identifier, Literal(resource.id))
    r.add(DCT.title, Literal(resource.title))
    r.add(DCT.description, Literal(resource.description))
    r.add(DCAT.downloadURL, URIRef(resource.url))
    r.add(DCAT.accessURL, URIRef(permalink))
    r.add(DCT.issued, Literal(resource.published))
    r.add(DCT.modified, Literal(resource.modified))
    if dataset and dataset.license:
        r.add(DCT.rights, Literal(dataset.license.title))
        if dataset.license.url:
            r.add(DCT.license, URIRef(dataset.license.url))
    if resource.filesize is not None:
        r.add(DCAT.bytesSize, Literal(resource.filesize))
    if resource.mime:
        r.add(DCAT.mediaType, Literal(resource.mime))
    if resource.format:
        r.add(DCT.format, Literal(resource.format))
    if resource.checksum:
        checksum = graph.resource(BNode())
        checksum.set(RDF.type, SPDX.Checksum)
        algorithm = 'checksumAlgorithm_{0}'.format(resource.checksum.type)
        checksum.add(SPDX.algorithm, getattr(SPDX, algorithm))
        checksum.add(SPDX.checksumValue, Literal(resource.checksum.value))
        r.add(SPDX.checksum, checksum)
    return r
Example #4
0
    def latest(self):
        '''
        Permanent link to the latest version of this resource.

        If this resource is updated and `url` changes, this property won't.
        '''
        return endpoint_for('datasets.resource',
                            'api.resource_redirect',
                            id=self.id,
                            _external=True)
Example #5
0
    def json_ld(self):
        result = {
            '@context':
            'http://schema.org',
            '@type':
            'Dataset',
            '@id':
            str(self.id),
            'alternateName':
            self.slug,
            'dateCreated':
            self.created_at.isoformat(),
            'dateModified':
            self.last_modified.isoformat(),
            'url':
            endpoint_for('datasets.show',
                         'api.dataset',
                         dataset=self,
                         _external=True),
            'name':
            self.title,
            'keywords':
            ','.join(self.tags),
            'distribution': [resource.json_ld for resource in self.resources],
            # Theses values are not standard
            'contributedDistribution':
            [resource.json_ld for resource in self.community_resources],
            'extras':
            [get_json_ld_extra(*item) for item in self.extras.items()],
        }

        if self.description:
            result['description'] = mdstrip(self.description)

        if self.license and self.license.url:
            result['license'] = self.license.url

        if self.organization:
            author = self.organization.json_ld
        elif self.owner:
            author = self.owner.json_ld
        else:
            author = None

        if author:
            result['author'] = author

        return result
Example #6
0
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(
            endpoint_for('datasets.show_redirect',
                         'api.dataset',
                         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.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
Example #7
0
def user_to_rdf(user, graph=None):
    '''
    Map a Resource domain model to a DCAT/RDF graph
    '''
    graph = graph or Graph(namespace_manager=namespace_manager)
    if user.id:
        user_url = endpoint_for('users.show_redirect', 'api.user', 
                                user=user.id, _external=True)
        id = URIRef(user_url)
    else:
        id = BNode()
    o = graph.resource(id)
    o.set(RDF.type, FOAF.Person)
    o.set(FOAF.name, Literal(user.fullname))
    o.set(RDFS.label, Literal(user.fullname))
    if user.website:
        o.set(FOAF.homepage, URIRef(user.website))
    return o
Example #8
0
def organization_to_rdf(org, graph=None):
    '''
    Map a Resource domain model to a DCAT/RDF graph
    '''
    graph = graph or Graph(namespace_manager=namespace_manager)
    if org.id:
        org_url = endpoint_for('organizations.show_redirect', 'api.organization',
                                org=org.id, _external=True)
        id = URIRef(org_url)
    else:
        id = BNode()
    o = graph.resource(id)
    o.set(RDF.type, FOAF.Organization)
    o.set(FOAF.name, Literal(org.name))
    o.set(RDFS.label, Literal(org.name))
    if org.url:
        o.set(FOAF.homepage, URIRef(org.url))

    return o
Example #9
0
 def url_for(self, *args, **kwargs):
     return endpoint_for('reuses.show',
                         'api.reuse',
                         reuse=self,
                         *args,
                         **kwargs)
Example #10
0
 def external_url(self):
     return endpoint_for('territories.territory',
                         territory=self,
                         _external=True)
Example #11
0
 def url(self):
     return endpoint_for('territories.territory', territory=self)
Example #12
0
 def url_for(self, *args, **kwargs):
     return endpoint_for('users.show',
                         'api.user',
                         user=self,
                         *args,
                         **kwargs)
Example #13
0
 def url_for(self, *args, **kwargs):
     return endpoint_for('datasets.show',
                         'api.dataset',
                         dataset=self,
                         *args,
                         **kwargs)
Example #14
0
 def url_for(self, *args, **kwargs):
     return endpoint_for('organizations.show',
                         'api.organization',
                         org=self,
                         *args,
                         **kwargs)
Example #15
0
 def output(self, key, obj, **kwargs):
     return endpoint_for(self.endpoint,
                         self.fallback_endpoint,
                         _external=True,
                         **self.mapper(obj))