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

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

Add first browser test.

  • Property svn:keywords set to Id
File size: 8.0 KB
Line 
1## $Id: browser.py 12222 2014-12-14 06:20: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.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    @property
182    def form_fields(self):
183        return grok.AutoFields(self.context.form_fields_interface)
184
185    def label(self):
186        return _('Manage document ') + self.context.document_id
187
188    @action(_('Save'), style='primary')
189    def save(self, **data):
190        return msave(self, **data)
191
192    @action(_('Cancel'), validator=NullValidator)
193    def cancel(self, **data):
194        self.redirect(self.url(self.context))
195        return
196
197    @action(_('Add local role'), validator=NullValidator)
198    def addLocalRole(self, **data):
199        return add_local_role(self,2,**data)
200
201    @action(_('Remove selected local roles'))
202    def delLocalRoles(self, **data):
203        return del_local_roles(self,2,**data)
204
205
206class DocumentTriggerTransitionFormPage(IkobaEditFormPage):
207    """ View to trigger public document transitions
208    """
209    grok.context(IPublicDocument)
210    grok.name('trigtrans')
211    grok.require('waeup.triggerTransition')
212    grok.template('trigtrans')
213    label = _('Trigger document transition')
214    pnav = 2
215
216    def update(self):
217        return super(IkobaEditFormPage, self).update()
218
219    def getTransitions(self):
220        """Return a list of dicts of allowed transition ids and titles.
221
222        Each list entry provides keys ``name`` and ``title`` for
223        internal name and (human readable) title of a single
224        transition.
225        """
226        wf_info = IWorkflowInfo(self.context)
227        allowed_transitions = [t for t in wf_info.getManualTransitions()]
228        return [dict(name='', title=_('No transition'))] +[
229            dict(name=x, title=y) for x, y in allowed_transitions]
230
231    @action(_('Save'), style='primary')
232    def save(self, **data):
233        form = self.request.form
234        if 'transition' in form and form['transition']:
235            transition_id = form['transition']
236            wf_info = IWorkflowInfo(self.context)
237            try:
238                wf_info.fireTransition(transition_id)
239            except InvalidTransitionError, error:
240                self.flash(error, type="warning")
241        return
Note: See TracBrowser for help on using the repository browser.