## $Id: documents.py 12214 2014-12-13 15:46:41Z 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 hashlib import md5
from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
from zope.component import queryUtility, getUtility
from zope.component.interfaces import IFactory
from zope.interface import implementedBy
from zope.event import notify

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()

    local_roles = []

    # Ikoba can store any number of files per Document object.
    # However, we highly recommend to associate and store
    # only one file per Document object. Thus the following 
    # tuple should contain only a single filename string.
    filenames = ()

    @property
    def state(self):
        state = IWorkflowState(self).getState()
        return state

    @property
    def translated_state(self):
        try:
            TRANSLATED_STATES = getUtility(
                ICustomersUtils).TRANSLATED_DOCUMENT_STATES
            return TRANSLATED_STATES[self.state]
        except KeyError:
            return
    @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_by_customer(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 is_editable_by_manager(self):
        try:
            # Document must be in state created
            cond = self.state in getUtility(
                ICustomersUtils).DOCMANAGE_DOCUMENT_STATES
            if not cond:
                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

    @property
    def connected_files(self):
        store = getUtility(IExtFileStore)
        files = []
        try:
            # Usually there is only a single element in self.filenames.
            for filename in self.filenames:
                attrname = filename.replace('.','_')
                file = store.getFileByContext(self, attr=filename)
                if file:
                    files.append((attrname, file))
        except AttributeError:
            # In unit tests we don't have a customer to
            # determine the file path.
            return
        return files

    @property
    def is_verifiable(self):
        files = self.connected_files
        if files is not None and len(files) != len(self.filenames):
            return False, _("No file uploaded.")
        return True, None

    def setMD5(self):
        """Set md5 checksum attribute for files connected to this document.
        """
        connected_files = self.connected_files
        if connected_files:
            for file in self.connected_files:
                attrname = '%s_md5' % file[0]
                checksum = md5(file[1].read()).hexdigest()
                setattr(self, attrname, checksum)
        return


class CustomerSampleDocument(CustomerDocumentBase):
    """This is a sample customer document.
    """

    # Ikoba can store any number of files per Document object.
    # However, we highly recommend to associate and store
    # only one file per Document object. Thus the following
    # tuple should contain only a single filename string.
    filenames = ('sample',)

    form_fields_interface = ICustomerDocument

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)

    # Ikoba can store any number of files per Document object.
    # However, we highly recommend to associate and store
    # only one file per Document object. Thus the following
    # tuple should contain only a single filename string.
    filenames = ('sample.pdf',)

    form_fields_interface = ICustomerPDFDocument

CustomerPDFDocument = attrs_to_fields(CustomerPDFDocument)


# Customer documents must be importable. So we 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/345/c999/nice_image_d123_c999.jpeg'``

        meaning that the nice image of this customer document would be
        stored in the site-wide file storage in path:

          ``customers/345/c999/nice_image_d123_c999.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)


@grok.subscribe(ICustomerDocument, grok.IObjectRemovedEvent)
def handle_document_removed(document, event):
    """If a document is deleted, we make sure that also referrers to
    customer contract objects are removed.
    """
    docid = document.document_id

    # Find all customer contracts that refer to given document...
    try:
        contracts = document.customer['contracts'].values()
    except AttributeError:
        # customer not available. This might happen during tests.
        return
    for contract in contracts:
        # Remove that referrer...
        for key, value in contract.__dict__.items():
            if key.endswith('_object') and \
                getattr(value, 'document_id', None) == docid:
                setattr(contract, key, None)
                notify(grok.ObjectModifiedEvent(contract))
                contract.customer.__parent__.logger.info(
                    'ObjectRemovedEvent - %s - %s - removed: %s' % (
                        contract.customer.customer_id,
                        contract.contract_id,
                        document.document_id))
    return

