entities/__init__.py
author sylvain.thenault@logilab.fr
Thu, 26 Mar 2009 19:15:57 +0100
branchtls-sprint
changeset 1150 2d1b721fded9
parent 1149 1e19b6ef53a1
child 1154 9b23a6836c32
permissions -rw-r--r--
remove rtags/methods to handle the action box from Entity, move it to the action box...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""base application's entities class implementation: `AnyEntity`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
:organization: Logilab
479
ac5c9442b1fd update some copyrights
sylvain.thenault@logilab.fr
parents: 253
diff changeset
     4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
from warnings import warn
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
from logilab.common.deprecation import deprecated_function
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
from logilab.common.decorators import cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
from cubicweb import Unauthorized, typed_eid
713
5adb6d8e5fa7 update imports of "cubicweb.common.entity" and use the new module path "cubicweb.entity"
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 479
diff changeset
    15
from cubicweb.entity import Entity
940
15dcdc863965 fix imports : common.utils -> utils
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 912
diff changeset
    16
from cubicweb.utils import dump_class
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
from cubicweb.schema import FormatConstraint
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
125
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
    19
from cubicweb.interfaces import IBreadCrumbs, IFeed
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
    20
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
class AnyEntity(Entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
    """an entity instance has e_schema automagically set on the class and
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
    instances have access to their issuing cursor
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
    id = 'Any'   
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
    __rtags__ = {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
        'is' : ('generated', 'link'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
        'is_instance_of' : ('generated', 'link'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
        'identity' : ('generated', 'link'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
        # use primary and not generated for eid since it has to be an hidden
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
        # field in edition
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
        ('eid',                '*', 'subject'): 'primary',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
        ('creation_date',      '*', 'subject'): 'generated',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
        ('modification_date',  '*', 'subject'): 'generated',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
        ('has_text',           '*', 'subject'): 'generated',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
        ('require_permission', '*', 'subject') : ('generated', 'link'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
        ('owned_by',           '*', 'subject') : ('generated', 'link'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
        ('created_by',         '*', 'subject') : ('generated', 'link'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
        ('wf_info_for',        '*', 'subject') : ('generated', 'link'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
        ('wf_info_for',        '*', 'object')  : ('generated', 'link'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
                 
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        ('description',        '*', 'subject'): 'secondary',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
        # XXX should be moved in their respective cubes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
        ('filed_under',        '*', 'subject') : ('generic', 'link'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
        ('filed_under',        '*', 'object')  : ('generic', 'create'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
        # generated since there is a componant to handle comments
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
        ('comments',           '*', 'subject') : ('generated', 'link'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
        ('comments',           '*', 'object')  : ('generated', 'link'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
        }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
125
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
    56
    __implements__ = (IBreadCrumbs, IFeed)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
    def selected(cls, etype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
        """the special Any entity is used as the default factory, so
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
        the actual class has to be constructed at selection time once we
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
        have an actual entity'type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
        if cls.id == etype:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
            return cls
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
        usercls = dump_class(cls, etype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
        usercls.id = etype
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
        usercls.__initialize__()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
        return usercls
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
    fetch_attrs = ('modification_date',)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
    def fetch_order(cls, attr, var):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        """class method used to control sort order when multiple entities of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
        this type are fetched
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
        return cls.fetch_unrelated_order(attr, var)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
    def fetch_unrelated_order(cls, attr, var):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        """class method used to control sort order when multiple entities of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
        this type are fetched to use in edition (eg propose them to create a
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
        new relation on an edited entity).
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
        if attr == 'modification_date':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
            return '%s DESC' % var
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
        return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    def __initialize__(cls): 
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
        super(ANYENTITY, cls).__initialize__() # XXX
1101
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1086
diff changeset
    92
        # set a default_ATTR method for rich text format fields
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1086
diff changeset
    93
        # XXX move this away once the old widgets have been dropped!
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        eschema = cls.e_schema
1101
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1086
diff changeset
    95
        for metaattr, (metadata, attr) in eschema.meta_attributes().iteritems():
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1086
diff changeset
    96
            if metadata == 'format' and not hasattr(cls, 'default_%s' % metaattr):
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1086
diff changeset
    97
                setattr(cls, 'default_%s' % metaattr, cls._default_format)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
    # meta data api ###########################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
    def dc_title(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        """return a suitable *unicode* title for this entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
        for rschema, attrschema in self.e_schema.attribute_definitions():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
            if rschema.meta:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
            value = self.get_value(rschema.type)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
            if value:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
                # make the value printable (dates, floats, bytes, etc.)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
                return self.printable_value(rschema.type, value, attrschema.type,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
                                            format='text/plain')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
        return u'%s #%s' % (self.dc_type(), self.eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
    def dc_long_title(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
        """return a more detailled title for this entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
        return self.dc_title()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
    def dc_description(self, format='text/plain'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
        """return a suitable description for this entity"""
253
57e88c0ba286 check we have a description *yams attribute*
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 142
diff changeset
   119
        if self.e_schema.has_subject_relation('description'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
            return self.printable_value('description', format=format)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
        return u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
    def dc_authors(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
        """return a suitable description for the author(s) of the entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
            return ', '.join(u.name() for u in self.owned_by)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
        except Unauthorized:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
            return u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
    def dc_creator(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
        """return a suitable description for the creator of the entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
        if self.creator:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
            return self.creator.name()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
        return u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
    def dc_date(self, date_format=None):# XXX default to ISO 8601 ?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
        """return latest modification date of this entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
        return self.format_date(self.modification_date, date_format=date_format)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
    def dc_type(self, form=''):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
        """return the display name for the type of this entity (translated)"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        return self.e_schema.display_name(self.req, form)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
    def dc_language(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
        """return language used by this entity (translated)"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
        # check if entities has internationalizable attributes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
        # XXX one is enough or check if all String attributes are internationalizable?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
        for rschema, attrschema in self.e_schema.attribute_definitions():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
            if rschema.rproperty(self.e_schema, attrschema,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
                                 'internationalizable'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
                return self.req._(self.req.user.property_value('ui.language'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
        return self.req._(self.vreg.property_value('ui.language'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
    @property
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
    def creator(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
        """return the EUser entity which has created this entity, or None if
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
        unknown or if the curent user doesn't has access to this euser
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
            return self.created_by[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
        except (Unauthorized, IndexError):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
            return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
    def breadcrumbs(self, view=None, recurs=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   165
        path = [self]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
        if hasattr(self, 'parent'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
            parent = self.parent()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
            if parent is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
                    path = parent.breadcrumbs(view, True) + [self]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
                except TypeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
                    warn("breadcrumbs method's now takes two arguments "
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
                         "(view=None, recurs=False), please update",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
                         DeprecationWarning)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
                    path = parent.breadcrumbs(view) + [self]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   176
        if not recurs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
            if view is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
                if 'vtitle' in self.req.form:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   179
                    # embeding for instance
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
                    path.append( self.req.form['vtitle'] )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
            elif view.id != 'primary' and hasattr(view, 'title'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
                path.append( self.req._(view.title) )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
        return path
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
125
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   185
    ## IFeed interface ########################################################
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   186
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   187
    def rss_feed_url(self):
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   188
        return self.absolute_url(vid='rss')
142
0425ee84cfa6 add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 125
diff changeset
   189
    
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   190
    # abstractions making the whole things (well, some at least) working ######
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   191
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
    def sortvalue(self, rtype=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
        """return a value which can be used to sort this entity or given
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   194
        entity's attribute
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
        if rtype is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
            return self.dc_title().lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
        value = self.get_value(rtype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
        # do not restrict to `unicode` because Bytes will return a `str` value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
        if isinstance(value, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
            return self.printable_value(rtype, format='text/plain').lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
        return value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   204
    # edition helper functions ################################################    
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   205
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   206
    def linked_to(self, rtype, target, remove=True):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   207
        """if entity should be linked to another using __linkto form param for
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   208
        the given relation/target, return eids of related entities
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   209
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   210
        This method is consuming matching link-to information from form params
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   211
        if `remove` is True (by default).
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   212
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   213
        try:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   214
            return self.__linkto[(rtype, target)]
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   215
        except AttributeError:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   216
            self.__linkto = {}
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   217
        except KeyError:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   218
            pass
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   219
        linktos = list(self.req.list_form_param('__linkto'))
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   220
        linkedto = []
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   221
        for linkto in linktos[:]:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   222
            ltrtype, eid, lttarget = linkto.split(':')
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   223
            if rtype == ltrtype and target == lttarget:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   224
                # delete __linkto from form param to avoid it being added as
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   225
                # hidden input
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   226
                if remove:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   227
                    linktos.remove(linkto)
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   228
                    self.req.form['__linkto'] = linktos
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   229
                linkedto.append(typed_eid(eid))
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   230
        self.__linkto[(rtype, target)] = linkedto
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   231
        return linkedto
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   232
    
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   233
    # edit controller callbacks ###############################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
    
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   235
    def after_deletion_path(self):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   236
        """return (path, parameters) which should be used as redirect
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   237
        information when this entity is being deleted
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   238
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   239
        return str(self.e_schema).lower(), {}
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   240
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   241
    def pre_web_edit(self):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   242
        """callback called by the web editcontroller when an entity will be
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   243
        created/modified, to let a chance to do some entity specific stuff.
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   244
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   245
        Do nothing by default.
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   246
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   247
        pass
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   248
    
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   249
    # server side helpers #####################################################
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   250
    
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   251
    def notification_references(self, view):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   252
        """used to control References field of email send on notification
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   253
        for this entity. `view` is the notification view.
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   254
        
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   255
        Should return a list of eids which can be used to generate message ids
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   256
        of previously sent email
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   257
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   258
        return ()
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   259
            
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   260
    # XXX deprecates, may be killed once old widgets system is gone ###########
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   261
    
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   262
    @classmethod
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   263
    def get_widget(cls, rschema, x='subject'):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   264
        """return a widget to view or edit a relation
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   265
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   266
        notice that when the relation support multiple target types, the widget
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   267
        is necessarily the same for all those types
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   268
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   269
        # let ImportError propage if web par isn't available
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   270
        from cubicweb.web.widgets import widget
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   271
        if isinstance(rschema, basestring):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   272
            rschema = cls.schema.rschema(rschema)
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   273
        if x == 'subject':
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   274
            tschema = rschema.objects(cls.e_schema)[0]
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   275
            wdg = widget(cls.vreg, cls, rschema, tschema, 'subject')
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   276
        else:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   277
            tschema = rschema.subjects(cls.e_schema)[0]
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   278
            wdg = widget(cls.vreg, tschema, rschema, cls, 'object')
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   279
        return wdg
1149
1e19b6ef53a1 pylint cleanup
sylvain.thenault@logilab.fr
parents: 1134
diff changeset
   280
    
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
    def relations_by_category(self, categories=None, permission=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
        if categories is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   283
            if not isinstance(categories, (list, tuple, set, frozenset)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
                categories = (categories,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   285
            if not isinstance(categories, (set, frozenset)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   286
                categories = frozenset(categories)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   287
        eschema, rtags  = self.e_schema, self.rtags
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   288
        if self.has_eid():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
            eid = self.eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   290
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   291
            eid = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   292
        for rschema, targetschemas, role in eschema.relation_definitions(True):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
            if rschema in ('identity', 'has_text'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   294
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   295
            # check category first, potentially lower cost than checking
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   296
            # permission which may imply rql queries
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   297
            if categories is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   298
                targetschemas = [tschema for tschema in targetschemas
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   299
                                 if rtags.get_tags(rschema.type, tschema.type, role).intersection(categories)]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   300
                if not targetschemas:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   302
            tags = rtags.get_tags(rschema.type, role=role)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   303
            if permission is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   304
                # tag allowing to hijack the permission machinery when
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   305
                # permission is not verifiable until the entity is actually
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   306
                # created...
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   307
                if eid is None and ('%s_on_new' % permission) in tags:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   308
                    yield (rschema, targetschemas, role)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   309
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   310
                if rschema.is_final():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   311
                    if not rschema.has_perm(self.req, permission, eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   312
                        continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   313
                elif role == 'subject':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
                    if not ((eid is None and rschema.has_local_role(permission)) or
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   315
                            rschema.has_perm(self.req, permission, fromeid=eid)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   316
                        continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   317
                    # on relation with cardinality 1 or ?, we need delete perm as well
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   318
                    # if the relation is already set
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   319
                    if (permission == 'add'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   320
                        and rschema.cardinality(eschema, targetschemas[0], role) in '1?'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
                        and self.has_eid() and self.related(rschema.type, role)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
                        and not rschema.has_perm(self.req, 'delete', fromeid=eid,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
                                                 toeid=self.related(rschema.type, role)[0][0])):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
                        continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
                elif role == 'object':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
                    if not ((eid is None and rschema.has_local_role(permission)) or
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   327
                            rschema.has_perm(self.req, permission, toeid=eid)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   328
                        continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   329
                    # on relation with cardinality 1 or ?, we need delete perm as well
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   330
                    # if the relation is already set
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
                    if (permission == 'add'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
                        and rschema.cardinality(targetschemas[0], eschema, role) in '1?'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   333
                        and self.has_eid() and self.related(rschema.type, role)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   334
                        and not rschema.has_perm(self.req, 'delete', toeid=eid,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
                                                 fromeid=self.related(rschema.type, role)[0][0])):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   336
                        continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   337
            yield (rschema, targetschemas, role)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   338
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   339
    def srelations_by_category(self, categories=None, permission=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   340
        result = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   341
        for rschema, ttypes, target in self.relations_by_category(categories,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
                                                                  permission):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   343
            if rschema.is_final():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   344
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   345
            result.append( (rschema.display_name(self.req, target), rschema, target) )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   346
        return sorted(result)
912
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   347
    
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   348
    def _default_format(self):
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   349
        return self.req.property_value('ui.default-text-format')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   350
                
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   351
    def attribute_values(self, attrname):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   352
        if self.has_eid() or attrname in self:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
                values = self[attrname]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   355
            except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   356
                values = getattr(self, attrname)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   357
            # actual relation return a list of entities
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   358
            if isinstance(values, list):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   359
                return [v.eid for v in values]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   360
            return (values,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   361
        # the entity is being created, try to find default value for
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   362
        # this attribute
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   363
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
            values = self.req.form[attrname]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   365
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   366
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   367
                values = self[attrname] # copying
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   368
            except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   369
                values = getattr(self, 'default_%s' % attrname,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   370
                                 self.e_schema.default(attrname))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   371
                if callable(values):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   372
                    values = values()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   373
        if values is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   374
            values = ()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
        elif not isinstance(values, (list, tuple)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
            values = (values,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
        return values
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
912
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   379
    def use_fckeditor(self, attr):
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   380
        """return True if fckeditor should be used to edit entity's attribute named
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   381
        `attr`, according to user preferences
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
        """
1107
961a478593a5 has_metadata is a schema method
sylvain.thenault@logilab.fr
parents: 1101
diff changeset
   383
        if self.req.use_fckeditor() and self.e_schema.has_metadata(attr, 'format'):
912
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   384
            if self.has_eid() or '%s_format' % attr in self:
1101
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1086
diff changeset
   385
                return self.attribute_metadata(attr, 'format') == 'text/html'
1013
948a3882c94a add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents: 1010
diff changeset
   386
            return self.req.property_value('ui.default-text-format') == 'text/html'
912
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   387
        return False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   389
# XXX:  store a reference to the AnyEntity class since it is hijacked in goa
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   390
#       configuration and we need the actual reference to avoid infinite loops
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   391
#       in mro
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   392
ANYENTITY = AnyEntity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   393
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   394
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
    if pclass is ANYENTITY:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   396
        pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   397
    if pclass is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   398
        fetchattrs += pclass.fetch_attrs
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   399
    if mainattr is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   400
        mainattr = fetchattrs[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   401
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   402
    def fetch_order(cls, attr, var):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   403
        if attr == mainattr:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   404
            return '%s %s' % (var, order)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   405
        return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   406
    return fetchattrs, fetch_order