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

Last change on this file since 5106 was 5102, checked in by uli, 15 years ago

Add special permission for handling AC batches.

File size: 3.2 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 ManageACBatches(grok.Permission):
11    grok.name('waeup.manageACBatches')
12
13class AccessCode(grok.Model):
14    grok.implements(IAccessCode)
15
16    def __init__(self, batch_serial, random_num, cost,
17                 invalidation_date=None, student_id=None):
18        self.batch_serial = batch_serial
19        self.random_num = random_num
20        self.cost = cost
21        self.invalidation_date = invalidation_date
22        self.student_id = student_id
23
24    @property
25    def representation(self):
26        return '%s-%s-%s' % (
27            self.batch_prefix, self.batch_num, self.random_num)
28
29    @property
30    def batch(self):
31        return getattr(self, '__parent__', None)
32
33    @property
34    def batch_prefix(self):
35        if self.batch is None:
36            return ''
37        return self.batch.prefix
38
39    @property
40    def batch_num(self):
41        if self.batch is None:
42            return ''
43        return self.batch.num
44
45class AccessCodeBatch(grok.Container):
46    """A batch of access codes.
47    """
48    grok.implements(IAccessCodeBatch)
49
50    def __init__(self, creation_date, creator, batch_prefix, cost,
51                 entry_num, num=None):
52        super(AccessCodeBatch, self).__init__()
53        self.creation_date = creation_date
54        self.creator = creator
55        self.prefix = batch_prefix
56        self.cost = cost
57        self.entry_num = entry_num
58        self.num = num
59
60    def createEntries(self):
61        """Create the entries for this batch.
62        """
63        self._used = []
64        for num in range(self.entry_num):
65            random_num = self.getNewRandomNum()
66            ac = AccessCode(num, random_num, self.cost)
67            self[str(num)] = ac
68        del self._used
69
70    def getNewRandomNum(self):
71        """Create a random number of 10 digits.
72
73        The number is returned as string.
74        """
75        result = ''
76        while result == '' or result in self._used:
77            result = ''
78            for x in range(10):
79                result += str(random().randint(0, 9))
80        self._used.append(result)
81        return result
82
83class AccessCodeBatchContainer(grok.Container):
84    grok.implements(IAccessCodeBatchContainer)
85
86    def addBatch(self, batch):
87        """Add a batch.
88        """
89        batch.num = self.getNum(batch.prefix)
90        key = "%s-%s" % (batch.prefix, batch.num)
91        self[key] = batch
92        batch.createEntries()
93        self._p_changed = True
94
95    def getNum(self, prefix):
96        """Get next unused num for given prefix.
97        """
98        num = 1
99        prefix = prefix.upper()
100        while self.get('%s-%s' % (prefix, num), None):
101            num += 1
102        return num
103
104class AccessCodePlugin(grok.GlobalUtility):
105    grok.name('accesscodes')
106    grok.implements(IWAeUPSIRPPluggable)
107
108    def setup(self, site, name, logger):
109        site['accesscodes'] = AccessCodeBatchContainer()
110        logger.info('Installed container for access code batches.')
111        return
112
113    def update(self, site, name, logger):
114        pass
Note: See TracBrowser for help on using the repository browser.