common/selectors.py
author Sylvain Thenault <sylvain.thenault@logilab.fr>
Mon, 22 Dec 2008 17:35:07 +0100
changeset 259 d7bb01e5be9d
parent 254 b1eda3dd844a
child 266 b36670d730b2
permissions -rw-r--r--
2.99.99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""This file contains some basic selectors required by application objects.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
A selector is responsible to score how well an object may be used with a
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
given result set (publishing time selection)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
:organization: Logilab
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
from logilab.common.compat import all
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    14
from logilab.common.deprecation import deprecated_function
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
230
6e71adc5ccdd new has_related_entities selector
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 215
diff changeset
    16
from cubicweb import Unauthorized, NoSelectableObject, role
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
from cubicweb.cwvreg import DummyCursorError
149
a7cd4296a85e cleaning code
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 148
diff changeset
    18
from cubicweb.vregistry import chainall, chainfirst, NoSelectableObject
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
from cubicweb.cwconfig import CubicWebConfiguration
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
from cubicweb.schema import split_expression
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
def lltrace(selector):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
    # don't wrap selectors if not in development mode
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
    if CubicWebConfiguration.mode == 'installed':
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
        return selector
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
    def traced(cls, *args, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
        ret = selector(cls, *args, **kwargs)
215
dfb4ca3ffe78 cleaning code
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 213
diff changeset
    29
        cls.lldebug('selector %s returned %s for %s', selector.__name__, ret, cls)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
        return ret
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
    return traced
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
# very basic selectors ########################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    35
def yes(cls, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
    """accept everything"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    return 1
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    38
yes_selector = deprecated_function(yes)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    41
def none_rset(cls, req, rset, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
    """accept no result set"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
    if rset is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
    return 0
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    46
norset_selector = deprecated_function(none_rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    49
def rset(cls, req, rset, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    """accept result set, whatever the number of result"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
    if rset is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
    return 0
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    54
rset_selector = deprecated_function(rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    55
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    56
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    57
def nonempty_rset(cls, req, rset, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
    """accept any non empty result set"""
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    59
    if rset is not None and rset.rowcount:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
    return 0
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    62
anyrset_selector = deprecated_function(nonempty_rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    65
def empty_rset(cls, req, rset, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
    """accept empty result set"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    if rset is not None and rset.rowcount == 0:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
    return 0
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    70
emptyrset_selector = deprecated_function(empty_rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    73
def one_line_rset(cls, req, rset, row=None, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
    """accept result set with a single line of result"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
    if rset is not None and (row is not None or rset.rowcount == 1):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
    return 0
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    78
onelinerset_selector = deprecated_function(one_line_rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    81
def two_lines_rset(cls, req, rset, *args, **kwargs):
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    82
    """accept result set with *at least* two lines of result"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
    if rset is not None and rset.rowcount > 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
    return 0
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    86
twolinerset_selector = deprecated_function(two_lines_rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    89
def two_cols_rset(cls, req, rset, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    """accept result set with at least one line and two columns of result"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    91
    if rset is not None and rset.rowcount > 0 and len(rset.rows[0]) > 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
    return 0
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    94
twocolrset_selector = deprecated_function(two_cols_rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    96
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
    97
def paginated_rset(cls, req, rset, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    98
    """accept result sets with more rows than the page size
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    99
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   100
    if rset is None or len(rset) <= req.property_value('navigation.page-size'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   101
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   102
    return 1
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   103
largerset_selector = deprecated_function(paginated_rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   104
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   105
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   106
def sorted_rset(cls, req, rset, row=None, col=None):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   107
    """accept sorted result set"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   108
    rqlst = rset.syntax_tree()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   109
    if len(rqlst.children) > 1 or not rqlst.children[0].orderby:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   110
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   111
    return 2
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   112
sortedrset_selector = deprecated_function(sorted_rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   113
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   114
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   115
def one_etype_rset(cls, req, rset, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   116
    """accept result set where entities in the first columns are all of the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   117
    same type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   118
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   119
    if len(rset.column_types(0)) != 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   120
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   121
    return 1
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   122
oneetyperset_selector = deprecated_function(one_etype_rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   123
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   124
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   125
def two_etypes_rset(cls, req, rset, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   126
    """accepts resultsets containing several entity types"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   127
    if rset:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   128
        etypes = rset.column_types(0)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   129
        if len(etypes) > 1:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   130
            return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   131
    return 0
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   132
multitype_selector = deprecated_function(two_etypes_rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   133
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   134
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   135
def match_search_state(cls, req, rset, row=None, col=None, **kwargs):
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   136
    """checks if the current search state is in a .search_states attribute of
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   137
    the wrapped class
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   138
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   139
    search state should be either 'normal' or 'linksearch' (eg searching for an
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   140
    object to create a relation with another)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   141
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   142
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   143
        if not req.search_state[0] in cls.search_states:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   144
            return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   145
    except AttributeError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   146
        return 1 # class don't care about search state, accept it
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   147
    return 1
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   148
searchstate_selector = deprecated_function(match_search_state)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   149
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   150
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   151
def anonymous_user(cls, req, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   152
    """accept if user is anonymous"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   153
    if req.cnx.anonymous_connection:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   154
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   155
    return 0
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   156
anonymous_selector = deprecated_function(anonymous_user)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   157
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   158
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   159
def authenticated_user(cls, req, *args, **kwargs):
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   160
    """accept if user is authenticated"""
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   161
    return not anonymous_selector(cls, req, *args, **kwargs)
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   162
not_anonymous_selector = deprecated_function(authenticated_user)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   163
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   164
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   165
def match_form_params(cls, req, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   166
    """check if parameters specified by the form_params attribute on
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   167
    the wrapped class are specified in request form parameters
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   168
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   169
    score = 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   170
    for param in cls.form_params:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   171
        val = req.form.get(param)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   172
        if not val:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   173
            return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   174
        score += 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   175
    return score + 1
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   176
req_form_params_selector = deprecated_function(match_form_params)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   177
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   178
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   179
def match_kwargs(cls, req, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   180
    """check if arguments specified by the expected_kwargs attribute on
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   181
    the wrapped class are specified in given named parameters
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   182
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   183
    values = []
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   184
    for arg in cls.expected_kwargs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   185
        if not arg in kwargs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   186
            return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   187
    return 1
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   188
kwargs_selector = deprecated_function(match_kwargs)
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   189
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   190
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   191
# not so basic selectors ######################################################
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   192
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   193
@lltrace
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 236
diff changeset
   194
def accept_etype(cls, req, *args, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   195
    """check etype presence in request form *and* accepts conformance"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   196
    if 'etype' not in req.form and 'etype' not in kwargs:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   197
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   198
    try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   199
        etype = req.form['etype']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   200
    except KeyError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   201
        etype = kwargs['etype']
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   202
    # value is a list or a tuple if web request form received several
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   203
    # values for etype parameter
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   204
    assert isinstance(etype, basestring), "got multiple etype parameters in req.form"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   205
    if 'Any' in cls.accepts:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   206
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   207
    # no Any found, we *need* exact match
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   208
    if etype not in cls.accepts:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   209
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   210
    # exact match must return a greater value than 'Any'-match
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   211
    return 2
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 236
diff changeset
   212
etype_form_selector = deprecated_function(accept_etype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   213
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   214
@lltrace
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   215
def _non_final_entity(cls, req, rset, row=None, col=None, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   216
    """accept non final entities
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   217
    if row is not specified, use the first one
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   218
    if col is not specified, use the first one
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   219
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   220
    etype = rset.description[row or 0][col or 0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   221
    if etype is None: # outer join
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   222
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   223
    if cls.schema.eschema(etype).is_final():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   224
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   225
    return 1
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   226
_nfentity_selector = deprecated_function(_non_final_entity)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   227
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   228
@lltrace
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   229
def _rql_condition(cls, req, rset, row=None, col=None, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   230
    """accept single entity result set if the entity match an rql condition
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   231
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   232
    if cls.condition:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   233
        eid = rset[row or 0][col or 0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   234
        if 'U' in frozenset(split_expression(cls.condition)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   235
            rql = 'Any X WHERE X eid %%(x)s, U eid %%(u)s, %s' % cls.condition
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   236
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   237
            rql = 'Any X WHERE X eid %%(x)s, %s' % cls.condition
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   238
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   239
            return len(req.execute(rql, {'x': eid, 'u': req.user.eid}, 'x'))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   240
        except Unauthorized:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   241
            return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   242
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   243
    return 1
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   244
_rqlcondition_selector = deprecated_function(_rql_condition)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   245
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   246
@lltrace
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   247
def _implement_interface(cls, req, rset, row=None, col=None, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   248
    """accept uniform result sets, and apply the following rules:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   249
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   250
    * wrapped class must have a accepts_interfaces attribute listing the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   251
      accepted ORed interfaces
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   252
    * if row is None, return the sum of values returned by the method
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   253
      for each entity's class in the result set. If any score is 0,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   254
      return 0.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   255
    * if row is specified, return the value returned by the method with
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   256
      the entity's class of this row
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   257
    """
184
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   258
    # XXX this selector can be refactored : extract the code testing
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   259
    #     for entity schema / interface compliance
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   260
    score = 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   261
    # check 'accepts' to give priority to more specific classes
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   262
    if row is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   263
        for etype in rset.column_types(col or 0):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   264
            eclass = cls.vreg.etype_class(etype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   265
            escore = 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   266
            for iface in cls.accepts_interfaces:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   267
                escore += iface.is_implemented_by(eclass)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   268
            if not escore:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   269
                return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   270
            score += escore
184
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   271
            accepts = set(getattr(cls, 'accepts', ()))
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   272
            # if accepts is defined on the vobject, eclass must match
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   273
            if accepts:
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   274
                eschema = eclass.e_schema
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   275
                etypes = set([eschema] + eschema.ancestors())
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   276
                if accepts & etypes:
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   277
                    score += 2
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   278
                elif 'Any' not in accepts:
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   279
                    return 0
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   280
        return score + 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   281
    etype = rset.description[row][col or 0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   282
    if etype is None: # outer join
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   283
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   284
    eclass = cls.vreg.etype_class(etype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   285
    for iface in cls.accepts_interfaces:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   286
        score += iface.is_implemented_by(eclass)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   287
    if score:
184
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   288
        accepts = set(getattr(cls, 'accepts', ()))
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   289
        # if accepts is defined on the vobject, eclass must match
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   290
        if accepts:
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   291
            eschema = eclass.e_schema
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   292
            etypes = set([eschema] + eschema.ancestors())
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   293
            if accepts & etypes:
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   294
                score += 1
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   295
            elif 'Any' not in accepts:
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   296
                return 0
92aebc6b533c fix interface_selector bug
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 162
diff changeset
   297
        score += 1
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   298
    return score
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   299
_interface_selector = deprecated_function(_implement_interface)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   300
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   301
@lltrace
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   302
def score_entity_selector(cls, req, rset, row=None, col=None, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   303
    if row is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   304
        rows = xrange(rset.rowcount)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   305
    else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   306
        rows = (row,)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   307
    for row in rows:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   308
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   309
            score = cls.score_entity(rset.get_entity(row, col or 0))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   310
        except DummyCursorError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   311
            # get a dummy cursor error, that means we are currently
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   312
            # using a dummy rset to list possible views for an entity
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   313
            # type, not for an actual result set. In that case, we
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   314
            # don't care of the value, consider the object as selectable
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   315
            return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   316
        if not score:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   317
            return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   318
    return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   319
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   320
@lltrace
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 236
diff changeset
   321
def accept_rset(cls, req, rset, row=None, col=None, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   322
    """simply delegate to cls.accept_rset method"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   323
    return cls.accept_rset(req, rset, row=row, col=col)
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 236
diff changeset
   324
accept_rset_selector = deprecated_function(accept_rset)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   325
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   326
@lltrace
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   327
def but_etype(cls, req, rset, row=None, col=None, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   328
    """restrict the searchstate_accept_one_selector to exclude entity's type
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   329
    refered by the .etype attribute
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   330
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   331
    if rset.description[row or 0][col or 0] == cls.etype:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   332
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   333
    return 1
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   334
but_etype_selector = deprecated_function(but_etype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   335
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   336
@lltrace
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   337
def etype_rtype_selector(cls, req, rset, row=None, col=None, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   338
    """only check if the user has read access on the entity's type refered
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   339
    by the .etype attribute and on the relations's type refered by the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   340
    .rtype attribute if set.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   341
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   342
    schema = cls.schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   343
    perm = getattr(cls, 'require_permission', 'read')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   344
    if hasattr(cls, 'etype'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   345
        eschema = schema.eschema(cls.etype)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   346
        if not (eschema.has_perm(req, perm) or eschema.has_local_role(perm)):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   347
            return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   348
    if hasattr(cls, 'rtype'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   349
        if not schema.rschema(cls.rtype).has_perm(req, perm):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   350
            return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   351
    return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   352
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   353
@lltrace
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   354
def has_relation(cls, req, rset, row=None, col=None, **kwargs):
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   355
    """check if the user has read access on the relations's type refered by the
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   356
    .rtype attribute of the class, and if all entities types in the
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   357
    result set has this relation.
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   358
    """
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   359
    if hasattr(cls, 'rtype'):
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   360
        perm = getattr(cls, 'require_permission', 'read')
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   361
        if not schema.rschema(cls.rtype).has_perm(req, perm):
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   362
            return 0
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   363
        if row is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   364
            for etype in rset.column_types(col or 0):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   365
                if not cls.relation_possible(etype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   366
                    return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   367
        elif not cls.relation_possible(rset.description[row][col or 0]):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   368
            return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   369
    return 1
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   370
accept_rtype_selector = deprecated_function(has_relation)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   371
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 236
diff changeset
   372
@lltrace
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   373
def one_has_relation(cls, req, rset, row=None, col=None, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   374
    """check if the user has read access on the relations's type refered by the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   375
    .rtype attribute of the class, and if at least one entity type in the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   376
    result set has this relation.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   377
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   378
    schema = cls.schema
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   379
    perm = getattr(cls, 'require_permission', 'read')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   380
    if not schema.rschema(cls.rtype).has_perm(req, perm):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   381
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   382
    if row is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   383
        for etype in rset.column_types(col or 0):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   384
            if cls.relation_possible(etype):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   385
                return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   386
    elif cls.relation_possible(rset.description[row][col or 0]):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   387
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   388
    return 0
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   389
one_has_relation_selector = deprecated_function(one_has_relation)
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   390
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   391
@lltrace
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   392
def has_related_entities(cls, req, rset, row=None, col=None, **kwargs):
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   393
    return bool(rset.get_entity(row or 0, col or 0).related(cls.rtype, role(cls)))
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   394
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   395
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   396
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   397
def match_user_group(cls, req, rset=None, row=None, col=None, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   398
    """select according to user's groups"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   399
    if not cls.require_groups:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   400
        return 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   401
    user = req.user
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   402
    if user is None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   403
        return int('guests' in cls.require_groups)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   404
    score = 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   405
    if 'owners' in cls.require_groups and rset:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   406
        if row is not None:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   407
            eid = rset[row][col or 0]
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   408
            if user.owns(eid):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   409
                score = 1
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   410
        else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   411
            score = all(user.owns(r[col or 0]) for r in rset)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   412
    score += user.matching_groups(cls.require_groups)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   413
    if score:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   414
        # add 1 so that an object with one matching group take priority
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   415
        # on an object without require_groups
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   416
        return score + 1 
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   417
    return 0
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   418
in_group_selector = deprecated_function(match_user_group)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   419
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   420
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   421
def user_can_add_etype(cls, req, rset, row=None, col=None, **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   422
    """only check if the user has add access on the entity's type refered
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   423
    by the .etype attribute.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   424
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   425
    if not cls.schema.eschema(cls.etype).has_perm(req, 'add'):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   426
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   427
    return 1
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   428
add_etype_selector = deprecated_function(user_can_add_etype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   429
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   430
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   431
def match_context_prop(cls, req, rset, row=None, col=None, context=None,
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   432
                       **kwargs):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   433
    propval = req.property_value('%s.%s.context' % (cls.__registry__, cls.id))
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   434
    if not propval:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   435
        propval = cls.context
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   436
    if context is not None and propval is not None and context != propval:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   437
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   438
    return 1
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   439
contextprop_selector = deprecated_function(match_context_prop)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   440
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   441
@lltrace
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   442
def primary_view(cls, req, rset, row=None, col=None, view=None,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   443
                          **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   444
    if view is not None and not view.is_primary():
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   445
        return 0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   446
    return 1
236
8ab9e72dd8f1 begin renaming for selectors, keeping bw compat
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 230
diff changeset
   447
primaryview_selector = deprecated_function(primary_view)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   448
142
0425ee84cfa6 add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   449
def appobject_selectable(registry, oid):
161
328bc32b5701 repairing error from preceding commit
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 157
diff changeset
   450
    """return a selector that will have a positive score if an object for the
328bc32b5701 repairing error from preceding commit
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 157
diff changeset
   451
    given registry and object id is selectable for the input context
328bc32b5701 repairing error from preceding commit
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 157
diff changeset
   452
    """
142
0425ee84cfa6 add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   453
    @lltrace
0425ee84cfa6 add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   454
    def selector(cls, req, rset, *args, **kwargs):
0425ee84cfa6 add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   455
        try:
0425ee84cfa6 add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   456
            cls.vreg.select_object(registry, oid, req, rset, *args, **kwargs)
0425ee84cfa6 add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   457
            return 1
0425ee84cfa6 add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   458
        except NoSelectableObject:
0425ee84cfa6 add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   459
            return 0
0425ee84cfa6 add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   460
    return selector
0425ee84cfa6 add selector to test if result set is an object (for rss feed component)
Laure Bourgois <Laure.Bourgois@logilab.fr>
parents: 0
diff changeset
   461
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   462
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   463
# compound selectors ##########################################################
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   464
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   465
non_final_entity = chainall(nonempty_rset, _non_final_entity)
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   466
nfentity_selector = deprecated_function(non_final_entity)
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 236
diff changeset
   467
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   468
implement_interface = chainall(non_final_entity, _implement_interface)
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 236
diff changeset
   469
interface_selector = deprecated_function(implement_interface)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   470
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   471
accept = chainall(non_final_entity, accept_rset)
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 236
diff changeset
   472
accept_selector = deprecated_function(accept)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   473
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   474
accept_one = chainall(one_line_rset, accept_selector)
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   475
accept_one_selector = deprecated_function(accept_one)
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 236
diff changeset
   476
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   477
rql_condition = chainall(non_final_entity, one_line_rset, _rql_condition)
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   478
rqlcondition_selector = deprecated_function(rql_condition)
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   479
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   480
254
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   481
searchstate_accept = chainall(nonempty_rset, match_search_state, accept)
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   482
searchstate_accept_selector = deprecated_function(searchstate_accept)
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   483
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   484
searchstate_accept_one = chainall(one_line_rset, match_search_state,
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   485
                                  accept, _rql_condition)
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   486
searchstate_accept_one_selector = deprecated_function(searchstate_accept_one)
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   487
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   488
searchstate_accept_one_but_etype = chainall(searchstate_accept_one, but_etype)
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   489
searchstate_accept_one_but_etype_selector = deprecated_function(
b1eda3dd844a more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 247
diff changeset
   490
    searchstate_accept_one_but_etype)
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   491
237
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 236
diff changeset
   492
#__all__ = [name for name in globals().keys() if name.endswith('selector')]
3df2e0ae2eba begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents: 236
diff changeset
   493
#__all__ += ['chainall', 'chainfirst']