entities/__init__.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 06 May 2010 14:26:47 +0200
branchstable
changeset 5494 f3bb53f1737c
parent 5424 8ecbcbff9777
child 5556 9ab2b4c74baf
permissions -rw-r--r--
[web session] fix potential key error on logout (occurs once the session has been transparently reconnected, hence session.sessionid and session.cnx.sessionid differs)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     1
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
"""base application's entities class implementation: `AnyEntity`
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
from warnings import warn
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
2613
5e19c2bb370e R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2459
diff changeset
    25
from logilab.common.deprecation import deprecated
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
from logilab.common.decorators import cached
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
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
    29
from cubicweb.entity import Entity
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
125
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
    31
from cubicweb.interfaces import IBreadCrumbs, IFeed
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
    32
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
class AnyEntity(Entity):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
    """an entity instance has e_schema automagically set on the class and
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    instances have access to their issuing cursor
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    """
3377
dd9d292b6a6d use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3369
diff changeset
    38
    __regid__ = 'Any'
125
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
    39
    __implements__ = (IBreadCrumbs, IFeed)
1417
06af20e663f2 remove spaces
sylvain.thenault@logilab.fr
parents: 1398
diff changeset
    40
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    fetch_attrs = ('modification_date',)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    def fetch_order(cls, attr, var):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
        """class method used to control sort order when multiple entities of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
        this type are fetched
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
        return cls.fetch_unrelated_order(attr, var)
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
    48
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    def fetch_unrelated_order(cls, attr, var):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
        """class method used to control sort order when multiple entities of
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
        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
    53
        new relation on an edited entity).
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
        if attr == 'modification_date':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
            return '%s DESC' % var
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
        return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
    # meta data api ###########################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
    def dc_title(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
        """return a suitable *unicode* title for this entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        for rschema, attrschema in self.e_schema.attribute_definitions():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
            if rschema.meta:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
                continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
            value = self.get_value(rschema.type)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
            if value:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
                # make the value printable (dates, floats, bytes, etc.)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
                return self.printable_value(rschema.type, value, attrschema.type,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
                                            format='text/plain')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
        return u'%s #%s' % (self.dc_type(), self.eid)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
    def dc_long_title(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        """return a more detailled title for this entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
        return self.dc_title()
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
    76
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
    def dc_description(self, format='text/plain'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
        """return a suitable description for this entity"""
3689
deb13e88e037 follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3578
diff changeset
    79
        if 'description' in self.e_schema.subjrels:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
            return self.printable_value('description', format=format)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
        return u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
    def dc_authors(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        """return a suitable description for the author(s) of the entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
            return ', '.join(u.name() for u in self.owned_by)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
        except Unauthorized:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
            return u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    def dc_creator(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
        """return a suitable description for the creator of the entity"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        if self.creator:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
            return self.creator.name()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
        return u''
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
    def dc_date(self, date_format=None):# XXX default to ISO 8601 ?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    97
        """return latest modification date of this entity"""
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3418
diff changeset
    98
        return self._cw.format_date(self.modification_date, date_format=date_format)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
    def dc_type(self, form=''):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        """return the display name for the type of this entity (translated)"""
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   102
        return self.e_schema.display_name(self._cw, form)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
    def dc_language(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
        """return language used by this entity (translated)"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   106
        # check if entities has internationalizable attributes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
        # XXX one is enough or check if all String attributes are internationalizable?
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
        for rschema, attrschema in self.e_schema.attribute_definitions():
4147
52fe79a98b21 3.6 remove deprecation warning
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 3720
diff changeset
   109
            if rschema.rdef(self.e_schema, attrschema).internationalizable:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3418
diff changeset
   110
                return self._cw._(self._cw.user.property_value('ui.language'))
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   111
        return self._cw._(self._cw.vreg.property_value('ui.language'))
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   112
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
    @property
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
    def creator(self):
1398
5fe84a5f7035 rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents: 1360
diff changeset
   115
        """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
   116
        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
   117
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
            return self.created_by[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        except (Unauthorized, IndexError):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
            return None
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 breadcrumbs(self, view=None, recurs=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
        path = [self]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   125
        if hasattr(self, 'parent'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
            parent = self.parent()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
            if parent is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
                try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
                    path = parent.breadcrumbs(view, True) + [self]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
                except TypeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
                    warn("breadcrumbs method's now takes two arguments "
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   132
                         "(view=None, recurs=False), please update",
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
                         DeprecationWarning)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
                    path = parent.breadcrumbs(view) + [self]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   135
        if not recurs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   136
            if view is None:
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   137
                if 'vtitle' in self._cw.form:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
                    # embeding for instance
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   139
                    path.append( self._cw.form['vtitle'] )
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3418
diff changeset
   140
            elif view.__regid__ != 'primary' and hasattr(view, 'title'):
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   141
                path.append( self._cw._(view.title) )
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
        return path
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
125
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   144
    ## IFeed interface ########################################################
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   145
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   146
    def rss_feed_url(self):
979dbe0cade3 views with rss feed
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   147
        return self.absolute_url(vid='rss')
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   148
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
    # abstractions making the whole things (well, some at least) working ######
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   150
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   151
    def sortvalue(self, rtype=None):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
        """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
   153
        entity's attribute
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
        """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
        if rtype is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   156
            return self.dc_title().lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
        value = self.get_value(rtype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
        # do not restrict to `unicode` because Bytes will return a `str` value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   159
        if isinstance(value, basestring):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   160
            return self.printable_value(rtype, format='text/plain').lower()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
        return value
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   162
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   163
    # edition helper functions ################################################
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   164
3348
97dca770c028 this is actually a role, fix argument name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2658
diff changeset
   165
    def linked_to(self, rtype, role, remove=True):
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   166
        """if entity should be linked to another using __linkto form param for
3348
97dca770c028 this is actually a role, fix argument name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2658
diff changeset
   167
        the given relation/role, return eids of related entities
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   168
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   169
        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
   170
        if `remove` is True (by default).
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   171
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   172
        try:
3348
97dca770c028 this is actually a role, fix argument name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2658
diff changeset
   173
            return self.__linkto[(rtype, role)]
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   174
        except AttributeError:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   175
            self.__linkto = {}
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   176
        except KeyError:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   177
            pass
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   178
        linktos = list(self._cw.list_form_param('__linkto'))
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   179
        linkedto = []
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   180
        for linkto in linktos[:]:
3348
97dca770c028 this is actually a role, fix argument name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2658
diff changeset
   181
            ltrtype, eid, ltrole = linkto.split(':')
97dca770c028 this is actually a role, fix argument name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2658
diff changeset
   182
            if rtype == ltrtype and role == ltrole:
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   183
                # 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
   184
                # hidden input
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   185
                if remove:
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   186
                    linktos.remove(linkto)
3418
7b49fa7e942d [api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3377
diff changeset
   187
                    self._cw.form['__linkto'] = linktos
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   188
                linkedto.append(typed_eid(eid))
3348
97dca770c028 this is actually a role, fix argument name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2658
diff changeset
   189
        self.__linkto[(rtype, role)] = linkedto
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   190
        return linkedto
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   191
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   192
    # edit controller callbacks ###############################################
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   193
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   194
    def after_deletion_path(self):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   195
        """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
   196
        information when this entity is being deleted
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   197
        """
3578
7f026d15cc82 doesn't necessarily have parent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3566
diff changeset
   198
        if hasattr(self, 'parent') and self.parent():
3566
521337e71fbb [entities] default implementation for after_deletion_path goes back to the parent if any
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 3348
diff changeset
   199
            return self.parent().rest_path(), {}
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   200
        return str(self.e_schema).lower(), {}
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   201
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   202
    def pre_web_edit(self):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   203
        """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
   204
        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
   205
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   206
        Do nothing by default.
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   207
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   208
        pass
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   209
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   210
    # server side helpers #####################################################
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   211
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   212
    def notification_references(self, view):
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   213
        """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
   214
        for this entity. `view` is the notification view.
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   215
1086
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   216
        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
   217
        of previously sent email
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   218
        """
c8ff5c7fd0ae move get_widget in the to-deprecate section
sylvain.thenault@logilab.fr
parents: 1013
diff changeset
   219
        return ()
1493
8270580b65a0 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1417
diff changeset
   220
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
# 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
   222
#       configuration and we need the actual reference to avoid infinite loops
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
#       in mro
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
ANYENTITY = AnyEntity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   226
def fetch_config(fetchattrs, mainattr=None, pclass=AnyEntity, order='ASC'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
    if pclass is ANYENTITY:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
        pclass = AnyEntity # AnyEntity and ANYENTITY may be different classes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   229
    if pclass is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
        fetchattrs += pclass.fetch_attrs
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
    if mainattr is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
        mainattr = fetchattrs[0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
    @classmethod
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
    def fetch_order(cls, attr, var):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
        if attr == mainattr:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
            return '%s %s' % (var, order)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
        return None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
    return fetchattrs, fetch_order