cubicweb/pyramid/tools.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Tue, 21 Feb 2017 08:54:20 +0100
changeset 11968 bb0dfc7d2d0e
parent 11967 83739be20fab
child 11987 d432911e3c26
permissions -rw-r--r--
[skeleton,pyramid] Move pyramid app definition in cubicweb.pyramid module The application definition is actually not specific to the final "cube" being bootstrapped from skeleton. This patch thus move the pyramid application function into cubicweb.pyramid module and let cubicweb register the "paste.app_factory" entry point (instead of the bootstrapped cube). Useless call to `config.scan` is dropped along the way.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11967
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
     1
# copyright 2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
     2
# copyright 2014-2016 UNLISH S.A.S. (Montpellier, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
     3
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
     4
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
     5
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
     6
# This file is part of CubicWeb.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
     7
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
     8
# CubicWeb is free software: you can redistribute it and/or modify it under the
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
     9
# terms of the GNU Lesser General Public License as published by the Free
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
    10
# Software Foundation, either version 2.1 of the License, or (at your option)
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
    11
# any later version.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
    12
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
    13
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
    14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
    15
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
    16
# details.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
    17
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
    18
# You should have received a copy of the GNU Lesser General Public License along
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
    19
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11958
diff changeset
    20
11551
444cd2bba89d [doc] Document tools
Christophe de Vienne <christophe@unlish.com>
parents: 11550
diff changeset
    21
"""Various tools.
444cd2bba89d [doc] Document tools
Christophe de Vienne <christophe@unlish.com>
parents: 11550
diff changeset
    22
444cd2bba89d [doc] Document tools
Christophe de Vienne <christophe@unlish.com>
parents: 11550
diff changeset
    23
.. warning::
444cd2bba89d [doc] Document tools
Christophe de Vienne <christophe@unlish.com>
parents: 11550
diff changeset
    24
444cd2bba89d [doc] Document tools
Christophe de Vienne <christophe@unlish.com>
parents: 11550
diff changeset
    25
    This module should be considered as internal implementation details. Use
444cd2bba89d [doc] Document tools
Christophe de Vienne <christophe@unlish.com>
parents: 11550
diff changeset
    26
    with caution, as the API may change without notice.
444cd2bba89d [doc] Document tools
Christophe de Vienne <christophe@unlish.com>
parents: 11550
diff changeset
    27
"""
11550
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    28
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    29
#: A short-term cache for user clones.
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
#: used by cached_build_user to speed-up repetitive calls to build_user
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
#: The expiration is handled in a dumb and brutal way: the whole cache is
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
#: cleared every 5 minutes.
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    33
_user_cache = {}
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    34
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    35
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    36
def clone_user(repo, user):
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    37
    """Clone a CWUser instance.
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    38
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    39
    .. warning::
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    40
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    41
        The returned clone is detached from any cnx.
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    42
        Before using it in any way, it should be attached to a cnx that has not
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    43
        this user already loaded.
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    44
    """
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    45
    CWUser = repo.vreg['etypes'].etype_class('CWUser')
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    46
    clone = CWUser(
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    47
        None,
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    48
        rset=user.cw_rset.copy(),
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    49
        row=user.cw_row,
11701
ca536eec556b [pyramid] Retrieve user's groups using a custom RQL instead of user's groups property
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11631
diff changeset
    50
        col=user.cw_col)
11550
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
    clone.cw_attr_cache = dict(user.cw_attr_cache)
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    52
    return clone
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    53
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    54
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    55
def cnx_attach_entity(cnx, entity):
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    56
    """Attach an entity to a cnx."""
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    57
    entity._cw = cnx
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    58
    if entity.cw_rset:
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    59
        entity.cw_rset.req = cnx
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    60
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    61
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    62
def cached_build_user(repo, eid):
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    63
    """Cached version of
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    64
    :meth:`cubicweb.server.repository.Repository._build_user`
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    65
    """
11827
851b6bd79b50 [pyramid] No more need for an internal connection when user is cached
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11824
diff changeset
    66
    if eid in _user_cache:
851b6bd79b50 [pyramid] No more need for an internal connection when user is cached
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11824
diff changeset
    67
        user, lang = _user_cache[eid]
851b6bd79b50 [pyramid] No more need for an internal connection when user is cached
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11824
diff changeset
    68
        entity = clone_user(repo, user)
851b6bd79b50 [pyramid] No more need for an internal connection when user is cached
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11824
diff changeset
    69
        return entity, lang
851b6bd79b50 [pyramid] No more need for an internal connection when user is cached
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11824
diff changeset
    70
11550
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    71
    with repo.internal_cnx() as cnx:
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    72
        user = repo._build_user(cnx, eid)
11824
d7ecf6dab085 Fix AttributeError for "lang" on repo/client connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11811
diff changeset
    73
        lang = user.prefered_language()
11550
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    74
        user.cw_clear_relation_cache()
11824
d7ecf6dab085 Fix AttributeError for "lang" on repo/client connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11811
diff changeset
    75
        _user_cache[eid] = (clone_user(repo, user), lang)
d7ecf6dab085 Fix AttributeError for "lang" on repo/client connections
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11811
diff changeset
    76
        return user, lang
11550
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    77
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    78
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    79
def clear_cache():
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    80
    """Clear the user cache"""
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    81
    _user_cache.clear()
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    82
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    83
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    84
def includeme(config):
11551
444cd2bba89d [doc] Document tools
Christophe de Vienne <christophe@unlish.com>
parents: 11550
diff changeset
    85
    """Start the cache maintenance loop task.
444cd2bba89d [doc] Document tools
Christophe de Vienne <christophe@unlish.com>
parents: 11550
diff changeset
    86
11958
950ce7d9f642 [pyramid] Rename make_cubicweb_application function as config_from_cwconfig
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11827
diff changeset
    87
    Automatically included by :mod:`cubicweb.pyramid`.
11551
444cd2bba89d [doc] Document tools
Christophe de Vienne <christophe@unlish.com>
parents: 11550
diff changeset
    88
    """
11550
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    89
    repo = config.registry['cubicweb.repository']
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    90
    interval = int(config.registry.settings.get(
11811
f09efeead7f9 Fix broken flake8 configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 11701
diff changeset
    91
        'cubicweb.usercache.expiration_time', 60 * 5))
11550
38ed4c3ac3de [tools] Provide a faster build_user
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    92
    repo.looping_task(interval, clear_cache)