cubicweb/entities/adapters.py
changeset 12892 0df0db725f07
parent 12891 eb0cd6060062
child 12910 c87c3943d6ab
--- a/cubicweb/entities/adapters.py	Fri Feb 14 18:15:55 2020 +0100
+++ b/cubicweb/entities/adapters.py	Fri Feb 28 17:11:01 2020 +0100
@@ -20,8 +20,11 @@
 """
 from cubicweb import _
 
+from hashlib import sha1
 from itertools import chain
 
+from rdflib import URIRef, Literal
+
 from logilab.mtconverter import TransformError
 from logilab.common.decorators import cached, cachedproperty
 
@@ -31,6 +34,8 @@
 from cubicweb.schema import constraint_name_for
 from cubicweb.predicates import is_instance, relation_possible, match_exception
 
+from cubicweb.rdf import NAMESPACES
+
 
 class EntityRDFAdapter(EntityAdapter):
     """EntityRDFAdapter is to be specialized for each entity that wants to
@@ -51,6 +56,25 @@
         raise NotImplementedError()
 
 
+class CWUserFoafAdapter(EntityRDFAdapter):
+    __regid__ = "rdf.foaf"
+    __select__ = is_instance("CWUser")
+
+    def triples(self):
+        RDF = NAMESPACES["rdf"]
+        FOAF = NAMESPACES["foaf"]
+        uri = URIRef(self.uri)
+        yield (uri, RDF.type, FOAF.Person)
+        if self.entity.surname:
+            yield (uri, FOAF.familyName, Literal(self.entity.surname))
+        if self.entity.firstname:
+            yield (uri, FOAF.givenName, Literal(self.entity.firstname))
+        emailaddr = self.entity.cw_adapt_to("IEmailable").get_email()
+        if emailaddr:
+            email_digest = sha1(emailaddr.encode("utf-8")).hexdigest()
+            yield (uri, FOAF.mbox_sha1sum, Literal(email_digest))
+
+
 class IDublinCoreAdapter(EntityAdapter):
     __regid__ = 'IDublinCore'
     __select__ = is_instance('Any')