"""Components to handle access codes. """ import csv import grok import os from BTrees.OIBTree import OIBTree from datetime import datetime from random import SystemRandom as random from waeup.sirp.interfaces import IWAeUPSIRPPluggable from waeup.sirp.accesscodes.interfaces import ( IAccessCode, IAccessCodeBatch, IAccessCodeBatchContainer ) class ManageACBatches(grok.Permission): grok.name('waeup.manageACBatches') class AccessCode(grok.Context): grok.implements(IAccessCode) def __init__(self, batch_serial, random_num, invalidation_date=None, student_id=None): self.batch_serial = batch_serial self.random_num = random_num self._invalidation_date = invalidation_date self.student_id = student_id @property def representation(self): return '%s-%s-%s' % ( self.batch_prefix, self.batch_num, self.random_num) @property def batch(self): return getattr(self, '__parent__', None) @property def batch_prefix(self): if self.batch is None: return '' return self.batch.prefix @property def batch_num(self): if self.batch is None: return '' return self.batch.num @property def cost(self): if self.batch is None: return None return self.batch.cost @property def invalidation_date(self): # We define this as a property to make it unwritable. # This attribute should be set by the surrounding batch only. return self._invalidation_date class AccessCodeBatch(grok.Model): """A batch of access codes. """ grok.implements(IAccessCodeBatch) def __init__(self, creation_date, creator, batch_prefix, cost, entry_num, num=None): super(AccessCodeBatch, self).__init__() self.creation_date = creation_date self.creator = creator self.prefix = batch_prefix.upper() self.cost = cost self.entry_num = entry_num self.num = num self.invalidated_num = 0 self._entries = list() self._acids = OIBTree() self._createEntries() def _createEntries(self): """Create the entries for this batch. """ for num, pin in enumerate(self._getNewRandomNum(num=self.entry_num)): self.addAccessCode(num, pin) self._p_changed = True # XXX: most probably not needed. return def _getNewRandomNum(self, num=1): """Create a set of ``num`` random numbers of 10 digits each. The number is returned as string. """ curr = 1 while curr <= num: pin = '' for x in range(10): pin += str(random().randint(0, 9)) if not '%s-%s-%s' % (self.prefix, self.num, pin) in self._acids: curr += 1 yield pin # PIN already in use def _getStoragePath(self): """Get the directory, where we store all batch-related CSV files. """ site = grok.getSite() storagepath = site['datacenter'].storage ac_storage = os.path.join(storagepath, 'accesscodes') if not os.path.exists(ac_storage): os.mkdir(ac_storage) return ac_storage def entries(self): """Get all entries of this batch as generator. """ for x in self._entries: yield x def getAccessCode(self, ac_id): """Get the AccessCode with ID ``ac_id`` or ``KeyError``. """ return self._entries[self._acids[ac_id]] def addAccessCode(self, num, pin): """Add an access-code. """ ac = AccessCode(num, pin) ac.__parent__ = self self._entries.append(ac) self._acids.update({ac.representation: num}) return def invalidate(self, ac_id, student_id=None): """Invalidate the AC with ID ``ac_id``. """ num = self._acids[ac_id] ac = self.getAccessCode(ac_id) ac._invalidation_date = datetime.now() ac.student_id = student_id self.invalidated_num += 1 def createCSVLogFile(self): """Create a CSV file with data in batch. Data will not contain invalidation date nor student ids. File will be created in ``accesscodes`` subdir of data center storage path. Returns name of created file. """ date = self.creation_date.strftime('%Y_%m_%d_%H_%M_%S') ac_storage = self._getStoragePath() csv_path = os.path.join( ac_storage, '%s-%s-%s-%s.csv' % ( self.prefix, self.num, date, self.creator) ) writer = csv.writer(open(csv_path, 'w'), quoting=csv.QUOTE_ALL) writer.writerow(['serial', 'ac', 'cost']) writer.writerow([self.prefix, str(self.num), "%0.2f" % self.cost]) for value in self._entries: writer.writerow( [str(value.batch_serial), str(value.representation)] ) site = grok.getSite() logger = site.logger logger.info( "Created batch %s-%s" % (self.prefix, self.num)) logger.info( "Written batch CSV to %s" % csv_path) return os.path.basename(csv_path) def archive(self): """Create a CSV file for archive. """ ac_storage = self._getStoragePath() now = datetime.now() timestamp = now.strftime('%Y_%m_%d_%H_%M_%S') csv_path = os.path.join( ac_storage, '%s-%s_archive-%s-%s.csv' % ( self.prefix, self.num, timestamp, self.creator) ) writer = csv.writer(open(csv_path, 'w'), quoting=csv.QUOTE_ALL) writer.writerow(['prefix', 'serial', 'ac', 'student', 'date']) writer.writerow([self.prefix, '%0.2f' % self.cost, str(self.num), str(self.entry_num)]) for value in self._entries: date = '' if value.invalidation_date is not None: date = value.invalidation_date.strftime( '%Y-%m-%d-%H-%M-%S') writer.writerow([ self.prefix, value.batch_serial, value.representation, value.student_id, date ]) return os.path.basename(csv_path) class AccessCodeBatchContainer(grok.Container): grok.implements(IAccessCodeBatchContainer) def addBatch(self, batch): """Add a batch. """ batch.num = self.getNum(batch.prefix) key = "%s-%s" % (batch.prefix, batch.num) self[key] = batch self._p_changed = True def createBatch(self, creation_date, creator, batch_prefix, cost, entry_num): """Create and add a batch. """ batch_num = self.getNum(batch_prefix) batch = AccessCodeBatch(creation_date, creator, batch_prefix, cost, entry_num, num=batch_num) self.addBatch(batch) return batch def getNum(self, prefix): """Get next unused num for given prefix. """ num = 1 while self.get('%s-%s' % (prefix, num), None) is not None: num += 1 return num class AccessCodePlugin(grok.GlobalUtility): grok.name('accesscodes') grok.implements(IWAeUPSIRPPluggable) def setup(self, site, name, logger): site['accesscodes'] = AccessCodeBatchContainer() logger.info('Installed container for access code batches.') return def update(self, site, name, logger): if not 'accesscodes' in site.keys(): logger.info('Updating site at %s. Installing access codes.' % ( site,)) self.setup(site, name, logger) else: logger.info( 'AccessCodePlugin: Updating site at %s: Nothing to do.' % ( site, )) return