cubicweb/entities/adapters.py
changeset 11946 8de62610cea2
parent 11767 432f87a63057
child 12160 608481168432
equal deleted inserted replaced
11945:ef6b18c56b5a 11946:8de62610cea2
    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 ValidationError, view, ViolatedConstraint, UniqueTogetherError
    28 from cubicweb import (Unauthorized, ValidationError, view, ViolatedConstraint,
       
    29                       UniqueTogetherError)
    29 from cubicweb.predicates import is_instance, relation_possible, match_exception
    30 from cubicweb.predicates import is_instance, relation_possible, match_exception
       
    31 
       
    32 
       
    33 class IDublinCoreAdapter(view.EntityAdapter):
       
    34     __regid__ = 'IDublinCore'
       
    35     __select__ = is_instance('Any')
       
    36 
       
    37     def title(self):
       
    38         """Return a suitable *unicode* title for entity"""
       
    39         entity = self.entity
       
    40         for rschema, attrschema in entity.e_schema.attribute_definitions():
       
    41             if rschema.meta:
       
    42                 continue
       
    43             value = entity.cw_attr_value(rschema.type)
       
    44             if value is not None:
       
    45                 # make the value printable (dates, floats, bytes, etc.)
       
    46                 return entity.printable_value(
       
    47                     rschema.type, value, attrschema.type, format='text/plain')
       
    48         return u'%s #%s' % (self.type(), entity.eid)
       
    49 
       
    50     def long_title(self):
       
    51         """Return a more detailled title for entity"""
       
    52         return self.title()
       
    53 
       
    54     def description(self, format='text/plain'):
       
    55         """Return a suitable description for entity"""
       
    56         if 'description' in self.entity.e_schema.subjrels:
       
    57             return self.entity.printable_value('description', format=format)
       
    58         return u''
       
    59 
       
    60     def authors(self):
       
    61         """Return a suitable description for the author(s) of the entity"""
       
    62         try:
       
    63             return u', '.join(u.name() for u in self.entity.owned_by)
       
    64         except Unauthorized:
       
    65             return u''
       
    66 
       
    67     def creator(self):
       
    68         """Return a suitable description for the creator of the entity"""
       
    69         if self.entity.creator:
       
    70             return self.entity.creator.name()
       
    71         return u''
       
    72 
       
    73     def date(self, date_format=None):  # XXX default to ISO 8601 ?
       
    74         """Return latest modification date of entity"""
       
    75         return self._cw.format_date(self.entity.modification_date,
       
    76                                     date_format=date_format)
       
    77 
       
    78     def type(self, form=''):
       
    79         """Return the display name for the type of entity (translated)"""
       
    80         return self.entity.e_schema.display_name(self._cw, form)
       
    81 
       
    82     def language(self):
       
    83         """Return language used by this entity (translated)"""
       
    84         eschema = self.entity.e_schema
       
    85         # check if entities has internationalizable attributes
       
    86         # XXX one is enough or check if all String attributes are internationalizable?
       
    87         for rschema, attrschema in eschema.attribute_definitions():
       
    88             if rschema.rdef(eschema, attrschema).internationalizable:
       
    89                 return self._cw._(self._cw.user.property_value('ui.language'))
       
    90         return self._cw._(self._cw.vreg.property_value('ui.language'))
    30 
    91 
    31 
    92 
    32 class IEmailableAdapter(view.EntityAdapter):
    93 class IEmailableAdapter(view.EntityAdapter):
    33     __regid__ = 'IEmailable'
    94     __regid__ = 'IEmailable'
    34     __select__ = relation_possible('primary_email') | relation_possible('use_email')
    95     __select__ = relation_possible('primary_email') | relation_possible('use_email')