[103] | 1 | #-*- mode: python; mode: fold -*- |
---|
| 2 | from Globals import InitializeClass |
---|
| 3 | from AccessControl import ClassSecurityInfo |
---|
| 4 | |
---|
| 5 | from Products.CMFCore.utils import UniqueObject, getToolByName |
---|
| 6 | from Products.CMFCore.permissions import View |
---|
| 7 | from Products.CMFCore.permissions import ModifyPortalContent |
---|
[575] | 8 | from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder |
---|
| 9 | #from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument |
---|
| 10 | from Products.CPSDocument.CPSDocument import CPSDocument |
---|
| 11 | from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder |
---|
[404] | 12 | from Products.WAeUP_SRP.WAeUPTables import AccommodationTable |
---|
[1410] | 13 | import logging |
---|
| 14 | import csv,re,os |
---|
[623] | 15 | import Globals |
---|
| 16 | import DateTime |
---|
[638] | 17 | import re |
---|
[623] | 18 | p_home = Globals.package_home(globals()) |
---|
| 19 | i_home = Globals.INSTANCE_HOME |
---|
[103] | 20 | |
---|
| 21 | class AccoFolder(CPSDocument): ###( |
---|
| 22 | """ |
---|
[575] | 23 | WAeUP AccoFolder containing Accommodation halls |
---|
[103] | 24 | """ |
---|
| 25 | meta_type = 'AccoFolder' |
---|
| 26 | portal_type = meta_type |
---|
| 27 | security = ClassSecurityInfo() |
---|
[630] | 28 | |
---|
[575] | 29 | security.declareProtected(View,"Title") |
---|
| 30 | def Title(self): |
---|
| 31 | """compose title""" |
---|
[630] | 32 | return "Accommodation Section" |
---|
[575] | 33 | |
---|
[404] | 34 | security.declareProtected(ModifyPortalContent,"generateFreeBedsList") ###( |
---|
| 35 | def generateFreeBedsList(self): |
---|
| 36 | """ |
---|
| 37 | generate the free Bedslist. |
---|
| 38 | """ |
---|
[623] | 39 | freelist = self.portal_accommodation |
---|
| 40 | l = self.portal_catalog({'meta_type': "AccoHall"}) |
---|
[404] | 41 | halls = [] |
---|
[623] | 42 | generated = [] |
---|
| 43 | generated.append('"Bed","BedType","Student"' % vars()) |
---|
[626] | 44 | beds_generated = [] |
---|
[404] | 45 | for h in l: |
---|
| 46 | halls.append(h.getObject()) |
---|
| 47 | for hall in halls: |
---|
[623] | 48 | #import pdb;pdb.set_trace() |
---|
[404] | 49 | h = hall.getContent() |
---|
[626] | 50 | hall_gen = {} |
---|
[630] | 51 | hall_gen['name'] = hall.Title |
---|
[626] | 52 | count = 0 |
---|
[926] | 53 | reserved = [(r.split('/')[0],int(r.split('/')[1])) for r in re.split(',|\.| ',h.reserved_rooms) |
---|
[714] | 54 | if r] |
---|
[926] | 55 | #for block in range(1,int(h.nr_of_blocks)+1): |
---|
[2823] | 56 | # for block in 'ABCDEFGHIJ'[:int(h.nr_of_blocks)]: |
---|
| 57 | if set(h.blocks_for_female).intersection(set(h.blocks_for_male)): |
---|
| 58 | return self.accommodation.acco_folder_view(beds_generated=beds_generated) |
---|
| 59 | for block in h.blocks_for_female + h.blocks_for_male: |
---|
[934] | 60 | sex = 'male' |
---|
| 61 | if block in h.blocks_for_female: |
---|
| 62 | sex = 'female' |
---|
[627] | 63 | for floor in range(1,int(h.nr_of_floors)+1): |
---|
| 64 | for room in range(1,int(h.rooms_per_floor)+1): |
---|
[404] | 65 | for bed in 'ABCDEFGH'[:int(h.beds_per_room)]: |
---|
| 66 | room_nr = floor*100 + room |
---|
[638] | 67 | bt = 're' |
---|
[714] | 68 | if (block,room_nr) in reserved: |
---|
[623] | 69 | bt = "reserved" |
---|
[2443] | 70 | elif h.special_handling and not h.special_handling.startswith("no"): |
---|
[926] | 71 | bt = h.special_handling |
---|
[623] | 72 | elif bed in h.beds_for_fresh: |
---|
[407] | 73 | bt = 'fr' |
---|
[2421] | 74 | elif bed in h.beds_for_pre: |
---|
| 75 | bt = 'pr' |
---|
[407] | 76 | elif bed in h.beds_for_final: |
---|
| 77 | bt = 'fi' |
---|
[936] | 78 | if h.special_handling.startswith("no_"): |
---|
[952] | 79 | bt += "_" + h.special_handling[3:] |
---|
[575] | 80 | bt = "%(sex)s_%(bt)s" % vars() |
---|
[926] | 81 | uid = '%s_%s_%d_%s' % (hall.getId(),block,room_nr,bed) |
---|
[626] | 82 | try: |
---|
[627] | 83 | freelist.addRecord(bed = uid, bed_type = bt,hall = hall.getId()) |
---|
[626] | 84 | count +=1 |
---|
| 85 | generated.append('"%(uid)s","%(bt)s"' % vars()) |
---|
| 86 | except ValueError: |
---|
[627] | 87 | #freelist.modifyRecord(bed = uid, bed_type = bt,hall =hall.getId()) |
---|
[626] | 88 | pass |
---|
| 89 | hall_gen['count']= count |
---|
| 90 | beds_generated.append(hall_gen) |
---|
[623] | 91 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
| 92 | open("%s/import/bedlist_%s.csv" % (i_home,current),"w+").write('\n'.join(generated)) |
---|
[650] | 93 | return self.accommodation.acco_folder_view(beds_generated=beds_generated) |
---|
[152] | 94 | |
---|
[404] | 95 | ###) |
---|
| 96 | |
---|
[1410] | 97 | security.declareProtected(ModifyPortalContent,"importReservedBeds")###( |
---|
| 98 | def importReservedBeds(self): |
---|
| 99 | """load Reserved Beds from CSV""" |
---|
| 100 | import transaction |
---|
| 101 | import random |
---|
| 102 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
| 103 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
| 104 | acco_folder = self.portal_url.getPortalObject().campus.accommodation |
---|
| 105 | tr_count = 1 |
---|
| 106 | total = 0 |
---|
| 107 | name = 'ReservedBeds' |
---|
| 108 | accommodation = self.portal_accommodation |
---|
| 109 | students_cat = self.students_catalog |
---|
| 110 | no_import = [] |
---|
| 111 | imported = [] |
---|
[1571] | 112 | logger = logging.getLogger('Accommodation.AccoFolder.importReservedBeds') |
---|
[1410] | 113 | try: |
---|
| 114 | beds = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) |
---|
| 115 | except: |
---|
| 116 | logger.error('Error reading %s.csv' % name) |
---|
| 117 | return |
---|
| 118 | halls = {} |
---|
| 119 | start = True |
---|
| 120 | for bed in beds: |
---|
| 121 | if start: |
---|
| 122 | start = False |
---|
| 123 | logger.info('start loading from %s.csv' % name) |
---|
| 124 | s = ','.join(['"%s"' % fn for fn in bed.keys()]) |
---|
| 125 | imported.append(s) |
---|
| 126 | no_import.append('%s,"Error"' % s) |
---|
| 127 | format = ','.join(['"%%(%s)s"' % fn for fn in bed.keys()]) |
---|
| 128 | format_error = format + ',"%(Error)s"' |
---|
| 129 | no_certificate = "no certificate %s" % format |
---|
| 130 | matric_no = bed.get('matric_no') |
---|
| 131 | jamb_reg_no = bed.get('jamb_reg_no') |
---|
| 132 | if matric_no != '': |
---|
| 133 | res = students_cat(matric_no = matric_no) |
---|
| 134 | if not res: |
---|
| 135 | bed['Error'] = "No such student" |
---|
| 136 | no_import.append( format_error % bed) |
---|
| 137 | continue |
---|
| 138 | student = res[0] |
---|
| 139 | elif jamb_reg_no != '': |
---|
| 140 | res = students_cat(jamb_reg_no = jamb_reg_no) |
---|
| 141 | if not res: |
---|
| 142 | bed['Error'] = "No such student" |
---|
| 143 | no_import.append( format_error % bed) |
---|
| 144 | continue |
---|
| 145 | student = res[0] |
---|
| 146 | else: |
---|
| 147 | bed['Error'] = "No such student" |
---|
| 148 | no_import.append( format_error % bed) |
---|
| 149 | continue |
---|
| 150 | sid = student.id |
---|
| 151 | names = bed.get('name').split() |
---|
| 152 | n = 0 |
---|
| 153 | for na in names: |
---|
| 154 | if na.endswith(','): |
---|
| 155 | names[n] = na[:-1] |
---|
| 156 | n += 1 |
---|
| 157 | s_names = student.name.split() |
---|
| 158 | found = False |
---|
| 159 | for sn in s_names: |
---|
| 160 | if sn.upper() in names: |
---|
| 161 | found = True |
---|
| 162 | break |
---|
| 163 | if not found: |
---|
| 164 | bed['Error'] = "Name mismatch %s %s" % (str(names),str(s_names)) |
---|
| 165 | no_import.append( format_error % bed) |
---|
| 166 | continue |
---|
| 167 | hall = bed.get('hall') |
---|
| 168 | if hall not in halls.keys(): |
---|
| 169 | hall_object = getattr(acco_folder,hall, None) |
---|
| 170 | if hall_object is None: |
---|
| 171 | bed['Error'] = "No such hall" |
---|
| 172 | no_import.append( format_error % bed) |
---|
| 173 | continue |
---|
[1571] | 174 | halls[hall] = hall_doc = hall_object.getContent() |
---|
[1410] | 175 | bid = "%(hall)s_%(block)s_%(room)s_%(bed)s" % bed |
---|
| 176 | res = accommodation(bed = bid) |
---|
| 177 | psm = '' |
---|
| 178 | while True: |
---|
| 179 | if not res: |
---|
| 180 | psm = "No bed with this id" |
---|
| 181 | break |
---|
| 182 | bed_brain = res[0] |
---|
| 183 | if not bed_brain.bed_type.endswith("reserved"): |
---|
| 184 | psm = "Not a reserved bed" |
---|
| 185 | break |
---|
| 186 | if bed_brain.student: |
---|
| 187 | psm = "Bed %s already reserved for %s" % (bid,bed_brain.student) |
---|
| 188 | break |
---|
| 189 | if student.sex and not bed.get('block') in hall_doc.blocks_for_female: |
---|
| 190 | psm = "Sex does not match %s" % bid |
---|
| 191 | break |
---|
| 192 | break |
---|
| 193 | if psm != '': |
---|
| 194 | bed['Error'] = psm |
---|
| 195 | no_import.append( format_error % bed) |
---|
| 196 | continue |
---|
[1449] | 197 | already = accommodation(student=sid) |
---|
| 198 | if already: |
---|
| 199 | accommodation.modifyRecord(bed=already[0].bed,student='') |
---|
| 200 | student_obj = getattr(students_folder, student.id) |
---|
[2454] | 201 | acco_id = "accommodation_%s" % self.getSessionId()[0] |
---|
[1449] | 202 | if acco_id in student_obj.objectIds(): |
---|
| 203 | acco_doc = getattr(student_obj, acco_id).getContent() |
---|
| 204 | acco_doc.edit(mapping={'bed': bid}) |
---|
| 205 | member = self.portal_membership.getAuthenticatedMember() |
---|
[1571] | 206 | logger.info('%s changed reserved bed %s for %s' % (member,bid,sid)) |
---|
[1410] | 207 | accommodation.modifyRecord(bed = bid, student = sid) |
---|
| 208 | imported.append( format % bed) |
---|
| 209 | tr_count += 1 |
---|
| 210 | if tr_count > 1000: |
---|
| 211 | if len(no_import) > 0: |
---|
| 212 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
| 213 | '\n'.join(no_import) + '\n') |
---|
| 214 | no_import = [] |
---|
| 215 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write( |
---|
| 216 | '\n'.join(no_import) + "\n") |
---|
| 217 | imported = [] |
---|
| 218 | em = '%d transactions commited total %s\n' % (tr_count,total) |
---|
| 219 | transaction.commit() |
---|
| 220 | regs = [] |
---|
| 221 | logger.info(em) |
---|
| 222 | total += tr_count |
---|
| 223 | tr_count = 0 |
---|
| 224 | #from pdb import set_trace;set_trace() |
---|
| 225 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write( |
---|
| 226 | '\n'.join(imported)) |
---|
| 227 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
| 228 | '\n'.join(no_import)) |
---|
[1449] | 229 | logger.info('finished loading from %s.csv' % name) |
---|
[1410] | 230 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
| 231 | |
---|
[1571] | 232 | |
---|
[103] | 233 | InitializeClass(AccoFolder) |
---|
| 234 | |
---|
| 235 | def addAccoFolder(container, id, REQUEST=None, **kw): |
---|
| 236 | """Add a AccoFolder.""" |
---|
| 237 | ob = AccoFolder(id, **kw) |
---|
| 238 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 239 | ###) |
---|
| 240 | |
---|
[622] | 241 | class AccoHall(CPSDocument): ###( |
---|
[103] | 242 | """ |
---|
[622] | 243 | WAeUP AccoHall containing Departments |
---|
[103] | 244 | """ |
---|
[622] | 245 | meta_type = 'AccoHall' |
---|
[103] | 246 | portal_type = meta_type |
---|
| 247 | security = ClassSecurityInfo() |
---|
[575] | 248 | |
---|
[404] | 249 | security.declareProtected(View,"Title") ###( |
---|
[152] | 250 | def Title(self): |
---|
| 251 | """compose title""" |
---|
| 252 | content = self.getContent() |
---|
| 253 | heading = getattr(content,'heading',None) |
---|
| 254 | if heading is None: |
---|
| 255 | return self.title |
---|
| 256 | return heading |
---|
[1393] | 257 | ###) |
---|
[404] | 258 | |
---|
[622] | 259 | InitializeClass(AccoHall) |
---|
[103] | 260 | |
---|
[1571] | 261 | |
---|
[622] | 262 | def addAccoHall(container, id, REQUEST=None, **kw): |
---|
| 263 | """Add a AccoHall.""" |
---|
| 264 | ob = AccoHall(id, **kw) |
---|
[103] | 265 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 266 | ###) |
---|