[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) |
---|
| 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 |
---|
[5068] | 41 | |
---|
[5079] | 42 | class AccessCodeBatch(grok.Container): |
---|
[5068] | 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): |
---|
[5079] | 49 | super(AccessCodeBatch, self).__init__() |
---|
[5068] | 50 | self.creation_date = creation_date |
---|
| 51 | self.creator = creator |
---|
[5079] | 52 | self.num = batch_num |
---|
| 53 | self.prefix = batch_prefix |
---|
[5068] | 54 | self.cost = cost |
---|
| 55 | self.entry_num = entry_num |
---|
[5079] | 56 | self._createEntries() |
---|
| 57 | |
---|
| 58 | def _createEntries(self): |
---|
| 59 | """Create the entries for this batch. |
---|
| 60 | """ |
---|
[5068] | 61 | self._used = [] |
---|
[5079] | 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 |
---|
[5068] | 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 |
---|
[5073] | 79 | |
---|
[5079] | 80 | class AccessCodeBatchContainer(grok.Container): |
---|
| 81 | grok.implements(IAccessCodeBatchContainer) |
---|
[5073] | 82 | |
---|
[5079] | 83 | def addBatch(self, batch): |
---|
| 84 | key = "%s_%s" % (batch.prefix, batch.num) |
---|
| 85 | self[key] = batch |
---|
| 86 | |
---|
[5073] | 87 | class AccessCodePlugin(grok.GlobalUtility): |
---|
| 88 | grok.name('accesscodes') |
---|
| 89 | grok.implements(IWAeUPSIRPPluggable) |
---|
| 90 | |
---|
| 91 | def setup(self, site, name, logger): |
---|
[5079] | 92 | site['accesscodes'] = AccessCodeBatchContainer() |
---|
| 93 | logger.info('Installed container for access code batches.') |
---|
| 94 | return |
---|
[5073] | 95 | |
---|
| 96 | def update(self, site, name, logger): |
---|
| 97 | pass |
---|