[489] | 1 | #-*- mode: python; mode: fold -*- |
---|
[805] | 2 | # $Id: ScratchCards.py 3077 2008-01-31 22:05:11Z henrik $ |
---|
[489] | 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 |
---|
[947] | 15 | import logging |
---|
| 16 | import os,csv,re |
---|
| 17 | import Globals |
---|
| 18 | MAX_TRANS = 1000 |
---|
[502] | 19 | p_home = Globals.package_home(globals()) |
---|
| 20 | i_home = Globals.INSTANCE_HOME |
---|
[489] | 21 | |
---|
| 22 | class ScratchCardBatchesFolder(CPSDocument): ###( |
---|
| 23 | """ |
---|
| 24 | WAeUP Student container for the various student data |
---|
| 25 | """ |
---|
| 26 | meta_type = 'ScratchCardBatchesFolder' |
---|
| 27 | portal_type = meta_type |
---|
| 28 | security = ClassSecurityInfo() |
---|
| 29 | |
---|
| 30 | security.declareProtected(View,"Title") |
---|
| 31 | def Title(self): |
---|
| 32 | """compose title""" |
---|
| 33 | return "Pin Batches" |
---|
| 34 | |
---|
[947] | 35 | security.declareProtected(ModifyPortalContent,"importPinBatch") ###( |
---|
| 36 | def importPinBatch(self): |
---|
| 37 | """import PinBatch""" |
---|
| 38 | name = "PINS" |
---|
[1571] | 39 | logger = logging.getLogger('Students.ScratchCardBatchesFolder.importPinBatch') |
---|
[947] | 40 | logger.info('Start loading from %s.csv' % name) |
---|
| 41 | pins_section = self.portal_url.getPortalObject().campus.pins |
---|
| 42 | p_pins = self.portal_pins |
---|
| 43 | base_dir = "%s/import/pins" % (i_home) |
---|
| 44 | files = os.listdir(base_dir) |
---|
| 45 | for fn in files: |
---|
[948] | 46 | pins = csv.DictReader(open("%s/%s" % (base_dir,fn),"rb")) |
---|
[947] | 47 | n = 0 |
---|
| 48 | for pin in pins: |
---|
| 49 | if n == 0: |
---|
| 50 | bid = "%(Serial)s_%(Pin)s" % pin |
---|
| 51 | pins_section.invokeFactory('ScratchCardBatch', bid) |
---|
| 52 | batch = getattr(pins_section,bid) |
---|
| 53 | dict = {} |
---|
| 54 | prefix = pin['Serial'] |
---|
[1082] | 55 | dict['prefix'] = pin['Serial'] |
---|
[947] | 56 | batch_no = pin['Pin'] |
---|
| 57 | dict['batch_no'] = int(batch_no) |
---|
| 58 | dict['cost'] = float(pin['Cost']) |
---|
| 59 | dict['sold_by'] = pin['Sold'] |
---|
| 60 | #import pdb;pdb.set_trace() |
---|
| 61 | n += 1 |
---|
| 62 | continue |
---|
[978] | 63 | puid = "".join(pin['Pin'].split('-')) |
---|
| 64 | p_pins.addRecord(pin=puid,serial=n,student="",prefix_batch="%s%s" % (prefix, batch_no)) |
---|
[947] | 65 | n += 1 |
---|
| 66 | dict['no_of_pins'] = n - 1 |
---|
| 67 | batch.getContent().edit(mapping=dict) |
---|
[1571] | 68 | logger.info('%s with %d PINs added' % (bid,n-1)) |
---|
[947] | 69 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
| 70 | ###) |
---|
[489] | 71 | |
---|
| 72 | InitializeClass(ScratchCardBatchesFolder) |
---|
| 73 | |
---|
| 74 | def addScratchCardBatchesFolder(container, id, REQUEST=None, **kw): |
---|
| 75 | """Add a Students personal data.""" |
---|
| 76 | ob = ScratchCardBatchesFolder(id, **kw) |
---|
| 77 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 78 | |
---|
| 79 | ###) |
---|
| 80 | |
---|
| 81 | from Products.WAeUP_SRP.WAeUPTables import PinTable |
---|
| 82 | |
---|
| 83 | class ScratchCardBatch(CPSDocument): ###( |
---|
| 84 | """ |
---|
| 85 | WAeUP Student container for the various student data |
---|
| 86 | """ |
---|
| 87 | meta_type = 'ScratchCardBatch' |
---|
| 88 | portal_type = meta_type |
---|
| 89 | security = ClassSecurityInfo() |
---|
| 90 | |
---|
| 91 | security.declareProtected(View,"Title") |
---|
| 92 | def Title(self): |
---|
| 93 | """compose title""" |
---|
| 94 | doc = self.getContent() |
---|
| 95 | return "Pin Batch %s BatchNo %d" % (doc.prefix, doc.batch_no) |
---|
| 96 | |
---|
[535] | 97 | security.declareProtected(ModifyPortalContent,"makePins") ###( |
---|
[489] | 98 | def makePins(self): |
---|
| 99 | """generate Pins""" |
---|
| 100 | batch = self.getContent() |
---|
| 101 | nr = batch.no_of_pins |
---|
[502] | 102 | prefix = batch.prefix |
---|
| 103 | sold = batch.sold_by |
---|
| 104 | cost = batch.cost |
---|
[489] | 105 | import random |
---|
| 106 | r = random |
---|
| 107 | b_no = batch.batch_no |
---|
[502] | 108 | #import pdb;pdb.set_trace() |
---|
| 109 | generated = [] |
---|
| 110 | generated.append('"Serial","Pin","Sold","Cost"' % vars()) |
---|
| 111 | generated.append('"%(prefix)s","%(b_no)d","%(sold)s","%(cost)f"' % vars()) |
---|
| 112 | pins = self.portal_pins |
---|
[682] | 113 | for i in range(1,nr+1): |
---|
[502] | 114 | ri = r.randint(1000000000,9999999999) |
---|
| 115 | pin = "%s%d%d" % (prefix,b_no,ri) |
---|
| 116 | while len(pins.searchResults({'pin': pin})) > 0: |
---|
| 117 | ri = r.randint(1000000000,9999999999) |
---|
| 118 | pin = "%s%d%d" % (prefix,b_no,ri) |
---|
[2063] | 119 | pins.addRecord(pin=pin, |
---|
| 120 | serial=i, |
---|
| 121 | student="", |
---|
| 122 | cost = cost, |
---|
| 123 | prefix_batch="%s%d" % (prefix, b_no)) |
---|
[502] | 124 | generated.append('"%(i)d","%(prefix)s-%(b_no)d-%(ri)d"' % vars()) |
---|
| 125 | print '\n'.join(generated) |
---|
| 126 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
[2241] | 127 | open("%s/export/%s-%s-%s.csv" % (i_home,prefix,b_no,current),"w+").write('\n'.join(generated)) |
---|
[535] | 128 | ###) |
---|
[489] | 129 | |
---|
[3076] | 130 | security.declareProtected(ModifyPortalContent,"archiveBatch") ###( |
---|
| 131 | def archiveBatch(self,delete=False): |
---|
| 132 | """archive a batch""" |
---|
| 133 | #import pdb;pdb.set_trace() |
---|
| 134 | batch = self.getContent() |
---|
| 135 | member = self.portal_membership.getAuthenticatedMember() |
---|
| 136 | logger = logging.getLogger('ScratchCards.archiveBatch') |
---|
| 137 | nr = batch.no_of_pins |
---|
| 138 | prefix = batch.prefix |
---|
| 139 | sold = batch.sold_by |
---|
| 140 | cost = batch.cost |
---|
| 141 | batch_no = batch.batch_no |
---|
| 142 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
| 143 | export_file = "%s/export/%s-%s_archive_%s.csv" % (i_home,prefix,batch_no,current) |
---|
| 144 | lines = [] |
---|
[3077] | 145 | fields = ['prefix','serial','pin','student'] |
---|
[3076] | 146 | headline = ','.join(fields) |
---|
| 147 | out = open(export_file,"wb") |
---|
[3077] | 148 | out.write('%(prefix)s,%(cost)8.2f,%(batch_no)d,%(nr)d,%(sold)s\n\n' % vars()) |
---|
[3076] | 149 | out.write(headline +'\n') |
---|
| 150 | out.close() |
---|
| 151 | out = open(export_file,"a") |
---|
| 152 | csv_writer = csv.DictWriter(out,fields,) |
---|
| 153 | pins = self.portal_pins(prefix_batch = "%(prefix)s%(batch_no)d" % vars()) |
---|
| 154 | logger.info('%s starts archiving %s_%s with %s records ' % (member, |
---|
| 155 | prefix, |
---|
| 156 | batch_no, |
---|
| 157 | len(pins))) |
---|
[3077] | 158 | |
---|
[3076] | 159 | pin_list = [] |
---|
| 160 | for pin in pins: |
---|
| 161 | pin_list += pin.pin, |
---|
| 162 | lines.append(self.portal_pins.record2dict(fields,pin)) |
---|
| 163 | total = len(lines) |
---|
| 164 | csv_writer.writerows(lines) |
---|
| 165 | msg = "wrote %(total)d records to %(export_file)s" % vars() |
---|
| 166 | logger.info(msg) |
---|
| 167 | if delete: |
---|
| 168 | for pin in pin_list: |
---|
[3077] | 169 | self.portal_pins.deleteRecord(pin) |
---|
| 170 | msg = "deleted %d pin from portal_pins" % len(pin_list) |
---|
[3076] | 171 | logger.info(msg) |
---|
| 172 | ###) |
---|
[947] | 173 | |
---|
[3076] | 174 | security.declareProtected(ModifyPortalContent,"getUnusedPins") ###( |
---|
[535] | 175 | def getUnusedPins(self): |
---|
| 176 | """return a list of unused Pins""" |
---|
[688] | 177 | max_pins = 10 |
---|
[535] | 178 | #import pdb;pdb.set_trace() |
---|
| 179 | pins_cat = self.portal_pins |
---|
| 180 | doc = self.getContent() |
---|
[686] | 181 | unused = [{'pin': p.pin, |
---|
[535] | 182 | 'serial': p.serial, |
---|
[612] | 183 | } for p in pins_cat.searchResults(prefix_batch = "%s%d" |
---|
| 184 | % (doc.prefix,doc.batch_no), |
---|
[1395] | 185 | ) if not p.student] |
---|
| 186 | if len(unused) > max_pins: |
---|
| 187 | return unused[-max_pins:] |
---|
[1002] | 188 | return unused |
---|
[3076] | 189 | ###) |
---|
[1571] | 190 | |
---|
[3076] | 191 | security.declareProtected(View,"getUsedPins") ###( |
---|
[1120] | 192 | def getUsedPins(self): |
---|
| 193 | """return a list of used Pins""" |
---|
| 194 | #import pdb;pdb.set_trace() |
---|
| 195 | pins_cat = self.portal_pins |
---|
| 196 | doc = self.getContent() |
---|
| 197 | used = [{'pin': p.pin, |
---|
| 198 | 'prefix_batch': p.prefix_batch, |
---|
| 199 | 'serial': p.serial, |
---|
| 200 | 'student': p.student, |
---|
| 201 | } for p in pins_cat.searchResults(prefix_batch = "%s%d" |
---|
| 202 | % (doc.prefix,doc.batch_no), |
---|
| 203 | ) if p.student] |
---|
| 204 | return used |
---|
[3076] | 205 | ###) |
---|
[1120] | 206 | |
---|
[3076] | 207 | security.declareProtected(View,"getNumberOfUsedPins") ###( |
---|
[1012] | 208 | def getNumberOfUsedPins(self): |
---|
| 209 | """return the number of used Pins""" |
---|
| 210 | #import pdb;pdb.set_trace() |
---|
| 211 | pins_cat = self.portal_pins |
---|
| 212 | doc = self.getContent() |
---|
| 213 | used = [{'pin': p.pin, |
---|
| 214 | 'serial': p.serial, |
---|
| 215 | 'student': p.student, |
---|
| 216 | } for p in pins_cat.searchResults(prefix_batch = "%s%d" |
---|
| 217 | % (doc.prefix,doc.batch_no), |
---|
| 218 | ) if p.student] |
---|
| 219 | return len(used) |
---|
[3076] | 220 | ###) |
---|
[1012] | 221 | |
---|
[489] | 222 | InitializeClass(ScratchCardBatch) |
---|
| 223 | |
---|
| 224 | def addScratchCardBatch(container, id, REQUEST=None, **kw): |
---|
| 225 | """Add a Students personal data.""" |
---|
| 226 | ob = ScratchCardBatch(id, **kw) |
---|
| 227 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 228 | |
---|
| 229 | ###) |
---|