cubicweb/web/test/data/views.py
author Philippe Pepiot <ph@itsalwaysdns.eu>
Tue, 31 Mar 2020 19:15:03 +0200
changeset 12957 0c973204033a
parent 11157 42fa15632493
permissions -rw-r--r--
[server] prevent returning closed cursor to the database pool In since c8c6ad8 init_repository use repo.internal_cnx() instead of repo.system_source.get_connection() so it use the pool and we should not close cursors from the pool before returning it back. Otherwise we may have "connection already closed" error. This bug only trigger when connection-pool-size = 1. Since we are moving to use a dynamic pooler we need to get this fixed. This does not occur with sqlite since the connection wrapper instantiate new cursor everytime, but this occur with other databases.
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
11157
42fa15632493 [web/test] return the hash of uploaded files in FileUploadTC instead of their contents
Julien Cristau <julien.cristau@logilab.fr>
parents: 11153
diff changeset
    19
import hashlib
42fa15632493 [web/test] return the hash of uploaded files in FileUploadTC instead of their contents
Julien Cristau <julien.cristau@logilab.fr>
parents: 11153
diff changeset
    20
10302
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    21
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
    22
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
    23
from cubicweb.web.views.ibreadcrumbs import IBreadCrumbsAdapter
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
11157
42fa15632493 [web/test] return the hash of uploaded files in FileUploadTC instead of their contents
Julien Cristau <julien.cristau@logilab.fr>
parents: 11153
diff changeset
    25
def _recursive_replace_stream_by_md5(tree):
9147
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    26
    """ Search for streams (i.e. object that have a 'read' method) in a tree
11157
42fa15632493 [web/test] return the hash of uploaded files in FileUploadTC instead of their contents
Julien Cristau <julien.cristau@logilab.fr>
parents: 11153
diff changeset
    27
    (whose branches are lists or tuples), and substitute them by their md5 hash,
9147
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    28
    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
    29
    branches is returned.
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    30
    """
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    31
    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
    32
        if hasattr(tree, 'read'):
11157
42fa15632493 [web/test] return the hash of uploaded files in FileUploadTC instead of their contents
Julien Cristau <julien.cristau@logilab.fr>
parents: 11153
diff changeset
    33
            return hashlib.md5(tree.read()).hexdigest()
9147
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    34
        return tree
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    35
    else:
11157
42fa15632493 [web/test] return the hash of uploaded files in FileUploadTC instead of their contents
Julien Cristau <julien.cristau@logilab.fr>
parents: 11153
diff changeset
    36
        return [_recursive_replace_stream_by_md5(value)
9147
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    37
                for value in tree]            
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    38
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    39
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    40
@ajaxfunc(output_type='json')
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    41
def fileupload(self):
11153
e48db902af71 [web/test] Reword doc string for fileupload ajaxfunc
Julien Cristau <julien.cristau@logilab.fr>
parents: 11057
diff changeset
    42
    """ Return a json copy of the web request form in which uploaded files
11157
42fa15632493 [web/test] return the hash of uploaded files in FileUploadTC instead of their contents
Julien Cristau <julien.cristau@logilab.fr>
parents: 11153
diff changeset
    43
    are read and the received streams are replaced by their md5 hash.
9147
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    44
    """
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    45
    try:
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    46
        result_dict = {}
10662
10942ed172de [py3k] dict.iteritems → dict.items
Rémi Cardona <remi.cardona@logilab.fr>
parents: 10588
diff changeset
    47
        for key, value in self._cw.form.items():
11157
42fa15632493 [web/test] return the hash of uploaded files in FileUploadTC instead of their contents
Julien Cristau <julien.cristau@logilab.fr>
parents: 11153
diff changeset
    48
            result_dict[key] = _recursive_replace_stream_by_md5(value)
9147
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    49
        return result_dict
10588
fdaa0e4b7eaf [py3k] except as
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 10302
diff changeset
    50
    except Exception as ex:
9147
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    51
        import traceback as tb
01124cfd4b1f [etwist] fix handling of multiple files per field
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8544
diff changeset
    52
        tb.print_exc(ex)
10302
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    53
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    54
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    55
class FolderIBreadCrumbsAdapter(IBreadCrumbsAdapter):
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    56
    __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
    57
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    58
    def parent_entity(self):
7725396eb3df [web/test] Drop folder cube dependency
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 9402
diff changeset
    59
        return self.entity.filed_under[0]