[12206] | 1 | ## $Id: browser.py 12206 2014-12-13 07:28:59Z 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 | |
---|
| 21 | import sys |
---|
| 22 | import grok |
---|
| 23 | import pytz |
---|
| 24 | from urllib import urlencode |
---|
| 25 | from datetime import datetime |
---|
| 26 | from zope.event import notify |
---|
| 27 | from zope.i18n import translate |
---|
| 28 | from zope.catalog.interfaces import ICatalog |
---|
| 29 | from zope.component import queryUtility, getUtility, createObject |
---|
| 30 | from zope.schema.interfaces import ConstraintNotSatisfied, RequiredMissing |
---|
| 31 | from zope.formlib.textwidgets import BytesDisplayWidget |
---|
| 32 | from zope.security import checkPermission |
---|
| 33 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 34 | from waeup.ikoba.interfaces import ( |
---|
| 35 | IContactForm, IObjectHistory, IIkobaObject, IIkobaUtils) |
---|
| 36 | from waeup.ikoba.browser.layout import ( |
---|
| 37 | IkobaPage, IkobaEditFormPage, IkobaAddFormPage, IkobaDisplayFormPage, |
---|
| 38 | IkobaForm, NullValidator, jsaction, action, UtilityView) |
---|
| 39 | from waeup.ikoba.widgets.datewidget import ( |
---|
| 40 | FriendlyDateWidget, FriendlyDateDisplayWidget, |
---|
| 41 | FriendlyDatetimeDisplayWidget) |
---|
| 42 | from waeup.ikoba.browser.breadcrumbs import Breadcrumb |
---|
| 43 | from waeup.ikoba.browser.pages import ( |
---|
| 44 | delSubobjects, add_local_role, del_local_roles, msave, |
---|
| 45 | LocalRoleAssignmentUtilityView) |
---|
| 46 | |
---|
| 47 | from waeup.ikoba.documents.interfaces import ( |
---|
| 48 | IDocumentsContainer, IDocument) |
---|
| 49 | |
---|
| 50 | grok.context(IIkobaObject) # Make IKofaObject the default context |
---|
| 51 | grok.templatedir('browser_templates') |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | class DocumentsBreadcrumb(Breadcrumb): |
---|
| 55 | """A breadcrumb for the customers container. |
---|
| 56 | """ |
---|
| 57 | grok.context(IDocumentsContainer) |
---|
| 58 | title = _('Documents') |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | class DocumentBreadcrumb(Breadcrumb): |
---|
| 62 | """A breadcrumb for the customer container. |
---|
| 63 | """ |
---|
| 64 | grok.context(IDocument) |
---|
| 65 | |
---|
| 66 | def title(self): |
---|
| 67 | return self.context.title |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | class DocumentsContainerPage(IkobaDisplayFormPage): |
---|
| 71 | """The standard view for document containers. |
---|
| 72 | """ |
---|
| 73 | grok.context(IDocumentsContainer) |
---|
| 74 | grok.name('index') |
---|
| 75 | grok.require('waeup.viewDocuments') |
---|
| 76 | grok.template('containerpage') |
---|
| 77 | pnav = 2 |
---|
| 78 | label = _('Documents') |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | class DocumentsContainerManageFormPage(IkobaEditFormPage, |
---|
| 82 | LocalRoleAssignmentUtilityView): |
---|
| 83 | """The manage page for customer containers. |
---|
| 84 | """ |
---|
| 85 | grok.context(IDocumentsContainer) |
---|
| 86 | grok.name('manage') |
---|
| 87 | grok.require('waeup.manageDocuments') |
---|
| 88 | grok.template('containermanagepage') |
---|
| 89 | pnav = 2 |
---|
| 90 | label = _('Manage document section') |
---|
| 91 | |
---|
| 92 | taboneactions = [_('Save'),_('Cancel')] |
---|
| 93 | tabtwoactions = [_('Add document'), _('Remove selected'),_('Cancel')] |
---|
| 94 | |
---|
| 95 | @jsaction(_('Remove selected')) |
---|
| 96 | def delDocuments(self, **data): |
---|
| 97 | delSubobjects(self, redirect='manage', tab='2') |
---|
| 98 | return |
---|
| 99 | |
---|
| 100 | @action(_('Save'), style='primary') |
---|
| 101 | def save(self, **data): |
---|
| 102 | return msave(self, **data) |
---|
| 103 | |
---|
| 104 | @action(_('Cancel'), validator=NullValidator) |
---|
| 105 | def cancel(self, **data): |
---|
| 106 | self.redirect(self.url(self.context)) |
---|
| 107 | return |
---|
| 108 | |
---|
| 109 | @action(_('Add document'), validator=NullValidator) |
---|
| 110 | def addSubunit(self, **data): |
---|
| 111 | self.redirect(self.url(self.context, 'adddocument')) |
---|
| 112 | return |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | class DocumentAddFormPage(IkobaAddFormPage): |
---|
| 116 | """Add-form to add a customer. |
---|
| 117 | """ |
---|
| 118 | grok.context(IDocumentsContainer) |
---|
| 119 | grok.require('waeup.manageDocuments') |
---|
| 120 | grok.name('adddocument') |
---|
| 121 | form_fields = grok.AutoFields(IDocument).omit('document_id') |
---|
| 122 | label = _('Add document') |
---|
| 123 | pnav = 2 |
---|
| 124 | |
---|
| 125 | @action(_('Create document'), style='primary') |
---|
| 126 | def addDocument(self, **data): |
---|
| 127 | document = createObject(u'waeup.Document') |
---|
| 128 | self.applyData(document, **data) |
---|
| 129 | try: |
---|
| 130 | self.context.addDocument(document) |
---|
| 131 | except KeyError: |
---|
| 132 | self.flash(_('The Id chosen already exists.'), |
---|
| 133 | type='danger') |
---|
| 134 | return |
---|
| 135 | self.flash( |
---|
| 136 | _("Document ${a} added.", mapping = {'a':document.document_id})) |
---|
| 137 | ob_class = self.__implemented__.__name__.replace('waeup.ikoba.','') |
---|
| 138 | self.context.__parent__.logger.info( |
---|
| 139 | '%s - added: %s' % (ob_class, document.document_id)) |
---|
| 140 | self.redirect(self.url(self.context, u'manage')+'#tab2') |
---|
| 141 | return |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | class DocumentDisplayFormPage(IkobaDisplayFormPage): |
---|
| 145 | """ Page to display document data |
---|
| 146 | """ |
---|
| 147 | grok.context(IDocument) |
---|
| 148 | grok.name('index') |
---|
| 149 | grok.require('waeup.viewDocuments') |
---|
| 150 | #grok.template('basepage') |
---|
| 151 | form_fields = grok.AutoFields(IDocument) |
---|
| 152 | pnav = 2 |
---|
| 153 | |
---|
| 154 | @property |
---|
| 155 | def label(self): |
---|
| 156 | return self.context.title |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | class DocumentManageFormPage(IkobaEditFormPage, |
---|
| 160 | LocalRoleAssignmentUtilityView): |
---|
| 161 | """ View to manage document data |
---|
| 162 | """ |
---|
| 163 | grok.context(IDocument) |
---|
| 164 | grok.name('manage') |
---|
| 165 | grok.require('waeup.manageDocuments') |
---|
| 166 | form_fields = grok.AutoFields(IDocument).omit('document_id') |
---|
| 167 | grok.template('documentmanagepage') |
---|
| 168 | pnav = 2 |
---|
| 169 | |
---|
| 170 | taboneactions = [_('Save'),_('Cancel')] |
---|
| 171 | tabtwoactions1 = [_('Remove selected local roles')] |
---|
| 172 | tabtwoactions2 = [_('Add local role')] |
---|
| 173 | |
---|
| 174 | def label(self): |
---|
| 175 | return _('Manage document ') + self.context.document_id |
---|
| 176 | |
---|
| 177 | @action(_('Save'), style='primary') |
---|
| 178 | def save(self, **data): |
---|
| 179 | return msave(self, **data) |
---|
| 180 | |
---|
| 181 | @action(_('Cancel'), validator=NullValidator) |
---|
| 182 | def cancel(self, **data): |
---|
| 183 | self.redirect(self.url(self.context)) |
---|
| 184 | return |
---|
| 185 | |
---|
| 186 | @action(_('Add local role'), validator=NullValidator) |
---|
| 187 | def addLocalRole(self, **data): |
---|
| 188 | return add_local_role(self,2,**data) |
---|
| 189 | |
---|
| 190 | @action(_('Remove selected local roles')) |
---|
| 191 | def delLocalRoles(self, **data): |
---|
| 192 | return del_local_roles(self,2,**data) |
---|