source: main/waeup.kofa/trunk/src/waeup/kofa/documents/browser.py @ 13639

Last change on this file since 13639 was 13218, checked in by Henrik Bettermann, 9 years ago

Add UI components to purge applicants containers.

  • Property svn:keywords set to Id
File size: 12.5 KB
Line 
1## $Id: browser.py 13218 2015-08-24 08:44:35Z 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
23from urllib import urlencode
24from datetime import datetime
25from hurry.workflow.interfaces import (
26    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
27from zope.event import notify
28from zope.i18n import translate
29from zope.catalog.interfaces import ICatalog
30from zope.component import queryUtility, getUtility, createObject
31from zope.schema.interfaces import ConstraintNotSatisfied, RequiredMissing
32from zope.formlib.textwidgets import BytesDisplayWidget
33from zope.security import checkPermission
34from waeup.kofa.utils.helpers import html2dict, rest2dict
35from waeup.kofa.interfaces import MessageFactory as _
36from waeup.kofa.interfaces import (
37    IContactForm, IKofaObject, IKofaUtils, DOCLINK)
38from waeup.kofa.browser.layout import (
39    KofaPage, KofaEditFormPage, KofaAddFormPage, KofaDisplayFormPage,
40    NullValidator, jsaction, action, UtilityView)
41from waeup.kofa.widgets.datewidget import (
42    FriendlyDateWidget, FriendlyDateDisplayWidget,
43    FriendlyDatetimeDisplayWidget)
44from waeup.kofa.browser.breadcrumbs import Breadcrumb
45from waeup.kofa.browser.pages import (
46    delSubobjects, add_local_role, del_local_roles, msave,
47    LocalRoleAssignmentUtilityView)
48
49from waeup.kofa.documents.interfaces import (
50    IDocumentsContainer, IPublicDocument,
51    IHTMLDocument, IRESTDocument, IDocumentsUtils)
52from waeup.kofa.documents.workflow import PUBLISHED
53
54grok.context(IKofaObject) # Make IKofaObject the default context
55grok.templatedir('browser_templates')
56
57
58class DocumentsBreadcrumb(Breadcrumb):
59    """A breadcrumb for the customers container.
60    """
61    grok.context(IDocumentsContainer)
62    title = _('Documents')
63
64
65class DocumentBreadcrumb(Breadcrumb):
66    """A breadcrumb for the customer container.
67    """
68    grok.context(IPublicDocument)
69
70    def title(self):
71        return self.context.title
72
73
74class DocumentsContainerPage(KofaDisplayFormPage):
75    """The standard view for document containers.
76    """
77    grok.context(IDocumentsContainer)
78    grok.name('index')
79    grok.require('waeup.viewDocuments')
80    grok.template('containerpage')
81    pnav = 2
82    label = _('Documents')
83    doclink = DOCLINK + '/documents.html'
84
85
86class DocumentsContainerManageFormPage(KofaEditFormPage,
87                                      LocalRoleAssignmentUtilityView):
88    """The manage page for customer containers.
89    """
90    grok.context(IDocumentsContainer)
91    grok.name('manage')
92    grok.require('waeup.manageDocuments')
93    grok.template('containermanagepage')
94    pnav = 2
95    label = _('Manage document section')
96
97    @action(_('Add document'), validator=NullValidator, style='primary')
98    def addSubunit(self, **data):
99        self.redirect(self.url(self.context, 'adddoc'))
100        return
101
102    @jsaction(_('Remove selected documents'))
103    def delDocuments(self, **data):
104        delSubobjects(self, redirect='manage', tab='2')
105        return
106
107    @action(_('Cancel'), validator=NullValidator)
108    def cancel(self, **data):
109        self.redirect(self.url(self.context))
110        return
111
112
113class DocumentAddFormPage(KofaAddFormPage):
114    """Add-form to add a customer.
115    """
116    grok.context(IDocumentsContainer)
117    grok.require('waeup.manageDocuments')
118    grok.name('adddoc')
119    grok.template('documentaddform')
120    label = _('Add document')
121    pnav = 2
122
123    form_fields = grok.AutoFields(IPublicDocument)
124
125    @property
126    def selectable_doctypes(self):
127        doctypes = getUtility(IDocumentsUtils).SELECTABLE_DOCTYPES_DICT
128        return sorted(doctypes.items())
129
130    @action(_('Add document'), style='primary')
131    def createDocument(self, **data):
132        form = self.request.form
133        doctype = form.get('doctype', None)
134        # Here we can create various instances of PublicDocument derived
135        # classes depending on the doctype parameter given in form.
136        document = createObject('waeup.%s' % doctype)
137        self.applyData(document, **data)
138        try:
139            self.context.addDocument(document)
140        except KeyError:
141            self.flash(_('The id chosen already exists.'),
142                       type='danger')
143            return
144        doctype = getUtility(IDocumentsUtils).SELECTABLE_DOCTYPES_DICT[doctype]
145        self.flash(_('${a} added.', mapping = {'a': doctype}))
146        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
147        self.context.__parent__.logger.info(
148            '%s - added: %s %s' % (ob_class, doctype, document.document_id))
149        self.redirect(self.url(self.context) +
150            '/%s/manage' % document.document_id)
151        return
152
153    @action(_('Cancel'), validator=NullValidator)
154    def cancel(self, **data):
155        self.redirect(self.url(self.context))
156
157
158class DocumentDisplayFormPage(KofaDisplayFormPage):
159    """ Page to display document data
160    """
161    grok.context(IPublicDocument)
162    grok.name('index')
163    grok.require('waeup.viewDocuments')
164    grok.template('documentpage')
165    pnav = 2
166    doclink = DOCLINK + '/documents.html'
167
168    @property
169    def form_fields(self):
170        return grok.AutoFields(self.context.form_fields_interface)
171
172    @property
173    def label(self):
174        return self.context.title
175
176
177class HTMLDocumentDisplayFormPage(DocumentDisplayFormPage):
178    """ Page to display html document data
179    """
180    grok.context(IHTMLDocument)
181    grok.template('htmldocumentpage')
182
183    @property
184    def form_fields(self):
185        return grok.AutoFields(self.context.form_fields_interface).omit(
186            'html_dict', 'html_multilingual')
187
188    @property
189    def html(self):
190        lang = self.request.cookies.get('kofa.language')
191        html = self.context.html_dict.get(lang,'')
192        if html =='':
193            portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
194            html = self.context.html_dict.get(portal_language,'')
195        return html
196
197
198class HTMLDocumentDisplayContentPage(KofaPage):
199    """ Page to display the html content of document
200    """
201    grok.context(IHTMLDocument)
202    grok.name('display')
203    grok.template('htmldisplaypage')
204    grok.require('waeup.Public')
205
206    @property
207    def label(self):
208        return self.context.title
209
210    def update(self):
211        if self.context.state != PUBLISHED:
212            self.flash(_('The document requested has not yet been published.'),
213                type="warning")
214            self.redirect(self.application_url())
215        super(HTMLDocumentDisplayContentPage, self).update()
216        return
217
218    @property
219    def content(self):
220        lang = self.request.cookies.get('kofa.language')
221        html = self.context.html_dict.get(lang,'')
222        if html =='':
223            portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
224            html = self.context.html_dict.get(portal_language,'')
225        return html
226
227
228class RESTDocumentDisplayFormPage(HTMLDocumentDisplayFormPage):
229    """ Page to display html document data
230    """
231    grok.context(IRESTDocument)
232
233    @property
234    def form_fields(self):
235        return grok.AutoFields(self.context.form_fields_interface).omit(
236            'html_dict', 'rest_multilingual')
237
238
239class RESTDocumentDisplayContentPage(HTMLDocumentDisplayContentPage):
240    """ Page to display the html content of document
241    """
242    grok.context(IRESTDocument)
243
244    label = None
245
246
247class DocumentManageFormPage(KofaEditFormPage,
248                            LocalRoleAssignmentUtilityView):
249    """ View to manage document data
250    """
251    grok.context(IPublicDocument)
252    grok.name('manage')
253    grok.require('waeup.manageDocuments')
254    grok.template('documentmanagepage')
255    pnav = 2
256
257    taboneactions = [_('Save'),_('Cancel')]
258    tabthreeactions1 = [_('Remove selected local roles')]
259    tabthreeactions2 = [_('Add local role')]
260
261    deletion_warning = _('Are you sure?')
262
263    @property
264    def form_fields(self):
265        return grok.AutoFields(
266            self.context.form_fields_interface).omit('document_id')
267
268    def label(self):
269        return _('Manage document ') + self.context.document_id
270
271    @action(_('Save'), style='primary')
272    def save(self, **data):
273        return msave(self, **data)
274
275    @action(_('Cancel'), validator=NullValidator)
276    def cancel(self, **data):
277        self.redirect(self.url(self.context))
278        return
279
280    @action(_('Add local role'), validator=NullValidator)
281    def addLocalRole(self, **data):
282        return add_local_role(self,3,**data)
283
284    @action(_('Remove selected local roles'))
285    def delLocalRoles(self, **data):
286        return del_local_roles(self,3,**data)
287
288
289class HTMLDocumentManageFormPage(DocumentManageFormPage):
290    """ View to manage htmldocument data
291    """
292    grok.context(IHTMLDocument)
293    grok.template('htmldocumentmanagepage')
294
295    @action(_('Save'), style='primary')
296    def save(self, **data):
297        msave(self, **data)
298        html_multilingual = getattr(self.context, 'html_multilingual', None)
299        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
300        self.context.html_dict = html2dict(html_multilingual, portal_language)
301        return
302
303    # Actions must be repeated. They are not inherited from the parent class.
304
305    @action(_('Cancel'), validator=NullValidator)
306    def cancel(self, **data):
307        self.redirect(self.url(self.context))
308        return
309
310    @action(_('Add local role'), validator=NullValidator)
311    def addLocalRole(self, **data):
312        return add_local_role(self,3,**data)
313
314    @action(_('Remove selected local roles'))
315    def delLocalRoles(self, **data):
316        return del_local_roles(self,3,**data)
317
318class RESTDocumentManageFormPage(DocumentManageFormPage):
319    """ View to manage restdocument data
320    """
321    grok.context(IRESTDocument)
322    grok.template('htmldocumentmanagepage')
323
324    @action(_('Save'), style='primary')
325    def save(self, **data):
326        msave(self, **data)
327        html_multilingual = getattr(self.context, 'rest_multilingual', None)
328        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
329        self.context.html_dict = rest2dict(html_multilingual, portal_language)
330        return
331
332    # Actions must be repeated. They are not inherited from the parent class.
333
334    @action(_('Cancel'), validator=NullValidator)
335    def cancel(self, **data):
336        self.redirect(self.url(self.context))
337        return
338
339    @action(_('Add local role'), validator=NullValidator)
340    def addLocalRole(self, **data):
341        return add_local_role(self,3,**data)
342
343    @action(_('Remove selected local roles'))
344    def delLocalRoles(self, **data):
345        return del_local_roles(self,3,**data)
346
347class DocumentTriggerTransitionFormPage(KofaEditFormPage):
348    """ View to trigger public document transitions
349    """
350    grok.context(IPublicDocument)
351    grok.name('trigtrans')
352    grok.require('waeup.triggerTransition')
353    grok.template('trigtrans')
354    label = _('Trigger document transition')
355    pnav = 2
356
357    def update(self):
358        return super(KofaEditFormPage, self).update()
359
360    def getTransitions(self):
361        """Return a list of dicts of allowed transition ids and titles.
362
363        Each list entry provides keys ``name`` and ``title`` for
364        internal name and (human readable) title of a single
365        transition.
366        """
367        wf_info = IWorkflowInfo(self.context)
368        allowed_transitions = [t for t in wf_info.getManualTransitions()]
369        return [dict(name='', title=_('No transition'))] +[
370            dict(name=x, title=y) for x, y in allowed_transitions]
371
372    @action(_('Apply now'), style='primary')
373    def apply(self, **data):
374        form = self.request.form
375        if 'transition' in form and form['transition']:
376            transition_id = form['transition']
377            wf_info = IWorkflowInfo(self.context)
378            try:
379                wf_info.fireTransition(transition_id)
380                self.flash(_("Transition '%s' executed." % transition_id))
381            except InvalidTransitionError, error:
382                self.flash(error, type="warning")
383            self.redirect(self.url(self.context))
384        return
Note: See TracBrowser for help on using the repository browser.