Ignore:
Timestamp:
31 Mar 2010, 23:21:04 (15 years ago)
Author:
uli
Message:

Add CSV file creator for AC batches.

File:
1 edited

Legend:

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

    r5107 r5110  
    11"""Components to handle access codes.
    22"""
     3import csv
    34import grok
     5import os
    46from random import SystemRandom as random
    57from waeup.sirp.interfaces import IWAeUPSIRPPluggable
     
    8183        return result
    8284
     85    def createCSVLogFile(self):
     86        """Create a CSV file with data in batch.
     87
     88        Data will not contain invalidation date nor student ids.  File
     89        will be created in ``accesscodes`` subdir of data center
     90        storage path.
     91
     92        Returns name of created file.
     93        """
     94        site = grok.getSite()
     95        storagepath = site['datacenter'].storage
     96        ac_storage = os.path.join(storagepath, 'accesscodes')
     97        if not os.path.exists(ac_storage):
     98            os.mkdir(ac_storage)
     99        date = self.creation_date.strftime('%Y_%m_%d_%H_%M_%S')
     100        csv_path = os.path.join(
     101            ac_storage, '%s-%s-%s-%s.csv' % (
     102                self.prefix, self.num, date, self.creator)
     103            )
     104        writer = csv.writer(open(csv_path, 'w'), quoting=csv.QUOTE_ALL)
     105        writer.writerow(['serial', 'ac', 'cost'])
     106        writer.writerow([self.prefix, str(self.num), "%0.2f" % self.cost])
     107        for key, value in self.items():
     108            writer.writerow(
     109                [str(value.batch_serial), str(value.representation)]
     110                )
     111        return os.path.basename(csv_path)
     112
     113   
    83114class AccessCodeBatchContainer(grok.Container):
    84115    grok.implements(IAccessCodeBatchContainer)
Note: See TracChangeset for help on using the changeset viewer.