|
1 # |
|
2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 import re |
|
16 |
|
17 from chameleon.astutil import Symbol |
|
18 from chameleon.tales import StringExpr |
|
19 from pyramid_zope_request import PyramidPublisherRequest |
|
20 from zope.contentprovider.interfaces import BeforeUpdateEvent, ContentProviderLookupError, IContentProvider |
|
21 from zope.contentprovider.tales import addTALNamespaceData |
|
22 from zope.location.interfaces import ILocation |
|
23 |
|
24 from pyams_utils.tales import ContextExprMixin |
|
25 |
|
26 |
|
27 CONTENT_PROVIDER_NAME = re.compile('([A-Za-z0-9_\-\.]+)') |
|
28 |
|
29 |
|
30 def render_content_provider(econtext, name): |
|
31 match = CONTENT_PROVIDER_NAME.match(name.strip()) |
|
32 if match: |
|
33 name = match.groups()[0] |
|
34 else: |
|
35 raise ContentProviderLookupError(name) |
|
36 |
|
37 context = econtext.get('context') |
|
38 request = econtext.get('request') |
|
39 if isinstance(request, PyramidPublisherRequest): |
|
40 request = request._request |
|
41 view = econtext.get('view') |
|
42 |
|
43 registry = request.registry |
|
44 provider = registry.queryMultiAdapter((context, request, view), IContentProvider, name=name) |
|
45 |
|
46 # provide a useful error message, if the provider was not found. |
|
47 if provider is None: |
|
48 raise ContentProviderLookupError(name) |
|
49 |
|
50 # add the __name__ attribute if it implements ILocation |
|
51 if ILocation.providedBy(provider): |
|
52 provider.__name__ = name |
|
53 |
|
54 # Insert the data gotten from the context |
|
55 addTALNamespaceData(provider, econtext) |
|
56 |
|
57 # Stage 1: Do the state update |
|
58 registry.notify(BeforeUpdateEvent(provider, request)) |
|
59 provider.update() |
|
60 |
|
61 # Stage 2: Render the HTML content |
|
62 return provider.render() |
|
63 |
|
64 |
|
65 class ProviderExpr(ContextExprMixin, StringExpr): |
|
66 """provider: TALES expression""" |
|
67 |
|
68 transform = Symbol(render_content_provider) |