[12015] | 1 | ## $Id: documents.py 12442 2015-01-11 16:53:38Z henrik $ |
---|
[11989] | 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 | """ |
---|
| 19 | Customer document components. |
---|
| 20 | """ |
---|
[12035] | 21 | import os |
---|
[11989] | 22 | import grok |
---|
[12161] | 23 | from hashlib import md5 |
---|
[12213] | 24 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
[12018] | 25 | from zope.component import queryUtility, getUtility |
---|
[11989] | 26 | from zope.component.interfaces import IFactory |
---|
| 27 | from zope.interface import implementedBy |
---|
[12194] | 28 | from zope.event import notify |
---|
[12035] | 29 | |
---|
| 30 | from waeup.ikoba.image import IkobaImageFile |
---|
| 31 | from waeup.ikoba.imagestorage import DefaultFileStoreHandler |
---|
[11989] | 32 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
[12035] | 33 | from waeup.ikoba.interfaces import ( |
---|
| 34 | IFileStoreNameChooser, IFileStoreHandler, |
---|
[12256] | 35 | IIkobaUtils, IExtFileStore, IIDSource) |
---|
[11989] | 36 | from waeup.ikoba.customers.interfaces import ( |
---|
[12018] | 37 | ICustomerDocumentsContainer, ICustomerNavigation, ICustomerDocument, |
---|
[12053] | 38 | ICustomersUtils, ICustomerPDFDocument) |
---|
[11989] | 39 | from waeup.ikoba.documents import DocumentsContainer, Document |
---|
[12018] | 40 | from waeup.ikoba.documents.interfaces import IDocumentsUtils |
---|
[11989] | 41 | from waeup.ikoba.utils.helpers import attrs_to_fields |
---|
| 42 | |
---|
[12035] | 43 | from waeup.ikoba.customers.utils import path_from_custid |
---|
| 44 | |
---|
[11989] | 45 | class CustomerDocumentsContainer(DocumentsContainer): |
---|
| 46 | """This is a container for customer documents. |
---|
| 47 | """ |
---|
| 48 | grok.implements(ICustomerDocumentsContainer, ICustomerNavigation) |
---|
| 49 | grok.provides(ICustomerDocumentsContainer) |
---|
| 50 | |
---|
| 51 | def __init__(self): |
---|
| 52 | super(CustomerDocumentsContainer, self).__init__() |
---|
| 53 | return |
---|
| 54 | |
---|
| 55 | @property |
---|
| 56 | def customer(self): |
---|
| 57 | return self.__parent__ |
---|
| 58 | |
---|
| 59 | def writeLogMessage(self, view, message): |
---|
| 60 | return self.__parent__.writeLogMessage(view, message) |
---|
| 61 | |
---|
| 62 | CustomerDocumentsContainer = attrs_to_fields(CustomerDocumentsContainer) |
---|
| 63 | |
---|
[12057] | 64 | class CustomerDocumentBase(Document): |
---|
| 65 | """This is a customer document baseclass. |
---|
[11989] | 66 | """ |
---|
[12057] | 67 | grok.baseclass() |
---|
[11989] | 68 | |
---|
[12211] | 69 | local_roles = [] |
---|
| 70 | |
---|
[12256] | 71 | def __init__(self): |
---|
| 72 | super(CustomerDocumentBase, self).__init__() |
---|
| 73 | source = getUtility(IIDSource) |
---|
| 74 | self.document_id = unicode(source.get_hex_uuid()) |
---|
| 75 | return |
---|
| 76 | |
---|
[11989] | 77 | @property |
---|
[12213] | 78 | def state(self): |
---|
| 79 | state = IWorkflowState(self).getState() |
---|
| 80 | return state |
---|
| 81 | |
---|
| 82 | @property |
---|
| 83 | def translated_state(self): |
---|
| 84 | try: |
---|
| 85 | TRANSLATED_STATES = getUtility( |
---|
[12214] | 86 | ICustomersUtils).TRANSLATED_DOCUMENT_STATES |
---|
[12213] | 87 | return TRANSLATED_STATES[self.state] |
---|
| 88 | except KeyError: |
---|
| 89 | return |
---|
| 90 | @property |
---|
[11989] | 91 | def customer(self): |
---|
| 92 | try: |
---|
| 93 | return self.__parent__.__parent__ |
---|
| 94 | except AttributeError: |
---|
| 95 | return None |
---|
| 96 | |
---|
[12128] | 97 | @property |
---|
| 98 | def user_id(self): |
---|
| 99 | if self.customer is not None: |
---|
| 100 | return self.customer.customer_id |
---|
| 101 | return |
---|
| 102 | |
---|
[11989] | 103 | def writeLogMessage(self, view, message): |
---|
| 104 | return self.__parent__.__parent__.writeLogMessage(view, message) |
---|
| 105 | |
---|
[12018] | 106 | @property |
---|
[12166] | 107 | def is_editable_by_customer(self): |
---|
[12018] | 108 | try: |
---|
| 109 | # Customer must be approved |
---|
| 110 | cond1 = self.customer.state in getUtility( |
---|
[12088] | 111 | ICustomersUtils).DOCMANAGE_CUSTOMER_STATES |
---|
[12018] | 112 | # Document must be in state created |
---|
| 113 | cond2 = self.state in getUtility( |
---|
[12088] | 114 | ICustomersUtils).DOCMANAGE_DOCUMENT_STATES |
---|
[12018] | 115 | if not (cond1 and cond2): |
---|
| 116 | return False |
---|
| 117 | except AttributeError: |
---|
| 118 | pass |
---|
| 119 | return True |
---|
| 120 | |
---|
[12053] | 121 | @property |
---|
[12166] | 122 | def is_editable_by_manager(self): |
---|
| 123 | try: |
---|
| 124 | # Document must be in state created |
---|
| 125 | cond = self.state in getUtility( |
---|
| 126 | ICustomersUtils).DOCMANAGE_DOCUMENT_STATES |
---|
| 127 | if not cond: |
---|
| 128 | return False |
---|
| 129 | except AttributeError: |
---|
| 130 | pass |
---|
| 131 | return True |
---|
| 132 | |
---|
| 133 | @property |
---|
[12056] | 134 | def translated_class_name(self): |
---|
[12053] | 135 | try: |
---|
| 136 | DOCTYPES_DICT = getUtility(ICustomersUtils).DOCTYPES_DICT |
---|
[12056] | 137 | return DOCTYPES_DICT[self.class_name] |
---|
[12053] | 138 | except KeyError: |
---|
| 139 | return |
---|
| 140 | |
---|
[12161] | 141 | @property |
---|
| 142 | def connected_files(self): |
---|
| 143 | store = getUtility(IExtFileStore) |
---|
| 144 | files = [] |
---|
| 145 | try: |
---|
[12164] | 146 | # Usually there is only a single element in self.filenames. |
---|
[12161] | 147 | for filename in self.filenames: |
---|
| 148 | attrname = filename.replace('.','_') |
---|
| 149 | file = store.getFileByContext(self, attr=filename) |
---|
[12165] | 150 | if file: |
---|
| 151 | files.append((attrname, file)) |
---|
[12161] | 152 | except AttributeError: |
---|
| 153 | # In unit tests we don't have a customer to |
---|
| 154 | # determine the file path. |
---|
| 155 | return |
---|
| 156 | return files |
---|
[11989] | 157 | |
---|
[12168] | 158 | @property |
---|
| 159 | def is_verifiable(self): |
---|
[12352] | 160 | if self.customer and not self.customer.state in getUtility( |
---|
| 161 | ICustomersUtils).CONMANAGE_CUSTOMER_STATES: |
---|
| 162 | return False, _("Customer has not yet been approved.") |
---|
[12169] | 163 | files = self.connected_files |
---|
| 164 | if files is not None and len(files) != len(self.filenames): |
---|
| 165 | return False, _("No file uploaded.") |
---|
[12168] | 166 | return True, None |
---|
| 167 | |
---|
[12161] | 168 | def setMD5(self): |
---|
[12164] | 169 | """Set md5 checksum attribute for files connected to this document. |
---|
[12161] | 170 | """ |
---|
[12162] | 171 | connected_files = self.connected_files |
---|
| 172 | if connected_files: |
---|
| 173 | for file in self.connected_files: |
---|
| 174 | attrname = '%s_md5' % file[0] |
---|
| 175 | checksum = md5(file[1].read()).hexdigest() |
---|
| 176 | setattr(self, attrname, checksum) |
---|
[12161] | 177 | return |
---|
| 178 | |
---|
| 179 | |
---|
[12057] | 180 | class CustomerSampleDocument(CustomerDocumentBase): |
---|
| 181 | """This is a sample customer document. |
---|
[12053] | 182 | """ |
---|
[12057] | 183 | |
---|
[12356] | 184 | grok.implements(ICustomerDocument, ICustomerNavigation) |
---|
| 185 | grok.provides(ICustomerDocument) |
---|
| 186 | |
---|
[12164] | 187 | # Ikoba can store any number of files per Document object. |
---|
| 188 | # However, we highly recommend to associate and store |
---|
| 189 | # only one file per Document object. Thus the following |
---|
| 190 | # tuple should contain only a single filename string. |
---|
[12161] | 191 | filenames = ('sample',) |
---|
| 192 | |
---|
[12214] | 193 | form_fields_interface = ICustomerDocument |
---|
| 194 | |
---|
[12057] | 195 | CustomerSampleDocument = attrs_to_fields(CustomerSampleDocument) |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | class CustomerPDFDocument(CustomerDocumentBase): |
---|
| 199 | """This is a customer document for a single pdf upload file. |
---|
| 200 | """ |
---|
[12053] | 201 | grok.implements(ICustomerPDFDocument, ICustomerNavigation) |
---|
| 202 | grok.provides(ICustomerPDFDocument) |
---|
[11989] | 203 | |
---|
[12164] | 204 | # Ikoba can store any number of files per Document object. |
---|
| 205 | # However, we highly recommend to associate and store |
---|
| 206 | # only one file per Document object. Thus the following |
---|
| 207 | # tuple should contain only a single filename string. |
---|
| 208 | filenames = ('sample.pdf',) |
---|
| 209 | |
---|
[12214] | 210 | form_fields_interface = ICustomerPDFDocument |
---|
| 211 | |
---|
[12053] | 212 | CustomerPDFDocument = attrs_to_fields(CustomerPDFDocument) |
---|
| 213 | |
---|
| 214 | |
---|
[12164] | 215 | # Customer documents must be importable. So we need a factory. |
---|
[11989] | 216 | class CustomerDocumentFactory(grok.GlobalUtility): |
---|
| 217 | """A factory for customer documents. |
---|
| 218 | """ |
---|
| 219 | grok.implements(IFactory) |
---|
[12057] | 220 | grok.name(u'waeup.CustomerSampleDocument') |
---|
[11989] | 221 | title = u"Create a new document.", |
---|
[12053] | 222 | description = u"This factory instantiates new sample document instances." |
---|
[11989] | 223 | |
---|
| 224 | def __call__(self, *args, **kw): |
---|
[12057] | 225 | return CustomerSampleDocument(*args, **kw) |
---|
[11989] | 226 | |
---|
| 227 | def getInterfaces(self): |
---|
[12057] | 228 | return implementedBy(CustomerSampleDocument) |
---|
[12035] | 229 | |
---|
[12053] | 230 | # Customer documents must be importable. So we might need a factory. |
---|
| 231 | class CustomerPDFDocumentFactory(grok.GlobalUtility): |
---|
| 232 | """A factory for customer pdf documents. |
---|
| 233 | """ |
---|
| 234 | grok.implements(IFactory) |
---|
| 235 | grok.name(u'waeup.CustomerPDFDocument') |
---|
| 236 | title = u"Create a new document.", |
---|
| 237 | description = u"This factory instantiates new pdf document instances." |
---|
| 238 | |
---|
| 239 | def __call__(self, *args, **kw): |
---|
| 240 | return CustomerPDFDocument(*args, **kw) |
---|
| 241 | |
---|
| 242 | def getInterfaces(self): |
---|
| 243 | return implementedBy(CustomerPDFDocument) |
---|
| 244 | |
---|
[12035] | 245 | #: The file id marker for customer files |
---|
| 246 | CUSTOMERDOCUMENT_FILE_STORE_NAME = 'file-customerdocument' |
---|
| 247 | |
---|
| 248 | |
---|
| 249 | class CustomerDocumentFileNameChooser(grok.Adapter): |
---|
| 250 | """A file id chooser for :class:`CustomerDocument` objects. |
---|
| 251 | |
---|
| 252 | `context` is an :class:`CustomerDocument` instance. |
---|
| 253 | |
---|
| 254 | The :class:`CustomerDocumentFileNameChooser` can build/check file ids for |
---|
| 255 | :class:`Customer` objects suitable for use with |
---|
| 256 | :class:`ExtFileStore` instances. The delivered file_id contains |
---|
| 257 | the file id marker for :class:`CustomerDocument` object and the customer id |
---|
| 258 | of the context customer. |
---|
| 259 | |
---|
| 260 | This chooser is registered as an adapter providing |
---|
| 261 | :class:`waeup.ikoba.interfaces.IFileStoreNameChooser`. |
---|
| 262 | |
---|
| 263 | File store name choosers like this one are only convenience |
---|
| 264 | components to ease the task of creating file ids for customer document |
---|
| 265 | objects. You are nevertheless encouraged to use them instead of |
---|
| 266 | manually setting up filenames for customer documents. |
---|
| 267 | |
---|
| 268 | .. seealso:: :mod:`waeup.ikoba.imagestorage` |
---|
| 269 | |
---|
| 270 | """ |
---|
| 271 | |
---|
| 272 | grok.context(ICustomerDocument) |
---|
| 273 | grok.implements(IFileStoreNameChooser) |
---|
| 274 | |
---|
| 275 | def checkName(self, name=None, attr=None): |
---|
| 276 | """Check whether the given name is a valid file id for the context. |
---|
| 277 | |
---|
| 278 | Returns ``True`` only if `name` equals the result of |
---|
| 279 | :meth:`chooseName`. |
---|
| 280 | |
---|
| 281 | """ |
---|
| 282 | return name == self.chooseName() |
---|
| 283 | |
---|
| 284 | def chooseName(self, attr, name=None): |
---|
| 285 | """Get a valid file id for customer document context. |
---|
| 286 | |
---|
| 287 | *Example:* |
---|
| 288 | |
---|
| 289 | For a customer with customer id ``'A123456'`` |
---|
| 290 | and document with id 'd123' |
---|
| 291 | with attr ``'nice_image.jpeg'`` stored in |
---|
| 292 | the customers container this chooser would create: |
---|
| 293 | |
---|
[12205] | 294 | ``'__file-customerdocument__customers/345/c999/nice_image_d123_c999.jpeg'`` |
---|
[12035] | 295 | |
---|
| 296 | meaning that the nice image of this customer document would be |
---|
| 297 | stored in the site-wide file storage in path: |
---|
| 298 | |
---|
[12205] | 299 | ``customers/345/c999/nice_image_d123_c999.jpeg`` |
---|
[12035] | 300 | |
---|
| 301 | """ |
---|
| 302 | basename, ext = os.path.splitext(attr) |
---|
| 303 | cust_id = self.context.customer.customer_id |
---|
| 304 | doc_id = self.context.document_id |
---|
| 305 | marked_filename = '__%s__%s/%s_%s_%s%s' % ( |
---|
| 306 | CUSTOMERDOCUMENT_FILE_STORE_NAME, path_from_custid(cust_id), |
---|
| 307 | basename, doc_id, cust_id, ext) |
---|
| 308 | return marked_filename |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | class CustomerDocumentFileStoreHandler(DefaultFileStoreHandler, grok.GlobalUtility): |
---|
| 312 | """Customer document specific file handling. |
---|
| 313 | |
---|
| 314 | This handler knows in which path in a filestore to store customer document |
---|
| 315 | files and how to turn this kind of data into some (browsable) |
---|
| 316 | file object. |
---|
| 317 | |
---|
| 318 | It is called from the global file storage, when it wants to |
---|
| 319 | get/store a file with a file id starting with |
---|
| 320 | ``__file-customerdocument__`` (the marker string for customer files). |
---|
| 321 | |
---|
| 322 | Like each other file store handler it does not handle the files |
---|
| 323 | really (this is done by the global file store) but only computes |
---|
| 324 | paths and things like this. |
---|
| 325 | """ |
---|
| 326 | grok.implements(IFileStoreHandler) |
---|
| 327 | grok.name(CUSTOMERDOCUMENT_FILE_STORE_NAME) |
---|
| 328 | |
---|
| 329 | def pathFromFileID(self, store, root, file_id): |
---|
| 330 | """All customer document files are put in directory ``customers``. |
---|
| 331 | """ |
---|
| 332 | marker, filename, basename, ext = store.extractMarker(file_id) |
---|
| 333 | sub_root = os.path.join(root, 'customers') |
---|
| 334 | return super(CustomerDocumentFileStoreHandler, self).pathFromFileID( |
---|
| 335 | store, sub_root, basename) |
---|
| 336 | |
---|
| 337 | def createFile(self, store, root, filename, file_id, file): |
---|
| 338 | """Create a browsable file-like object. |
---|
| 339 | """ |
---|
| 340 | # call super method to ensure that any old files with |
---|
| 341 | # different filename extension are deleted. |
---|
| 342 | file, path, file_obj = super( |
---|
| 343 | CustomerDocumentFileStoreHandler, self).createFile( |
---|
| 344 | store, root, filename, file_id, file) |
---|
| 345 | return file, path, IkobaImageFile( |
---|
| 346 | file_obj.filename, file_obj.data) |
---|
| 347 | |
---|
[12194] | 348 | |
---|
| 349 | @grok.subscribe(ICustomerDocument, grok.IObjectRemovedEvent) |
---|
| 350 | def handle_document_removed(document, event): |
---|
| 351 | """If a document is deleted, we make sure that also referrers to |
---|
| 352 | customer contract objects are removed. |
---|
| 353 | """ |
---|
| 354 | docid = document.document_id |
---|
| 355 | |
---|
| 356 | # Find all customer contracts that refer to given document... |
---|
| 357 | try: |
---|
| 358 | contracts = document.customer['contracts'].values() |
---|
| 359 | except AttributeError: |
---|
| 360 | # customer not available. This might happen during tests. |
---|
| 361 | return |
---|
| 362 | for contract in contracts: |
---|
| 363 | # Remove that referrer... |
---|
| 364 | for key, value in contract.__dict__.items(): |
---|
| 365 | if key.endswith('_object') and \ |
---|
| 366 | getattr(value, 'document_id', None) == docid: |
---|
| 367 | setattr(contract, key, None) |
---|
| 368 | notify(grok.ObjectModifiedEvent(contract)) |
---|
| 369 | contract.customer.__parent__.logger.info( |
---|
| 370 | 'ObjectRemovedEvent - %s - %s - removed: %s' % ( |
---|
| 371 | contract.customer.customer_id, |
---|
| 372 | contract.contract_id, |
---|
| 373 | document.document_id)) |
---|
| 374 | return |
---|
| 375 | |
---|