|
1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
3 # |
|
4 # This file is part of CubicWeb. |
|
5 # |
|
6 # CubicWeb is free software: you can redistribute it and/or modify it under the |
|
7 # terms of the GNU Lesser General Public License as published by the Free |
|
8 # Software Foundation, either version 2.1 of the License, or (at your option) |
|
9 # any later version. |
|
10 # |
|
11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
|
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|
14 # details. |
|
15 # |
|
16 # You should have received a copy of the GNU Lesser General Public License along |
|
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
|
18 """abstract action classes for CubicWeb web client |
|
19 |
|
20 Actions are typically displayed in an action box, but can also be used |
|
21 in other parts of the interface (the user menu, the footer, etc.). The |
|
22 'order', 'category' and 'title' class attributes control how the action will |
|
23 be displayed. The 'submenu' attribute is only used for actions in the |
|
24 action box. |
|
25 |
|
26 The most important method from a developper point of view in the |
|
27 :meth:'Action.url' method, which returns a URL on which the navigation |
|
28 should be directed to perform the action. The common way of |
|
29 writing that method is to simply return a URL to the current rset with a |
|
30 special view (with `self._cw.build_url(...)` for instance) |
|
31 |
|
32 Many examples are available in :mod:`cubicweb.web.views.actions`. |
|
33 """ |
|
34 |
|
35 __docformat__ = "restructuredtext en" |
|
36 from cubicweb import _ |
|
37 |
|
38 from cubicweb import target |
|
39 from cubicweb.predicates import (partial_relation_possible, match_search_state, |
|
40 one_line_rset) |
|
41 from cubicweb.appobject import AppObject |
|
42 |
|
43 |
|
44 class Action(AppObject): |
|
45 """abstract action. Handle the .search_states attribute to match |
|
46 request search state. |
|
47 """ |
|
48 __registry__ = 'actions' |
|
49 __select__ = match_search_state('normal') |
|
50 order = 99 |
|
51 category = 'moreactions' |
|
52 # actions in category 'moreactions' can specify a sub-menu in which they should be filed |
|
53 submenu = None |
|
54 |
|
55 def actual_actions(self): |
|
56 yield self |
|
57 |
|
58 def fill_menu(self, box, menu): |
|
59 """add action(s) to the given submenu of the given box""" |
|
60 for action in self.actual_actions(): |
|
61 menu.append(box.action_link(action)) |
|
62 |
|
63 def html_class(self): |
|
64 if self._cw.selected(self.url()): |
|
65 return 'selected' |
|
66 |
|
67 def build_action(self, title, url, **kwargs): |
|
68 return UnregisteredAction(self._cw, title, url, **kwargs) |
|
69 |
|
70 def url(self): |
|
71 """return the url associated with this action""" |
|
72 raise NotImplementedError |
|
73 |
|
74 |
|
75 class UnregisteredAction(Action): |
|
76 """non registered action, used to build boxes""" |
|
77 category = None |
|
78 id = None |
|
79 |
|
80 def __init__(self, req, title, url, **kwargs): |
|
81 Action.__init__(self, req) |
|
82 self.title = req._(title) |
|
83 self._url = url |
|
84 self.__dict__.update(kwargs) |
|
85 |
|
86 def url(self): |
|
87 return self._url |
|
88 |
|
89 |
|
90 class LinkToEntityAction(Action): |
|
91 """base class for actions consisting to create a new object with an initial |
|
92 relation set to an entity. |
|
93 |
|
94 Additionally to EntityAction behaviour, this class is parametrized using |
|
95 .rtype, .role and .target_etype attributes to check if the action apply and |
|
96 if the logged user has access to it (see |
|
97 :class:`~cubicweb.selectors.partial_relation_possible` selector |
|
98 documentation for more information). |
|
99 """ |
|
100 __select__ = (match_search_state('normal') & one_line_rset() |
|
101 & partial_relation_possible(action='add', strict=True)) |
|
102 |
|
103 submenu = 'addrelated' |
|
104 # to be defined in concrete classes |
|
105 target_etype = rtype = None |
|
106 |
|
107 def url(self): |
|
108 ttype = self.target_etype |
|
109 entity = self.cw_rset.get_entity(self.cw_row or 0, self.cw_col or 0) |
|
110 linkto = '%s:%s:%s' % (self.rtype, entity.eid, target(self)) |
|
111 return self._cw.vreg["etypes"].etype_class(ttype).cw_create_url(self._cw, |
|
112 __redirectpath=entity.rest_path(), __linkto=linkto, |
|
113 __redirectvid=self._cw.form.get('__redirectvid', '')) |