Changeset 5079 for main/waeup.sirp/trunk
- Timestamp:
- 24 Mar 2010, 12:57:53 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/accesscodes.py
r5073 r5079 4 4 from random import SystemRandom as random 5 5 from waeup.sirp.interfaces import IWAeUPSIRPPluggable 6 from waeup.sirp.accesscodes.interfaces import IAccessCode, IAccessCodeBatch 6 from waeup.sirp.accesscodes.interfaces import ( 7 IAccessCode, IAccessCodeBatch, IAccessCodeBatchContainer 8 ) 7 9 8 10 class AccessCode(grok.Model): 9 11 grok.implements(IAccessCode) 10 12 11 def __init__(self, batch_serial, batch_prefix, batch_num, random_num,12 cost,invalidation_date=None, student_id=None):13 def __init__(self, batch_serial, random_num, cost, 14 invalidation_date=None, student_id=None): 13 15 self.batch_serial = batch_serial 14 self.batch_prefix = batch_prefix.upper()15 self.batch_num = str(batch_num)16 16 self.random_num = random_num 17 17 self.cost = cost … … 24 24 self.batch_prefix, self.batch_num, self.random_num) 25 25 26 @property 27 def batch(self): 28 return getattr(self, '__parent__', None) 29 30 @property 31 def batch_prefix(self): 32 if self.batch is None: 33 return '' 34 return self.batch.prefix 35 36 @property 37 def batch_num(self): 38 if self.batch is None: 39 return '' 40 return self.batch.num 26 41 27 class AccessCodeBatch( object):42 class AccessCodeBatch(grok.Container): 28 43 """A batch of access codes. 29 44 """ … … 32 47 def __init__(self, creation_date, creator, batch_num, batch_prefix, cost, 33 48 entry_num): 49 super(AccessCodeBatch, self).__init__() 34 50 self.creation_date = creation_date 35 51 self.creator = creator 36 self. batch_num = batch_num37 self. batch_prefix = batch_prefix52 self.num = batch_num 53 self.prefix = batch_prefix 38 54 self.cost = cost 39 55 self.entry_num = entry_num 56 self._createEntries() 57 58 def _createEntries(self): 59 """Create the entries for this batch. 60 """ 40 61 self._used = [] 41 42 def __iter__(self): 43 if len(self._used) >= self.entry_num: 44 for num in range(self.entry_num): 45 random_num = self._used[num] 46 ac = AccessCode(len(self._used), self.batch_prefix, 47 self.batch_num, random_num, self.cost) 48 yield ac 49 else: 50 while len(self._used) < self.entry_num: 51 random_num = self.getNewRandomNum() 52 ac = AccessCode(len(self._used), self.batch_prefix, 53 self.batch_num, random_num, self.cost) 54 self._used.append(random_num) 55 yield ac 62 for num in range(self.entry_num): 63 random_num = self.getNewRandomNum() 64 ac = AccessCode(num, random_num, self.cost) 65 self[str(num)] = ac 66 del self._used 56 67 57 68 def getNewRandomNum(self): … … 67 78 return result 68 79 69 class AccessCodeContainer(grok.Container): 70 pass 80 class AccessCodeBatchContainer(grok.Container): 81 grok.implements(IAccessCodeBatchContainer) 82 83 def addBatch(self, batch): 84 key = "%s_%s" % (batch.prefix, batch.num) 85 self[key] = batch 71 86 72 87 class AccessCodePlugin(grok.GlobalUtility): … … 75 90 76 91 def setup(self, site, name, logger): 77 pass 92 site['accesscodes'] = AccessCodeBatchContainer() 93 logger.info('Installed container for access code batches.') 94 return 78 95 79 96 def update(self, site, name, logger):
Note: See TracChangeset for help on using the changeset viewer.