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