web/test/data/views.py
changeset 9147 01124cfd4b1f
parent 8544 3d049071957e
child 9402 2c48c091b6a2
equal deleted inserted replaced
9146:9b58a6406a64 9147:01124cfd4b1f
    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 
    18 
    19 from cubicweb.web import Redirect
    19 from cubicweb.web import Redirect
    20 from cubicweb.web.application import CubicWebPublisher
    20 from cubicweb.web.application import CubicWebPublisher
       
    21 from cubicweb.web.views.ajaxcontroller import ajaxfunc
    21 
    22 
    22 # proof of concept : monkey patch handle method so that if we are in an
    23 # proof of concept : monkey patch handle method so that if we are in an
    23 # anonymous session and __fblogin is found is req.form, the user with the
    24 # anonymous session and __fblogin is found is req.form, the user with the
    24 # given login is created if necessary and then a session is opened for that
    25 # given login is created if necessary and then a session is opened for that
    25 # user
    26 # user
    38         except Redirect:
    39         except Redirect:
    39             pass
    40             pass
    40         assert req.user.login == login
    41         assert req.user.login == login
    41     return orig_handle(self, req, path)
    42     return orig_handle(self, req, path)
    42 
    43 
       
    44 
       
    45 def _recursive_replace_stream_by_content(tree):
       
    46     """ Search for streams (i.e. object that have a 'read' method) in a tree
       
    47     (which branches are lists or tuples), and substitute them by their content,
       
    48     leaving other leafs identical. A copy of the tree with only lists as
       
    49     branches is returned.
       
    50     """
       
    51     if not isinstance(tree, (list, tuple)):
       
    52         if hasattr(tree, 'read'):
       
    53             return tree.read()
       
    54         return tree
       
    55     else:
       
    56         return [_recursive_replace_stream_by_content(value)
       
    57                 for value in tree]            
       
    58 
       
    59 
       
    60 @ajaxfunc(output_type='json')
       
    61 def fileupload(self):
       
    62     """ Return a json copy of the web request formin which uploaded files
       
    63     are read and their content substitute the received streams.
       
    64     """
       
    65     try:
       
    66         result_dict = {}
       
    67         for key, value in self._cw.form.iteritems():
       
    68             result_dict[key] = _recursive_replace_stream_by_content(value)
       
    69         return result_dict
       
    70     except Exception, ex:
       
    71         import traceback as tb
       
    72         tb.print_exc(ex)
       
    73 
    43 orig_handle = CubicWebPublisher.main_handle_request
    74 orig_handle = CubicWebPublisher.main_handle_request
    44 CubicWebPublisher.main_handle_request = auto_login_handle_request
    75 CubicWebPublisher.main_handle_request = auto_login_handle_request