web/test/data/views.py
changeset 9402 2c48c091b6a2
parent 9147 01124cfd4b1f
child 10302 7725396eb3df
equal deleted inserted replaced
9127:aff75b69db92 9402:2c48c091b6a2
       
     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.web.views.ajaxcontroller import ajaxfunc
       
    20 
       
    21 def _recursive_replace_stream_by_content(tree):
       
    22     """ Search for streams (i.e. object that have a 'read' method) in a tree
       
    23     (which branches are lists or tuples), and substitute them by their content,
       
    24     leaving other leafs identical. A copy of the tree with only lists as
       
    25     branches is returned.
       
    26     """
       
    27     if not isinstance(tree, (list, tuple)):
       
    28         if hasattr(tree, 'read'):
       
    29             return tree.read()
       
    30         return tree
       
    31     else:
       
    32         return [_recursive_replace_stream_by_content(value)
       
    33                 for value in tree]            
       
    34 
       
    35 
       
    36 @ajaxfunc(output_type='json')
       
    37 def fileupload(self):
       
    38     """ Return a json copy of the web request formin which uploaded files
       
    39     are read and their content substitute the received streams.
       
    40     """
       
    41     try:
       
    42         result_dict = {}
       
    43         for key, value in self._cw.form.iteritems():
       
    44             result_dict[key] = _recursive_replace_stream_by_content(value)
       
    45         return result_dict
       
    46     except Exception, ex:
       
    47         import traceback as tb
       
    48         tb.print_exc(ex)