cubicweb/entities/adapters.py
changeset 12892 0df0db725f07
parent 12891 eb0cd6060062
child 12910 c87c3943d6ab
equal deleted inserted replaced
12891:eb0cd6060062 12892:0df0db725f07
    18 """some basic entity adapter implementations, for interfaces used in the
    18 """some basic entity adapter implementations, for interfaces used in the
    19 framework itself.
    19 framework itself.
    20 """
    20 """
    21 from cubicweb import _
    21 from cubicweb import _
    22 
    22 
       
    23 from hashlib import sha1
    23 from itertools import chain
    24 from itertools import chain
       
    25 
       
    26 from rdflib import URIRef, Literal
    24 
    27 
    25 from logilab.mtconverter import TransformError
    28 from logilab.mtconverter import TransformError
    26 from logilab.common.decorators import cached, cachedproperty
    29 from logilab.common.decorators import cached, cachedproperty
    27 
    30 
    28 from cubicweb.entity import EntityAdapter
    31 from cubicweb.entity import EntityAdapter
    29 from cubicweb import (Unauthorized, ValidationError, ViolatedConstraint,
    32 from cubicweb import (Unauthorized, ValidationError, ViolatedConstraint,
    30                       UniqueTogetherError)
    33                       UniqueTogetherError)
    31 from cubicweb.schema import constraint_name_for
    34 from cubicweb.schema import constraint_name_for
    32 from cubicweb.predicates import is_instance, relation_possible, match_exception
    35 from cubicweb.predicates import is_instance, relation_possible, match_exception
    33 
    36 
       
    37 from cubicweb.rdf import NAMESPACES
       
    38 
    34 
    39 
    35 class EntityRDFAdapter(EntityAdapter):
    40 class EntityRDFAdapter(EntityAdapter):
    36     """EntityRDFAdapter is to be specialized for each entity that wants to
    41     """EntityRDFAdapter is to be specialized for each entity that wants to
    37     be converted to RDF using the mechanism from cubicweb.rdf
    42     be converted to RDF using the mechanism from cubicweb.rdf
    38     """
    43     """
    47         return self.entity.cwuri
    52         return self.entity.cwuri
    48 
    53 
    49     def triples(self):
    54     def triples(self):
    50         """return sequence of 3-tuple of rdflib identifiers"""
    55         """return sequence of 3-tuple of rdflib identifiers"""
    51         raise NotImplementedError()
    56         raise NotImplementedError()
       
    57 
       
    58 
       
    59 class CWUserFoafAdapter(EntityRDFAdapter):
       
    60     __regid__ = "rdf.foaf"
       
    61     __select__ = is_instance("CWUser")
       
    62 
       
    63     def triples(self):
       
    64         RDF = NAMESPACES["rdf"]
       
    65         FOAF = NAMESPACES["foaf"]
       
    66         uri = URIRef(self.uri)
       
    67         yield (uri, RDF.type, FOAF.Person)
       
    68         if self.entity.surname:
       
    69             yield (uri, FOAF.familyName, Literal(self.entity.surname))
       
    70         if self.entity.firstname:
       
    71             yield (uri, FOAF.givenName, Literal(self.entity.firstname))
       
    72         emailaddr = self.entity.cw_adapt_to("IEmailable").get_email()
       
    73         if emailaddr:
       
    74             email_digest = sha1(emailaddr.encode("utf-8")).hexdigest()
       
    75             yield (uri, FOAF.mbox_sha1sum, Literal(email_digest))
    52 
    76 
    53 
    77 
    54 class IDublinCoreAdapter(EntityAdapter):
    78 class IDublinCoreAdapter(EntityAdapter):
    55     __regid__ = 'IDublinCore'
    79     __regid__ = 'IDublinCore'
    56     __select__ = is_instance('Any')
    80     __select__ = is_instance('Any')