#-*- mode: python; mode: fold -*- # $Id: ScratchCards.py 948 2006-11-27 12:54:43Z 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 import logging import os,csv,re import Globals MAX_TRANS = 1000 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" security.declareProtected(ModifyPortalContent,"importPinBatch") ###( def importPinBatch(self): """import PinBatch""" name = "PINS" logger = logging.getLogger('%s_import' % name) logger.info('Start loading from %s.csv' % name) pins_section = self.portal_url.getPortalObject().campus.pins p_pins = self.portal_pins base_dir = "%s/import/pins" % (i_home) files = os.listdir(base_dir) for fn in files: pins = csv.DictReader(open("%s/%s" % (base_dir,fn),"rb")) n = 0 for pin in pins: if n == 0: bid = "%(Serial)s_%(Pin)s" % pin pins_section.invokeFactory('ScratchCardBatch', bid) batch = getattr(pins_section,bid) dict = {} prefix = pin['Serial'] dict['prefix'] = pin['Serial'] batch_no = pin['Pin'] dict['batch_no'] = int(batch_no) dict['cost'] = float(pin['Cost']) dict['sold_by'] = pin['Sold'] #import pdb;pdb.set_trace() n += 1 continue p_pins.addRecord(pin=pin['Pin'],serial=n,student="",prefix_batch="%s%s" % (prefix, batch_no)) n += 1 dict['no_of_pins'] = n - 1 batch.getContent().edit(mapping=dict) logger.info('Added %s with %d Pins' % (bid,n-1)) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) 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) ###)