# HG changeset patch # User Denis Laxalde # Date 1484060935 -3600 # Node ID 8de62610cea26404646e01b0cdd958f1c616b524 # Parent ef6b18c56b5aa7a0615dee4d15d30b186941be1d [entities] Introduce an IDublinCore adapter Move all dc_ methods from AnyEntity to the new IDublinCore adapter and proxy them through a __getattr__ method on AnyEntity. Current test suite should be enough. Closes #3119992. diff -r ef6b18c56b5a -r 8de62610cea2 cubicweb/entities/__init__.py --- a/cubicweb/entities/__init__.py Fri Jan 27 11:05:07 2017 +0100 +++ b/cubicweb/entities/__init__.py Tue Jan 10 16:08:55 2017 +0100 @@ -98,57 +98,15 @@ # meta data api ########################################################### - def dc_title(self): - """return a suitable *unicode* title for this entity""" - for rschema, attrschema in self.e_schema.attribute_definitions(): - if rschema.meta: - continue - value = self.cw_attr_value(rschema.type) - if value is not None: - # make the value printable (dates, floats, bytes, etc.) - return self.printable_value(rschema.type, value, attrschema.type, - format='text/plain') - return u'%s #%s' % (self.dc_type(), self.eid) - - def dc_long_title(self): - """return a more detailled title for this entity""" - return self.dc_title() - - def dc_description(self, format='text/plain'): - """return a suitable description for this entity""" - if 'description' in self.e_schema.subjrels: - return self.printable_value('description', format=format) - return u'' - - def dc_authors(self): - """return a suitable description for the author(s) of the entity""" - try: - return ', '.join(u.name() for u in self.owned_by) - except Unauthorized: - return u'' - - def dc_creator(self): - """return a suitable description for the creator of the entity""" - if self.creator: - return self.creator.name() - return u'' - - def dc_date(self, date_format=None):# XXX default to ISO 8601 ? - """return latest modification date of this entity""" - return self._cw.format_date(self.modification_date, date_format=date_format) - - def dc_type(self, form=''): - """return the display name for the type of this entity (translated)""" - return self.e_schema.display_name(self._cw, form) - - def dc_language(self): - """return language used by this entity (translated)""" - # check if entities has internationalizable attributes - # XXX one is enough or check if all String attributes are internationalizable? - for rschema, attrschema in self.e_schema.attribute_definitions(): - if rschema.rdef(self.e_schema, attrschema).internationalizable: - return self._cw._(self._cw.user.property_value('ui.language')) - return self._cw._(self._cw.vreg.property_value('ui.language')) + def __getattr__(self, name): + prefix = 'dc_' + if name.startswith(prefix): + # Proxy to IDublinCore adapter for bw compat. + adapted = self.cw_adapt_to('IDublinCore') + method = name[len(prefix):] + if hasattr(adapted, method): + return getattr(adapted, method) + raise AttributeError(name) @property def creator(self): diff -r ef6b18c56b5a -r 8de62610cea2 cubicweb/entities/adapters.py --- a/cubicweb/entities/adapters.py Fri Jan 27 11:05:07 2017 +0100 +++ b/cubicweb/entities/adapters.py Tue Jan 10 16:08:55 2017 +0100 @@ -25,10 +25,71 @@ from logilab.mtconverter import TransformError from logilab.common.decorators import cached -from cubicweb import ValidationError, view, ViolatedConstraint, UniqueTogetherError +from cubicweb import (Unauthorized, ValidationError, view, ViolatedConstraint, + UniqueTogetherError) from cubicweb.predicates import is_instance, relation_possible, match_exception +class IDublinCoreAdapter(view.EntityAdapter): + __regid__ = 'IDublinCore' + __select__ = is_instance('Any') + + def title(self): + """Return a suitable *unicode* title for entity""" + entity = self.entity + for rschema, attrschema in entity.e_schema.attribute_definitions(): + if rschema.meta: + continue + value = entity.cw_attr_value(rschema.type) + if value is not None: + # make the value printable (dates, floats, bytes, etc.) + return entity.printable_value( + rschema.type, value, attrschema.type, format='text/plain') + return u'%s #%s' % (self.type(), entity.eid) + + def long_title(self): + """Return a more detailled title for entity""" + return self.title() + + def description(self, format='text/plain'): + """Return a suitable description for entity""" + if 'description' in self.entity.e_schema.subjrels: + return self.entity.printable_value('description', format=format) + return u'' + + def authors(self): + """Return a suitable description for the author(s) of the entity""" + try: + return u', '.join(u.name() for u in self.entity.owned_by) + except Unauthorized: + return u'' + + def creator(self): + """Return a suitable description for the creator of the entity""" + if self.entity.creator: + return self.entity.creator.name() + return u'' + + def date(self, date_format=None): # XXX default to ISO 8601 ? + """Return latest modification date of entity""" + return self._cw.format_date(self.entity.modification_date, + date_format=date_format) + + def type(self, form=''): + """Return the display name for the type of entity (translated)""" + return self.entity.e_schema.display_name(self._cw, form) + + def language(self): + """Return language used by this entity (translated)""" + eschema = self.entity.e_schema + # check if entities has internationalizable attributes + # XXX one is enough or check if all String attributes are internationalizable? + for rschema, attrschema in eschema.attribute_definitions(): + if rschema.rdef(eschema, attrschema).internationalizable: + return self._cw._(self._cw.user.property_value('ui.language')) + return self._cw._(self._cw.vreg.property_value('ui.language')) + + class IEmailableAdapter(view.EntityAdapter): __regid__ = 'IEmailable' __select__ = relation_possible('primary_email') | relation_possible('use_email')