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 |
|
19 from cubicweb.predicates import has_related_entities |
|
20 from cubicweb.web.views.ajaxcontroller import ajaxfunc |
|
21 from cubicweb.web.views.ibreadcrumbs import IBreadCrumbsAdapter |
|
22 |
|
23 def _recursive_replace_stream_by_content(tree): |
|
24 """ Search for streams (i.e. object that have a 'read' method) in a tree |
|
25 (which branches are lists or tuples), and substitute them by their content, |
|
26 leaving other leafs identical. A copy of the tree with only lists as |
|
27 branches is returned. |
|
28 """ |
|
29 if not isinstance(tree, (list, tuple)): |
|
30 if hasattr(tree, 'read'): |
|
31 return tree.read() |
|
32 return tree |
|
33 else: |
|
34 return [_recursive_replace_stream_by_content(value) |
|
35 for value in tree] |
|
36 |
|
37 |
|
38 @ajaxfunc(output_type='json') |
|
39 def fileupload(self): |
|
40 """ Return a json copy of the web request formin which uploaded files |
|
41 are read and their content substitute the received streams. |
|
42 """ |
|
43 try: |
|
44 result_dict = {} |
|
45 for key, value in self._cw.form.items(): |
|
46 result_dict[key] = _recursive_replace_stream_by_content(value) |
|
47 return result_dict |
|
48 except Exception as ex: |
|
49 import traceback as tb |
|
50 tb.print_exc(ex) |
|
51 |
|
52 |
|
53 class FolderIBreadCrumbsAdapter(IBreadCrumbsAdapter): |
|
54 __select__ = IBreadCrumbsAdapter.__select__ & has_related_entities('filed_under') |
|
55 |
|
56 def parent_entity(self): |
|
57 return self.entity.filed_under[0] |
|