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