[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 | |
---|
[5102] | 10 | class ManageACBatches(grok.Permission): |
---|
| 11 | grok.name('waeup.manageACBatches') |
---|
| 12 | |
---|
[5068] | 13 | class AccessCode(grok.Model): |
---|
| 14 | grok.implements(IAccessCode) |
---|
| 15 | |
---|
[5079] | 16 | def __init__(self, batch_serial, random_num, cost, |
---|
| 17 | invalidation_date=None, student_id=None): |
---|
[5068] | 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 | |
---|
[5079] | 29 | @property |
---|
| 30 | def batch(self): |
---|
| 31 | return getattr(self, '__parent__', None) |
---|
[5086] | 32 | |
---|
[5079] | 33 | @property |
---|
| 34 | def batch_prefix(self): |
---|
| 35 | if self.batch is None: |
---|
| 36 | return '' |
---|
| 37 | return self.batch.prefix |
---|
[5086] | 38 | |
---|
[5079] | 39 | @property |
---|
| 40 | def batch_num(self): |
---|
| 41 | if self.batch is None: |
---|
| 42 | return '' |
---|
| 43 | return self.batch.num |
---|
[5068] | 44 | |
---|
[5079] | 45 | class AccessCodeBatch(grok.Container): |
---|
[5068] | 46 | """A batch of access codes. |
---|
| 47 | """ |
---|
| 48 | grok.implements(IAccessCodeBatch) |
---|
| 49 | |
---|
[5086] | 50 | def __init__(self, creation_date, creator, batch_prefix, cost, |
---|
| 51 | entry_num, num=None): |
---|
[5079] | 52 | super(AccessCodeBatch, self).__init__() |
---|
[5068] | 53 | self.creation_date = creation_date |
---|
| 54 | self.creator = creator |
---|
[5079] | 55 | self.prefix = batch_prefix |
---|
[5068] | 56 | self.cost = cost |
---|
| 57 | self.entry_num = entry_num |
---|
[5086] | 58 | self.num = num |
---|
[5079] | 59 | |
---|
[5086] | 60 | def createEntries(self): |
---|
[5079] | 61 | """Create the entries for this batch. |
---|
| 62 | """ |
---|
[5068] | 63 | self._used = [] |
---|
[5079] | 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 |
---|
[5086] | 69 | |
---|
[5068] | 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)) |
---|
[5095] | 80 | self._used.append(result) |
---|
[5068] | 81 | return result |
---|
[5073] | 82 | |
---|
[5079] | 83 | class AccessCodeBatchContainer(grok.Container): |
---|
| 84 | grok.implements(IAccessCodeBatchContainer) |
---|
[5073] | 85 | |
---|
[5079] | 86 | def addBatch(self, batch): |
---|
[5086] | 87 | """Add a batch. |
---|
| 88 | """ |
---|
| 89 | batch.num = self.getNum(batch.prefix) |
---|
| 90 | key = "%s-%s" % (batch.prefix, batch.num) |
---|
[5079] | 91 | self[key] = batch |
---|
[5086] | 92 | batch.createEntries() |
---|
| 93 | self._p_changed = True |
---|
[5079] | 94 | |
---|
[5086] | 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 |
---|
[5095] | 103 | |
---|
[5073] | 104 | class AccessCodePlugin(grok.GlobalUtility): |
---|
| 105 | grok.name('accesscodes') |
---|
| 106 | grok.implements(IWAeUPSIRPPluggable) |
---|
| 107 | |
---|
| 108 | def setup(self, site, name, logger): |
---|
[5079] | 109 | site['accesscodes'] = AccessCodeBatchContainer() |
---|
| 110 | logger.info('Installed container for access code batches.') |
---|
| 111 | return |
---|
[5073] | 112 | |
---|
| 113 | def update(self, site, name, logger): |
---|
| 114 | pass |
---|