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