source: main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/accesscodes.py @ 5085

Last change on this file since 5085 was 5079, checked in by uli, 15 years ago
  • Let component implementations apply to new interfaces.
  • Make access codes retrive their batch-specific values from parent in order not to waste memory.
File size: 2.8 KB
Line 
1"""Components to handle access codes.
2"""
3import grok
4from random import SystemRandom as random
5from waeup.sirp.interfaces import IWAeUPSIRPPluggable
6from waeup.sirp.accesscodes.interfaces import (
7    IAccessCode, IAccessCodeBatch, IAccessCodeBatchContainer
8    )
9
10class AccessCode(grok.Model):
11    grok.implements(IAccessCode)
12
13    def __init__(self, batch_serial, random_num, cost,
14                 invalidation_date=None, student_id=None):
15        self.batch_serial = batch_serial
16        self.random_num = random_num
17        self.cost = cost
18        self.invalidation_date = invalidation_date
19        self.student_id = student_id
20
21    @property
22    def representation(self):
23        return '%s-%s-%s' % (
24            self.batch_prefix, self.batch_num, self.random_num)
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
41
42class AccessCodeBatch(grok.Container):
43    """A batch of access codes.
44    """
45    grok.implements(IAccessCodeBatch)
46
47    def __init__(self, creation_date, creator, batch_num, batch_prefix, cost,
48                 entry_num):
49        super(AccessCodeBatch, self).__init__()
50        self.creation_date = creation_date
51        self.creator = creator
52        self.num = batch_num
53        self.prefix = batch_prefix
54        self.cost = cost
55        self.entry_num = entry_num
56        self._createEntries()
57
58    def _createEntries(self):
59        """Create the entries for this batch.
60        """
61        self._used = []
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
67       
68    def getNewRandomNum(self):
69        """Create a random number of 10 digits.
70
71        The number is returned as string.
72        """
73        result = ''
74        while result == '' or result in self._used:
75            result = ''
76            for x in range(10):
77                result += str(random().randint(0, 9))
78        return result
79
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
86
87class AccessCodePlugin(grok.GlobalUtility):
88    grok.name('accesscodes')
89    grok.implements(IWAeUPSIRPPluggable)
90
91    def setup(self, site, name, logger):
92        site['accesscodes'] = AccessCodeBatchContainer()
93        logger.info('Installed container for access code batches.')
94        return
95
96    def update(self, site, name, logger):
97        pass
Note: See TracBrowser for help on using the repository browser.