entities/__init__.py
author sylvain.thenault@logilab.fr
Mon, 23 Mar 2009 18:20:23 +0100
branchtls-sprint
changeset 1134 f885df228fc0
parent 1107 961a478593a5
child 1149 1e19b6ef53a1
permissions -rw-r--r--
remove some bw compat code
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
    def add_related_schemas(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
        """this is actually used ui method to generate 'addrelated' actions from
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
        the schema.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
        If you're using explicit 'addrelated' actions for an entity types, you
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
        should probably overrides this method to return an empty list else you
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
        may get some unexpected actions.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   212
        req = self.req
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
        eschema = self.e_schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
        for role, rschemas in (('subject', eschema.subject_relations()),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   215
                               ('object', eschema.object_relations())):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
            for rschema in rschemas:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
                if rschema.is_final():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
                # check the relation can be added as well
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
                if role == 'subject'and not rschema.has_perm(req, 'add', fromeid=self.eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
                if role == 'object'and not rschema.has_perm(req, 'add', toeid=self.eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
                # check the target types can be added as well
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
                for teschema in rschema.targets(eschema, role):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
                    if not self.relation_mode(rschema, teschema, role) == 'create':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
                        continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
                    if teschema.has_local_role('add') or teschema.has_perm(req, 'add'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
                        yield rschema, teschema, role
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
    def relation_mode(self, rtype, targettype, role='subject'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
        """return a string telling if the given relation is usually created
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
        to a new entity ('create' mode) or to an existant entity ('link' mode)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
        return self.rtags.get_mode(rtype, targettype, role)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   237
    # edition helper functions ################################################    
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
    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
   240
        """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
   241
        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
   242
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   243
        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
   244
        if `remove` is True (by default).
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   245
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   246
        try:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   247
            return self.__linkto[(rtype, target)]
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   248
        except AttributeError:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   249
            self.__linkto = {}
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   250
        except KeyError:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   251
            pass
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   252
        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
   253
        linkedto = []
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   254
        for linkto in linktos[:]:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   255
            ltrtype, eid, lttarget = linkto.split(':')
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   256
            if rtype == ltrtype and target == lttarget:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   257
                # 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
   258
                # hidden input
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   259
                if remove:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   260
                    linktos.remove(linkto)
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   261
                    self.req.form['__linkto'] = linktos
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   262
                linkedto.append(typed_eid(eid))
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   263
        self.__linkto[(rtype, target)] = linkedto
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   264
        return linkedto
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
    # edit controller callbacks ###############################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   267
    
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   268
    def after_deletion_path(self):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   269
        """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
   270
        information when this entity is being deleted
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   271
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   272
        return str(self.e_schema).lower(), {}
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   273
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   274
    def pre_web_edit(self):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   275
        """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
   276
        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
   277
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   278
        Do nothing by default.
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   279
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   280
        pass
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   281
    
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   282
    # server side helpers #####################################################
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   283
    
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   284
    def notification_references(self, view):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   285
        """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
   286
        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
   287
        
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   288
        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
   289
        of previously sent email
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   290
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   291
        return ()
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   292
            
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   293
    # 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
   294
    
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   295
    @classmethod
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   296
    def get_widget(cls, rschema, x='subject'):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   297
        """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
   298
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   299
        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
   300
        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
   301
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   302
        # 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
   303
        from cubicweb.web.widgets import widget
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   304
        if isinstance(rschema, basestring):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   305
            rschema = cls.schema.rschema(rschema)
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   306
        if x == 'subject':
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   307
            tschema = rschema.objects(cls.e_schema)[0]
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   308
            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
   309
        else:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   310
            tschema = rschema.subjects(cls.e_schema)[0]
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   311
            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
   312
        return wdg
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   313
    def relations_by_category(self, categories=None, permission=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
        if categories is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   315
            if not isinstance(categories, (list, tuple, set, frozenset)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   316
                categories = (categories,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   317
            if not isinstance(categories, (set, frozenset)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   318
                categories = frozenset(categories)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   319
        eschema, rtags  = self.e_schema, self.rtags
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   320
        if self.has_eid():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
            eid = self.eid
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
            eid = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
        for rschema, targetschemas, role in eschema.relation_definitions(True):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
            if rschema in ('identity', 'has_text'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   327
            # check category first, potentially lower cost than checking
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   328
            # permission which may imply rql queries
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   329
            if categories is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   330
                targetschemas = [tschema for tschema in targetschemas
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
                                 if rtags.get_tags(rschema.type, tschema.type, role).intersection(categories)]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
                if not targetschemas:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   333
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   334
            tags = rtags.get_tags(rschema.type, role=role)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
            if permission is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   336
                # tag allowing to hijack the permission machinery when
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   337
                # permission is not verifiable until the entity is actually
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   338
                # created...
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   339
                if eid is None and ('%s_on_new' % permission) in tags:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   340
                    yield (rschema, targetschemas, role)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   341
                    continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
                if rschema.is_final():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   343
                    if not rschema.has_perm(self.req, permission, eid):
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
                elif role == 'subject':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   346
                    if not ((eid is None and rschema.has_local_role(permission)) or
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   347
                            rschema.has_perm(self.req, permission, fromeid=eid)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   348
                        continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   349
                    # on relation with cardinality 1 or ?, we need delete perm as well
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   350
                    # if the relation is already set
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   351
                    if (permission == 'add'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   352
                        and rschema.cardinality(eschema, targetschemas[0], role) in '1?'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
                        and self.has_eid() and self.related(rschema.type, role)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   354
                        and not rschema.has_perm(self.req, 'delete', fromeid=eid,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   355
                                                 toeid=self.related(rschema.type, role)[0][0])):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   356
                        continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   357
                elif role == 'object':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   358
                    if not ((eid is None and rschema.has_local_role(permission)) or
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   359
                            rschema.has_perm(self.req, permission, toeid=eid)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   360
                        continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   361
                    # on relation with cardinality 1 or ?, we need delete perm as well
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   362
                    # if the relation is already set
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   363
                    if (permission == 'add'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
                        and rschema.cardinality(targetschemas[0], eschema, role) in '1?'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   365
                        and self.has_eid() and self.related(rschema.type, role)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   366
                        and not rschema.has_perm(self.req, 'delete', toeid=eid,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   367
                                                 fromeid=self.related(rschema.type, role)[0][0])):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   368
                        continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   369
            yield (rschema, targetschemas, role)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   370
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   371
    def srelations_by_category(self, categories=None, permission=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   372
        result = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   373
        for rschema, ttypes, target in self.relations_by_category(categories,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   374
                                                                  permission):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
            if rschema.is_final():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
            result.append( (rschema.display_name(self.req, target), rschema, target) )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
        return sorted(result)
912
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   379
    
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   380
    def _default_format(self):
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   381
        return self.req.property_value('ui.default-text-format')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
                
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   383
    def attribute_values(self, attrname):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   384
        if self.has_eid() or attrname in self:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   385
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   386
                values = self[attrname]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
            except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
                values = getattr(self, attrname)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   389
            # actual relation return a list of entities
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   390
            if isinstance(values, list):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   391
                return [v.eid for v in values]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   392
            return (values,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   393
        # the entity is being created, try to find default value for
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   394
        # this attribute
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   396
            values = self.req.form[attrname]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   397
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   398
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   399
                values = self[attrname] # copying
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   400
            except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   401
                values = getattr(self, 'default_%s' % attrname,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   402
                                 self.e_schema.default(attrname))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   403
                if callable(values):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   404
                    values = values()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   405
        if values is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   406
            values = ()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   407
        elif not isinstance(values, (list, tuple)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   408
            values = (values,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   409
        return values
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   410
912
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   411
    def use_fckeditor(self, attr):
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   412
        """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
   413
        `attr`, according to user preferences
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   414
        """
1107
961a478593a5 has_metadata is a schema method
sylvain.thenault@logilab.fr
parents: 1101
diff changeset
   415
        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
   416
            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
   417
                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
   418
            return self.req.property_value('ui.default-text-format') == 'text/html'
912
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   419
        return False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   420
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   421
# 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
   422
#       configuration and we need the actual reference to avoid infinite loops
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   423
#       in mro
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   424
ANYENTITY = AnyEntity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   425
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   426
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   427
    if pclass is ANYENTITY:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   428
        pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
    if pclass is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   430
        fetchattrs += pclass.fetch_attrs
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   431
    if mainattr is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   432
        mainattr = fetchattrs[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   433
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   434
    def fetch_order(cls, attr, var):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   435
        if attr == mainattr:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   436
            return '%s %s' % (var, order)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   437
        return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   438
    return fetchattrs, fetch_order