Ignore:
Timestamp:
7 Dec 2014, 10:07:29 (10 years ago)
Author:
Henrik Bettermann
Message:

Define connected_files and getMD5 for all kind of customer documents.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/documents.py

    r12128 r12161  
    2121import os
    2222import grok
     23from hashlib import md5
    2324from zope.component import queryUtility, getUtility
    2425from zope.component.interfaces import IFactory
     
    6667    grok.baseclass()
    6768
     69    filenames = ()
     70
    6871    @property
    6972    def customer(self):
     
    105108            return
    106109
     110    @property
     111    def connected_files(self):
     112        store = getUtility(IExtFileStore)
     113        files = []
     114        try:
     115            for filename in self.filenames:
     116                attrname = filename.replace('.','_')
     117                file = store.getFileByContext(self, attr=filename)
     118                files.append((attrname, file))
     119        except AttributeError:
     120            # In unit tests we don't have a customer to
     121            # determine the file path.
     122            return
     123        return files
     124
     125    def setMD5(self):
     126        """Set md5 checksum attribute for all files connected to this document.
     127        """
     128        for file in self.connected_files:
     129            attrname = '%s_md5' % file[0]
     130            checksum = md5(file[1].read()).hexdigest()
     131            setattr(self, attrname, checksum)
     132        return
     133
    107134
    108135class CustomerSampleDocument(CustomerDocumentBase):
    109136    """This is a sample customer document.
    110137    """
     138
     139    filenames = ('sample',)
    111140
    112141CustomerSampleDocument = attrs_to_fields(CustomerSampleDocument)
Note: See TracChangeset for help on using the changeset viewer.