Changeset 5079


Ignore:
Timestamp:
24 Mar 2010, 12:57:53 (15 years ago)
Author:
uli
Message:
  • Let component implementations apply to new interfaces.
  • Make access codes retrive their batch-specific values from parent in order not to waste memory.
File:
1 edited

Legend:

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

    r5073 r5079  
    44from random import SystemRandom as random
    55from waeup.sirp.interfaces import IWAeUPSIRPPluggable
    6 from waeup.sirp.accesscodes.interfaces import IAccessCode, IAccessCodeBatch
     6from waeup.sirp.accesscodes.interfaces import (
     7    IAccessCode, IAccessCodeBatch, IAccessCodeBatchContainer
     8    )
    79
    810class AccessCode(grok.Model):
    911    grok.implements(IAccessCode)
    1012
    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):
    1315        self.batch_serial = batch_serial
    14         self.batch_prefix = batch_prefix.upper()
    15         self.batch_num = str(batch_num)
    1616        self.random_num = random_num
    1717        self.cost = cost
     
    2424            self.batch_prefix, self.batch_num, self.random_num)
    2525
     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
    2641
    27 class AccessCodeBatch(object):
     42class AccessCodeBatch(grok.Container):
    2843    """A batch of access codes.
    2944    """
     
    3247    def __init__(self, creation_date, creator, batch_num, batch_prefix, cost,
    3348                 entry_num):
     49        super(AccessCodeBatch, self).__init__()
    3450        self.creation_date = creation_date
    3551        self.creator = creator
    36         self.batch_num = batch_num
    37         self.batch_prefix = batch_prefix
     52        self.num = batch_num
     53        self.prefix = batch_prefix
    3854        self.cost = cost
    3955        self.entry_num = entry_num
     56        self._createEntries()
     57
     58    def _createEntries(self):
     59        """Create the entries for this batch.
     60        """
    4061        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
    5667       
    5768    def getNewRandomNum(self):
     
    6778        return result
    6879
    69 class AccessCodeContainer(grok.Container):
    70     pass
     80class 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
    7186
    7287class AccessCodePlugin(grok.GlobalUtility):
     
    7590
    7691    def setup(self, site, name, logger):
    77         pass
     92        site['accesscodes'] = AccessCodeBatchContainer()
     93        logger.info('Installed container for access code batches.')
     94        return
    7895
    7996    def update(self, site, name, logger):
Note: See TracChangeset for help on using the changeset viewer.