|
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 pyams_content.interfaces import MANAGE_CONTENT_PERMISSION |
|
13 from pyams_template.template import template_config, ViewTemplate, get_view_template |
|
14 |
|
15 __docformat__ = 'restructuredtext' |
|
16 |
|
17 |
|
18 # import standard library |
|
19 |
|
20 # import interfaces |
|
21 from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget, IHTMLParagraph, \ |
|
22 IParagraphContainer, IBaseParagraph, IParagraphSummary |
|
23 from pyams_content.shared.common.interfaces import IWfSharedContent |
|
24 from pyams_i18n.interfaces import II18n |
|
25 from pyams_skin.interfaces.viewlet import IToolbarAddingMenu |
|
26 from pyams_skin.layer import IPyAMSLayer |
|
27 |
|
28 # import packages |
|
29 from pyams_content.component.paragraph.html import HTMLParagraph |
|
30 from pyams_content.component.paragraph.zmi.container import ParagraphContainerView |
|
31 from pyams_form.form import AJAXAddForm, AJAXEditForm |
|
32 from pyams_form.security import ProtectedFormObjectMixin |
|
33 from pyams_pagelet.pagelet import pagelet_config |
|
34 from pyams_skin.viewlet.toolbar import ToolbarMenuItem |
|
35 from pyams_utils.adapter import adapter_config, ContextRequestAdapter |
|
36 from pyams_utils.traversing import get_parent |
|
37 from pyams_viewlet.viewlet import viewlet_config, ContentProvider |
|
38 from pyams_zmi.form import AdminDialogAddForm, AdminDialogEditForm |
|
39 from pyramid.view import view_config |
|
40 from z3c.form import field |
|
41 |
|
42 from pyams_content import _ |
|
43 |
|
44 |
|
45 # |
|
46 # HTML paragraph |
|
47 # |
|
48 |
|
49 @viewlet_config(name='add-html-paragraph.menu', context=IParagraphContainerTarget, view=ParagraphContainerView, |
|
50 layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=50) |
|
51 class HTMLParagraphAddMenu(ProtectedFormObjectMixin, ToolbarMenuItem): |
|
52 """HTML paragraph add menu""" |
|
53 |
|
54 label = _("Add HTML paragraph...") |
|
55 label_css_class = 'fa fa-fw fa-html5' |
|
56 url = 'add-html-paragraph.html' |
|
57 modal_target = True |
|
58 |
|
59 |
|
60 @pagelet_config(name='add-html-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer, |
|
61 permission=MANAGE_CONTENT_PERMISSION) |
|
62 class HTMLParagraphAddForm(AdminDialogAddForm): |
|
63 """HTML paragraph add form""" |
|
64 |
|
65 legend = _("Add new HTML paragraph") |
|
66 dialog_class = 'modal-max' |
|
67 icon_css_class = 'fa fa-fw fa-html5' |
|
68 label_css_class = 'control-label col-md-2' |
|
69 input_css_class = 'col-md-10' |
|
70 |
|
71 fields = field.Fields(IHTMLParagraph).omit('__parent__', '__name__') |
|
72 ajax_handler = 'add-html-paragraph.json' |
|
73 edit_permission = MANAGE_CONTENT_PERMISSION |
|
74 |
|
75 def updateWidgets(self, prefix=None): |
|
76 super(HTMLParagraphAddForm, self).updateWidgets(prefix) |
|
77 self.widgets['body'].label_css_class = 'textarea' |
|
78 |
|
79 def create(self, data): |
|
80 return HTMLParagraph() |
|
81 |
|
82 def add(self, object): |
|
83 IParagraphContainer(self.context)['none'] = object |
|
84 |
|
85 |
|
86 @view_config(name='add-html-paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer, |
|
87 permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True) |
|
88 class HTMLParagraphAJAXAddForm(AJAXAddForm, HTMLParagraphAddForm): |
|
89 """HTML paragraph add form, JSON renderer""" |
|
90 |
|
91 def get_ajax_output(self, changes): |
|
92 return {'status': 'reload', |
|
93 'location': '#paragraphs.html'} |
|
94 |
|
95 |
|
96 @pagelet_config(name='properties.html', context=IHTMLParagraph, layer=IPyAMSLayer, |
|
97 permission=MANAGE_CONTENT_PERMISSION) |
|
98 class HTMLParagraphPropertiesEditForm(AdminDialogEditForm): |
|
99 """HTML paragraph properties edit form""" |
|
100 |
|
101 @property |
|
102 def title(self): |
|
103 content = get_parent(self.context, IWfSharedContent) |
|
104 return II18n(content).query_attribute('title', request=self.request) |
|
105 |
|
106 legend = _("Edit paragraph properties") |
|
107 dialog_class = 'modal-max' |
|
108 icon_css_class = 'fa fa-fw fa-html5' |
|
109 label_css_class = 'control-label col-md-2' |
|
110 input_css_class = 'col-md-10' |
|
111 |
|
112 fields = field.Fields(IHTMLParagraph).omit('__parent__', '__name__') |
|
113 ajax_handler = 'properties.json' |
|
114 edit_permission = 'pyams.ManageContent' |
|
115 |
|
116 def updateWidgets(self, prefix=None): |
|
117 super(HTMLParagraphPropertiesEditForm, self).updateWidgets(prefix) |
|
118 self.widgets['body'].label_css_class = 'textarea' |
|
119 |
|
120 |
|
121 @view_config(name='properties.json', context=IHTMLParagraph, request_type=IPyAMSLayer, |
|
122 permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True) |
|
123 class HTMLParagraphPropertiesAJAXEditForm(AJAXEditForm, HTMLParagraphPropertiesEditForm): |
|
124 """HTML paragraph properties edit form, JSON renderer""" |
|
125 |
|
126 def get_ajax_output(self, changes): |
|
127 if 'title' in changes.get(IBaseParagraph, ()): |
|
128 return {'status': 'reload', |
|
129 'location': '#paragraphs.html'} |
|
130 else: |
|
131 return super(HTMLParagraphPropertiesAJAXEditForm, self).get_ajax_output(changes) |
|
132 |
|
133 |
|
134 # |
|
135 # HTML paragraph summary |
|
136 # |
|
137 |
|
138 @adapter_config(context=(IHTMLParagraph, IPyAMSLayer), provides=IParagraphSummary) |
|
139 @template_config(template='templates/html-summary.pt', layer=IPyAMSLayer) |
|
140 class HTMLParagraphSummary(ContextRequestAdapter): |
|
141 """HTML paragraph renderer""" |
|
142 |
|
143 language = None |
|
144 |
|
145 def update(self): |
|
146 i18n = II18n(self.context) |
|
147 if self.language: |
|
148 for attr in ('title', 'body'): |
|
149 setattr(self, attr, i18n.get_attribute(attr, self.language, request=self.request)) |
|
150 else: |
|
151 for attr in ('title', 'body'): |
|
152 setattr(self, attr, i18n.query_attribute(attr, request=self.request)) |
|
153 |
|
154 render = get_view_template() |