entities/adapters.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 10 Jun 2010 16:25:15 +0200
changeset 5716 0e2af244dea5
parent 5694 ce2c108a9595
child 5718 8d246203730a
permissions -rw-r--r--
[web] move itree adapter to entities, it may be used outside the web interface
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
# copyright 2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
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
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
    25
5694
ce2c108a9595 [pylint] fix detected name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5574
diff changeset
    26
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
    27
from logilab.common.decorators import cached
5694
ce2c108a9595 [pylint] fix detected name errors
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5574
diff changeset
    28
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
from cubicweb.view import EntityAdapter, implements_adapter_compat
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
from cubicweb.selectors import implements, relation_possible
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
    31
from cubicweb.interfaces import IDownloadable, ITree
5556
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    32
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    33
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    34
class IEmailableAdapter(EntityAdapter):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    35
    __regid__ = 'IEmailable'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    36
    __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
    37
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    38
    def get_email(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    39
        if getattr(self.entity, 'primary_email', None):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    40
            return self.entity.primary_email[0].address
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    41
        if getattr(self.entity, 'use_email', None):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    42
            return self.entity.use_email[0].address
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    43
        return None
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    44
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    45
    def allowed_massmail_keys(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    46
        """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
    47
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    48
        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
    49
        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
    50
        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
    51
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    52
        return set(rschema.type
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    53
                   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
    54
                   if attrtype.type not in ('Password', 'Bytes'))
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    55
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    56
    def as_email_context(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    57
        """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
    58
        build email bodies.
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    59
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    60
        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
    61
        `allowed_massmail_keys` method.
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
        return dict( (attr, getattr(self.entity, attr))
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    64
                     for attr in self.allowed_massmail_keys() )
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
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    67
class INotifiableAdapter(EntityAdapter):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    68
    __regid__ = 'INotifiable'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    69
    __select__ = implements('Any')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    70
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    71
    @implements_adapter_compat('INotifiableAdapter')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    72
    def notification_references(self, view):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    73
        """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
    74
        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
    75
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    76
        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
    77
        identifiers of previously sent email(s)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    78
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    79
        itree = self.entity.cw_adapt_to('ITree')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    80
        if itree is not None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    81
            return itree.path()[:-1]
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    82
        return ()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    83
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    84
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    85
class IFTIndexableAdapter(EntityAdapter):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    86
    __regid__ = 'IFTIndexable'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    87
    __select__ = implements('Any')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    88
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    89
    def fti_containers(self, _done=None):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    90
        if _done is None:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    91
            _done = set()
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    92
        entity = self.entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    93
        _done.add(entity.eid)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    94
        containers = tuple(entity.e_schema.fulltext_containers())
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    95
        if containers:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    96
            for rschema, target in containers:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    97
                if target == 'object':
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    98
                    targets = getattr(entity, rschema.type)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    99
                else:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   100
                    targets = getattr(entity, 'reverse_%s' % rschema)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   101
                for entity in targets:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   102
                    if entity.eid in _done:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   103
                        continue
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   104
                    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
   105
                        yield container
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   106
                        yielded = True
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   107
        else:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   108
            yield entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   109
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   110
    def get_words(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   111
        """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
   112
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   113
        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
   114
        on the logilab.database package
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   115
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   116
        :rtype: list
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   117
        :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
   118
        """
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   119
        from logilab.database.fti import tokenize
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   120
        # 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
   121
        entity = self.entity
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   122
        pending = self._cw.transaction_data.setdefault('pendingrdefs', set())
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   123
        words = []
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   124
        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
   125
            if (entity.e_schema, rschema) in pending:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   126
                continue
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   127
            try:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   128
                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
   129
            except TransformError:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   130
                continue
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   131
            except:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   132
                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
   133
                               rschema, entity.eid)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   134
                continue
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   135
            if value:
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   136
                words += tokenize(value)
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   137
        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
   138
            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
   139
                for entity_ in getattr(entity, rschema.type):
e082a57c2207 [fti] fix name conflict introduced by the new IFTIAdapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   140
                    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
   141
            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
   142
                for entity_ in getattr(entity, 'reverse_%s' % rschema.type):
e082a57c2207 [fti] fix name conflict introduced by the new IFTIAdapter
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5556
diff changeset
   143
                    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
   144
        return words
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   145
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   146
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   147
class IDownloadableAdapter(EntityAdapter):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   148
    """interface for downloadable entities"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   149
    __regid__ = 'IDownloadable'
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   150
    __select__ = implements(IDownloadable) # XXX for bw compat, else should be abstract
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   151
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   152
    @implements_adapter_compat('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   153
    def download_url(self): # XXX not really part of this interface
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   154
        """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
   155
        raise NotImplementedError
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   156
    @implements_adapter_compat('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   157
    def download_content_type(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   158
        """return MIME type of the downloadable content"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   159
        raise NotImplementedError
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   160
    @implements_adapter_compat('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   161
    def download_encoding(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   162
        """return encoding of the downloadable content"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   163
        raise NotImplementedError
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   164
    @implements_adapter_compat('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   165
    def download_file_name(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   166
        """return file name of the downloadable content"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   167
        raise NotImplementedError
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   168
    @implements_adapter_compat('IDownloadable')
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   169
    def download_data(self):
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   170
        """return actual data of the downloadable content"""
9ab2b4c74baf [entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
   171
        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
   172
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
   173
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
   174
class ITreeAdapter(EntityAdapter):
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
   175
    """This adapter has to be overriden to be configured using the
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
   176
    tree_relation, child_role and parent_role class attributes to
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
   177
    benefit from this default implementation
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
   178
    """
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
   179
    __regid__ = '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
   180
    __select__ = implements(ITree) # XXX for bw compat, else should be abstract
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
   181
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
   182
    tree_relation = 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
   183
    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
   184
    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
   185
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
   186
    @implements_adapter_compat('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
   187
    def children_rql(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
   188
        """returns RQL to get 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
   189
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
   190
        XXX should be removed from the public interface
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
   191
        """
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
   192
        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
   193
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
   194
    @implements_adapter_compat('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
   195
    def different_type_children(self, entities=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
   196
        """return children entities of different type as this 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
   197
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
   198
        according to the `entities` parameter, return entity objects or the
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
   199
        equivalent result 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
   200
        """
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
   201
        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
   202
                                  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
   203
        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
   204
        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
   205
            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
   206
        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
   207
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
   208
    @implements_adapter_compat('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
   209
    def same_type_children(self, entities=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
   210
        """return children entities of the same type as this 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
   211
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
        according to the `entities` parameter, return entity objects or the
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
   213
        equivalent result 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
   214
        """
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
   215
        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
   216
                                  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
   217
        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
   218
        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
   219
            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
   220
        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
   221
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
   222
    @implements_adapter_compat('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
   223
    def is_leaf(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
   224
        """returns true if this node as no 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
   225
        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
   226
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
   227
    @implements_adapter_compat('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
   228
    def is_root(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
   229
        """returns true if this node has no 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
   230
        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
   231
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
    @implements_adapter_compat('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
   233
    def root(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
   234
        """return the root 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
   235
        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
   236
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
   237
    @implements_adapter_compat('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
   238
    def parent(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
   239
        """return the parent entity if any, else None (e.g. if we are on the
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
        root)
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
        """
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
        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
   243
            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
   244
                                       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
   245
        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
   246
            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
   247
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
    @implements_adapter_compat('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
   249
    def children(self, entities=True, sametype=False):
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
        """return 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
   251
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
   252
        according to the `entities` parameter, return entity objects or the
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
        equivalent result 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
   254
        """
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
        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
   256
            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
   257
        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
   258
            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
   259
                                       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
   260
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
   261
    @implements_adapter_compat('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
   262
    def iterparents(self, strict=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
   263
        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
   264
            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
   265
            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
   266
                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
   267
                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
   268
                    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
   269
                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
   270
                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
   271
        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
   272
            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
   273
        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
   274
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
    @implements_adapter_compat('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
   276
    def iterchildren(self, _done=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
   277
        """iterates over the item's 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
   278
        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
   279
            _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
   280
        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
   281
            if child.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
   282
                self.error('loop in %s tree', child.__regid__.lower())
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
                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
   284
            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
   285
            _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
   286
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
    @implements_adapter_compat('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
   288
    def prefixiter(self, _done=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
   289
        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
   290
            _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
   291
        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
   292
            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
   293
        _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
   294
        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
   295
        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
   296
            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
   297
                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
   298
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
    @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
   300
    @implements_adapter_compat('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
   301
    def path(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
   302
        """returns the list of eids from the root object to this 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
   303
        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
   304
        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
   305
        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
   306
        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
   307
            if entity.eid in 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
   308
                self.error('loop in %s tree', entity.__regid__.lower())
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
            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
   311
            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
   312
                # 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
   313
                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
   314
                    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
   315
                    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
   316
                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
   317
                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
   318
            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
   319
                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
   320
        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
   321
        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
   322