|
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 from pyramid.view import view_config |
|
13 from pyams_content.component.paragraph.illustration import Illustration |
|
14 from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget, IIllustrationParagraph, \ |
|
15 IParagraphContainer, IBaseParagraph, IParagraphSummary, IIllustrationRenderer |
|
16 from pyams_content.component.paragraph.zmi.container import ParagraphContainerView |
|
17 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION |
|
18 from pyams_content.shared.common.interfaces import IWfSharedContent |
|
19 from pyams_form.form import AJAXAddForm, AJAXEditForm |
|
20 from pyams_form.security import ProtectedFormObjectMixin |
|
21 from pyams_i18n.interfaces import II18n |
|
22 from pyams_pagelet.pagelet import pagelet_config |
|
23 from pyams_skin.interfaces.viewlet import IToolbarAddingMenu |
|
24 from pyams_skin.layer import IPyAMSLayer |
|
25 from pyams_skin.viewlet.toolbar import ToolbarMenuItem |
|
26 from pyams_template.template import template_config, get_view_template |
|
27 from pyams_utils.adapter import ContextRequestAdapter, adapter_config |
|
28 from pyams_utils.traversing import get_parent |
|
29 from pyams_viewlet.viewlet import viewlet_config |
|
30 from pyams_zmi.form import AdminDialogAddForm, AdminDialogEditForm |
|
31 |
|
32 __docformat__ = 'restructuredtext' |
|
33 |
|
34 |
|
35 # import standard library |
|
36 |
|
37 # import interfaces |
|
38 |
|
39 # import packages |
|
40 from z3c.form import field |
|
41 |
|
42 from pyams_content import _ |
|
43 |
|
44 |
|
45 # |
|
46 # Illustration |
|
47 # |
|
48 |
|
49 @viewlet_config(name='add-illustration.menu', context=IParagraphContainerTarget, view=ParagraphContainerView, |
|
50 layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=60) |
|
51 class IllustrationAddMenu(ProtectedFormObjectMixin, ToolbarMenuItem): |
|
52 """Illustration add menu""" |
|
53 |
|
54 label = _("Add illustration...") |
|
55 label_css_class = 'fa fa-fw fa-file-image-o' |
|
56 url = 'add-illustration.html' |
|
57 modal_target = True |
|
58 |
|
59 |
|
60 @pagelet_config(name='add-illustration.html', context=IParagraphContainerTarget, layer=IPyAMSLayer, |
|
61 permission=MANAGE_CONTENT_PERMISSION) |
|
62 class IllustrationAddForm(AdminDialogAddForm): |
|
63 """Illustration add form""" |
|
64 |
|
65 legend = _("Add new illustration") |
|
66 dialog_class = 'modal-large' |
|
67 icon_css_class = 'fa fa-fw fa-file-image-o' |
|
68 |
|
69 fields = field.Fields(IIllustrationParagraph).omit('__parent__', '__name__') |
|
70 ajax_handler = 'add-illustration.json' |
|
71 edit_permission = MANAGE_CONTENT_PERMISSION |
|
72 |
|
73 def create(self, data): |
|
74 return Illustration() |
|
75 |
|
76 def add(self, object): |
|
77 IParagraphContainer(self.context)['none'] = object |
|
78 |
|
79 |
|
80 @view_config(name='add-illustration.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer, |
|
81 permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True) |
|
82 class IllustrationAJAXAddForm(AJAXAddForm, IllustrationAddForm): |
|
83 """HTML paragraph add form, JSON renderer""" |
|
84 |
|
85 def get_ajax_output(self, changes): |
|
86 return {'status': 'reload', |
|
87 'location': '#paragraphs.html'} |
|
88 |
|
89 |
|
90 @pagelet_config(name='properties.html', context=IIllustrationParagraph, layer=IPyAMSLayer, |
|
91 permission=MANAGE_CONTENT_PERMISSION) |
|
92 class IllustrationPropertiesEditForm(AdminDialogEditForm): |
|
93 """Illustration properties edit form""" |
|
94 |
|
95 @property |
|
96 def title(self): |
|
97 content = get_parent(self.context, IWfSharedContent) |
|
98 return II18n(content).query_attribute('title', request=self.request) |
|
99 |
|
100 legend = _("Edit illustration properties") |
|
101 dialog_class = 'modal-large' |
|
102 icon_css_class = 'fa fa-fw fa-file-image-o' |
|
103 |
|
104 fields = field.Fields(IIllustrationParagraph).omit('__parent__', '__name__') |
|
105 ajax_handler = 'properties.json' |
|
106 edit_permission = MANAGE_CONTENT_PERMISSION |
|
107 |
|
108 |
|
109 @view_config(name='properties.json', context=IIllustrationParagraph, request_type=IPyAMSLayer, |
|
110 permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True) |
|
111 class IllustrationPropertiesAJAXEditForm(AJAXEditForm, IllustrationPropertiesEditForm): |
|
112 """HTML paragraph properties edit form, JSON renderer""" |
|
113 |
|
114 def get_ajax_output(self, changes): |
|
115 if 'title' in changes.get(IBaseParagraph, ()): |
|
116 return {'status': 'reload', |
|
117 'location': '#paragraphs.html'} |
|
118 else: |
|
119 return super(IllustrationPropertiesAJAXEditForm, self).get_ajax_output(changes) |
|
120 |
|
121 |
|
122 # |
|
123 # Illustration summary |
|
124 # |
|
125 |
|
126 @adapter_config(context=(IIllustrationParagraph, IPyAMSLayer), provides=IParagraphSummary) |
|
127 class IllustrationSummary(ContextRequestAdapter): |
|
128 """Illustration renderer""" |
|
129 |
|
130 def __init__(self, context, request): |
|
131 super(IllustrationSummary, self).__init__(context, request) |
|
132 self.renderer = request.registry.queryMultiAdapter((context, request), IIllustrationRenderer, |
|
133 name=self.context.renderer) |
|
134 |
|
135 language = None |
|
136 |
|
137 def update(self): |
|
138 if self.renderer is not None: |
|
139 self.renderer.language = self.language |
|
140 self.renderer.update() |
|
141 |
|
142 def render(self): |
|
143 if self.renderer is not None: |
|
144 return self.renderer.render() |
|
145 else: |
|
146 return '' |
|
147 |
|
148 |
|
149 # |
|
150 # Illustration renderers |
|
151 # |
|
152 |
|
153 class BaseIllustrationRenderer(ContextRequestAdapter): |
|
154 """Base illustration renderer""" |
|
155 |
|
156 language = None |
|
157 |
|
158 def update(self): |
|
159 i18n = II18n(self.context) |
|
160 if self.language: |
|
161 self.legend = i18n.get_attribute('legend', self.language, request=self.request) |
|
162 else: |
|
163 self.legend = i18n.query_attribute('legend', request=self.request) |
|
164 |
|
165 render = get_view_template() |
|
166 |
|
167 |
|
168 @adapter_config(name='default', context=(IIllustrationParagraph, IPyAMSLayer), provides=IIllustrationRenderer) |
|
169 @template_config(template='templates/illustration.pt', layer=IPyAMSLayer) |
|
170 class DefaultIllustrationRenderer(BaseIllustrationRenderer): |
|
171 """Default illustration renderer""" |
|
172 |
|
173 label = _("Centered illustration") |
|
174 weight = 1 |
|
175 |
|
176 |
|
177 @adapter_config(name='left+zoom', context=(IIllustrationParagraph, IPyAMSLayer), provides=IIllustrationRenderer) |
|
178 @template_config(template='templates/illustration-left.pt', layer=IPyAMSLayer) |
|
179 class LeftIllustrationWithZoomRenderer(BaseIllustrationRenderer): |
|
180 """Illustrtaion renderer with small image and zoom""" |
|
181 |
|
182 label = _("Small illustration on the left with zoom") |
|
183 weight = 2 |
|
184 |
|
185 |
|
186 @adapter_config(name='right+zoom', context=(IIllustrationParagraph, IPyAMSLayer), provides=IIllustrationRenderer) |
|
187 @template_config(template='templates/illustration-right.pt', layer=IPyAMSLayer) |
|
188 class RightIllustrationWithZoomRenderer(BaseIllustrationRenderer): |
|
189 """Illustrtaion renderer with small image and zoom""" |
|
190 |
|
191 label = _("Small illustration on the right with zoom") |
|
192 weight = 3 |