[489] | 1 | #-*- mode: python; mode: fold -*- |
---|
| 2 | # $Id: Students.py 472 2006-09-02 14:51:14Z henrik $ |
---|
| 3 | from Globals import InitializeClass |
---|
| 4 | from AccessControl import ClassSecurityInfo |
---|
| 5 | from AccessControl.SecurityManagement import newSecurityManager |
---|
| 6 | from zExceptions import BadRequest |
---|
| 7 | from Products.CMFCore.utils import UniqueObject, getToolByName |
---|
| 8 | from Products.CMFCore.permissions import View |
---|
| 9 | from Products.CMFCore.permissions import ModifyPortalContent |
---|
| 10 | from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder |
---|
| 11 | #from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument |
---|
| 12 | from Products.CPSDocument.CPSDocument import CPSDocument |
---|
[502] | 13 | import DateTime |
---|
| 14 | import Globals |
---|
| 15 | p_home = Globals.package_home(globals()) |
---|
| 16 | i_home = Globals.INSTANCE_HOME |
---|
[489] | 17 | |
---|
| 18 | class ScratchCardBatchesFolder(CPSDocument): ###( |
---|
| 19 | """ |
---|
| 20 | WAeUP Student container for the various student data |
---|
| 21 | """ |
---|
| 22 | meta_type = 'ScratchCardBatchesFolder' |
---|
| 23 | portal_type = meta_type |
---|
| 24 | security = ClassSecurityInfo() |
---|
| 25 | |
---|
| 26 | security.declareProtected(View,"Title") |
---|
| 27 | def Title(self): |
---|
| 28 | """compose title""" |
---|
| 29 | return "Pin Batches" |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | InitializeClass(ScratchCardBatchesFolder) |
---|
| 33 | |
---|
| 34 | def addScratchCardBatchesFolder(container, id, REQUEST=None, **kw): |
---|
| 35 | """Add a Students personal data.""" |
---|
| 36 | ob = ScratchCardBatchesFolder(id, **kw) |
---|
| 37 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 38 | |
---|
| 39 | ###) |
---|
| 40 | |
---|
| 41 | from Products.WAeUP_SRP.WAeUPTables import PinTable |
---|
| 42 | |
---|
| 43 | class ScratchCardBatch(CPSDocument): ###( |
---|
| 44 | """ |
---|
| 45 | WAeUP Student container for the various student data |
---|
| 46 | """ |
---|
| 47 | meta_type = 'ScratchCardBatch' |
---|
| 48 | portal_type = meta_type |
---|
| 49 | security = ClassSecurityInfo() |
---|
| 50 | |
---|
| 51 | security.declareProtected(View,"Title") |
---|
| 52 | def Title(self): |
---|
| 53 | """compose title""" |
---|
| 54 | doc = self.getContent() |
---|
| 55 | return "Pin Batch %s BatchNo %d" % (doc.prefix, doc.batch_no) |
---|
| 56 | |
---|
[535] | 57 | security.declareProtected(ModifyPortalContent,"makePins") ###( |
---|
[489] | 58 | def makePins(self): |
---|
| 59 | """generate Pins""" |
---|
| 60 | batch = self.getContent() |
---|
| 61 | nr = batch.no_of_pins |
---|
[502] | 62 | prefix = batch.prefix |
---|
| 63 | sold = batch.sold_by |
---|
| 64 | cost = batch.cost |
---|
[489] | 65 | import random |
---|
| 66 | r = random |
---|
| 67 | b_no = batch.batch_no |
---|
[502] | 68 | #import pdb;pdb.set_trace() |
---|
| 69 | generated = [] |
---|
| 70 | generated.append('"Serial","Pin","Sold","Cost"' % vars()) |
---|
| 71 | generated.append('"%(prefix)s","%(b_no)d","%(sold)s","%(cost)f"' % vars()) |
---|
| 72 | pins = self.portal_pins |
---|
[682] | 73 | for i in range(1,nr+1): |
---|
[502] | 74 | ri = r.randint(1000000000,9999999999) |
---|
| 75 | pin = "%s%d%d" % (prefix,b_no,ri) |
---|
| 76 | while len(pins.searchResults({'pin': pin})) > 0: |
---|
| 77 | ri = r.randint(1000000000,9999999999) |
---|
| 78 | pin = "%s%d%d" % (prefix,b_no,ri) |
---|
[535] | 79 | pins.addRecord(pin=pin,serial=i,student="",prefix_batch="%s%d" % (prefix, b_no)) |
---|
[502] | 80 | generated.append('"%(i)d","%(prefix)s-%(b_no)d-%(ri)d"' % vars()) |
---|
| 81 | print '\n'.join(generated) |
---|
| 82 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
[612] | 83 | open("%s/import/%s-%s-%s.csv" % (i_home,prefix,b_no,current),"w+").write('\n'.join(generated)) |
---|
[535] | 84 | ###) |
---|
[489] | 85 | |
---|
[535] | 86 | security.declareProtected(ModifyPortalContent,"getUnusedPins") |
---|
| 87 | def getUnusedPins(self): |
---|
| 88 | """return a list of unused Pins""" |
---|
[688] | 89 | max_pins = 10 |
---|
[535] | 90 | #import pdb;pdb.set_trace() |
---|
| 91 | pins_cat = self.portal_pins |
---|
| 92 | doc = self.getContent() |
---|
[686] | 93 | unused = [{'pin': p.pin, |
---|
[535] | 94 | 'serial': p.serial, |
---|
[612] | 95 | } for p in pins_cat.searchResults(prefix_batch = "%s%d" |
---|
| 96 | % (doc.prefix,doc.batch_no), |
---|
[686] | 97 | ) if not p.student] |
---|
| 98 | used = [{'pin': p.pin, |
---|
| 99 | 'serial': p.serial, |
---|
| 100 | 'student': p.student, |
---|
| 101 | } for p in pins_cat.searchResults(prefix_batch = "%s%d" |
---|
| 102 | % (doc.prefix,doc.batch_no), |
---|
| 103 | ) if p.student] |
---|
| 104 | if len (unused) <= max_pins: |
---|
| 105 | return unused,used |
---|
| 106 | return unused[:max_pins],used |
---|
[489] | 107 | |
---|
| 108 | InitializeClass(ScratchCardBatch) |
---|
| 109 | |
---|
| 110 | def addScratchCardBatch(container, id, REQUEST=None, **kw): |
---|
| 111 | """Add a Students personal data.""" |
---|
| 112 | ob = ScratchCardBatch(id, **kw) |
---|
| 113 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 114 | |
---|
| 115 | ###) |
---|