Commit 41ab8893 authored by Simon Chabot's avatar Simon Chabot
Browse files

[Adapters, rdf] Use RDF adapters and triples function

parent a45cb1f731eb
......@@ -9,13 +9,95 @@
from logilab.common.date import todate
from rdflib import URIRef, Literal
from cubicweb.entities import AnyEntity, fetch_config
from cubicweb.entities.adapters import (
EntityRDFAdapter, CWUserRDFAdapter as _CWUserRDFAdapter,
)
from cubicweb.view import EntityAdapter
from cubicweb.predicates import is_instance
__docformat__ = "restructuredtext en"
class CWUserRDFAdapter(_CWUserRDFAdapter):
def triples(self):
yield from super().triples()
uri = URIRef(self.uri)
blog_entries = self._cw.find("BlogEntry", created_by=self.entity)
for entry in blog_entries.entities():
yield (uri, NAMESPACES["sioc"].creator_of, URIRef(entry.cwuri))
class BlogRDFAdapter(EntityRDFAdapter):
__regid__ = "rdf"
__select__ = is_instance("Blog")
def triples(self):
SIOC = NAMESPACES["sioc"]
RDF = NAMESPACES["rdf"]
DCTERMS = NAMESPACES["dcterms"]
uri = URIRef(self.uri)
yield (uri, RDF.type, SIOC.Weblog)
yield (uri, DCTERMS.title, Literal(self.entity.dc_title()))
yield (uri, DCTERMS.description, Literal(self.entity.description))
yield (uri, SIOC.num_items, Literal(len(self.entity.reverse_entry_of)))
yield (uri, SIOC.feed, URIRef(self.entity.cw_adapt_to('IFeed').rss_feed_url()))
if self.entity.creator:
yield (uri, SIOC.has_creator, URIRef(self.entity.creator.cwuri))
for blog_entry in self.entity.reverse_entry_of:
yield (uri, SIOC.container_of, URIRef(blog_entry.cwuri))
class BlogEntryRDFAdapter(EntityRDFAdapter):
__regid__ = "rdf"
__select__ = is_instance("BlogEntry")
def triples(self):
SIOC = NAMESPACES["sioc"]
RDF = NAMESPACES["rdf"]
DCTERMS = NAMESPACES["dcterms"]
uri = URIRef(self.uri)
yield (uri, RDF.type, SIOC.BlogPost)
yield (uri, DCTERMS.title, Literal(self.entity.dc_title()))
yield (uri, DCTERMS.date, Literal(self.entity.creation_date))
yield (uri, DCTERMS.modified, Literal(self.entity.modification_date))
yield (uri, SIOC.content, Literal(self.entity.content))
if self.entity.entry_of:
yield (uri, SIOC.container, URIRef(self.entity.entry_of[0].cwuri))
if self.entity.creator:
yield (uri, SIOC.has_owner, URIRef(self.entity.creator.cwuri))
class MicroBlogRDFAdapter(BlogRDFAdapter):
__regid__ = "rdf"
__select__ = is_instance("MicroBlog")
def triples(self):
yield from super().triples()
SIOC = NAMESPACES["sioc"]
RDF = NAMESPACES["rdf"]
uri = URIRef(self.uri)
yield(uri, RDF.type, SIOC.Microblog)
class MicroBlogEntryRDFAdapter(BlogEntryRDFAdapter):
__regid__ = "rdf"
__select__ = is_instance("MicroBlogEntry")
def triples(self):
yield from super().triples()
SIOC = NAMESPACES["sioc"]
RDF = NAMESPACES["rdf"]
uri = URIRef(self.uri)
yield (uri, RDF.type, SIOC.MicroblogPost)
###################################################
class BlogIFeedAdapter(EntityAdapter):
__regid__ = 'IFeed'
__select__ = is_instance('Blog', 'MicroBlog')
......@@ -110,3 +192,11 @@ class BlogEntryISiocItemAdapter(EntityAdapter):
def isioc_topics(self):
# XXX link to tags, folders?
return []
def registration_callback(vreg):
vreg.register(BlogRDFAdapter)
vreg.register(BlogEntryRDFAdapter)
vreg.register(MicroBlogRDFAdapter)
vreg.register(MicroBlogEntryRDFAdapter)
vreg.register_and_replace(CWUserRDFAdapter, _CWUserRDFAdapter)
#!/usr/bin/env python3
# coding: utf-8
from rdflib import Namespace
from cubicweb.rdf import NAMESPACES
NAMESPACES["sioc"] = Namespace("http://rdfs.org/sioc/types#")
NAMESPACES["dcterms"] = Namespace("http://purl.org/dc/terms/")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment