entities/__init__.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Thu, 14 May 2009 16:28:57 +0200
changeset 1824 c9c492787a8a
parent 1498 2c6eec0b46b9
child 1854 9302d0e66076
permissions -rw-r--r--
Added tag cubicweb-version-3_2_0 for changeset c14231e3a4f9
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
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
    11
from logilab.common.deprecation import deprecated_function, obsolete
0
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
    """
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
    26
    id = 'Any'
125
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
    27
    __implements__ = (IBreadCrumbs, IFeed)
1417
06af20e663f2 remove spaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    28
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
    def selected(cls, etype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
        """the special Any entity is used as the default factory, so
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
        the actual class has to be constructed at selection time once we
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
        have an actual entity'type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
        if cls.id == etype:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
            return cls
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
        usercls = dump_class(cls, etype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
        usercls.id = etype
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
        usercls.__initialize__()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
        return usercls
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
    41
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    fetch_attrs = ('modification_date',)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
    def fetch_order(cls, attr, var):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        """class method used to control sort order when multiple entities of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        this type are fetched
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
        return cls.fetch_unrelated_order(attr, var)
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
    49
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
    def fetch_unrelated_order(cls, attr, var):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
        """class method used to control sort order when multiple entities of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
        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
    54
        new relation on an edited entity).
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
        if attr == 'modification_date':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
            return '%s DESC' % var
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
        return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
    @classmethod
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
    61
    def __initialize__(cls):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
        super(ANYENTITY, cls).__initialize__() # XXX
1101
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1086
diff changeset
    63
        # set a default_ATTR method for rich text format fields
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1086
diff changeset
    64
        # XXX move this away once the old widgets have been dropped!
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
        eschema = cls.e_schema
1101
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1086
diff changeset
    66
        for metaattr, (metadata, attr) in eschema.meta_attributes().iteritems():
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1086
diff changeset
    67
            if metadata == 'format' and not hasattr(cls, 'default_%s' % metaattr):
0c067de38e46 unification of meta-attributes handling:
sylvain.thenault@logilab.fr
parents: 1086
diff changeset
    68
                setattr(cls, 'default_%s' % metaattr, cls._default_format)
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
    69
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
    # meta data api ###########################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    def dc_title(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
        """return a suitable *unicode* title for this entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        for rschema, attrschema in self.e_schema.attribute_definitions():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
            if rschema.meta:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
            value = self.get_value(rschema.type)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
            if value:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
                # make the value printable (dates, floats, bytes, etc.)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
                return self.printable_value(rschema.type, value, attrschema.type,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
                                            format='text/plain')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
        return u'%s #%s' % (self.dc_type(), self.eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
    def dc_long_title(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
        """return a more detailled title for this entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
        return self.dc_title()
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
    87
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
    def dc_description(self, format='text/plain'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
        """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
    90
        if self.e_schema.has_subject_relation('description'):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
            return self.printable_value('description', format=format)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        return u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
    def dc_authors(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
        """return a suitable description for the author(s) of the entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
            return ', '.join(u.name() for u in self.owned_by)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
        except Unauthorized:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
            return u''
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_creator(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
        """return a suitable description for the creator of the entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
        if self.creator:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
            return self.creator.name()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        return u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
    def dc_date(self, date_format=None):# XXX default to ISO 8601 ?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
        """return latest modification date of this entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
        return self.format_date(self.modification_date, date_format=date_format)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
    def dc_type(self, form=''):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   112
        """return the display name for the type of this entity (translated)"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
        return self.e_schema.display_name(self.req, form)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   115
    def dc_language(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
        """return language used by this entity (translated)"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
        # check if entities has internationalizable attributes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
        # XXX one is enough or check if all String attributes are internationalizable?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
        for rschema, attrschema in self.e_schema.attribute_definitions():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
            if rschema.rproperty(self.e_schema, attrschema,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
                                 'internationalizable'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   122
                return self.req._(self.req.user.property_value('ui.language'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
        return self.req._(self.vreg.property_value('ui.language'))
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   124
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
    @property
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
    def creator(self):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1360
diff changeset
   127
        """return the CWUser entity which has created this entity, or None if
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
        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
   129
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
            return self.created_by[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
        except (Unauthorized, IndexError):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
            return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
    def breadcrumbs(self, view=None, recurs=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
        path = [self]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   137
        if hasattr(self, 'parent'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
            parent = self.parent()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
            if parent is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
                    path = parent.breadcrumbs(view, True) + [self]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
                except TypeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
                    warn("breadcrumbs method's now takes two arguments "
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
                         "(view=None, recurs=False), please update",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
                         DeprecationWarning)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
                    path = parent.breadcrumbs(view) + [self]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
        if not recurs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   148
            if view is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
                if 'vtitle' in self.req.form:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
                    # embeding for instance
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
                    path.append( self.req.form['vtitle'] )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
            elif view.id != 'primary' and hasattr(view, 'title'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
                path.append( self.req._(view.title) )
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
        return path
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
125
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   156
    ## IFeed interface ########################################################
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   157
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   158
    def rss_feed_url(self):
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   159
        return self.absolute_url(vid='rss')
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   160
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
    # abstractions making the whole things (well, some at least) working ######
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   162
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
    def sortvalue(self, rtype=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
        """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
   165
        entity's attribute
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
        if rtype is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
            return self.dc_title().lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
        value = self.get_value(rtype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
        # do not restrict to `unicode` because Bytes will return a `str` value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
        if isinstance(value, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
            return self.printable_value(rtype, format='text/plain').lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
        return value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   175
    # edition helper functions ################################################
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   176
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   177
    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
   178
        """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
   179
        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
   180
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   181
        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
   182
        if `remove` is True (by default).
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   183
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   184
        try:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   185
            return self.__linkto[(rtype, target)]
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   186
        except AttributeError:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   187
            self.__linkto = {}
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   188
        except KeyError:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   189
            pass
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   190
        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
   191
        linkedto = []
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   192
        for linkto in linktos[:]:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   193
            ltrtype, eid, lttarget = linkto.split(':')
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   194
            if rtype == ltrtype and target == lttarget:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   195
                # 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
   196
                # hidden input
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   197
                if remove:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   198
                    linktos.remove(linkto)
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   199
                    self.req.form['__linkto'] = linktos
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   200
                linkedto.append(typed_eid(eid))
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   201
        self.__linkto[(rtype, target)] = linkedto
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   202
        return linkedto
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   203
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   204
    # edit controller callbacks ###############################################
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   205
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   206
    def after_deletion_path(self):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   207
        """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
   208
        information when this entity is being deleted
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
        return str(self.e_schema).lower(), {}
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   211
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   212
    def pre_web_edit(self):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   213
        """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
   214
        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
   215
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   216
        Do nothing by default.
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   217
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   218
        pass
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   219
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   220
    # server side helpers #####################################################
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   221
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   222
    def notification_references(self, view):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   223
        """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
   224
        for this entity. `view` is the notification view.
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   225
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   226
        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
   227
        of previously sent email
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   228
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   229
        return ()
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   230
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   231
    # XXX deprecates, may be killed once old widgets system is gone ###########
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   232
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   233
    @classmethod
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   234
    def get_widget(cls, rschema, x='subject'):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   235
        """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
   236
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   237
        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
   238
        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
   239
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   240
        # 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
   241
        from cubicweb.web.widgets import widget
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   242
        if isinstance(rschema, basestring):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   243
            rschema = cls.schema.rschema(rschema)
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   244
        if x == 'subject':
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   245
            tschema = rschema.objects(cls.e_schema)[0]
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   246
            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
   247
        else:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   248
            tschema = rschema.subjects(cls.e_schema)[0]
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   249
            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
   250
        return wdg
1154
9b23a6836c32 support for old __rtags__
sylvain.thenault@logilab.fr
parents: 1150
diff changeset
   251
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   252
    @obsolete('use EntityFieldsForm.subject_relation_vocabulary')
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   253
    def subject_relation_vocabulary(self, rtype, limit):
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   254
        from cubicweb.web.form import EntityFieldsForm
1182
78e4080252be fix form instantiation
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   255
        return EntityFieldsForm(self.req, None, entity=self).subject_relation_vocabulary(rtype, limit)
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   256
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   257
    @obsolete('use EntityFieldsForm.object_relation_vocabulary')
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   258
    def object_relation_vocabulary(self, rtype, limit):
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   259
        from cubicweb.web.form import EntityFieldsForm
1182
78e4080252be fix form instantiation
sylvain.thenault@logilab.fr
parents: 1179
diff changeset
   260
        return EntityFieldsForm(self.req, None, entity=self).object_relation_vocabulary(rtype, limit)
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   261
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   262
    @obsolete('use AutomaticEntityForm.[e]relations_by_category')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
    def relations_by_category(self, categories=None, permission=None):
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 1493
diff changeset
   264
        from cubicweb.web.views.autoform import AutomaticEntityForm
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   265
        return AutomaticEntityForm.erelations_by_category(self, categories, permission)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   266
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   267
    @obsolete('use AutomaticEntityForm.[e]srelations_by_category')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
    def srelations_by_category(self, categories=None, permission=None):
1498
2c6eec0b46b9 fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents: 1493
diff changeset
   269
        from cubicweb.web.views.autoform import AutomaticEntityForm
1179
70825477c6ce fix *relations_by_categories + rtags api/key mess
sylvain.thenault@logilab.fr
parents: 1154
diff changeset
   270
        return AutomaticEntityForm.esrelations_by_category(self, categories, permission)
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   271
912
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   272
    def _default_format(self):
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   273
        return self.req.property_value('ui.default-text-format')
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   274
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   275
    def attribute_values(self, attrname):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   276
        if self.has_eid() or attrname in self:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   277
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   278
                values = self[attrname]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   279
            except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   280
                values = getattr(self, attrname)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
            # actual relation return a list of entities
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
            if isinstance(values, list):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   283
                return [v.eid for v in values]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
            return (values,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   285
        # the entity is being created, try to find default value for
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   286
        # this attribute
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   287
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   288
            values = self.req.form[attrname]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   289
        except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   290
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   291
                values = self[attrname] # copying
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   292
            except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   293
                values = getattr(self, 'default_%s' % attrname,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   294
                                 self.e_schema.default(attrname))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   295
                if callable(values):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   296
                    values = values()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   297
        if values is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   298
            values = ()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   299
        elif not isinstance(values, (list, tuple)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   300
            values = (values,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
        return values
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   302
912
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   303
    def use_fckeditor(self, attr):
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   304
        """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
   305
        `attr`, according to user preferences
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   306
        """
1107
961a478593a5 has_metadata is a schema method
sylvain.thenault@logilab.fr
parents: 1101
diff changeset
   307
        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
   308
            if self.has_eid() or '%s_format' % attr in self:
1360
13ae1121835e rename attribute_metadata method to attr_metadata to save a few chars
sylvain.thenault@logilab.fr
parents: 1182
diff changeset
   309
                return self.attr_metadata(attr, 'format') == 'text/html'
1013
948a3882c94a add a use_fckeditor method on http request
sylvain.thenault@logilab.fr
parents: 1010
diff changeset
   310
            return self.req.property_value('ui.default-text-format') == 'text/html'
912
9ad0867692e1 prepare for deprecation
sylvain.thenault@logilab.fr
parents: 713
diff changeset
   311
        return False
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   312
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   313
# 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
   314
#       configuration and we need the actual reference to avoid infinite loops
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   315
#       in mro
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   316
ANYENTITY = AnyEntity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   317
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   318
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   319
    if pclass is ANYENTITY:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   320
        pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   321
    if pclass is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
        fetchattrs += pclass.fetch_attrs
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
    if mainattr is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   324
        mainattr = fetchattrs[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
    def fetch_order(cls, attr, var):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   327
        if attr == mainattr:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   328
            return '%s %s' % (var, order)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   329
        return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   330
    return fetchattrs, fetch_order