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