entities/adapters.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Fri, 25 May 2012 16:19:21 +0200
changeset 8427 63c4c65e4db1
parent 8190 2a3c1b787688
child 8518 153a7c9cdca9
permissions -rw-r--r--
[web/views/staticcontroller] safety belt for unguessed mimetypes (closes #2380304)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8037
diff changeset
     1
# copyright 2010-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
#
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
#
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
# any later version.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
#
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    14
# details.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    15
#
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    18
"""some basic entity adapter implementations, for interfaces used in the
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    19
framework itself.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
__docformat__ = "restructuredtext en"
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
    24
from itertools import chain
5720
f0e521487903 [ITree] more fixes, fix fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5718
diff changeset
    25
from warnings import warn
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
    26
5694
ce2c108a9595 [pylint] fix detected name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5574
diff changeset
    27
from logilab.mtconverter import TransformError
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
    28
from logilab.common.decorators import cached
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    29
from logilab.common.deprecation import class_deprecated
5694
ce2c108a9595 [pylint] fix detected name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5574
diff changeset
    30
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    31
from cubicweb import ValidationError, view
8190
2a3c1b787688 [vreg] move base registry implementation to logilab.common. Closes #1916014
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8037
diff changeset
    32
from cubicweb.predicates import (implements, is_instance, relation_possible,
6465
6401a9d0b5aa [architecture] introduce adapter to ease transformation of errors before display to the end user. Use it for UniqueTogetherError first
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6155
diff changeset
    33
                                match_exception)
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
    34
from cubicweb.interfaces import IDownloadable, ITree, IProgress, IMileStone
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    37
class IEmailableAdapter(view.EntityAdapter):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
    __regid__ = 'IEmailable'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
    __select__ = relation_possible('primary_email') | relation_possible('use_email')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    40
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
    def get_email(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    42
        if getattr(self.entity, 'primary_email', None):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
            return self.entity.primary_email[0].address
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
        if getattr(self.entity, 'use_email', None):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
            return self.entity.use_email[0].address
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
        return None
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    47
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
    def allowed_massmail_keys(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    49
        """returns a set of allowed email substitution keys
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    50
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    51
        The default is to return the entity's attribute list but you might
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    52
        override this method to allow extra keys.  For instance, a Person
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
        class might want to return a `companyname` key.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    54
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
        return set(rschema.type
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    56
                   for rschema, attrtype in self.entity.e_schema.attribute_definitions()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    57
                   if attrtype.type not in ('Password', 'Bytes'))
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    58
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    59
    def as_email_context(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
        """returns the dictionary as used by the sendmail controller to
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    61
        build email bodies.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    62
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    63
        NOTE: the dictionary keys should match the list returned by the
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    64
        `allowed_massmail_keys` method.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    65
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    66
        return dict( (attr, getattr(self.entity, attr))
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
                     for attr in self.allowed_massmail_keys() )
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    69
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    70
class INotifiableAdapter(view.EntityAdapter):
6864
ea95004494a2 [adapters] fix #1424467 caused by precedence of bw compat method while the adapter is concret for a given class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6752
diff changeset
    71
    __needs_bw_compat__ = True
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    72
    __regid__ = 'INotifiable'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5768
diff changeset
    73
    __select__ = is_instance('Any')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    74
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    75
    @view.implements_adapter_compat('INotifiableAdapter')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    76
    def notification_references(self, view):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    77
        """used to control References field of email send on notification
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
        for this entity. `view` is the notification view.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    79
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    80
        Should return a list of eids which can be used to generate message
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    81
        identifiers of previously sent email(s)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    82
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    83
        itree = self.entity.cw_adapt_to('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    84
        if itree is not None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    85
            return itree.path()[:-1]
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    86
        return ()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    87
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    88
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
    89
class IFTIndexableAdapter(view.EntityAdapter):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    90
    __regid__ = 'IFTIndexable'
5877
0c7b7b76a84f [selectors] provide a new, optimized, is_instance selector that should at some point replace implements (along with the adaptable selector)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5768
diff changeset
    91
    __select__ = is_instance('Any')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    92
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    93
    def fti_containers(self, _done=None):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    94
        if _done is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    95
            _done = set()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    96
        entity = self.entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    97
        _done.add(entity.eid)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    98
        containers = tuple(entity.e_schema.fulltext_containers())
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    99
        if containers:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   100
            for rschema, target in containers:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   101
                if target == 'object':
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   102
                    targets = getattr(entity, rschema.type)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   103
                else:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   104
                    targets = getattr(entity, 'reverse_%s' % rschema)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   105
                for entity in targets:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   106
                    if entity.eid in _done:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   107
                        continue
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   108
                    for container in entity.cw_adapt_to('IFTIndexable').fti_containers(_done):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   109
                        yield container
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   110
                        yielded = True
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   111
        else:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   112
            yield entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   113
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   114
    # weight in ABCD
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   115
    entity_weight = 1.0
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   116
    attr_weight = {}
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   117
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   118
    def get_words(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   119
        """used by the full text indexer to get words to index
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   120
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   121
        this method should only be used on the repository side since it depends
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   122
        on the logilab.database package
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   123
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   124
        :rtype: list
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   125
        :return: the list of indexable word of this entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   126
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   127
        from logilab.database.fti import tokenize
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   128
        # take care to cases where we're modyfying the schema
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   129
        entity = self.entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   130
        pending = self._cw.transaction_data.setdefault('pendingrdefs', set())
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   131
        words = {}
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   132
        for rschema in entity.e_schema.indexable_attributes():
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   133
            if (entity.e_schema, rschema) in pending:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   134
                continue
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   135
            weight = self.attr_weight.get(rschema, 'C')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   136
            try:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   137
                value = entity.printable_value(rschema, format='text/plain')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   138
            except TransformError:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   139
                continue
7815
2a164a9cf81c [exceptions] stop catching any exception in various places (closes #1942716)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6974
diff changeset
   140
            except Exception:
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   141
                self.exception("can't add value of %s to text index for entity %s",
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   142
                               rschema, entity.eid)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   143
                continue
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   144
            if value:
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   145
                words.setdefault(weight, []).extend(tokenize(value))
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   146
        for rschema, role in entity.e_schema.fulltext_relations():
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   147
            if role == 'subject':
5574
e082a57c2207 [fti] fix name conflict introduced by the new IFTIAdapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   148
                for entity_ in getattr(entity, rschema.type):
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   149
                    merge_weight_dict(words, entity_.cw_adapt_to('IFTIndexable').get_words())
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   150
            else: # if role == 'object':
5574
e082a57c2207 [fti] fix name conflict introduced by the new IFTIAdapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   151
                for entity_ in getattr(entity, 'reverse_%s' % rschema.type):
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   152
                    merge_weight_dict(words, entity_.cw_adapt_to('IFTIndexable').get_words())
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   153
        return words
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   154
5768
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   155
def merge_weight_dict(maindict, newdict):
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   156
    for weight, words in newdict.iteritems():
1e73a466aa69 [fti] support for fti ranking: has_text query results sorted by relevance, and provides a way to control weight per entity / entity's attribute
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5722
diff changeset
   157
        maindict.setdefault(weight, []).extend(words)
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   158
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   159
class IDownloadableAdapter(view.EntityAdapter):
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   160
    """interface for downloadable entities"""
6864
ea95004494a2 [adapters] fix #1424467 caused by precedence of bw compat method while the adapter is concret for a given class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6752
diff changeset
   161
    __needs_bw_compat__ = True
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   162
    __regid__ = 'IDownloadable'
5895
6a3f776292a5 [selectors] avoid spurious warning when using implements by design
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
   163
    __select__ = implements(IDownloadable, warn=False) # XXX for bw compat, else should be abstract
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   164
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   165
    @view.implements_adapter_compat('IDownloadable')
5981
3472c051da77 [idownloadable] fix download_url prototype: should accept arbitrary kwargs to add as form parameters to the generated url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5895
diff changeset
   166
    def download_url(self, **kwargs): # XXX not really part of this interface
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   167
        """return an url to download entity's content"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   168
        raise NotImplementedError
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   169
    @view.implements_adapter_compat('IDownloadable')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   170
    def download_content_type(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   171
        """return MIME type of the downloadable content"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   172
        raise NotImplementedError
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   173
    @view.implements_adapter_compat('IDownloadable')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   174
    def download_encoding(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   175
        """return encoding of the downloadable content"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   176
        raise NotImplementedError
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   177
    @view.implements_adapter_compat('IDownloadable')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   178
    def download_file_name(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   179
        """return file name of the downloadable content"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   180
        raise NotImplementedError
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   181
    @view.implements_adapter_compat('IDownloadable')
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   182
    def download_data(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   183
        """return actual data of the downloadable content"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   184
        raise NotImplementedError
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   185
6974
6f23b2baf99b add a note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6864
diff changeset
   186
# XXX should propose to use two different relations for children/parent
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   187
class ITreeAdapter(view.EntityAdapter):
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   188
    """This adapter has to be overriden to be configured using the
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   189
    tree_relation, child_role and parent_role class attributes to benefit from
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   190
    this default implementation.
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   191
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   192
    This adapter provides a tree interface. It has to be overriden to be
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   193
    configured using the tree_relation, child_role and parent_role class
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   194
    attributes to benefit from this default implementation.
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   195
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   196
    This class provides the following methods:
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   197
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   198
    .. automethod: iterparents
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   199
    .. automethod: iterchildren
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   200
    .. automethod: prefixiter
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   201
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   202
    .. automethod: is_leaf
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   203
    .. automethod: is_root
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   204
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   205
    .. automethod: root
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   206
    .. automethod: parent
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   207
    .. automethod: children
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   208
    .. automethod: different_type_children
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   209
    .. automethod: same_type_children
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   210
    .. automethod: children_rql
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   211
    .. automethod: path
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   212
    """
6864
ea95004494a2 [adapters] fix #1424467 caused by precedence of bw compat method while the adapter is concret for a given class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6752
diff changeset
   213
    __needs_bw_compat__ = True
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   214
    __regid__ = 'ITree'
5895
6a3f776292a5 [selectors] avoid spurious warning when using implements by design
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
   215
    __select__ = implements(ITree, warn=False) # XXX for bw compat, else should be abstract
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   216
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   217
    child_role = 'subject'
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   218
    parent_role = 'object'
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   219
5718
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
   220
    @property
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
   221
    def tree_relation(self):
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
   222
        warn('[3.9] tree_attribute is deprecated, define tree_relation on a custom '
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
   223
             'ITree for %s instead' % (self.entity.__class__),
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
   224
             DeprecationWarning)
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
   225
        return self.entity.tree_attribute
8d246203730a [ITree] fix adapter/_done_init to handle tree_[attribute|relation], add maxlevel argument to base tree view since one side effect of the new code is that it find tree view for entity types it didn't find before, hence one may want to limit tree view depth when it wasn't necessary before
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5716
diff changeset
   226
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   227
    # XXX should be removed from the public interface
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   228
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   229
    def children_rql(self):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   230
        """Returns RQL to get the children of the entity."""
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   231
        return self.entity.cw_related_rql(self.tree_relation, self.parent_role)
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   232
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   233
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   234
    def different_type_children(self, entities=True):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   235
        """Return children entities of different type as this entity.
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   236
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   237
        According to the `entities` parameter, return entity objects or the
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   238
        equivalent result set.
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   239
        """
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   240
        res = self.entity.related(self.tree_relation, self.parent_role,
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   241
                                  entities=entities)
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   242
        eschema = self.entity.e_schema
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   243
        if entities:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   244
            return [e for e in res if e.e_schema != eschema]
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   245
        return res.filtered_rset(lambda x: x.e_schema != eschema, self.entity.cw_col)
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   246
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   247
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   248
    def same_type_children(self, entities=True):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   249
        """Return children entities of the same type as this entity.
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   250
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   251
        According to the `entities` parameter, return entity objects or the
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   252
        equivalent result set.
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   253
        """
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   254
        res = self.entity.related(self.tree_relation, self.parent_role,
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   255
                                  entities=entities)
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   256
        eschema = self.entity.e_schema
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   257
        if entities:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   258
            return [e for e in res if e.e_schema == eschema]
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   259
        return res.filtered_rset(lambda x: x.e_schema is eschema, self.entity.cw_col)
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   260
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   261
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   262
    def is_leaf(self):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   263
        """Returns True if the entity does not have any children."""
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   264
        return len(self.children()) == 0
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   265
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   266
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   267
    def is_root(self):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   268
        """Returns true if the entity is root of the tree (e.g. has no parent).
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   269
        """
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   270
        return self.parent() is None
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   271
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   272
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   273
    def root(self):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   274
        """Return the root entity of the tree."""
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   275
        return self._cw.entity_from_eid(self.path()[0])
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   276
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   277
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   278
    def parent(self):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   279
        """Returns the parent entity if any, else None (e.g. if we are on the
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   280
        root).
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   281
        """
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   282
        try:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   283
            return self.entity.related(self.tree_relation, self.child_role,
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   284
                                       entities=True)[0]
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   285
        except (KeyError, IndexError):
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   286
            return None
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   287
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   288
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   289
    def children(self, entities=True, sametype=False):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   290
        """Return children entities.
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   291
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   292
        According to the `entities` parameter, return entity objects or the
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   293
        equivalent result set.
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   294
        """
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   295
        if sametype:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   296
            return self.same_type_children(entities)
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   297
        else:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   298
            return self.entity.related(self.tree_relation, self.parent_role,
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   299
                                       entities=entities)
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   300
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   301
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   302
    def iterparents(self, strict=True):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   303
        """Return an iterator on the parents of the entity."""
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   304
        def _uptoroot(self):
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   305
            curr = self
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   306
            while True:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   307
                curr = curr.parent()
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   308
                if curr is None:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   309
                    break
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   310
                yield curr
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   311
                curr = curr.cw_adapt_to('ITree')
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   312
        if not strict:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   313
            return chain([self.entity], _uptoroot(self))
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   314
        return _uptoroot(self)
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   315
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   316
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   317
    def iterchildren(self, _done=None):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   318
        """Return an iterator over the item's children."""
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   319
        if _done is None:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   320
            _done = set()
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   321
        for child in self.children():
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   322
            if child.eid in _done:
6011
b5f15098f282 [debug] when a loop is detected in a tree, log the entity involved in it to ease repair
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5981
diff changeset
   323
                self.error('loop in %s tree: %s', child.__regid__.lower(), child)
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   324
                continue
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   325
            yield child
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   326
            _done.add(child.eid)
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   327
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   328
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   329
    def prefixiter(self, _done=None):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   330
        """Return an iterator over the item's descendants in a prefixed order."""
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   331
        if _done is None:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   332
            _done = set()
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   333
        if self.entity.eid in _done:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   334
            return
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   335
        _done.add(self.entity.eid)
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   336
        yield self.entity
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   337
        for child in self.same_type_children():
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   338
            for entity in child.cw_adapt_to('ITree').prefixiter(_done):
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   339
                yield entity
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   340
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   341
    @view.implements_adapter_compat('ITree')
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   342
    @cached
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   343
    def path(self):
6155
16fad8d00787 [doc] clean ITreeAdapter documentation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6011
diff changeset
   344
        """Returns the list of eids from the root object to this object."""
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   345
        path = []
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   346
        adapter = self
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   347
        entity = adapter.entity
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   348
        while entity is not None:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   349
            if entity.eid in path:
6011
b5f15098f282 [debug] when a loop is detected in a tree, log the entity involved in it to ease repair
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5981
diff changeset
   350
                self.error('loop in %s tree: %s', entity.__regid__.lower(), entity)
5716
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   351
                break
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   352
            path.append(entity.eid)
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   353
            try:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   354
                # check we are not jumping to another tree
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   355
                if (adapter.tree_relation != self.tree_relation or
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   356
                    adapter.child_role != self.child_role):
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   357
                    break
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   358
                entity = adapter.parent()
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   359
                adapter = entity.cw_adapt_to('ITree')
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   360
            except AttributeError:
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   361
                break
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   362
        path.reverse()
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   363
        return path
0e2af244dea5 [web] move itree adapter to entities, it may be used outside the web interface
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5694
diff changeset
   364
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   365
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   366
# error handling adapters ######################################################
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   367
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   368
from cubicweb import UniqueTogetherError
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   369
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   370
class IUserFriendlyError(view.EntityAdapter):
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   371
    __regid__ = 'IUserFriendlyError'
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   372
    __abstract__ = True
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   373
    def __init__(self, *args, **kwargs):
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   374
        self.exc = kwargs.pop('exc')
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   375
        super(IUserFriendlyError, self).__init__(*args, **kwargs)
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   376
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   377
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   378
class IUserFriendlyUniqueTogether(IUserFriendlyError):
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   379
    __select__ = match_exception(UniqueTogetherError)
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   380
    def raise_user_exception(self):
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   381
        etype, rtypes = self.exc.args
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   382
        msg = self._cw._('violates unique_together constraints (%s)') % (
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   383
            ', '.join([self._cw._(rtype) for rtype in rtypes]))
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   384
        raise ValidationError(self.entity.eid, dict((col, msg) for col in rtypes))
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   385
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   386
# deprecated ###################################################################
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   387
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   388
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   389
class adapter_deprecated(view.auto_unwrap_bw_compat):
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   390
    """metaclass to print a warning on instantiation of a deprecated class"""
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   391
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   392
    def __call__(cls, *args, **kwargs):
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   393
        msg = getattr(cls, "__deprecation_warning__",
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   394
                      "%(cls)s is deprecated") % {'cls': cls.__name__}
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   395
        warn(msg, DeprecationWarning, stacklevel=2)
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   396
        return type.__call__(cls, *args, **kwargs)
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   397
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   398
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   399
class IProgressAdapter(view.EntityAdapter):
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   400
    """something that has a cost, a state and a progression.
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   401
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   402
    You should at least override progress_info an in_progress methods on
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7815
diff changeset
   403
    concrete implementations.
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   404
    """
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   405
    __metaclass__ = adapter_deprecated
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   406
    __deprecation_warning__ = '[3.14] IProgressAdapter has been moved to iprogress cube'
6864
ea95004494a2 [adapters] fix #1424467 caused by precedence of bw compat method while the adapter is concret for a given class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6752
diff changeset
   407
    __needs_bw_compat__ = True
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   408
    __regid__ = 'IProgress'
5895
6a3f776292a5 [selectors] avoid spurious warning when using implements by design
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
   409
    __select__ = implements(IProgress, warn=False) # XXX for bw compat, should be abstract
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   410
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   411
    @property
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   412
    @view.implements_adapter_compat('IProgress')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   413
    def cost(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   414
        """the total cost"""
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   415
        return self.progress_info()['estimated']
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   416
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   417
    @property
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   418
    @view.implements_adapter_compat('IProgress')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   419
    def revised_cost(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   420
        return self.progress_info().get('estimatedcorrected', self.cost)
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   421
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   422
    @property
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   423
    @view.implements_adapter_compat('IProgress')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   424
    def done(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   425
        """what is already done"""
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   426
        return self.progress_info()['done']
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   427
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   428
    @property
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   429
    @view.implements_adapter_compat('IProgress')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   430
    def todo(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   431
        """what remains to be done"""
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   432
        return self.progress_info()['todo']
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   433
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   434
    @view.implements_adapter_compat('IProgress')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   435
    def progress_info(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   436
        """returns a dictionary describing progress/estimated cost of the
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   437
        version.
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   438
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   439
        - mandatory keys are (''estimated', 'done', 'todo')
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   440
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   441
        - optional keys are ('notestimated', 'notestimatedcorrected',
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   442
          'estimatedcorrected')
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   443
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   444
        'noestimated' and 'notestimatedcorrected' should default to 0
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   445
        'estimatedcorrected' should default to 'estimated'
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   446
        """
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   447
        raise NotImplementedError
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   448
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   449
    @view.implements_adapter_compat('IProgress')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   450
    def finished(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   451
        """returns True if status is finished"""
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   452
        return not self.in_progress()
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   453
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   454
    @view.implements_adapter_compat('IProgress')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   455
    def in_progress(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   456
        """returns True if status is not finished"""
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   457
        raise NotImplementedError
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   458
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   459
    @view.implements_adapter_compat('IProgress')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   460
    def progress(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   461
        """returns the % progress of the task item"""
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   462
        try:
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   463
            return 100. * self.done / self.revised_cost
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   464
        except ZeroDivisionError:
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   465
            # total cost is 0 : if everything was estimated, task is completed
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   466
            if self.progress_info().get('notestimated'):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   467
                return 0.
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   468
            return 100
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   469
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   470
    @view.implements_adapter_compat('IProgress')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   471
    def progress_class(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   472
        return ''
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   473
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   474
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   475
class IMileStoneAdapter(IProgressAdapter):
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   476
    __metaclass__ = adapter_deprecated
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   477
    __deprecation_warning__ = '[3.14] IMileStoneAdapter has been moved to iprogress cube'
6864
ea95004494a2 [adapters] fix #1424467 caused by precedence of bw compat method while the adapter is concret for a given class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6752
diff changeset
   478
    __needs_bw_compat__ = True
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   479
    __regid__ = 'IMileStone'
5895
6a3f776292a5 [selectors] avoid spurious warning when using implements by design
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5877
diff changeset
   480
    __select__ = implements(IMileStone, warn=False) # XXX for bw compat, should be abstract
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   481
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   482
    parent_type = None # specify main task's type
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   483
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   484
    @view.implements_adapter_compat('IMileStone')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   485
    def get_main_task(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   486
        """returns the main ITask entity"""
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   487
        raise NotImplementedError
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   488
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   489
    @view.implements_adapter_compat('IMileStone')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   490
    def initial_prevision_date(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   491
        """returns the initial expected end of the milestone"""
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   492
        raise NotImplementedError
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   493
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   494
    @view.implements_adapter_compat('IMileStone')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   495
    def eta_date(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   496
        """returns expected date of completion based on what remains
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   497
        to be done
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   498
        """
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   499
        raise NotImplementedError
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   500
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   501
    @view.implements_adapter_compat('IMileStone')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   502
    def completion_date(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   503
        """returns date on which the subtask has been completed"""
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   504
        raise NotImplementedError
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   505
8037
a36bd56f33bb [diet] move iprogress to its own cube. Closes #1916016
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   506
    @view.implements_adapter_compat('IMileStone')
5722
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   507
    def contractors(self):
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   508
        """returns the list of persons supposed to work on this task"""
61d6a4caa963 [iprogress] move adapter to entities.adapters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5720
diff changeset
   509
        raise NotImplementedError
6465
6401a9d0b5aa [architecture] introduce adapter to ease transformation of errors before display to the end user. Use it for UniqueTogetherError first
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6155
diff changeset
   510