web/test/data/views.py
author Rémi Cardona <remi.cardona@logilab.fr>
Wed, 16 Sep 2015 16:02:04 +0200
changeset 10692 14e7ddb0f670
parent 10662 10942ed172de
permissions -rw-r--r--
[py3k] unicode → six.text_type
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8544
3d049071957e massive copyright update to avoid clutering later patches
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8312
diff changeset
     1
# copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4212
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
1977
606923dff11b big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 1802
diff changeset
    18
10302
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    19
from cubicweb.predicates import has_related_entities
9147
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    20
from cubicweb.web.views.ajaxcontroller import ajaxfunc
10302
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    21
from cubicweb.web.views.ibreadcrumbs import IBreadCrumbsAdapter
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
9147
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    23
def _recursive_replace_stream_by_content(tree):
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    24
    """ Search for streams (i.e. object that have a 'read' method) in a tree
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    25
    (which branches are lists or tuples), and substitute them by their content,
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    26
    leaving other leafs identical. A copy of the tree with only lists as
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    27
    branches is returned.
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    28
    """
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    29
    if not isinstance(tree, (list, tuple)):
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    30
        if hasattr(tree, 'read'):
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    31
            return tree.read()
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    32
        return tree
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    33
    else:
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    34
        return [_recursive_replace_stream_by_content(value)
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    35
                for value in tree]            
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    36
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    37
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    38
@ajaxfunc(output_type='json')
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    39
def fileupload(self):
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    40
    """ Return a json copy of the web request formin which uploaded files
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    41
    are read and their content substitute the received streams.
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    42
    """
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    43
    try:
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    44
        result_dict = {}
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10588
diff changeset
    45
        for key, value in self._cw.form.items():
9147
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    46
            result_dict[key] = _recursive_replace_stream_by_content(value)
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    47
        return result_dict
10588
fdaa0e4b7eaf [py3k] except as
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10302
diff changeset
    48
    except Exception as ex:
9147
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    49
        import traceback as tb
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    50
        tb.print_exc(ex)
10302
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    51
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    52
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    53
class FolderIBreadCrumbsAdapter(IBreadCrumbsAdapter):
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    54
    __select__ = IBreadCrumbsAdapter.__select__ & has_related_entities('filed_under')
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    55
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    56
    def parent_entity(self):
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    57
        return self.entity.filed_under[0]