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

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

Add file viewlets to documents.

  • Property svn:keywords set to Id
File size: 8.0 KB
Line 
1## $Id: browser.py 12225 2014-12-14 09:45: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
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.interfaces import MessageFactory as _
36from waeup.ikoba.interfaces import (
37    IContactForm, IObjectHistory, IIkobaObject, IIkobaUtils)
38from waeup.ikoba.browser.layout import (
39    IkobaPage, IkobaEditFormPage, IkobaAddFormPage, IkobaDisplayFormPage,
40    IkobaForm, NullValidator, jsaction, action, UtilityView)
41from waeup.ikoba.widgets.datewidget import (
42    FriendlyDateWidget, FriendlyDateDisplayWidget,
43    FriendlyDatetimeDisplayWidget)
44from waeup.ikoba.browser.breadcrumbs import Breadcrumb
45from waeup.ikoba.browser.pages import (
46    delSubobjects, add_local_role, del_local_roles, msave,
47    LocalRoleAssignmentUtilityView)
48
49from waeup.ikoba.documents.interfaces import (
50    IDocumentsContainer, IPublicDocument, IDocumentsUtils)
51
52grok.context(IIkobaObject) # Make IKofaObject the default context
53grok.templatedir('browser_templates')
54
55
56class DocumentsBreadcrumb(Breadcrumb):
57    """A breadcrumb for the customers container.
58    """
59    grok.context(IDocumentsContainer)
60    title = _('Documents')
61
62
63class DocumentBreadcrumb(Breadcrumb):
64    """A breadcrumb for the customer container.
65    """
66    grok.context(IPublicDocument)
67
68    def title(self):
69        return self.context.title
70
71
72class DocumentsContainerPage(IkobaDisplayFormPage):
73    """The standard view for document containers.
74    """
75    grok.context(IDocumentsContainer)
76    grok.name('index')
77    grok.require('waeup.viewDocuments')
78    grok.template('containerpage')
79    pnav = 2
80    label = _('Documents')
81
82
83class DocumentsContainerManageFormPage(IkobaEditFormPage,
84                                      LocalRoleAssignmentUtilityView):
85    """The manage page for customer containers.
86    """
87    grok.context(IDocumentsContainer)
88    grok.name('manage')
89    grok.require('waeup.manageDocuments')
90    grok.template('containermanagepage')
91    pnav = 2
92    label = _('Manage document section')
93
94    @action(_('Add document'), validator=NullValidator, style='primary')
95    def addSubunit(self, **data):
96        self.redirect(self.url(self.context, 'adddoc'))
97        return
98
99    @jsaction(_('Remove selected documents'))
100    def delDocuments(self, **data):
101        delSubobjects(self, redirect='manage', tab='2')
102        return
103
104    @action(_('Cancel'), validator=NullValidator)
105    def cancel(self, **data):
106        self.redirect(self.url(self.context))
107        return
108
109
110class DocumentAddFormPage(IkobaAddFormPage):
111    """Add-form to add a customer.
112    """
113    grok.context(IDocumentsContainer)
114    grok.require('waeup.manageDocuments')
115    grok.name('adddoc')
116    grok.template('documentaddform')
117    label = _('Add document')
118    pnav = 2
119
120    form_fields = grok.AutoFields(IPublicDocument)
121
122    @property
123    def selectable_doctypes(self):
124        doctypes = getUtility(IDocumentsUtils).SELECTABLE_DOCTYPES_DICT
125        return sorted(doctypes.items())
126
127    @action(_('Add document'), style='primary')
128    def createDocument(self, **data):
129        form = self.request.form
130        doctype = form.get('doctype', None)
131        # Here we can create various instances of PublicDocument derived
132        # classes depending on the doctype parameter given in form.
133        document = createObject('waeup.%s' % doctype)
134        self.applyData(document, **data)
135        self.context.addDocument(document)
136        doctype = getUtility(IDocumentsUtils).SELECTABLE_DOCTYPES_DICT[doctype]
137        self.flash(_('${a} added.', mapping = {'a': doctype}))
138        ob_class = self.__implemented__.__name__.replace('waeup.ikoba.','')
139        self.context.__parent__.logger.info(
140            '%s - added: %s %s' % (ob_class, doctype, document.document_id))
141        self.redirect(self.url(self.context, u'manage'))
142        return
143
144    @action(_('Cancel'), validator=NullValidator)
145    def cancel(self, **data):
146        self.redirect(self.url(self.context))
147
148
149class DocumentDisplayFormPage(IkobaDisplayFormPage):
150    """ Page to display document data
151    """
152    grok.context(IPublicDocument)
153    grok.name('index')
154    grok.require('waeup.viewDocuments')
155    grok.template('documentpage')
156    pnav = 2
157
158    @property
159    def form_fields(self):
160        return grok.AutoFields(self.context.form_fields_interface)
161
162    @property
163    def label(self):
164        return self.context.title
165
166
167class DocumentManageFormPage(IkobaEditFormPage,
168                            LocalRoleAssignmentUtilityView):
169    """ View to manage document data
170    """
171    grok.context(IPublicDocument)
172    grok.name('manage')
173    grok.require('waeup.manageDocuments')
174    grok.template('documentmanagepage')
175    pnav = 2
176
177    taboneactions = [_('Save'),_('Cancel')]
178    tabtwoactions1 = [_('Remove selected local roles')]
179    tabtwoactions2 = [_('Add local role')]
180
181    deletion_warning = _('Are you sure?')
182
183    @property
184    def form_fields(self):
185        return grok.AutoFields(self.context.form_fields_interface)
186
187    def label(self):
188        return _('Manage document ') + self.context.document_id
189
190    @action(_('Save'), style='primary')
191    def save(self, **data):
192        return msave(self, **data)
193
194    @action(_('Cancel'), validator=NullValidator)
195    def cancel(self, **data):
196        self.redirect(self.url(self.context))
197        return
198
199    @action(_('Add local role'), validator=NullValidator)
200    def addLocalRole(self, **data):
201        return add_local_role(self,2,**data)
202
203    @action(_('Remove selected local roles'))
204    def delLocalRoles(self, **data):
205        return del_local_roles(self,2,**data)
206
207
208class DocumentTriggerTransitionFormPage(IkobaEditFormPage):
209    """ View to trigger public document transitions
210    """
211    grok.context(IPublicDocument)
212    grok.name('trigtrans')
213    grok.require('waeup.triggerTransition')
214    grok.template('trigtrans')
215    label = _('Trigger document transition')
216    pnav = 2
217
218    def update(self):
219        return super(IkobaEditFormPage, self).update()
220
221    def getTransitions(self):
222        """Return a list of dicts of allowed transition ids and titles.
223
224        Each list entry provides keys ``name`` and ``title`` for
225        internal name and (human readable) title of a single
226        transition.
227        """
228        wf_info = IWorkflowInfo(self.context)
229        allowed_transitions = [t for t in wf_info.getManualTransitions()]
230        return [dict(name='', title=_('No transition'))] +[
231            dict(name=x, title=y) for x, y in allowed_transitions]
232
233    @action(_('Save'), style='primary')
234    def save(self, **data):
235        form = self.request.form
236        if 'transition' in form and form['transition']:
237            transition_id = form['transition']
238            wf_info = IWorkflowInfo(self.context)
239            try:
240                wf_info.fireTransition(transition_id)
241            except InvalidTransitionError, error:
242                self.flash(error, type="warning")
243        return
Note: See TracBrowser for help on using the repository browser.