author | sylvain.thenault@logilab.fr |
Mon, 16 Feb 2009 20:51:41 +0100 | |
branch | tls-sprint |
changeset 644 | 5e5b296a657e |
parent 640 | 8e64f12be69c |
child 648 | a89ba1054cb2 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract action classes for CubicWeb web client |
2 |
||
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 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
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
|
9 |
from cubicweb import target |
0 | 10 |
from cubicweb.common.appobject import AppRsetObject |
11 |
from cubicweb.common.registerers import action_registerer |
|
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
|
12 |
from cubicweb.common.selectors import user_can_add_etype, \ |
254
b1eda3dd844a
more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
13 |
match_search_state, searchstate_accept_one, \ |
b1eda3dd844a
more selector renaming
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
14 |
searchstate_accept_one_but_etype |
0 | 15 |
|
16 |
_ = unicode |
|
17 |
||
18 |
||
19 |
class Action(AppRsetObject): |
|
20 |
"""abstract action. Handle the .search_states attribute to match |
|
21 |
request search state. |
|
22 |
""" |
|
23 |
__registry__ = 'actions' |
|
24 |
__registerer__ = action_registerer |
|
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
|
25 |
|
0 | 26 |
property_defs = { |
27 |
'visible': dict(type='Boolean', default=True, |
|
28 |
help=_('display the action or not')), |
|
29 |
'order': dict(type='Int', default=99, |
|
30 |
help=_('display order of the action')), |
|
31 |
'category': dict(type='String', default='moreactions', |
|
32 |
vocabulary=('mainactions', 'moreactions', 'addrelated', |
|
33 |
'useractions', 'siteactions', 'hidden'), |
|
34 |
help=_('context where this component should be displayed')), |
|
35 |
} |
|
36 |
site_wide = True # don't want user to configuration actions eproperties |
|
37 |
category = 'moreactions' |
|
38 |
||
39 |
def url(self): |
|
40 |
"""return the url associated with this action""" |
|
41 |
raise NotImplementedError |
|
42 |
||
43 |
def html_class(self): |
|
44 |
if self.req.selected(self.url()): |
|
45 |
return 'selected' |
|
46 |
if self.category: |
|
47 |
return 'box' + self.category.capitalize() |
|
48 |
||
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
|
49 |
|
0 | 50 |
class UnregisteredAction(Action): |
51 |
"""non registered action used to build boxes. Unless you set them |
|
52 |
explicitly, .vreg and .schema attributes at least are None. |
|
53 |
""" |
|
54 |
category = None |
|
55 |
id = None |
|
56 |
||
57 |
def __init__(self, req, rset, title, path, **kwargs): |
|
58 |
Action.__init__(self, req, rset) |
|
59 |
self.title = req._(title) |
|
60 |
self._path = path |
|
61 |
self.__dict__.update(kwargs) |
|
62 |
||
63 |
def url(self): |
|
64 |
return self._path |
|
65 |
||
66 |
||
67 |
class AddEntityAction(Action): |
|
68 |
"""link to the entity creation form. Concrete class must set .etype and |
|
69 |
may override .vid |
|
70 |
""" |
|
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
|
71 |
__selectors__ = (user_can_add_etype,) |
0 | 72 |
vid = 'creation' |
73 |
etype = None |
|
74 |
||
75 |
def url(self): |
|
76 |
return self.build_url(vid=self.vid, etype=self.etype) |
|
77 |
||
78 |
||
79 |
class EntityAction(Action): |
|
80 |
"""an action for an entity. By default entity actions are only |
|
81 |
displayable on single entity result if accept match. |
|
82 |
""" |
|
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
|
83 |
# XXX deprecate |
0 | 84 |
|
85 |
||
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
86 |
class LinkToEntityAction(Action): |
0 | 87 |
"""base class for actions consisting to create a new object |
88 |
with an initial relation set to an entity. |
|
89 |
Additionaly to EntityAction behaviour, this class is parametrized |
|
90 |
using .etype, .rtype and .target attributes to check if the |
|
91 |
action apply and if the logged user has access to it |
|
92 |
""" |
|
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
|
93 |
def my_selector(cls, req, rset, row=None, col=0, **kwargs): |
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
|
94 |
return chainall(match_search_state('normal'), |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
95 |
one_line_rset, |
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
|
96 |
relation_possible(cls.rtype, role(cls), cls.etype, |
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
|
97 |
permission='add'), |
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
|
98 |
may_add_relation(cls.rtype, role(cls))) |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
99 |
__selectors__ = (my_selector,) |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
631
diff
changeset
|
100 |
registered = accepts_compat(Action.registered.im_func) |
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
|
101 |
|
0 | 102 |
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
|
103 |
|
0 | 104 |
def url(self): |
105 |
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
|
106 |
linkto = '%s:%s:%s' % (self.rtype, current_entity.eid, target(self)) |
0 | 107 |
return self.build_url(vid='creation', etype=self.etype, |
108 |
__linkto=linkto, |
|
109 |
__redirectpath=current_entity.rest_path(), # should not be url quoted! |
|
110 |
__redirectvid=self.req.form.get('__redirectvid', '')) |
|
111 |
||
112 |
||
113 |
class LinkToEntityAction2(LinkToEntityAction): |
|
114 |
"""LinkToEntity action where the action is not usable on the same |
|
115 |
entity's type as the one refered by the .etype attribute |
|
116 |
""" |
|
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
|
117 |
def my_selector(cls, req, rset, row=None, col=0, **kwargs): |
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
|
118 |
return chainall(match_search_state('normal'), |
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
|
119 |
but_etype, one_line_rset, accept, |
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
|
120 |
relation_possible(cls.rtype, role(cls), cls.etype), |
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
|
121 |
may_add_relation(cls.rtype, role(cls))) |
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
|
122 |
__selectors__ = my_selector, |
0 | 123 |