source: main/waeup.ikoba/trunk/src/waeup/ikoba/documents/browser.py @ 12242

Last change on this file since 12242 was 12242, checked in by Henrik Bettermann, 10 years ago

Add HTMLDocumentDisplayContentPage which renders only the content part of HTMLDocuments.

Include <a> element on display pages for copy and paste.

  • Property svn:keywords set to Id
File size: 10.1 KB
Line 
1## $Id: browser.py 12242 2014-12-15 10:48:40Z henrik $
2##
3## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""UI components for documents offered for download by the company.
19"""
20
21import sys
22import grok
23import pytz
24from urllib import urlencode
25from datetime import datetime
26from hurry.workflow.interfaces import (
27    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
28from zope.event import notify
29from zope.i18n import translate
30from zope.catalog.interfaces import ICatalog
31from zope.component import queryUtility, getUtility, createObject
32from zope.schema.interfaces import ConstraintNotSatisfied, RequiredMissing
33from zope.formlib.textwidgets import BytesDisplayWidget
34from zope.security import checkPermission
35from waeup.ikoba.utils.helpers import html2dict
36from waeup.ikoba.interfaces import MessageFactory as _
37from waeup.ikoba.interfaces import (
38    IContactForm, IObjectHistory, IIkobaObject, IIkobaUtils, PUBLISHED)
39from waeup.ikoba.browser.layout import (
40    IkobaPage, IkobaEditFormPage, IkobaAddFormPage, IkobaDisplayFormPage,
41    IkobaForm, NullValidator, jsaction, action, UtilityView)
42from waeup.ikoba.widgets.datewidget import (
43    FriendlyDateWidget, FriendlyDateDisplayWidget,
44    FriendlyDatetimeDisplayWidget)
45from waeup.ikoba.browser.breadcrumbs import Breadcrumb
46from waeup.ikoba.browser.pages import (
47    delSubobjects, add_local_role, del_local_roles, msave,
48    LocalRoleAssignmentUtilityView)
49
50from waeup.ikoba.documents.interfaces import (
51    IDocumentsContainer, IPublicDocument, IHTMLDocument, IDocumentsUtils)
52
53grok.context(IIkobaObject) # Make IKofaObject the default context
54grok.templatedir('browser_templates')
55
56
57class DocumentsBreadcrumb(Breadcrumb):
58    """A breadcrumb for the customers container.
59    """
60    grok.context(IDocumentsContainer)
61    title = _('Documents')
62
63
64class DocumentBreadcrumb(Breadcrumb):
65    """A breadcrumb for the customer container.
66    """
67    grok.context(IPublicDocument)
68
69    def title(self):
70        return self.context.title
71
72
73class DocumentsContainerPage(IkobaDisplayFormPage):
74    """The standard view for document containers.
75    """
76    grok.context(IDocumentsContainer)
77    grok.name('index')
78    grok.require('waeup.viewDocuments')
79    grok.template('containerpage')
80    pnav = 2
81    label = _('Documents')
82
83
84class DocumentsContainerManageFormPage(IkobaEditFormPage,
85                                      LocalRoleAssignmentUtilityView):
86    """The manage page for customer containers.
87    """
88    grok.context(IDocumentsContainer)
89    grok.name('manage')
90    grok.require('waeup.manageDocuments')
91    grok.template('containermanagepage')
92    pnav = 2
93    label = _('Manage document section')
94
95    @action(_('Add document'), validator=NullValidator, style='primary')
96    def addSubunit(self, **data):
97        self.redirect(self.url(self.context, 'adddoc'))
98        return
99
100    @jsaction(_('Remove selected documents'))
101    def delDocuments(self, **data):
102        delSubobjects(self, redirect='manage', tab='2')
103        return
104
105    @action(_('Cancel'), validator=NullValidator)
106    def cancel(self, **data):
107        self.redirect(self.url(self.context))
108        return
109
110
111class DocumentAddFormPage(IkobaAddFormPage):
112    """Add-form to add a customer.
113    """
114    grok.context(IDocumentsContainer)
115    grok.require('waeup.manageDocuments')
116    grok.name('adddoc')
117    grok.template('documentaddform')
118    label = _('Add document')
119    pnav = 2
120
121    form_fields = grok.AutoFields(IPublicDocument)
122
123    @property
124    def selectable_doctypes(self):
125        doctypes = getUtility(IDocumentsUtils).SELECTABLE_DOCTYPES_DICT
126        return sorted(doctypes.items())
127
128    @action(_('Add document'), style='primary')
129    def createDocument(self, **data):
130        form = self.request.form
131        doctype = form.get('doctype', None)
132        # Here we can create various instances of PublicDocument derived
133        # classes depending on the doctype parameter given in form.
134        document = createObject('waeup.%s' % doctype)
135        self.applyData(document, **data)
136        self.context.addDocument(document)
137        doctype = getUtility(IDocumentsUtils).SELECTABLE_DOCTYPES_DICT[doctype]
138        self.flash(_('${a} added.', mapping = {'a': doctype}))
139        ob_class = self.__implemented__.__name__.replace('waeup.ikoba.','')
140        self.context.__parent__.logger.info(
141            '%s - added: %s %s' % (ob_class, doctype, document.document_id))
142        self.redirect(self.url(self.context, u'manage'))
143        return
144
145    @action(_('Cancel'), validator=NullValidator)
146    def cancel(self, **data):
147        self.redirect(self.url(self.context))
148
149
150class DocumentDisplayFormPage(IkobaDisplayFormPage):
151    """ Page to display document data
152    """
153    grok.context(IPublicDocument)
154    grok.name('index')
155    grok.require('waeup.viewDocuments')
156    grok.template('documentpage')
157    pnav = 2
158
159    @property
160    def form_fields(self):
161        return grok.AutoFields(self.context.form_fields_interface)
162
163    @property
164    def label(self):
165        return self.context.title
166
167
168class HTMLDocumentDisplayFormPage(DocumentDisplayFormPage):
169    """ Page to display html document data
170    """
171    grok.context(IHTMLDocument)
172    grok.template('htmldocumentpage')
173
174    @property
175    def form_fields(self):
176        return grok.AutoFields(self.context.form_fields_interface).omit(
177            'html_dict', 'html_multilingual')
178
179    @property
180    def html(self):
181        lang = self.request.cookies.get('ikoba.language')
182        html = self.context.html_dict.get(lang,'')
183        if html =='':
184            portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE
185            html = self.context.html_dict.get(portal_language,'')
186        return html
187
188
189class HTMLDocumentDisplayContentPage(IkobaPage):
190    """ Page to display the html content of document
191    """
192    grok.context(IHTMLDocument)
193    grok.name('display')
194    grok.template('htmldisplaypage')
195
196    @property
197    def label(self):
198        return self.context.title
199
200    def update(self):
201        if self.context.state != PUBLISHED:
202            self.flash(_('The document requested has not yet been published.'),
203                type="warning")
204            self.redirect(self.url(self.context))
205        super(HTMLDocumentDisplayContentPage, self).update()
206        return
207
208    @property
209    def content(self):
210        lang = self.request.cookies.get('ikoba.language')
211        html = self.context.html_dict.get(lang,'')
212        if html =='':
213            portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE
214            html = self.context.html_dict.get(portal_language,'')
215        return html
216
217
218class DocumentManageFormPage(IkobaEditFormPage,
219                            LocalRoleAssignmentUtilityView):
220    """ View to manage document data
221    """
222    grok.context(IPublicDocument)
223    grok.name('manage')
224    grok.require('waeup.manageDocuments')
225    grok.template('documentmanagepage')
226    pnav = 2
227
228    taboneactions = [_('Save'),_('Cancel')]
229    tabthreeactions1 = [_('Remove selected local roles')]
230    tabthreeactions2 = [_('Add local role')]
231
232    deletion_warning = _('Are you sure?')
233
234    @property
235    def form_fields(self):
236        return grok.AutoFields(self.context.form_fields_interface)
237
238    def label(self):
239        return _('Manage document ') + self.context.document_id
240
241    @action(_('Save'), style='primary')
242    def save(self, **data):
243        return msave(self, **data)
244
245    @action(_('Cancel'), validator=NullValidator)
246    def cancel(self, **data):
247        self.redirect(self.url(self.context))
248        return
249
250    @action(_('Add local role'), validator=NullValidator)
251    def addLocalRole(self, **data):
252        return add_local_role(self,2,**data)
253
254    @action(_('Remove selected local roles'))
255    def delLocalRoles(self, **data):
256        return del_local_roles(self,2,**data)
257
258
259class HTMLDocumentManageFormPage(DocumentManageFormPage):
260    """ View to manage htmldocument data
261    """
262    grok.context(IHTMLDocument)
263    grok.template('htmldocumentmanagepage')
264
265    @action(_('Save'), style='primary')
266    def save(self, **data):
267        msave(self, **data)
268        html_multilingual = getattr(self.context, 'html_multilingual', None)
269        portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE
270        self.context.html_dict = html2dict(html_multilingual, portal_language)
271        return
272
273
274class DocumentTriggerTransitionFormPage(IkobaEditFormPage):
275    """ View to trigger public document transitions
276    """
277    grok.context(IPublicDocument)
278    grok.name('trigtrans')
279    grok.require('waeup.triggerTransition')
280    grok.template('trigtrans')
281    label = _('Trigger document transition')
282    pnav = 2
283
284    def update(self):
285        return super(IkobaEditFormPage, self).update()
286
287    def getTransitions(self):
288        """Return a list of dicts of allowed transition ids and titles.
289
290        Each list entry provides keys ``name`` and ``title`` for
291        internal name and (human readable) title of a single
292        transition.
293        """
294        wf_info = IWorkflowInfo(self.context)
295        allowed_transitions = [t for t in wf_info.getManualTransitions()]
296        return [dict(name='', title=_('No transition'))] +[
297            dict(name=x, title=y) for x, y in allowed_transitions]
298
299    @action(_('Save'), style='primary')
300    def save(self, **data):
301        form = self.request.form
302        if 'transition' in form and form['transition']:
303            transition_id = form['transition']
304            wf_info = IWorkflowInfo(self.context)
305            try:
306                wf_info.fireTransition(transition_id)
307            except InvalidTransitionError, error:
308                self.flash(error, type="warning")
309        return
Note: See TracBrowser for help on using the repository browser.