#-*- mode: python; mode: fold -*- # $Id: ScratchCards.py 805 2006-11-09 09:38:29Z joachim $ from Globals import InitializeClass from AccessControl import ClassSecurityInfo from AccessControl.SecurityManagement import newSecurityManager from zExceptions import BadRequest from Products.CMFCore.utils import UniqueObject, getToolByName from Products.CMFCore.permissions import View from Products.CMFCore.permissions import ModifyPortalContent from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder #from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument from Products.CPSDocument.CPSDocument import CPSDocument import DateTime import Globals p_home = Globals.package_home(globals()) i_home = Globals.INSTANCE_HOME class ScratchCardBatchesFolder(CPSDocument): ###( """ WAeUP Student container for the various student data """ meta_type = 'ScratchCardBatchesFolder' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") def Title(self): """compose title""" return "Pin Batches" InitializeClass(ScratchCardBatchesFolder) def addScratchCardBatchesFolder(container, id, REQUEST=None, **kw): """Add a Students personal data.""" ob = ScratchCardBatchesFolder(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) from Products.WAeUP_SRP.WAeUPTables import PinTable class ScratchCardBatch(CPSDocument): ###( """ WAeUP Student container for the various student data """ meta_type = 'ScratchCardBatch' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") def Title(self): """compose title""" doc = self.getContent() return "Pin Batch %s BatchNo %d" % (doc.prefix, doc.batch_no) security.declareProtected(ModifyPortalContent,"makePins") ###( def makePins(self): """generate Pins""" batch = self.getContent() nr = batch.no_of_pins prefix = batch.prefix sold = batch.sold_by cost = batch.cost import random r = random b_no = batch.batch_no #import pdb;pdb.set_trace() generated = [] generated.append('"Serial","Pin","Sold","Cost"' % vars()) generated.append('"%(prefix)s","%(b_no)d","%(sold)s","%(cost)f"' % vars()) pins = self.portal_pins for i in range(1,nr+1): ri = r.randint(1000000000,9999999999) pin = "%s%d%d" % (prefix,b_no,ri) while len(pins.searchResults({'pin': pin})) > 0: ri = r.randint(1000000000,9999999999) pin = "%s%d%d" % (prefix,b_no,ri) pins.addRecord(pin=pin,serial=i,student="",prefix_batch="%s%d" % (prefix, b_no)) generated.append('"%(i)d","%(prefix)s-%(b_no)d-%(ri)d"' % vars()) print '\n'.join(generated) current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") open("%s/import/%s-%s-%s.csv" % (i_home,prefix,b_no,current),"w+").write('\n'.join(generated)) ###) security.declareProtected(ModifyPortalContent,"getUnusedPins") def getUnusedPins(self): """return a list of unused Pins""" max_pins = 10 #import pdb;pdb.set_trace() pins_cat = self.portal_pins doc = self.getContent() unused = [{'pin': p.pin, 'serial': p.serial, } for p in pins_cat.searchResults(prefix_batch = "%s%d" % (doc.prefix,doc.batch_no), ) if not p.student] used = [{'pin': p.pin, 'serial': p.serial, 'student': p.student, } for p in pins_cat.searchResults(prefix_batch = "%s%d" % (doc.prefix,doc.batch_no), ) if p.student] if len (unused) <= max_pins: return unused,used return unused[:max_pins],used InitializeClass(ScratchCardBatch) def addScratchCardBatch(container, id, REQUEST=None, **kw): """Add a Students personal data.""" ob = ScratchCardBatch(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###)