Ignore:
Timestamp:
5 Apr 2010, 18:16:56 (15 years ago)
Author:
uli
Message:

Support reimport of AC batches.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/accesscodes.py

    r5127 r5132  
    198198    grok.implements(IAccessCodeBatchContainer)
    199199
     200    def _getStoragePath(self):
     201        """Get the directory, where batch import files are stored.
     202        """
     203        site = grok.getSite()
     204        storagepath = site['datacenter'].storage
     205        ac_storage = os.path.join(storagepath, 'accesscodes')
     206        import_path = os.path.join(ac_storage, 'imports')
     207        if not os.path.exists(import_path):
     208            os.mkdir(import_path)
     209        return import_path
     210
    200211    def addBatch(self, batch):
    201212        """Add a batch.
     
    224235        return num
    225236
     237    def getImportFiles(self):
     238        """Return a generator with basenames of available import files.
     239        """
     240        path = self._getStoragePath()
     241        for filename in sorted(os.listdir(path)):
     242            yield filename
     243   
     244    def reimport(self, filename, creator=u'UNKNOWN'):
     245        """Reimport a batch given in CSV file.
     246
     247        CSV file must be of format as generated by createCSVLogFile
     248        method.
     249        """
     250        path = os.path.join(self._getStoragePath(), filename)
     251        reader = csv.DictReader(open(path, 'rb'), quoting=csv.QUOTE_ALL)
     252        entry = reader.next()
     253        cost = float(entry['cost'])
     254        num = int(entry['ac'])
     255        batch_name = '%s-%s' % (entry['serial'], num)
     256        if batch_name in self.keys():
     257            raise KeyError('Batch already exists: %s' % batch_name)
     258        batch = AccessCodeBatch(
     259            datetime.now(), creator, entry['serial'], cost, 0, num=num)
     260        num_entries = 0
     261        for row in reader:
     262            pin = row['ac']
     263            serial = int(row['serial'])
     264            rand_num = pin.rsplit('-', 1)[-1]
     265            batch.addAccessCode(serial, rand_num)
     266            num_entries += 1
     267        batch.entry_num = num_entries
     268        self[batch_name] = batch
     269        batch.createCSVLogFile()
     270        return
     271   
    226272class AccessCodePlugin(grok.GlobalUtility):
    227273    grok.name('accesscodes')
Note: See TracChangeset for help on using the changeset viewer.