[pyramid] add a simple pyramid configuration to create static views for assets from cubes

authorDavid Douard <david.douard@logilab.fr>
changeset6f3f65423c3b
branchdefault
phasedraft
hiddenno
parent revision#a4d465a3e77d fix(ci): manually remove the .tox/doc directory
child revision<not specified>
files modified by this revision
cubicweb/pyramid/assets.py
# HG changeset patch
# User David Douard <david.douard@logilab.fr>
# Date 1533225324 -7200
# Thu Aug 02 17:55:24 2018 +0200
# Node ID 6f3f65423c3bc7cd4684ff195e7fe8459a764a07
# Parent a4d465a3e77d07cf6a79c121c10b2d6484cd7468
[pyramid] add a simple pyramid configuration to create static views for assets from cubes

diff --git a/cubicweb/pyramid/assets.py b/cubicweb/pyramid/assets.py
@@ -0,0 +1,51 @@
1 +# copyright 2018 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
2 +#
3 +# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
4 +#
5 +# This file is part of CubicWeb.
6 +#
7 +# CubicWeb is free software: you can redistribute it and/or modify it under the
8 +# terms of the GNU Lesser General Public License as published by the Free
9 +# Software Foundation, either version 2.1 of the License, or (at your option)
10 +# any later version.
11 +#
12 +# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
13 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 +# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
15 +# details.
16 +#
17 +# You should have received a copy of the GNU Lesser General Public License along
18 +# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
19 +
20 +"""Pyramid routes for assets from cubiceweb and cubes.
21 +
22 +When included, you can use assets from cubes like::
23 +
24 +  request.static_url('cubicweb_stuff:data/css/stylish.css')
25 +  request.static_url('cubes.old_style:data/css/stylish.css')
26 +  request.static_url('cubicweb:web/data/jquery.js')
27 +
28 +in your pyramid views.
29 +
30 +Note that it does not support automatic old-style vs. new-style cube 
31 +"discovery", so you must use the correct package name 
32 +(ie. 'cubes.stuff:something.js' won't work if the stuff cube is 
33 +a new-style cube and vice-versa.)
34 +
35 +"""
36 +
37 +from __future__ import absolute_import
38 +
39 +import sys
40 +
41 +
42 +def includeme(config):
43 +    cwcfg = config.registry['cubicweb.config']
44 +    for cube in cwcfg.cubes():
45 +        for pkg in ('cubicweb_{}', 'cubes.{}'):
46 +            if pkg.format(cube) in sys.modules:
47 +                path = pkg.format(cube) + ':data'
48 +                config.add_static_view('data/{}'.format(cube),
49 +                                       path=path)
50 +                break
51 +    config.add_static_view('data', path='cubicweb:web/data')