13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
14 # details. |
14 # details. |
15 # |
15 # |
16 # You should have received a copy of the GNU Lesser General Public License along |
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/>. |
17 # with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
18 """abstract action classes for CubicWeb web client""" |
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 directed to perform the action. There are two common ways of |
|
29 writing that method: |
|
30 |
|
31 * do nothing special and simply return a URL to the current rset with |
|
32 a special view (with `self._cw.build_url(...)` for instance) |
|
33 |
|
34 * define an inner function `callback_func(req, *args)` which will do |
|
35 the work and call it through `self._cw.user_callback(callback_func, |
|
36 args, msg)`: this method will return a URL which calls the inner |
|
37 function, and displays the message in the web interface when the |
|
38 callback has completed (and report any exception occuring in the |
|
39 callback too) |
|
40 |
|
41 Many examples of the first approach are available in :mod:`cubicweb.web.views.actions`. |
|
42 |
|
43 Here is an example of the second approach: |
|
44 |
|
45 .. sourcecode:: python |
|
46 |
|
47 from cubicweb.web import action |
|
48 class SomeAction(action.Action): |
|
49 __regid__ = 'mycube_some_action' |
|
50 title = _(some action) |
|
51 __select__ = action.Action.__select__ & is_instance('TargetEntity') |
|
52 |
|
53 def url(self): |
|
54 if self.cw_row is None: |
|
55 eids = [row[0] for row in self.cw_rset] |
|
56 else: |
|
57 eids = (self.cw_rset[self.cw_row][self.cw_col or 0],) |
|
58 def do_action(req, eids): |
|
59 for eid in eids: |
|
60 entity = req.entity_from_eid(eid, 'TargetEntity') |
|
61 entity.perform_action() |
|
62 msg = self._cw._('some_action performed') |
|
63 return self._cw.user_callback(do_action, (eids,), msg) |
|
64 |
|
65 """ |
19 |
66 |
20 __docformat__ = "restructuredtext en" |
67 __docformat__ = "restructuredtext en" |
21 _ = unicode |
68 _ = unicode |
22 |
69 |
23 from cubicweb import target |
70 from cubicweb import target |