Ignore:
Timestamp:
20 Nov 2014, 05:40:52 (10 years ago)
Author:
Henrik Bettermann
Message:

Implement document_id generator. Ensure that document_ids remain unique during import.

Location:
main/waeup.ikoba/trunk/src/waeup/ikoba
Files:
1 added
9 edited

Legend:

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

    r11956 r12005  
    3939    grok.implements(ICompany)
    4040
     41    _curr_doc_id = 101
     42
    4143    # Setup authentication for this app. Note: this is only
    4244    # initialized, when a new instance of this app is created.
     
    4951        self.setup()
    5052        return
     53
     54    @property
     55    def unique_document_id(self):
     56        """A unique document id for all documents in company.
     57
     58        The document id returned is guaranteed to be unique. It
     59        consists of some prefix (normally a single letter) followed by
     60        a number with at least 7 digits.
     61
     62        Once a document id was issued, it won't be issued again.
     63
     64        Obtaining a document id is currently not thread-safe but can be
     65        made easily by enabling commented lines.
     66        """
     67        # lock.acquire() # lock data
     68        new_id = u'd%s' % (self._curr_doc_id)
     69        self._curr_doc_id += 1
     70        # self._p_changed = True
     71        # commit()
     72        # lock.release() # end of lock
     73        return new_id
    5174
    5275    def setup(self):
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/batching.py

    r12004 r12005  
    4040from waeup.ikoba.interfaces import IIkobaUtils
    4141from waeup.ikoba.interfaces import MessageFactory as _
     42from waeup.ikoba.documents.utils import generate_document_id
    4243from waeup.ikoba.customers.interfaces import (
    4344    ICustomer, ICustomerUpdateByRegNo,
     
    440441        document_id = row.get('document_id', None)
    441442        if not document_id:
    442             timestamp = ("%d" % int(time()*10000))[1:]
    443             document_id = "d%s" % timestamp
     443            document_id = generate_document_id()
    444444            conv_dict['document_id'] = document_id
    445445            return errs, inv_errs, conv_dict
    446         if not document_id.startswith('d') or len(document_id) != 14:
     446        # document_id must not exist.
     447        if mode == 'create':
     448            cat = queryUtility(ICatalog, name='documents_catalog')
     449            results = list(
     450                cat.searchResults(document_id=(document_id, document_id)))
     451            if results:
     452                errs.append(('document_id','id exists'))
     453        if not document_id.startswith('d'):
    447454            errs.append(('document_id','invalid format'))
    448455        return errs, inv_errs, conv_dict
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/customer.py

    r11997 r12005  
    1 ## $Id: customer.py 11604 2014-04-29 07:31:54Z henrik $
     1## $Id$
    22##
    33## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/sample_document_data.csv

    r12004 r12005  
    11document_id,reg_number,title
    2 d1266236341953,1,My first doc
    3 d1266236341954,2,My second doc
    4 d1266236341955,3,My third doc
     2d3,1,My first doc
     3d4,2,My second doc
     4d5,3,My third doc
    55,1,My 4th doc
     6d5,2,My stolen doc
  • main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_batching.py

    r12004 r12005  
    328328        num, num_warns, fin_file, fail_file = self.processor.doImport(
    329329            self.csv_file, DOCUMENT_HEADER_FIELDS,'create')
    330         self.assertEqual(num_warns,0)
     330        self.assertEqual(num_warns,1)
     331        # document_id must be unique
     332        fail_file = open(fail_file).read()
     333        self.assertEqual(fail_file,
     334            'reg_number,document_id,title,--ERRORS--\r\n'
     335            '2,d5,My stolen doc,document_id: id exists\r\n')
    331336        document = self.processor.getEntry(dict(reg_number='1',
    332             document_id='d1266236341953'), self.app)
     337            document_id='d3'), self.app)
    333338        self.assertEqual(
    334             self.app['customers']['X666666']['documents']['d1266236341953'],
     339            self.app['customers']['X666666']['documents']['d3'],
    335340            document)
    336         self.assertEqual(document.document_id, 'd1266236341953')
     341        self.assertEqual(document.document_id, 'd3')
    337342        document = self.processor.getEntry(dict(reg_number='3',
    338             document_id='d1266236341955'), self.app)
     343            document_id='d5'), self.app)
    339344        shutil.rmtree(os.path.dirname(fin_file))
    340345        logcontent = open(self.logfile).read()
     
    343348            'INFO - system - CustomerDocument Processor - '
    344349            'sample_document_data - X666666 - updated: '
    345             'document_id=d1266236341953, title=My first doc'
     350            'document_id=d3, title=My first doc'
    346351            in logcontent)
    347352
     
    355360        num, num_warns, fin_file, fail_file = self.processor.doImport(
    356361            self.csv_file, DOCUMENT_HEADER_FIELDS,'update')
    357         self.assertEqual(num_warns,1)  # There is one record without document_id
     362        # There is one record without document_id and one duplicate
     363        self.assertEqual(num_warns,2)
    358364        shutil.rmtree(os.path.dirname(fin_file))
    359365
     
    367373        num, num_warns, fin_file, fail_file = self.processor.doImport(
    368374            self.csv_file, DOCUMENT_HEADER_FIELDS,'remove')
    369         self.assertEqual(num_warns,1)  # There is one record without document_id
     375        # There is one record without document_id and one duplicate
     376        self.assertEqual(num_warns,2)
    370377        shutil.rmtree(os.path.dirname(fin_file))
    371378        logcontent = open(self.logfile).read()
    372379        self.assertTrue(
    373             'INFO - system - K1000001 - Document removed: d1266236341955'
     380            'INFO - system - K1000001 - Document removed: d5'
    374381            in logcontent)
  • main/waeup.ikoba/trunk/src/waeup/ikoba/documents/container.py

    r12004 r12005  
    3030    grok.provides(IDocumentsContainer)
    3131
    32     def __init__(self):
    33         super(DocumentsContainer, self).__init__()
    34         return
    3532
    3633    def addDocument(self, document):
  • main/waeup.ikoba/trunk/src/waeup/ikoba/documents/document.py

    r12004 r12005  
    3030from waeup.ikoba.interfaces import IIkobaUtils, IObjectHistory
    3131from waeup.ikoba.interfaces import MessageFactory as _
    32 from waeup.ikoba.documents.interfaces import IDocument, IDocument
    3332from waeup.ikoba.utils.helpers import attrs_to_fields, get_current_principal
    3433from waeup.ikoba.utils.logger import Logger
     34from waeup.ikoba.documents.interfaces import IDocument, IDocument
     35from waeup.ikoba.documents.utils import generate_document_id
    3536
    3637class Document(grok.Container, Logger):
     
    5152        return
    5253
     54    #def __init__(self):
     55    #    super(Document, self).__init__()
     56    #    timestamp = ("%d" % int(time()*10000))[1:]
     57    #    self.document_id = "d%s" % timestamp
     58     #   return
     59
    5360    def __init__(self):
    5461        super(Document, self).__init__()
    55         timestamp = ("%d" % int(time()*10000))[1:]
    56         self.document_id = "d%s" % timestamp
     62        # The site doesn't exist in unit tests
     63        try:
     64            self.document_id = generate_document_id()
     65        except AttributeError:
     66            self.document_id = u'd123'
    5767        return
    5868
  • main/waeup.ikoba/trunk/src/waeup/ikoba/documents/interfaces.py

    r12004 r12005  
    3535    """
    3636
     37    def addDocument(document):
     38        """Add a document.
     39        """
     40
    3741class IDocument(IIkobaObject):
    3842    """A base representation of documents.
  • main/waeup.ikoba/trunk/src/waeup/ikoba/documents/tests.py

    r12004 r12005  
    6161        self.assertEqual(container[document_id], document)
    6262        self.assertRaises(TypeError, container.addDocument, object())
     63        self.assertEqual(document_id, 'd123')
    6364        return
    6465
Note: See TracChangeset for help on using the changeset viewer.