web/action.py
author Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
Wed, 18 Feb 2009 10:03:26 +0100
branchtls-sprint
changeset 757 01740274e774
parent 753 17d38f000bea
child 767 b094cf8535b4
permissions -rw-r--r--
remove explicit access to .im_func when overriding `registered`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""abstract action classes for CubicWeb web client
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
:organization: Logilab
631
99f5852f8604 major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents: 254
diff changeset
     4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
652
603c782dc092 various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 651
diff changeset
     9
from cubicweb import role, target
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 722
diff changeset
    10
from cubicweb.vregistry import objectify_selector
652
603c782dc092 various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 651
diff changeset
    11
from cubicweb.selectors import (relation_possible, match_search_state,
657
fd019f41aa2f work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 653
diff changeset
    12
                                one_line_rset, may_add_relation, yes,
fd019f41aa2f work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 653
diff changeset
    13
                                accepts_compat, condition_compat, deprecate)
722
50a99184cf47 update some imports
sylvain.thenault@logilab.fr
parents: 670
diff changeset
    14
from cubicweb.appobject import AppRsetObject
670
6c332f5c969c drop more registerers
sylvain.thenault@logilab.fr
parents: 661
diff changeset
    15
from cubicweb.common.registerers import accepts_registerer
653
189877d9547d use & operator instead of chainall, deprecate EntityAction, fix relation_possible action argument
sylvain.thenault@logilab.fr
parents: 652
diff changeset
    16
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
_ = unicode
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
class Action(AppRsetObject):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
    """abstract action. Handle the .search_states attribute to match
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
    request search state. 
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
    """
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
    __registry__ = 'actions'
670
6c332f5c969c drop more registerers
sylvain.thenault@logilab.fr
parents: 661
diff changeset
    25
    __registerer__ = accepts_registerer
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 722
diff changeset
    26
    __select__ = yes()
657
fd019f41aa2f work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 653
diff changeset
    27
    
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
    property_defs = {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
        'visible':  dict(type='Boolean', default=True,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
                         help=_('display the action or not')),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
        'order':    dict(type='Int', default=99,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
                         help=_('display order of the action')),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
        'category': dict(type='String', default='moreactions',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
                         vocabulary=('mainactions', 'moreactions', 'addrelated',
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
                                     'useractions', 'siteactions', 'hidden'),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
                         help=_('context where this component should be displayed')),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
    site_wide = True # don't want user to configuration actions eproperties
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
    category = 'moreactions'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    def url(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
        """return the url associated with this action"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
        raise NotImplementedError
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
    def html_class(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
        if self.req.selected(self.url()):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
            return 'selected'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
        if self.category:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
            return 'box' + self.category.capitalize()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
631
99f5852f8604 major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents: 254
diff changeset
    51
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
class UnregisteredAction(Action):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    53
    """non registered action used to build boxes. Unless you set them
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    54
    explicitly, .vreg and .schema attributes at least are None.
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
    category = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    57
    id = None
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    58
    
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    59
    def __init__(self, req, rset, title, path, **kwargs):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    60
        Action.__init__(self, req, rset)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    61
        self.title = req._(title)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    62
        self._path = path
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
        self.__dict__.update(kwargs)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
        
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
    def url(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
        return self._path
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
640
8e64f12be69c drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents: 631
diff changeset
    69
class LinkToEntityAction(Action):
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
    """base class for actions consisting to create a new object
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
    with an initial relation set to an entity.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
    Additionaly to EntityAction behaviour, this class is parametrized
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
    using .etype, .rtype and .target attributes to check if the
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
    action apply and if the logged user has access to it
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
    """
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 722
diff changeset
    76
    @objectify_selector
631
99f5852f8604 major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents: 254
diff changeset
    77
    def my_selector(cls, req, rset, row=None, col=0, **kwargs):
753
17d38f000bea some minor __select__ composition fixes
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 742
diff changeset
    78
        selector = (match_search_state('normal') & one_line_rset()
653
189877d9547d use & operator instead of chainall, deprecate EntityAction, fix relation_possible action argument
sylvain.thenault@logilab.fr
parents: 652
diff changeset
    79
                    & relation_possible(cls.rtype, role(cls), cls.etype,
189877d9547d use & operator instead of chainall, deprecate EntityAction, fix relation_possible action argument
sylvain.thenault@logilab.fr
parents: 652
diff changeset
    80
                                        action='add')
189877d9547d use & operator instead of chainall, deprecate EntityAction, fix relation_possible action argument
sylvain.thenault@logilab.fr
parents: 652
diff changeset
    81
                    & may_add_relation(cls.rtype, role(cls)))
652
603c782dc092 various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 651
diff changeset
    82
        return selector(cls, req, rset, row, col, **kwargs)
603c782dc092 various SyntaxErrors / missing import fixes + reorganization of the `registered` classmethod
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 651
diff changeset
    83
742
99115e029dca replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 722
diff changeset
    84
    __select__ = my_selector()
757
01740274e774 remove explicit access to .im_func when overriding `registered`
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 753
diff changeset
    85
    registered = accepts_compat(Action.registered)
631
99f5852f8604 major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents: 254
diff changeset
    86
    
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    87
    category = 'addrelated'
631
99f5852f8604 major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents: 254
diff changeset
    88
                
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
    def url(self):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
        current_entity = self.rset.get_entity(self.row or 0, self.col or 0)
631
99f5852f8604 major selector refactoring (mostly to avoid looking for select parameters on the target class), start accept / interface unification)
sylvain.thenault@logilab.fr
parents: 254
diff changeset
    91
        linkto = '%s:%s:%s' % (self.rtype, current_entity.eid, target(self))
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
        return self.build_url(vid='creation', etype=self.etype,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    93
                              __linkto=linkto,
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    94
                              __redirectpath=current_entity.rest_path(), # should not be url quoted!
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    95
                              __redirectvid=self.req.form.get('__redirectvid', ''))
653
189877d9547d use & operator instead of chainall, deprecate EntityAction, fix relation_possible action argument
sylvain.thenault@logilab.fr
parents: 652
diff changeset
    96
657
fd019f41aa2f work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 653
diff changeset
    97
class EntityAction(Action):
fd019f41aa2f work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 653
diff changeset
    98
    """DEPRECATED / BACKWARD COMPAT
fd019f41aa2f work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 653
diff changeset
    99
    """
fd019f41aa2f work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 653
diff changeset
   100
    registered = deprecate(accepts_compat(condition_compat(Action.registered)),
fd019f41aa2f work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 653
diff changeset
   101
                           msg='EntityAction is deprecated, use Action with '
fd019f41aa2f work in progress - fix deprecation of EntityAction - fix various selectors bugs
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 653
diff changeset
   102
                           'appropriate selectors')
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
   103