[wip, rdf] Add rdf adapters draft
authorSimon Chabot <simon.chabot@logilab.fr>
Fri, 14 Feb 2020 18:03:54 +0100
changeset 399 dceb8fb22e57
parent 396 f704e6feab2d
child 400 8eb729fccb8b
[wip, rdf] Add rdf adapters
cubicweb_blog/__pkginfo__.py
cubicweb_blog/entities.py
cubicweb_blog/rdf.py
--- a/cubicweb_blog/__pkginfo__.py	Tue Nov 26 10:51:20 2019 +0100
+++ b/cubicweb_blog/__pkginfo__.py	Fri Feb 14 18:03:54 2020 +0100
@@ -20,7 +20,7 @@
     'Programming Language :: JavaScript',
 ]
 
-__depends__ = {'cubicweb': '>= 3.24.0',
+__depends__ = {'cubicweb[rdf]': '>= 3.28.0',
                'six': '>= 1.4.0', }
 __recommends__ = {'cubicweb-tag': None,
                   'cubicweb-preview': None,
--- a/cubicweb_blog/entities.py	Tue Nov 26 10:51:20 2019 +0100
+++ b/cubicweb_blog/entities.py	Fri Feb 14 18:03:54 2020 +0100
@@ -10,12 +10,76 @@
 from logilab.common.date import todate
 
 from cubicweb.entities import AnyEntity, fetch_config
+from cubicweb.entities.adapters import (
+    EntityRDFAdapter, CWUserFoafAdapter as _CWUserFoafAdapter,
+)
 from cubicweb.view import EntityAdapter
 from cubicweb.predicates import is_instance
 
+from cubicweb_blog.rdf import NS_VARS
+
 __docformat__ = "restructuredtext en"
 
 
+class CWUserFoafAdapter(_CWUserFoafAdapter):
+    def relations(self):
+        yield from super().relations()
+        blog_entries = self._cw.find("BlogEntry", created_by=self.entity)
+        for entry in blog_entries.entities():
+            yield ("sioc", "creator_of", entry.cwuri)
+
+
+class BlogSiocAdapter(EntityRDFAdapter):
+    __regid__ = "rdf.sioc"
+    __select__ = is_instance("Blog")
+    rdf_type = NS_VARS["sioc"] + "Weblog"
+
+    def properties(self):
+        yield ("dcterms", "title", self.entity.dc_title(), {})
+        yield ("dcterms", "description", self.entity.description, {})
+        yield ("sioc", "num_items", len(self.entity.reverse_entry_of), {})
+        yield ("sioc", "feed", self.entity.cw_adapt_to('IFeed').rss_feed_url(), {})
+
+    def relations(self):
+        if self.entity.creator:
+            yield ("sioc", "has_creator", self.entity.creator.cwuri)
+
+        for blog_entry in self.entity.reverse_entry_of:
+            yield ("sioc", "container_of", blog_entry.cwuri)
+
+
+class BlogEntrySiocAdapter(EntityRDFAdapter):
+    __regid__ = "rdf.sioc"
+    __select__ = is_instance("BlogEntry")
+    rdf_type = NS_VARS["sioc"] + "BlogPost"
+
+    def properties(self):
+        yield ("dcterms", "title", self.entity.dc_title(), {})
+        yield ("dcterms", "date", self.entity.creation_date, {})
+        yield ("dcterms", "modified", self.entity.modification_date, {})
+        yield ("sioc", "content", self.entity.content, {})
+
+    def relations(self):
+        if self.entity.entry_of:
+            yield ("sioc", "container", self.entity.entry_of[0].cwuri)
+        if self.entity.creator:
+            yield ("sioc", "has_owner", self.entity.creator.cwuri)
+
+
+class MicroBlogSiocAdapter(BlogSiocAdapter):
+    __regid__ = "rdf.sioc"
+    __select__ = is_instance("MicroBlog")
+    rdf_type = NS_VARS["sioc"] + "Microblog"
+
+
+class MicroBlogEntrySiocAdapter(BlogEntrySiocAdapter):
+    __regid__ = "rdf.sioc"
+    __select__ = is_instance("MicroBlogEntry")
+    rdf_type = NS_VARS["sioc"] + "MicroblogPost"
+
+
+###################################################
+
 class BlogIFeedAdapter(EntityAdapter):
     __regid__ = 'IFeed'
     __select__ = is_instance('Blog', 'MicroBlog')
@@ -110,3 +174,11 @@
     def isioc_topics(self):
         # XXX link to tags, folders?
         return []
+
+
+def registration_callback(vreg):
+    vreg.register(BlogSiocAdapter)
+    vreg.register(BlogEntrySiocAdapter)
+    vreg.register(MicroBlogSiocAdapter)
+    vreg.register(MicroBlogEntrySiocAdapter)
+    vreg.register_and_replace(CWUserFoafAdapter, _CWUserFoafAdapter)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cubicweb_blog/rdf.py	Fri Feb 14 18:03:54 2020 +0100
@@ -0,0 +1,13 @@
+#!/usr/bin/env python3
+# coding: utf-8
+
+from rdflib import Namespace
+
+from cubicweb.rdf import ETYPES_ADAPTERS, NS_VARS
+
+NS_VARS["sioc"] = Namespace("http://rdfs.org/sioc/types#")
+NS_VARS["dcterms"] = Namespace("http://purl.org/dc/terms/")
+
+ETYPES_ADAPTERS["Blog"] = ("rdf.sioc", )
+ETYPES_ADAPTERS["BlogEntry"] = ("rdf.sioc", )
+ETYPES_ADAPTERS["MicroBlog"] = ("rdf.sioc", )