cubicweb/entities/adapters.py
changeset 12880 59d4ad7e7df3
parent 12512 661dd0436c01
child 12891 eb0cd6060062
equal deleted inserted replaced
12879:7347715bf0ee 12880:59d4ad7e7df3
    23 from itertools import chain
    23 from itertools import chain
    24 
    24 
    25 from logilab.mtconverter import TransformError
    25 from logilab.mtconverter import TransformError
    26 from logilab.common.decorators import cached
    26 from logilab.common.decorators import cached
    27 
    27 
    28 from cubicweb import (Unauthorized, ValidationError, view, ViolatedConstraint,
    28 from cubicweb.entity import EntityAdapter
       
    29 from cubicweb import (Unauthorized, ValidationError, ViolatedConstraint,
    29                       UniqueTogetherError)
    30                       UniqueTogetherError)
    30 from cubicweb.schema import constraint_name_for
    31 from cubicweb.schema import constraint_name_for
    31 from cubicweb.predicates import is_instance, relation_possible, match_exception
    32 from cubicweb.predicates import is_instance, relation_possible, match_exception
    32 
    33 
    33 
    34 
    34 class IDublinCoreAdapter(view.EntityAdapter):
    35 class IDublinCoreAdapter(EntityAdapter):
    35     __regid__ = 'IDublinCore'
    36     __regid__ = 'IDublinCore'
    36     __select__ = is_instance('Any')
    37     __select__ = is_instance('Any')
    37 
    38 
    38     def title(self):
    39     def title(self):
    39         """Return a suitable *unicode* title for entity"""
    40         """Return a suitable *unicode* title for entity"""
    91             if rschema.rdef(eschema, attrschema).internationalizable:
    92             if rschema.rdef(eschema, attrschema).internationalizable:
    92                 return self._cw._(self._cw.user.property_value('ui.language'))
    93                 return self._cw._(self._cw.user.property_value('ui.language'))
    93         return self._cw._(self._cw.vreg.property_value('ui.language'))
    94         return self._cw._(self._cw.vreg.property_value('ui.language'))
    94 
    95 
    95 
    96 
    96 class IEmailableAdapter(view.EntityAdapter):
    97 class IEmailableAdapter(EntityAdapter):
    97     __regid__ = 'IEmailable'
    98     __regid__ = 'IEmailable'
    98     __select__ = relation_possible('primary_email') | relation_possible('use_email')
    99     __select__ = relation_possible('primary_email') | relation_possible('use_email')
    99 
   100 
   100     def get_email(self):
   101     def get_email(self):
   101         if getattr(self.entity, 'primary_email', None):
   102         if getattr(self.entity, 'primary_email', None):
   124         """
   125         """
   125         return dict((attr, getattr(self.entity, attr))
   126         return dict((attr, getattr(self.entity, attr))
   126                     for attr in self.allowed_massmail_keys())
   127                     for attr in self.allowed_massmail_keys())
   127 
   128 
   128 
   129 
   129 class INotifiableAdapter(view.EntityAdapter):
   130 class INotifiableAdapter(EntityAdapter):
   130     __regid__ = 'INotifiable'
   131     __regid__ = 'INotifiable'
   131     __select__ = is_instance('Any')
   132     __select__ = is_instance('Any')
   132 
   133 
   133     def notification_references(self, view):
   134     def notification_references(self, view):
   134         """used to control References field of email send on notification
   135         """used to control References field of email send on notification
   143         if view.msgid_timestamp:
   144         if view.msgid_timestamp:
   144             return (self.entity.eid,)
   145             return (self.entity.eid,)
   145         return ()
   146         return ()
   146 
   147 
   147 
   148 
   148 class IFTIndexableAdapter(view.EntityAdapter):
   149 class IFTIndexableAdapter(EntityAdapter):
   149     """standard adapter to handle fulltext indexing
   150     """standard adapter to handle fulltext indexing
   150 
   151 
   151     .. automethod:: cubicweb.entities.adapters.IFTIndexableAdapter.fti_containers
   152     .. automethod:: cubicweb.entities.adapters.IFTIndexableAdapter.fti_containers
   152     .. automethod:: cubicweb.entities.adapters.IFTIndexableAdapter.get_words
   153     .. automethod:: cubicweb.entities.adapters.IFTIndexableAdapter.get_words
   153     """
   154     """
   224 def merge_weight_dict(maindict, newdict):
   225 def merge_weight_dict(maindict, newdict):
   225     for weight, words in newdict.items():
   226     for weight, words in newdict.items():
   226         maindict.setdefault(weight, []).extend(words)
   227         maindict.setdefault(weight, []).extend(words)
   227 
   228 
   228 
   229 
   229 class IDownloadableAdapter(view.EntityAdapter):
   230 class IDownloadableAdapter(EntityAdapter):
   230     """interface for downloadable entities"""
   231     """interface for downloadable entities"""
   231     __regid__ = 'IDownloadable'
   232     __regid__ = 'IDownloadable'
   232     __abstract__ = True
   233     __abstract__ = True
   233 
   234 
   234     def download_url(self, **kwargs):  # XXX not really part of this interface
   235     def download_url(self, **kwargs):  # XXX not really part of this interface
   254         """return actual data (bytes) of the downloadable content"""
   255         """return actual data (bytes) of the downloadable content"""
   255         raise NotImplementedError
   256         raise NotImplementedError
   256 
   257 
   257 
   258 
   258 # XXX should propose to use two different relations for children/parent
   259 # XXX should propose to use two different relations for children/parent
   259 class ITreeAdapter(view.EntityAdapter):
   260 class ITreeAdapter(EntityAdapter):
   260     """This adapter provides a tree interface.
   261     """This adapter provides a tree interface.
   261 
   262 
   262     It has to be overriden to be configured using the tree_relation,
   263     It has to be overriden to be configured using the tree_relation,
   263     child_role and parent_role class attributes to benefit from this default
   264     child_role and parent_role class attributes to benefit from this default
   264     implementation.
   265     implementation.
   410                 break
   411                 break
   411         path.reverse()
   412         path.reverse()
   412         return path
   413         return path
   413 
   414 
   414 
   415 
   415 class ISerializableAdapter(view.EntityAdapter):
   416 class ISerializableAdapter(EntityAdapter):
   416     """Adapter to serialize an entity to a bare python structure that may be
   417     """Adapter to serialize an entity to a bare python structure that may be
   417     directly serialized to e.g. JSON.
   418     directly serialized to e.g. JSON.
   418     """
   419     """
   419 
   420 
   420     __regid__ = 'ISerializable'
   421     __regid__ = 'ISerializable'
   439 
   440 
   440 
   441 
   441 # error handling adapters ######################################################
   442 # error handling adapters ######################################################
   442 
   443 
   443 
   444 
   444 class IUserFriendlyError(view.EntityAdapter):
   445 class IUserFriendlyError(EntityAdapter):
   445     __regid__ = 'IUserFriendlyError'
   446     __regid__ = 'IUserFriendlyError'
   446     __abstract__ = True
   447     __abstract__ = True
   447 
   448 
   448     def __init__(self, *args, **kwargs):
   449     def __init__(self, *args, **kwargs):
   449         self.exc = kwargs.pop('exc')
   450         self.exc = kwargs.pop('exc')