## $Id: documents.py 12128 2014-12-03 18:10:43Z henrik $ ## ## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """ Customer document components. """ import os import grok from zope.component import queryUtility, getUtility from zope.component.interfaces import IFactory from zope.interface import implementedBy from waeup.ikoba.image import IkobaImageFile from waeup.ikoba.imagestorage import DefaultFileStoreHandler from waeup.ikoba.interfaces import MessageFactory as _ from waeup.ikoba.interfaces import ( IFileStoreNameChooser, IFileStoreHandler, IIkobaUtils, IExtFileStore) from waeup.ikoba.customers.interfaces import ( ICustomerDocumentsContainer, ICustomerNavigation, ICustomerDocument, ICustomersUtils, ICustomerPDFDocument) from waeup.ikoba.documents import DocumentsContainer, Document from waeup.ikoba.documents.interfaces import IDocumentsUtils from waeup.ikoba.utils.helpers import attrs_to_fields from waeup.ikoba.customers.utils import path_from_custid class CustomerDocumentsContainer(DocumentsContainer): """This is a container for customer documents. """ grok.implements(ICustomerDocumentsContainer, ICustomerNavigation) grok.provides(ICustomerDocumentsContainer) def __init__(self): super(CustomerDocumentsContainer, self).__init__() return @property def customer(self): return self.__parent__ def writeLogMessage(self, view, message): return self.__parent__.writeLogMessage(view, message) CustomerDocumentsContainer = attrs_to_fields(CustomerDocumentsContainer) class CustomerDocumentBase(Document): """This is a customer document baseclass. """ grok.implements(ICustomerDocument, ICustomerNavigation) grok.provides(ICustomerDocument) grok.baseclass() @property def customer(self): try: return self.__parent__.__parent__ except AttributeError: return None @property def user_id(self): if self.customer is not None: return self.customer.customer_id return def writeLogMessage(self, view, message): return self.__parent__.__parent__.writeLogMessage(view, message) @property def is_editable(self): try: # Customer must be approved cond1 = self.customer.state in getUtility( ICustomersUtils).DOCMANAGE_CUSTOMER_STATES # Document must be in state created cond2 = self.state in getUtility( ICustomersUtils).DOCMANAGE_DOCUMENT_STATES if not (cond1 and cond2): return False except AttributeError: pass return True @property def translated_class_name(self): try: DOCTYPES_DICT = getUtility(ICustomersUtils).DOCTYPES_DICT return DOCTYPES_DICT[self.class_name] except KeyError: return class CustomerSampleDocument(CustomerDocumentBase): """This is a sample customer document. """ CustomerSampleDocument = attrs_to_fields(CustomerSampleDocument) class CustomerPDFDocument(CustomerDocumentBase): """This is a customer document for a single pdf upload file. """ grok.implements(ICustomerPDFDocument, ICustomerNavigation) grok.provides(ICustomerPDFDocument) CustomerPDFDocument = attrs_to_fields(CustomerPDFDocument) # Customer documents must be importable. So we might need a factory. class CustomerDocumentFactory(grok.GlobalUtility): """A factory for customer documents. """ grok.implements(IFactory) grok.name(u'waeup.CustomerSampleDocument') title = u"Create a new document.", description = u"This factory instantiates new sample document instances." def __call__(self, *args, **kw): return CustomerSampleDocument(*args, **kw) def getInterfaces(self): return implementedBy(CustomerSampleDocument) # Customer documents must be importable. So we might need a factory. class CustomerPDFDocumentFactory(grok.GlobalUtility): """A factory for customer pdf documents. """ grok.implements(IFactory) grok.name(u'waeup.CustomerPDFDocument') title = u"Create a new document.", description = u"This factory instantiates new pdf document instances." def __call__(self, *args, **kw): return CustomerPDFDocument(*args, **kw) def getInterfaces(self): return implementedBy(CustomerPDFDocument) #: The file id marker for customer files CUSTOMERDOCUMENT_FILE_STORE_NAME = 'file-customerdocument' class CustomerDocumentFileNameChooser(grok.Adapter): """A file id chooser for :class:`CustomerDocument` objects. `context` is an :class:`CustomerDocument` instance. The :class:`CustomerDocumentFileNameChooser` can build/check file ids for :class:`Customer` objects suitable for use with :class:`ExtFileStore` instances. The delivered file_id contains the file id marker for :class:`CustomerDocument` object and the customer id of the context customer. This chooser is registered as an adapter providing :class:`waeup.ikoba.interfaces.IFileStoreNameChooser`. File store name choosers like this one are only convenience components to ease the task of creating file ids for customer document objects. You are nevertheless encouraged to use them instead of manually setting up filenames for customer documents. .. seealso:: :mod:`waeup.ikoba.imagestorage` """ grok.context(ICustomerDocument) grok.implements(IFileStoreNameChooser) def checkName(self, name=None, attr=None): """Check whether the given name is a valid file id for the context. Returns ``True`` only if `name` equals the result of :meth:`chooseName`. """ return name == self.chooseName() def chooseName(self, attr, name=None): """Get a valid file id for customer document context. *Example:* For a customer with customer id ``'A123456'`` and document with id 'd123' with attr ``'nice_image.jpeg'`` stored in the customers container this chooser would create: ``'__file-customerdocument__customers/A/A123456/nice_image_d123_A123456.jpeg'`` meaning that the nice image of this customer document would be stored in the site-wide file storage in path: ``customers/A/A123456/nice_image_d123_A123456.jpeg`` """ basename, ext = os.path.splitext(attr) cust_id = self.context.customer.customer_id doc_id = self.context.document_id marked_filename = '__%s__%s/%s_%s_%s%s' % ( CUSTOMERDOCUMENT_FILE_STORE_NAME, path_from_custid(cust_id), basename, doc_id, cust_id, ext) return marked_filename class CustomerDocumentFileStoreHandler(DefaultFileStoreHandler, grok.GlobalUtility): """Customer document specific file handling. This handler knows in which path in a filestore to store customer document files and how to turn this kind of data into some (browsable) file object. It is called from the global file storage, when it wants to get/store a file with a file id starting with ``__file-customerdocument__`` (the marker string for customer files). Like each other file store handler it does not handle the files really (this is done by the global file store) but only computes paths and things like this. """ grok.implements(IFileStoreHandler) grok.name(CUSTOMERDOCUMENT_FILE_STORE_NAME) def pathFromFileID(self, store, root, file_id): """All customer document files are put in directory ``customers``. """ marker, filename, basename, ext = store.extractMarker(file_id) sub_root = os.path.join(root, 'customers') return super(CustomerDocumentFileStoreHandler, self).pathFromFileID( store, sub_root, basename) def createFile(self, store, root, filename, file_id, file): """Create a browsable file-like object. """ # call super method to ensure that any old files with # different filename extension are deleted. file, path, file_obj = super( CustomerDocumentFileStoreHandler, self).createFile( store, root, filename, file_id, file) return file, path, IkobaImageFile( file_obj.filename, file_obj.data)