[12206] | 1 | ## $Id: browser.py 12246 2014-12-15 12:49:01Z 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 |
---|
[12215] | 26 | from hurry.workflow.interfaces import ( |
---|
| 27 | IWorkflowInfo, IWorkflowState, InvalidTransitionError) |
---|
[12206] | 28 | from zope.event import notify |
---|
| 29 | from zope.i18n import translate |
---|
| 30 | from zope.catalog.interfaces import ICatalog |
---|
| 31 | from zope.component import queryUtility, getUtility, createObject |
---|
| 32 | from zope.schema.interfaces import ConstraintNotSatisfied, RequiredMissing |
---|
| 33 | from zope.formlib.textwidgets import BytesDisplayWidget |
---|
| 34 | from zope.security import checkPermission |
---|
[12229] | 35 | from waeup.ikoba.utils.helpers import html2dict |
---|
[12206] | 36 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 37 | from waeup.ikoba.interfaces import ( |
---|
[12242] | 38 | IContactForm, IObjectHistory, IIkobaObject, IIkobaUtils, PUBLISHED) |
---|
[12206] | 39 | from waeup.ikoba.browser.layout import ( |
---|
| 40 | IkobaPage, IkobaEditFormPage, IkobaAddFormPage, IkobaDisplayFormPage, |
---|
| 41 | IkobaForm, NullValidator, jsaction, action, UtilityView) |
---|
| 42 | from waeup.ikoba.widgets.datewidget import ( |
---|
| 43 | FriendlyDateWidget, FriendlyDateDisplayWidget, |
---|
| 44 | FriendlyDatetimeDisplayWidget) |
---|
| 45 | from waeup.ikoba.browser.breadcrumbs import Breadcrumb |
---|
| 46 | from waeup.ikoba.browser.pages import ( |
---|
| 47 | delSubobjects, add_local_role, del_local_roles, msave, |
---|
| 48 | LocalRoleAssignmentUtilityView) |
---|
| 49 | |
---|
| 50 | from waeup.ikoba.documents.interfaces import ( |
---|
[12227] | 51 | IDocumentsContainer, IPublicDocument, IHTMLDocument, IDocumentsUtils) |
---|
[12206] | 52 | |
---|
| 53 | grok.context(IIkobaObject) # Make IKofaObject the default context |
---|
| 54 | grok.templatedir('browser_templates') |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | class DocumentsBreadcrumb(Breadcrumb): |
---|
| 58 | """A breadcrumb for the customers container. |
---|
| 59 | """ |
---|
| 60 | grok.context(IDocumentsContainer) |
---|
| 61 | title = _('Documents') |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | class DocumentBreadcrumb(Breadcrumb): |
---|
| 65 | """A breadcrumb for the customer container. |
---|
| 66 | """ |
---|
[12214] | 67 | grok.context(IPublicDocument) |
---|
[12206] | 68 | |
---|
| 69 | def title(self): |
---|
| 70 | return self.context.title |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | class 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 | |
---|
| 84 | class 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 | |
---|
[12214] | 95 | @action(_('Add document'), validator=NullValidator, style='primary') |
---|
| 96 | def addSubunit(self, **data): |
---|
| 97 | self.redirect(self.url(self.context, 'adddoc')) |
---|
| 98 | return |
---|
[12206] | 99 | |
---|
[12214] | 100 | @jsaction(_('Remove selected documents')) |
---|
[12206] | 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 | |
---|
| 111 | class DocumentAddFormPage(IkobaAddFormPage): |
---|
| 112 | """Add-form to add a customer. |
---|
| 113 | """ |
---|
| 114 | grok.context(IDocumentsContainer) |
---|
| 115 | grok.require('waeup.manageDocuments') |
---|
[12214] | 116 | grok.name('adddoc') |
---|
| 117 | grok.template('documentaddform') |
---|
[12206] | 118 | label = _('Add document') |
---|
| 119 | pnav = 2 |
---|
| 120 | |
---|
[12215] | 121 | form_fields = grok.AutoFields(IPublicDocument) |
---|
| 122 | |
---|
[12214] | 123 | @property |
---|
| 124 | def selectable_doctypes(self): |
---|
| 125 | doctypes = getUtility(IDocumentsUtils).SELECTABLE_DOCTYPES_DICT |
---|
| 126 | return sorted(doctypes.items()) |
---|
| 127 | |
---|
[12222] | 128 | @action(_('Add document'), style='primary') |
---|
[12214] | 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) |
---|
[12206] | 135 | self.applyData(document, **data) |
---|
[12214] | 136 | self.context.addDocument(document) |
---|
| 137 | doctype = getUtility(IDocumentsUtils).SELECTABLE_DOCTYPES_DICT[doctype] |
---|
| 138 | self.flash(_('${a} added.', mapping = {'a': doctype})) |
---|
[12206] | 139 | ob_class = self.__implemented__.__name__.replace('waeup.ikoba.','') |
---|
| 140 | self.context.__parent__.logger.info( |
---|
[12214] | 141 | '%s - added: %s %s' % (ob_class, doctype, document.document_id)) |
---|
[12220] | 142 | self.redirect(self.url(self.context, u'manage')) |
---|
[12206] | 143 | return |
---|
| 144 | |
---|
[12214] | 145 | @action(_('Cancel'), validator=NullValidator) |
---|
| 146 | def cancel(self, **data): |
---|
| 147 | self.redirect(self.url(self.context)) |
---|
[12206] | 148 | |
---|
[12214] | 149 | |
---|
[12206] | 150 | class DocumentDisplayFormPage(IkobaDisplayFormPage): |
---|
| 151 | """ Page to display document data |
---|
| 152 | """ |
---|
[12214] | 153 | grok.context(IPublicDocument) |
---|
[12206] | 154 | grok.name('index') |
---|
| 155 | grok.require('waeup.viewDocuments') |
---|
[12215] | 156 | grok.template('documentpage') |
---|
[12206] | 157 | pnav = 2 |
---|
| 158 | |
---|
| 159 | @property |
---|
[12214] | 160 | def form_fields(self): |
---|
| 161 | return grok.AutoFields(self.context.form_fields_interface) |
---|
| 162 | |
---|
| 163 | @property |
---|
[12206] | 164 | def label(self): |
---|
| 165 | return self.context.title |
---|
| 166 | |
---|
| 167 | |
---|
[12227] | 168 | class 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( |
---|
[12239] | 177 | 'html_dict', 'html_multilingual') |
---|
[12227] | 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 | |
---|
[12242] | 189 | class HTMLDocumentDisplayContentPage(IkobaPage): |
---|
| 190 | """ Page to display the html content of document |
---|
| 191 | """ |
---|
| 192 | grok.context(IHTMLDocument) |
---|
| 193 | grok.name('display') |
---|
| 194 | grok.template('htmldisplaypage') |
---|
[12245] | 195 | grok.require('waeup.Public') |
---|
[12242] | 196 | |
---|
| 197 | @property |
---|
| 198 | def label(self): |
---|
| 199 | return self.context.title |
---|
| 200 | |
---|
| 201 | def update(self): |
---|
| 202 | if self.context.state != PUBLISHED: |
---|
| 203 | self.flash(_('The document requested has not yet been published.'), |
---|
| 204 | type="warning") |
---|
[12243] | 205 | self.redirect(self.application_url()) |
---|
[12242] | 206 | super(HTMLDocumentDisplayContentPage, self).update() |
---|
| 207 | return |
---|
| 208 | |
---|
| 209 | @property |
---|
| 210 | def content(self): |
---|
| 211 | lang = self.request.cookies.get('ikoba.language') |
---|
| 212 | html = self.context.html_dict.get(lang,'') |
---|
| 213 | if html =='': |
---|
| 214 | portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE |
---|
| 215 | html = self.context.html_dict.get(portal_language,'') |
---|
| 216 | return html |
---|
| 217 | |
---|
| 218 | |
---|
[12206] | 219 | class DocumentManageFormPage(IkobaEditFormPage, |
---|
| 220 | LocalRoleAssignmentUtilityView): |
---|
| 221 | """ View to manage document data |
---|
| 222 | """ |
---|
[12214] | 223 | grok.context(IPublicDocument) |
---|
[12206] | 224 | grok.name('manage') |
---|
| 225 | grok.require('waeup.manageDocuments') |
---|
| 226 | grok.template('documentmanagepage') |
---|
| 227 | pnav = 2 |
---|
| 228 | |
---|
| 229 | taboneactions = [_('Save'),_('Cancel')] |
---|
[12227] | 230 | tabthreeactions1 = [_('Remove selected local roles')] |
---|
| 231 | tabthreeactions2 = [_('Add local role')] |
---|
[12206] | 232 | |
---|
[12225] | 233 | deletion_warning = _('Are you sure?') |
---|
| 234 | |
---|
[12214] | 235 | @property |
---|
| 236 | def form_fields(self): |
---|
| 237 | return grok.AutoFields(self.context.form_fields_interface) |
---|
| 238 | |
---|
[12206] | 239 | def label(self): |
---|
| 240 | return _('Manage document ') + self.context.document_id |
---|
| 241 | |
---|
| 242 | @action(_('Save'), style='primary') |
---|
| 243 | def save(self, **data): |
---|
| 244 | return msave(self, **data) |
---|
| 245 | |
---|
| 246 | @action(_('Cancel'), validator=NullValidator) |
---|
| 247 | def cancel(self, **data): |
---|
| 248 | self.redirect(self.url(self.context)) |
---|
| 249 | return |
---|
| 250 | |
---|
| 251 | @action(_('Add local role'), validator=NullValidator) |
---|
| 252 | def addLocalRole(self, **data): |
---|
| 253 | return add_local_role(self,2,**data) |
---|
| 254 | |
---|
| 255 | @action(_('Remove selected local roles')) |
---|
| 256 | def delLocalRoles(self, **data): |
---|
[12215] | 257 | return del_local_roles(self,2,**data) |
---|
| 258 | |
---|
| 259 | |
---|
[12227] | 260 | class HTMLDocumentManageFormPage(DocumentManageFormPage): |
---|
| 261 | """ View to manage htmldocument data |
---|
| 262 | """ |
---|
| 263 | grok.context(IHTMLDocument) |
---|
[12236] | 264 | grok.template('htmldocumentmanagepage') |
---|
[12227] | 265 | |
---|
| 266 | @action(_('Save'), style='primary') |
---|
| 267 | def save(self, **data): |
---|
| 268 | msave(self, **data) |
---|
[12239] | 269 | html_multilingual = getattr(self.context, 'html_multilingual', None) |
---|
[12229] | 270 | portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE |
---|
[12239] | 271 | self.context.html_dict = html2dict(html_multilingual, portal_language) |
---|
[12227] | 272 | return |
---|
| 273 | |
---|
| 274 | |
---|
[12215] | 275 | class DocumentTriggerTransitionFormPage(IkobaEditFormPage): |
---|
| 276 | """ View to trigger public document transitions |
---|
| 277 | """ |
---|
| 278 | grok.context(IPublicDocument) |
---|
| 279 | grok.name('trigtrans') |
---|
| 280 | grok.require('waeup.triggerTransition') |
---|
| 281 | grok.template('trigtrans') |
---|
| 282 | label = _('Trigger document transition') |
---|
| 283 | pnav = 2 |
---|
| 284 | |
---|
| 285 | def update(self): |
---|
| 286 | return super(IkobaEditFormPage, self).update() |
---|
| 287 | |
---|
| 288 | def getTransitions(self): |
---|
| 289 | """Return a list of dicts of allowed transition ids and titles. |
---|
| 290 | |
---|
| 291 | Each list entry provides keys ``name`` and ``title`` for |
---|
| 292 | internal name and (human readable) title of a single |
---|
| 293 | transition. |
---|
| 294 | """ |
---|
| 295 | wf_info = IWorkflowInfo(self.context) |
---|
| 296 | allowed_transitions = [t for t in wf_info.getManualTransitions()] |
---|
| 297 | return [dict(name='', title=_('No transition'))] +[ |
---|
| 298 | dict(name=x, title=y) for x, y in allowed_transitions] |
---|
| 299 | |
---|
[12246] | 300 | @action(_('Apply now'), style='primary') |
---|
| 301 | def apply(self, **data): |
---|
[12215] | 302 | form = self.request.form |
---|
| 303 | if 'transition' in form and form['transition']: |
---|
| 304 | transition_id = form['transition'] |
---|
| 305 | wf_info = IWorkflowInfo(self.context) |
---|
| 306 | try: |
---|
| 307 | wf_info.fireTransition(transition_id) |
---|
[12246] | 308 | self.flash(_("Transition '%s' executed." % transition_id)) |
---|
[12215] | 309 | except InvalidTransitionError, error: |
---|
| 310 | self.flash(error, type="warning") |
---|
[12246] | 311 | self.redirect(self.url(self.context)) |
---|
[12215] | 312 | return |
---|