Changeset 12161


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.

Location:
main/waeup.ikoba/trunk/src/waeup/ikoba
Files:
5 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)
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/export.py

    r12156 r12161  
    160160                  'translated_state',
    161161                  'formatted_transition_date',
    162                   'translated_class_name'])))
     162                  'translated_class_name',
     163                  'connected_files',   # Could be used to export file URLs
     164                  ])))
    163165
    164166    #: The title under which this exporter will be displayed
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_browser.py

    r12151 r12161  
    4343from waeup.ikoba.customers.customer import Customer
    4444from waeup.ikoba.interfaces import (
    45     IUserAccount, IJobManager, APPROVED, SUBMITTED)
     45    IUserAccount, IJobManager, APPROVED, SUBMITTED,
     46    IFileStoreNameChooser, IExtFileStore, IFileStoreHandler)
     47from waeup.ikoba.imagestorage import (
     48    FileStoreNameChooser, ExtFileStore, DefaultFileStoreHandler,
     49    DefaultStorage)
    4650from waeup.ikoba.authentication import LocalRoleSetEvent
    4751from waeup.ikoba.tests.test_async import FunctionalAsyncTestCase
     
    12121216
    12131217    def test_contract_approval(self):
    1214         # This is not a UI test. It just tests the approval
    1215         # of contracts.
     1218        # This is not a UI test. It just a functional test.
    12161219        self.assertRaises(ConstraintNotSatisfied,
    12171220            self.contract.document_object, self.document)
     
    12271230        self.assertEqual(IWorkflowState(self.contract).getState(), 'approved')
    12281231
    1229 
    12301232    def test_contract_approval_in_UI(self):
    12311233        # Now let's see what the UI says why trying to approve a contract
     
    12461248        self.browser.getControl("Save").click()
    12471249        self.assertEqual(IWorkflowState(self.contract).getState(), 'approved')
     1250
     1251    def test_get_setmd5_file(self):
     1252        # A proper file name chooser is registered for customer documents.
     1253        # This is not a UI test. It just a functional test.
     1254        file_id = IFileStoreNameChooser(self.document).chooseName(attr='sample')
     1255        fs = ExtFileStore(root=self.dc_root)
     1256        fs.createFile(file_id, StringIO('my sample 1'))
     1257        result = fs.getFileByContext(self.document, attr='sample')
     1258        self.assertEqual(file_id, '__file-customerdocument__01000/K1000000/sample_d101_K1000000')
     1259        self.assertEqual(result.read(), 'my sample 1')
     1260        self.assertEqual(self.document.connected_files[0][1].read(), 'my sample 1')
     1261        self.document.setMD5()
     1262        self.assertEqual(self.document.sample_md5, 'a406995ee8eb6772bacf51aa4b0caa24')
     1263        return
     1264
  • main/waeup.ikoba/trunk/src/waeup/ikoba/documents/document.py

    r12160 r12161  
    8787        return
    8888
    89     def setMD5(self, files):
    90         """Set md5 checksum of selected files connected to this document.
     89    def setMD5(self):
     90        """Determine md5 checksum of all files and store checksums as
     91        document attributes.
    9192        """
    9293        return
    9394       
     95Document = attrs_to_fields(Document)
    9496
    95 Document = attrs_to_fields(Document)
    9697
    9798class DocumentFactory(grok.GlobalUtility):
  • main/waeup.ikoba/trunk/src/waeup/ikoba/documents/interfaces.py

    r12160 r12161  
    5252    formatted_transition_date = Attribute('Last transition formatted date string')
    5353    user_id = Attribute('Id of a user')
    54     connected_files = Attribute('Get files connected to a document')
     54    connected_files = Attribute('Names of files connected to a document')
    5555
    5656    title = schema.TextLine(
     
    6565        )
    6666
    67     def setMD5(files):
    68         """Set md5 checksum of selected files connected to this document.
     67    def setMD5():
     68        """Determine md5 checksum of selected files and store checksums as
     69        document attributes.
    6970        """
    7071
Note: See TracChangeset for help on using the changeset viewer.