[57] | 1 | #-*- mode: python; mode: fold -*- |
---|
[200] | 2 | # $Id: Students.py 2654 2007-11-14 17:54:25Z henrik $ |
---|
[708] | 3 | from string import Template |
---|
[45] | 4 | from Globals import InitializeClass |
---|
| 5 | from AccessControl import ClassSecurityInfo |
---|
[164] | 6 | from AccessControl.SecurityManagement import newSecurityManager |
---|
[429] | 7 | from zExceptions import BadRequest |
---|
[502] | 8 | from Products.ZCatalog.ZCatalog import ZCatalog |
---|
[47] | 9 | from Products.CMFCore.utils import UniqueObject, getToolByName |
---|
[45] | 10 | from Products.CMFCore.permissions import View |
---|
| 11 | from Products.CMFCore.permissions import ModifyPortalContent |
---|
[154] | 12 | from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder |
---|
| 13 | #from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument |
---|
| 14 | from Products.CPSDocument.CPSDocument import CPSDocument |
---|
| 15 | from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder |
---|
[164] | 16 | from Products.CPSCore.CPSMembershipTool import CPSUnrestrictedUser |
---|
[361] | 17 | from Products.WAeUP_SRP.Academics import makeCertificateCode |
---|
[2125] | 18 | |
---|
[1649] | 19 | from Products.AdvancedQuery import Eq, Between, Le,In |
---|
[1820] | 20 | import DateTime |
---|
[362] | 21 | import logging |
---|
[971] | 22 | import csv,re,os |
---|
[362] | 23 | import Globals |
---|
| 24 | p_home = Globals.package_home(globals()) |
---|
| 25 | i_home = Globals.INSTANCE_HOME |
---|
[981] | 26 | MAX_TRANS = 1000 |
---|
[1599] | 27 | from urllib import urlencode |
---|
| 28 | |
---|
[966] | 29 | import DateTime |
---|
[1801] | 30 | #import PIL.Image |
---|
[971] | 31 | from StringIO import StringIO |
---|
[154] | 32 | |
---|
[958] | 33 | def makeCertificateCode(code): ###( |
---|
| 34 | code = code.replace('.','') |
---|
| 35 | code = code.replace('(','') |
---|
| 36 | code = code.replace(')','') |
---|
| 37 | code = code.replace('/','') |
---|
| 38 | code = code.replace(' ','') |
---|
| 39 | code = code.replace('_','') |
---|
| 40 | return code |
---|
| 41 | |
---|
| 42 | ###) |
---|
| 43 | |
---|
[1939] | 44 | def response_write(response,s): ###( |
---|
[1599] | 45 | response.setHeader('Content-type','text/html; charset=ISO-8859-15') |
---|
| 46 | while s.find('<') > -1: |
---|
| 47 | s = s.replace('<','<') |
---|
| 48 | while s.find('>') > -1: |
---|
| 49 | #from pdb import set_trace;set_trace() |
---|
| 50 | s = s.replace('>','>') |
---|
| 51 | response.write("%s<br>\n" % s) |
---|
| 52 | |
---|
[1939] | 53 | ###) |
---|
| 54 | |
---|
[958] | 55 | def getInt(s): ###( |
---|
[723] | 56 | try: |
---|
| 57 | return int(s) |
---|
| 58 | except: |
---|
| 59 | return 0 |
---|
[422] | 60 | |
---|
[725] | 61 | def getFloat(s): |
---|
| 62 | try: |
---|
| 63 | return float(s) |
---|
| 64 | except: |
---|
| 65 | return 0.0 |
---|
| 66 | |
---|
[958] | 67 | ###) |
---|
| 68 | |
---|
[714] | 69 | def getStudentByRegNo(self,reg_no): ###( |
---|
[502] | 70 | """search student by JAMB Reg No and return StudentFolder""" |
---|
[1969] | 71 | res = self.students_catalog(jamb_reg_no = reg_no.upper()) |
---|
[1902] | 72 | if len(res) == 1: |
---|
| 73 | return getattr(self.portal_url.getPortalObject().campus.students,res[0].id) |
---|
| 74 | else: |
---|
[502] | 75 | return None |
---|
[1902] | 76 | # don't search in portal_catalog |
---|
| 77 | # search = ZCatalog.searchResults(self.portal_catalog_real,{'meta_type': 'StudentApplication', |
---|
| 78 | # 'SearchableText': reg_no, |
---|
| 79 | # }) |
---|
| 80 | # if len(search) < 1: |
---|
| 81 | # return None |
---|
| 82 | # return search[0].getObject().aq_parent |
---|
[502] | 83 | |
---|
[714] | 84 | ###) |
---|
| 85 | |
---|
[1939] | 86 | def checkJambNo(jnr): ###( |
---|
[1111] | 87 | try: |
---|
| 88 | if len(jnr) != 10: |
---|
| 89 | return False |
---|
| 90 | except: |
---|
| 91 | return False |
---|
| 92 | try: |
---|
| 93 | int(jnr[:8]) |
---|
| 94 | return True |
---|
| 95 | except: |
---|
| 96 | return False |
---|
[1119] | 97 | |
---|
[1939] | 98 | ###) |
---|
| 99 | |
---|
[2125] | 100 | def formatLGA(lga,voc=None): |
---|
| 101 | if voc is not None: |
---|
| 102 | if voc.has_key(lga): |
---|
| 103 | state,lga = voc[lga].split(' / ') |
---|
| 104 | elif lga.find(' / ') > -1: |
---|
| 105 | state,lga = lga.split(' / ') |
---|
| 106 | else: |
---|
| 107 | state,lga = "",lga |
---|
| 108 | return state.upper(),lga.upper() |
---|
[1939] | 109 | |
---|
[361] | 110 | class StudentsFolder(CPSDocument): ###( |
---|
| 111 | """ |
---|
| 112 | WAeUP container for the various WAeUP containers data |
---|
| 113 | """ |
---|
[362] | 114 | meta_type = 'StudentsFolder' |
---|
[361] | 115 | portal_type = meta_type |
---|
| 116 | security = ClassSecurityInfo() |
---|
[154] | 117 | |
---|
[1799] | 118 | |
---|
[1820] | 119 | security.declareProtected(ModifyPortalContent,"transferStudents")###( |
---|
| 120 | def transferStudents(self,filename): |
---|
| 121 | """ |
---|
| 122 | load Interfaculty transferStudents Studentdata from CSV values. |
---|
| 123 | """ |
---|
| 124 | import transaction |
---|
| 125 | import random |
---|
| 126 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
| 127 | pm = self.portal_membership |
---|
| 128 | member = pm.getAuthenticatedMember() |
---|
| 129 | logger = logging.getLogger('Students.StudentsFolder.transferStudents') |
---|
| 130 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject() |
---|
| 131 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
| 132 | csv_fields = ('old_matric_no', |
---|
| 133 | 'matric_no', |
---|
| 134 | 'study_course', |
---|
| 135 | 'current_mode', |
---|
| 136 | 'current_level', |
---|
| 137 | ) |
---|
| 138 | tr_count = 0 |
---|
| 139 | total = 0 |
---|
| 140 | total_imported = 0 |
---|
| 141 | total_not_imported = 0 |
---|
| 142 | imported = [] |
---|
| 143 | not_imported = [] |
---|
| 144 | certs = {} |
---|
| 145 | try: |
---|
| 146 | results = csv.DictReader(open("%s/import/%s.csv" % (i_home,filename),"rb")) |
---|
| 147 | except: |
---|
| 148 | logger.error('Error reading %s.csv' % filename) |
---|
| 149 | return |
---|
| 150 | start = True |
---|
| 151 | for result in results: |
---|
| 152 | total += 1 |
---|
| 153 | if start: |
---|
| 154 | start = False |
---|
| 155 | logger.info('%s starts import from %s.csv' % (member,filename)) |
---|
| 156 | import_keys = [k for k in result.keys() if not k.startswith('ignore')] |
---|
| 157 | diff2schema = set(import_keys).difference(set(csv_fields)) |
---|
| 158 | if diff2schema: |
---|
| 159 | em = "not ignorable key(s) %s found in heading" % diff2schema |
---|
| 160 | return em |
---|
| 161 | s = ','.join(['"%s"' % fn for fn in import_keys]) |
---|
| 162 | open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write(s + ',"Error"'+ '\n') |
---|
| 163 | s = '"id",' + s |
---|
| 164 | open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write(s + '\n') |
---|
| 165 | format = ','.join(['"%%(%s)s"' % fn for fn in import_keys]) |
---|
| 166 | format_error = format + ',"%(Error)s"' |
---|
| 167 | format = '"%(id)s",'+ format |
---|
| 168 | old_matric_no = result.get('old_matric_no') |
---|
| 169 | res = self.students_catalog(matric_no = old_matric_no) |
---|
| 170 | result['id'] = "None" |
---|
| 171 | if not res: |
---|
[1834] | 172 | em = 'Student with matric_no %s not found' % old_matric_no |
---|
[1820] | 173 | logger.info(em) |
---|
[1834] | 174 | result['Error'] = "Student does not exist" |
---|
[1820] | 175 | not_imported.append(format_error % result) |
---|
| 176 | total_not_imported += 1 |
---|
| 177 | continue |
---|
| 178 | student_brain = res[0] |
---|
| 179 | student_object = getattr(students_folder,student_brain.id) |
---|
| 180 | result['id'] = student_brain.id |
---|
| 181 | cert_id = makeCertificateCode(result.get('study_course')) |
---|
| 182 | if cert_id not in certs.keys(): |
---|
| 183 | res = self.portal_catalog(meta_type = "Certificate",id = cert_id) |
---|
| 184 | if not res: |
---|
[1834] | 185 | em = 'No certificate with ID %s \n' % cert_id |
---|
[1820] | 186 | logger.info(em) |
---|
| 187 | result['Error'] = "No Certificate %s" % cert_id |
---|
| 188 | not_imported.append( format_error % result) |
---|
| 189 | total_not_imported += 1 |
---|
| 190 | continue |
---|
| 191 | cert = res[0] |
---|
| 192 | cert_path = cert.getPath().split('/') |
---|
| 193 | certificate = certs[cert_id] = {'faculty': cert_path[-4], |
---|
| 194 | 'department': cert_path[-3]} |
---|
| 195 | cert_doc = certs[cert_id] |
---|
| 196 | clearance = getattr(student_object,'clearance',None) |
---|
| 197 | if clearance is None: |
---|
| 198 | em = 'Student has no clearance object' |
---|
[2124] | 199 | logger.info('%s (%s) %s' % (student_brain.id, old_matric_no, em)) |
---|
[1820] | 200 | result['Error'] = em |
---|
| 201 | not_imported.append( format_error % result) |
---|
| 202 | total_not_imported += 1 |
---|
| 203 | continue |
---|
| 204 | clearance_doc = clearance.getContent() |
---|
| 205 | study_course = student_object.study_course |
---|
| 206 | study_course_doc = study_course.getContent() |
---|
| 207 | old_study_course = study_course_doc.study_course |
---|
| 208 | old_current_level = study_course_doc.current_level |
---|
| 209 | current_level = result.get('current_level',None) |
---|
[1834] | 210 | new_study_course = result.get('study_course',None) |
---|
[1820] | 211 | try: |
---|
| 212 | icl = int(current_level) |
---|
| 213 | except: |
---|
| 214 | em = 'Invalid new level %s' % current_level |
---|
| 215 | logger.info(em) |
---|
| 216 | result['Error'] = em |
---|
| 217 | not_imported.append( format_error % result) |
---|
| 218 | total_not_imported += 1 |
---|
| 219 | continue |
---|
[1880] | 220 | if icl == int(old_current_level) and old_study_course == new_study_course: |
---|
| 221 | em = 'Already transferred' |
---|
[2124] | 222 | logger.info('%s (%s) %s' % (student_brain.id, old_matric_no, em)) |
---|
[1880] | 223 | result['Error'] = em |
---|
| 224 | not_imported.append( format_error % result) |
---|
| 225 | total_not_imported += 1 |
---|
[1887] | 226 | continue |
---|
[1880] | 227 | if study_course.objectIds(): |
---|
[1834] | 228 | em = 'Already registered level %s for %s, but is supposed to study %s at level %s' % (old_current_level,old_study_course,new_study_course,current_level) |
---|
[2124] | 229 | logger.info('%s (%s) %s' % (student_brain.id, old_matric_no, em)) |
---|
[1820] | 230 | result['Error'] = em |
---|
| 231 | not_imported.append( format_error % result) |
---|
| 232 | total_not_imported += 1 |
---|
| 233 | continue |
---|
| 234 | #from pdb import set_trace; set_trace() |
---|
| 235 | cd = {} |
---|
| 236 | matric_no_history = getattr(clearance_doc,'matric_no_history',[]) |
---|
| 237 | if not matric_no_history: |
---|
| 238 | matric_no_history = [] |
---|
| 239 | matric_no_history.append(old_matric_no) |
---|
| 240 | cd['matric_no_history'] = matric_no_history |
---|
| 241 | cd['matric_no'] = result.get('matric_no') |
---|
| 242 | clearance_doc.edit(mapping = cd) |
---|
| 243 | dsc = {} |
---|
| 244 | study_course_history = getattr(study_course_doc,'study_course_history',[]) |
---|
| 245 | if not study_course_history: |
---|
| 246 | study_course_history = [] |
---|
| 247 | study_course_history.append(old_study_course) |
---|
| 248 | dsc['study_course_history'] = study_course_history |
---|
[1834] | 249 | dsc['study_course'] = new_study_course |
---|
[1820] | 250 | dsc['current_level'] = current_level |
---|
| 251 | dsc['current_mode'] = result.get('current_mode') |
---|
| 252 | study_course_doc.edit(mapping=dsc) |
---|
| 253 | imported.append( format % result) |
---|
| 254 | tr_count += 1 |
---|
| 255 | total_imported += 1 |
---|
| 256 | if tr_count > 1000: |
---|
| 257 | if len(not_imported) > 0: |
---|
| 258 | open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
| 259 | '\n'.join(not_imported) + '\n') |
---|
| 260 | not_imported = [] |
---|
| 261 | if len(imported) > 0: |
---|
| 262 | open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
| 263 | '\n'.join(imported) + '\n') |
---|
| 264 | imported = [] |
---|
| 265 | em = '%d transactions committed\n' % (tr_count) |
---|
| 266 | transaction.commit() |
---|
| 267 | regs = [] |
---|
| 268 | logger.info(em) |
---|
| 269 | tr_count = 0 |
---|
| 270 | if len(imported) > 0: |
---|
| 271 | open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
| 272 | '\n'.join(imported)) |
---|
| 273 | if len(not_imported) > 0: |
---|
| 274 | open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write( |
---|
| 275 | '\n'.join(not_imported)) |
---|
| 276 | em = "Imported: %d, not imported: %d of total %d" % (total_imported,total_not_imported,total) |
---|
| 277 | logger.info(em) |
---|
| 278 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
| 279 | ###) |
---|
| 280 | |
---|
[1146] | 281 | |
---|
[1970] | 282 | security.declareProtected(ModifyPortalContent,"dumpStudentsCatalog")###( |
---|
| 283 | def dumpStudentsCatalog(self): |
---|
| 284 | """dump all data in students_catalog to a csv""" |
---|
| 285 | member = self.portal_membership.getAuthenticatedMember() |
---|
| 286 | logger = logging.getLogger('Students.dumpStudentsCatalog') |
---|
| 287 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
| 288 | export_file = "%s/export/students_catalog_%s.csv" % (i_home,current) |
---|
| 289 | res_list = [] |
---|
| 290 | lines = [] |
---|
| 291 | fields = [] |
---|
| 292 | for f in self.students_catalog.schema(): |
---|
| 293 | fields.append(f) |
---|
[2083] | 294 | fields.append('state') |
---|
[1970] | 295 | headline = ','.join(fields) |
---|
[2128] | 296 | #open(export_file,"a").write(headline +'\n') |
---|
| 297 | out = open(export_file,"wb") |
---|
| 298 | out.write(headline +'\n') |
---|
| 299 | out.close() |
---|
| 300 | out = open(export_file,"a") |
---|
| 301 | csv_writer = csv.DictWriter(out,fields,) |
---|
[1970] | 302 | format = '"%(' + ')s","%('.join(fields) + ')s"' |
---|
| 303 | students = self.students_catalog() |
---|
| 304 | nr2export = len(students) |
---|
[2012] | 305 | logger.info('%s starts dumpStudentsCatalog, %s student records to export' % (member,nr2export)) |
---|
[1970] | 306 | chunk = 2000 |
---|
| 307 | total = 0 |
---|
| 308 | start = DateTime.DateTime().timeTime() |
---|
| 309 | start_chunk = DateTime.DateTime().timeTime() |
---|
| 310 | for student in students: |
---|
| 311 | not_all = False |
---|
| 312 | d = self.getFormattedStudentEntry(student) |
---|
[2125] | 313 | d['state'],d['lga'] = formatLGA(d['lga'],voc = self.portal_vocabularies.local_gov_areas) |
---|
[2128] | 314 | #lines.append(format % d) |
---|
| 315 | lines.append(d) |
---|
[1970] | 316 | total += 1 |
---|
| 317 | if total and not total % chunk or total == len(students): |
---|
[2128] | 318 | #open(export_file,"a").write('\n'.join(lines) +'\n') |
---|
| 319 | csv_writer.writerows(lines) |
---|
[1970] | 320 | anz = len(lines) |
---|
| 321 | logger.info("wrote %(anz)d total written %(total)d" % vars()) |
---|
| 322 | end_chunk = DateTime.DateTime().timeTime() |
---|
| 323 | duration = end_chunk-start_chunk |
---|
| 324 | per_record = duration/anz |
---|
| 325 | till_now = end_chunk - start |
---|
| 326 | avarage_per_record = till_now/total |
---|
| 327 | estimated_end = DateTime.DateTime(start + avarage_per_record * nr2export) |
---|
| 328 | estimated_end = estimated_end.strftime("%H:%M:%S") |
---|
| 329 | logger.info('%(duration)4.1f, %(per_record)4.3f,end %(estimated_end)s' % vars()) |
---|
| 330 | start_chunk = DateTime.DateTime().timeTime() |
---|
| 331 | lines = [] |
---|
| 332 | end = DateTime.DateTime().timeTime() |
---|
| 333 | logger.info('total time %6.2f m' % ((end-start)/60)) |
---|
| 334 | filename, extension = os.path.splitext(export_file) |
---|
| 335 | from subprocess import call |
---|
| 336 | msg = "wrote %(total)d records to %(export_file)s" % vars() |
---|
| 337 | try: |
---|
| 338 | retcode = call('gzip %s' % (export_file),shell=True) |
---|
| 339 | if retcode == 0: |
---|
| 340 | msg = "wrote %(total)d records to %(export_file)s.gz" % vars() |
---|
| 341 | except OSError, e: |
---|
| 342 | retcode = -99 |
---|
| 343 | logger.info("zip failed with %s" % e) |
---|
| 344 | logger.info(msg) |
---|
| 345 | args = {'portal_status_message': msg} |
---|
| 346 | #url = self.REQUEST.get('URL1') + '?' + urlencode(args) |
---|
| 347 | url = self.REQUEST.get('URL2') |
---|
| 348 | return self.REQUEST.RESPONSE.redirect(url) |
---|
| 349 | ###) |
---|
| 350 | |
---|
| 351 | |
---|
[1146] | 352 | security.declareProtected(ModifyPortalContent,"importResults")###( |
---|
| 353 | def importResults(self): |
---|
[1151] | 354 | """load Returning Students Results from CSV""" |
---|
[1146] | 355 | import transaction |
---|
| 356 | import random |
---|
| 357 | #from pdb import set_trace |
---|
| 358 | wftool = self.portal_workflow |
---|
| 359 | current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") |
---|
[1802] | 360 | #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject() |
---|
| 361 | students_folder = self.portal_url.getPortalObject().campus.students |
---|
[1146] | 362 | tr_count = 1 |
---|
[2072] | 363 | total = 0 |
---|
[1146] | 364 | #name = 'pume_results' |
---|
| 365 | name = 'Results' |
---|
| 366 | table = self.results_import |
---|
| 367 | no_import = [] |
---|
| 368 | imported = [] |
---|
[1571] | 369 | logger = logging.getLogger('Students.StudentsFolder.importResults') |
---|
[1146] | 370 | try: |
---|
| 371 | results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) |
---|
| 372 | except: |
---|
| 373 | logger.error('Error reading %s.csv' % name) |
---|
| 374 | return |
---|
[2071] | 375 | |
---|
[1146] | 376 | l = self.portal_catalog({'meta_type': "Course"}) |
---|
| 377 | courses = [f.getId for f in l] |
---|
| 378 | start = True |
---|
| 379 | res = table() |
---|
| 380 | regs = [] |
---|
| 381 | if len(res) > 0: |
---|
| 382 | regs = [s.key for s in res] |
---|
| 383 | no_course = [] |
---|
| 384 | no_course_list = [] |
---|
| 385 | course_count = 0 |
---|
| 386 | for result in results: |
---|
| 387 | if start: |
---|
| 388 | start = False |
---|
[1571] | 389 | logger.info('Start loading from %s.csv' % name) |
---|
[1321] | 390 | s = ','.join(['"%s"' % fn for fn in result.keys()]) |
---|
[1146] | 391 | imported.append(s) |
---|
| 392 | no_import.append('%s,"Error"' % s) |
---|
| 393 | format = ','.join(['"%%(%s)s"' % fn for fn in result.keys()]) |
---|
| 394 | format_error = format + ',"%(Error)s"' |
---|
| 395 | no_certificate = "no certificate %s" % format |
---|
| 396 | course_id = result.get('CosCode') |
---|
[1448] | 397 | if not course_id: |
---|
| 398 | course_id = 'N/A' |
---|
| 399 | result['CosCode'] = course_id |
---|
[1168] | 400 | matric_no = result.get('matric_no').upper() |
---|
| 401 | result['matric_no'] = matric_no |
---|
| 402 | key = matric_no+course_id |
---|
| 403 | if matric_no == '': |
---|
[1252] | 404 | result['Error'] = "Empty matric_no" |
---|
[1146] | 405 | no_import.append( format_error % result) |
---|
| 406 | continue |
---|
[1168] | 407 | if key in regs or self.results_import(key = key): |
---|
[1252] | 408 | result['Error'] = "Duplicate" |
---|
[1146] | 409 | no_import.append( format_error % result) |
---|
| 410 | continue |
---|
| 411 | if course_id not in courses: |
---|
| 412 | if course_id not in no_course: |
---|
| 413 | course_count +=1 |
---|
| 414 | no_course.append(course_id) |
---|
| 415 | no_course_list.append('"%s"' % course_id) |
---|
| 416 | #result['Error'] = "No Course" |
---|
| 417 | #logger.info(format_error % result) |
---|
[1829] | 418 | |
---|
[1816] | 419 | result['key'] = key |
---|
| 420 | try: |
---|
| 421 | table.addRecord(**result) |
---|
| 422 | except ValueError: |
---|
| 423 | #import pdb;pdb.set_trace() |
---|
| 424 | result['Error'] = "Duplicate" |
---|
| 425 | no_import.append( format_error % result) |
---|
| 426 | continue |
---|
[1829] | 427 | |
---|
[1146] | 428 | regs.append(key) |
---|
| 429 | imported.append(format % result) |
---|
| 430 | tr_count += 1 |
---|
| 431 | if tr_count > 1000: |
---|
| 432 | if len(no_import) > 0: |
---|
| 433 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
[1151] | 434 | '\n'.join(no_import)+'\n') |
---|
[1146] | 435 | no_import = [] |
---|
| 436 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write( |
---|
[1151] | 437 | '\n'.join(imported) + '\n') |
---|
[1146] | 438 | imported = [] |
---|
| 439 | if no_course_list: |
---|
| 440 | open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write( |
---|
[1151] | 441 | '\n'.join(no_course_list) + '\n') |
---|
[1146] | 442 | no_course_list = [] |
---|
| 443 | transaction.commit() |
---|
[1168] | 444 | regs = [] |
---|
[2072] | 445 | total += tr_count |
---|
[2080] | 446 | em = '%d transactions totally comitted, %s courses not found ' % (total,course_count) |
---|
| 447 | logger.info(em) |
---|
[2072] | 448 | tr_count = 0 |
---|
[1146] | 449 | open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write( |
---|
| 450 | '\n'.join(imported)) |
---|
| 451 | open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write( |
---|
| 452 | '\n'.join(no_import)) |
---|
| 453 | if no_course_list: |
---|
| 454 | open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write( |
---|
| 455 | '\n'.join(no_course_list)) |
---|
[2072] | 456 | em = '%d transactions totally committed, %s courses not found ' % (total,course_count) |
---|
[1393] | 457 | logger.info(em) |
---|
[1146] | 458 | return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) |
---|
| 459 | ###) |
---|
| 460 | |
---|
[1065] | 461 | |
---|
[2654] | 462 | |
---|
[1594] | 463 | security.declareProtected(View,"fixOwnership") ###( |
---|
[511] | 464 | def fixOwnership(self): |
---|
| 465 | """fix Ownership""" |
---|
| 466 | for s in self.portal_catalog(meta_type = 'Student'): |
---|
| 467 | student = s.getObject() |
---|
| 468 | sid = s.getId |
---|
| 469 | import pdb;pdb.set_trace() |
---|
| 470 | student.application.manage_setLocalRoles(sid, ['Owner',]) |
---|
| 471 | student.personal.manage_setLocalRoles(sid, ['Owner',]) |
---|
[1594] | 472 | ###) |
---|
[603] | 473 | |
---|
[1594] | 474 | security.declareProtected(View,"Title") ###( |
---|
[364] | 475 | def Title(self): |
---|
| 476 | """compose title""" |
---|
[382] | 477 | return "Student Section" |
---|
[1594] | 478 | ###) |
---|
[361] | 479 | |
---|
[1700] | 480 | def generateStudentId(self,letter,students = None): ###( |
---|
[714] | 481 | import random |
---|
| 482 | r = random |
---|
[1700] | 483 | if students is None: |
---|
| 484 | students = self.portal_url.getPortalObject().campus.students |
---|
[714] | 485 | if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'): |
---|
| 486 | letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY') |
---|
| 487 | sid = "%c%d" % (letter,r.randint(99999,1000000)) |
---|
| 488 | while hasattr(students, sid): |
---|
| 489 | sid = "%c%d" % (letter,r.randint(99999,1000000)) |
---|
[758] | 490 | return sid |
---|
[714] | 491 | #return "%c%d" % (r.choice('ABCDEFGHKLMNPQRSTUVWXY'),r.randint(99999,1000000)) |
---|
| 492 | ###) |
---|
| 493 | |
---|
[361] | 494 | InitializeClass(StudentsFolder) |
---|
| 495 | |
---|
[1594] | 496 | def addStudentsFolder(container, id, REQUEST=None, **kw): ###( |
---|
[361] | 497 | """Add a Student.""" |
---|
| 498 | ob = StudentsFolder(id, **kw) |
---|
| 499 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
[1594] | 500 | ###) |
---|
| 501 | |
---|
[361] | 502 | ###) |
---|
| 503 | |
---|
[57] | 504 | class Student(CPSDocument): ###( |
---|
| 505 | """ |
---|
[154] | 506 | WAeUP Student container for the various student data |
---|
[57] | 507 | """ |
---|
| 508 | meta_type = 'Student' |
---|
| 509 | portal_type = meta_type |
---|
| 510 | security = ClassSecurityInfo() |
---|
[154] | 511 | |
---|
[152] | 512 | security.declareProtected(View,"Title") |
---|
| 513 | def Title(self): |
---|
| 514 | """compose title""" |
---|
[153] | 515 | reg_nr = self.getId()[1:] |
---|
[362] | 516 | data = getattr(self,'personal',None) |
---|
[152] | 517 | if data: |
---|
| 518 | content = data.getContent() |
---|
[1143] | 519 | return "%s %s %s" % (content.firstname,content.middlename,content.lastname) |
---|
[472] | 520 | data = getattr(self,'application',None) |
---|
[464] | 521 | if data: |
---|
| 522 | content = data.getContent() |
---|
| 523 | return "%s" % (content.jamb_lastname) |
---|
[152] | 524 | return self.title |
---|
[154] | 525 | |
---|
[511] | 526 | security.declarePrivate('makeStudentMember') ###( |
---|
| 527 | def makeStudentMember(self,sid,password='uNsEt'): |
---|
| 528 | """make the student a member""" |
---|
| 529 | membership = self.portal_membership |
---|
[603] | 530 | membership.addMember(sid, |
---|
[511] | 531 | password , |
---|
| 532 | roles=('Member', |
---|
| 533 | 'Student', |
---|
[522] | 534 | ), |
---|
[511] | 535 | domains='', |
---|
[904] | 536 | properties = {'memberareaCreationFlag': False, |
---|
| 537 | 'homeless': True},) |
---|
[511] | 538 | member = membership.getMemberById(sid) |
---|
| 539 | self.portal_registration.afterAdd(member, sid, password, None) |
---|
| 540 | self.manage_setLocalRoles(sid, ['Owner',]) |
---|
| 541 | |
---|
| 542 | ###) |
---|
| 543 | |
---|
[764] | 544 | security.declareProtected(View,'createSubObjects') ###( |
---|
| 545 | def createSubObjects(self): |
---|
| 546 | """make the student a member""" |
---|
| 547 | dp = {'Title': 'Personal Data'} |
---|
| 548 | app_doc = self.application.getContent() |
---|
| 549 | names = app_doc.jamb_lastname.split() |
---|
| 550 | if len(names) == 3: |
---|
| 551 | dp['firstname'] = names[0].capitalize() |
---|
| 552 | dp['middlename'] = names[1].capitalize() |
---|
| 553 | dp['lastname'] = names[2].capitalize() |
---|
| 554 | elif len(names) == 2: |
---|
| 555 | dp['firstname'] = names[0].capitalize() |
---|
| 556 | dp['lastname'] = names[1].capitalize() |
---|
| 557 | else: |
---|
| 558 | dp['lastname'] = app_doc.jamb_lastname |
---|
| 559 | dp['sex'] = app_doc.jamb_sex == 'F' |
---|
| 560 | dp['lga'] = "%s/%s" % (app_doc.jamb_state,app_doc.jamb_lga ) |
---|
| 561 | proxy = self.aq_parent |
---|
| 562 | proxy.invokeFactory('StudentPersonal','personal') |
---|
| 563 | per = proxy.personal |
---|
| 564 | per_doc = per.getContent() |
---|
| 565 | per_doc.edit(mapping = dp) |
---|
[927] | 566 | per.manage_setLocalRoles(proxy.getId(), ['Owner',]) |
---|
[764] | 567 | #self.portal_workflow.doActionFor(per,'open',dest_container=per) |
---|
[603] | 568 | |
---|
[511] | 569 | ###) |
---|
| 570 | |
---|
[57] | 571 | InitializeClass(Student) |
---|
| 572 | |
---|
| 573 | def addStudent(container, id, REQUEST=None, **kw): |
---|
| 574 | """Add a Student.""" |
---|
| 575 | ob = Student(id, **kw) |
---|
| 576 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 577 | |
---|
| 578 | ###) |
---|
[91] | 579 | |
---|
[639] | 580 | class StudentAccommodation(CPSDocument): ###( |
---|
| 581 | """ |
---|
| 582 | WAeUP Student container for the various student data |
---|
| 583 | """ |
---|
| 584 | meta_type = 'StudentAccommodation' |
---|
| 585 | portal_type = meta_type |
---|
| 586 | security = ClassSecurityInfo() |
---|
| 587 | |
---|
| 588 | security.declareProtected(View,"Title") |
---|
| 589 | def Title(self): |
---|
| 590 | """compose title""" |
---|
| 591 | content = self.getContent() |
---|
| 592 | #return "Accommodation Data for %s %s" % (content.firstname,content.lastname) |
---|
| 593 | return "Accommodation Data for Session %s" % content.session |
---|
| 594 | |
---|
| 595 | |
---|
| 596 | InitializeClass(StudentAccommodation) |
---|
| 597 | |
---|
| 598 | def addStudentAccommodation(container, id, REQUEST=None, **kw): |
---|
| 599 | """Add a Students personal data.""" |
---|
| 600 | ob = StudentAccommodation(id, **kw) |
---|
| 601 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 602 | |
---|
| 603 | ###) |
---|
| 604 | |
---|
[89] | 605 | class StudentPersonal(CPSDocument): ###( |
---|
| 606 | """ |
---|
[154] | 607 | WAeUP Student container for the various student data |
---|
[89] | 608 | """ |
---|
| 609 | meta_type = 'StudentPersonal' |
---|
| 610 | portal_type = meta_type |
---|
| 611 | security = ClassSecurityInfo() |
---|
[152] | 612 | |
---|
| 613 | security.declareProtected(View,"Title") |
---|
| 614 | def Title(self): |
---|
| 615 | """compose title""" |
---|
| 616 | content = self.getContent() |
---|
[364] | 617 | #return "Personal Data for %s %s" % (content.firstname,content.lastname) |
---|
| 618 | return "Personal Data" |
---|
[152] | 619 | |
---|
[154] | 620 | |
---|
[89] | 621 | InitializeClass(StudentPersonal) |
---|
| 622 | |
---|
| 623 | def addStudentPersonal(container, id, REQUEST=None, **kw): |
---|
| 624 | """Add a Students personal data.""" |
---|
| 625 | ob = StudentPersonal(id, **kw) |
---|
| 626 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 627 | |
---|
| 628 | ###) |
---|
| 629 | |
---|
[423] | 630 | class StudentClearance(CPSDocument): ###( |
---|
| 631 | """ |
---|
| 632 | WAeUP Student container for the various student data |
---|
| 633 | """ |
---|
| 634 | meta_type = 'StudentClearance' |
---|
| 635 | portal_type = meta_type |
---|
| 636 | security = ClassSecurityInfo() |
---|
| 637 | |
---|
| 638 | security.declareProtected(View,"Title") |
---|
| 639 | def Title(self): |
---|
| 640 | """compose title""" |
---|
| 641 | content = self.getContent() |
---|
[840] | 642 | #return "Clearance/Eligibility Record for %s %s" % (content.firstname,content.lastname) |
---|
| 643 | return "Clearance/Eligibility Record" |
---|
[423] | 644 | |
---|
| 645 | |
---|
| 646 | InitializeClass(StudentClearance) |
---|
| 647 | |
---|
| 648 | def addStudentClearance(container, id, REQUEST=None, **kw): |
---|
| 649 | """Add a Students personal data.""" |
---|
| 650 | ob = StudentClearance(id, **kw) |
---|
| 651 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 652 | |
---|
| 653 | ###) |
---|
| 654 | |
---|
[454] | 655 | class StudentStudyLevel(CPSDocument): ###( |
---|
| 656 | """ |
---|
| 657 | WAeUP Student container for the various student data |
---|
| 658 | """ |
---|
| 659 | meta_type = 'StudentStudyLevel' |
---|
| 660 | portal_type = meta_type |
---|
| 661 | security = ClassSecurityInfo() |
---|
| 662 | |
---|
| 663 | security.declareProtected(View,"Title") |
---|
| 664 | def Title(self): |
---|
| 665 | """compose title""" |
---|
[2640] | 666 | return self.portal_vocabularies.student_levels.get(self.aq_parent.getId()) |
---|
| 667 | #return "Level %s" % self.aq_parent.getId() |
---|
[454] | 668 | |
---|
[1700] | 669 | def create_course_results(self,cert_id,current_level): ###( |
---|
[1649] | 670 | "create all courses in a level" |
---|
| 671 | aq_portal = self.portal_catalog.evalAdvancedQuery |
---|
| 672 | res = self.portal_catalog(portal_type="Certificate", id = cert_id) |
---|
| 673 | l = [] |
---|
| 674 | import transaction |
---|
| 675 | if res: |
---|
| 676 | cert = res[0] |
---|
| 677 | path = cert.getPath() |
---|
| 678 | query = Eq("path","%s/%s" % (path,current_level)) &\ |
---|
| 679 | Eq('portal_type','CertificateCourse') |
---|
| 680 | courses = aq_portal(query) |
---|
| 681 | #from pdb import set_trace;set_trace() |
---|
| 682 | self_proxy = self.aq_parent |
---|
| 683 | for c in courses: |
---|
| 684 | d = self.getCourseInfo(c.getId) |
---|
| 685 | cr_id = self_proxy.invokeFactory('StudentCourseResult',c.getId) |
---|
| 686 | course_result = getattr(self_proxy,cr_id) |
---|
| 687 | self.portal_workflow.doActionFor(course_result,'open') |
---|
| 688 | d['core_or_elective'] = getattr(c.getObject().getContent(),'core_or_elective') |
---|
| 689 | course_result.getContent().edit(mapping=d) |
---|
[1820] | 690 | #transaction.commit() |
---|
[1700] | 691 | ###) |
---|
[472] | 692 | |
---|
[454] | 693 | InitializeClass(StudentStudyLevel) |
---|
| 694 | |
---|
| 695 | def addStudentStudyLevel(container, id, REQUEST=None, **kw): |
---|
| 696 | """Add a Students personal data.""" |
---|
| 697 | ob = StudentStudyLevel(id, **kw) |
---|
| 698 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 699 | |
---|
| 700 | ###) |
---|
| 701 | |
---|
[362] | 702 | class StudentStudyCourse(CPSDocument): ###( |
---|
| 703 | """ |
---|
| 704 | WAeUP Student container for the various student data |
---|
| 705 | """ |
---|
| 706 | meta_type = 'StudentStudyCourse' |
---|
| 707 | portal_type = meta_type |
---|
| 708 | security = ClassSecurityInfo() |
---|
| 709 | |
---|
[364] | 710 | security.declareProtected(View,"Title") |
---|
| 711 | def Title(self): |
---|
| 712 | """compose title""" |
---|
| 713 | content = self.getContent() |
---|
[453] | 714 | return "Study Course" |
---|
[362] | 715 | |
---|
| 716 | |
---|
| 717 | InitializeClass(StudentStudyCourse) |
---|
| 718 | |
---|
| 719 | def addStudentStudyCourse(container, id, REQUEST=None, **kw): |
---|
| 720 | """Add a Students personal data.""" |
---|
| 721 | ob = StudentStudyCourse(id, **kw) |
---|
| 722 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 723 | |
---|
| 724 | ###) |
---|
| 725 | |
---|
[472] | 726 | class StudentApplication(CPSDocument): ###( |
---|
[179] | 727 | """ |
---|
| 728 | WAeUP Student container for the various student data |
---|
| 729 | """ |
---|
[472] | 730 | meta_type = 'StudentApplication' |
---|
[179] | 731 | portal_type = meta_type |
---|
| 732 | security = ClassSecurityInfo() |
---|
| 733 | |
---|
[181] | 734 | security.declareProtected(View,"Title") |
---|
| 735 | def Title(self): |
---|
| 736 | """compose title""" |
---|
[472] | 737 | return "Application Data" |
---|
[179] | 738 | |
---|
[181] | 739 | |
---|
[472] | 740 | InitializeClass(StudentApplication) |
---|
[179] | 741 | |
---|
[472] | 742 | def addStudentApplication(container, id, REQUEST=None, **kw): |
---|
[179] | 743 | """Add a Students eligibility data.""" |
---|
[472] | 744 | ob = StudentApplication(id, **kw) |
---|
[179] | 745 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
[763] | 746 | ###) |
---|
[179] | 747 | |
---|
[758] | 748 | class StudentPume(CPSDocument): ###( |
---|
| 749 | """ |
---|
| 750 | WAeUP Student container for the various student data |
---|
| 751 | """ |
---|
| 752 | meta_type = 'StudentPume' |
---|
| 753 | portal_type = meta_type |
---|
| 754 | security = ClassSecurityInfo() |
---|
| 755 | |
---|
| 756 | security.declareProtected(View,"Title") |
---|
| 757 | def Title(self): |
---|
| 758 | """compose title""" |
---|
| 759 | return "PUME Results" |
---|
| 760 | |
---|
| 761 | |
---|
| 762 | InitializeClass(StudentPume) |
---|
| 763 | |
---|
| 764 | def addStudentPume(container, id, REQUEST=None, **kw): |
---|
| 765 | """Add a Students PUME data.""" |
---|
| 766 | ob = StudentPume(id, **kw) |
---|
| 767 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
[179] | 768 | ###) |
---|
[181] | 769 | |
---|
[565] | 770 | ##class StudentSemester(CPSDocument): ###( |
---|
| 771 | ## """ |
---|
| 772 | ## WAeUP StudentSemester containing the courses and students |
---|
| 773 | ## """ |
---|
| 774 | ## meta_type = 'StudentSemester' |
---|
| 775 | ## portal_type = meta_type |
---|
| 776 | ## security = ClassSecurityInfo() |
---|
| 777 | ## |
---|
| 778 | ##InitializeClass(StudentSemester) |
---|
| 779 | ## |
---|
| 780 | ##def addStudentSemester(container, id, REQUEST=None, **kw): |
---|
| 781 | ## """Add a StudentSemester.""" |
---|
| 782 | ## ob = StudentSemester(id, **kw) |
---|
| 783 | ## return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 784 | ## |
---|
| 785 | #####) |
---|
[464] | 786 | |
---|
[758] | 787 | ##class Semester(CPSDocument): ###( |
---|
| 788 | ## """ |
---|
| 789 | ## WAeUP Semester containing the courses and students |
---|
| 790 | ## """ |
---|
| 791 | ## meta_type = 'Semester' |
---|
| 792 | ## portal_type = meta_type |
---|
| 793 | ## security = ClassSecurityInfo() |
---|
| 794 | ## |
---|
| 795 | ##InitializeClass(Semester) |
---|
| 796 | ## |
---|
| 797 | ##def addSemester(container, id, REQUEST=None, **kw): |
---|
| 798 | ## """Add a Semester.""" |
---|
| 799 | ## ob = Semester(id, **kw) |
---|
| 800 | ## return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
| 801 | ## |
---|
| 802 | #####) |
---|
[464] | 803 | |
---|
| 804 | class StudentCourseResult(CPSDocument): ###( |
---|
[89] | 805 | """ |
---|
[464] | 806 | WAeUP StudentCourseResult |
---|
[89] | 807 | """ |
---|
[464] | 808 | meta_type = 'StudentCourseResult' |
---|
[89] | 809 | portal_type = meta_type |
---|
| 810 | security = ClassSecurityInfo() |
---|
[472] | 811 | |
---|
[454] | 812 | def getCourseEntry(self,cid): |
---|
[723] | 813 | res = self.portal_catalog({'meta_type': "Course", |
---|
[454] | 814 | 'id': cid}) |
---|
| 815 | if res: |
---|
| 816 | return res[-1] |
---|
| 817 | else: |
---|
| 818 | return None |
---|
[154] | 819 | |
---|
[454] | 820 | security.declareProtected(View,"Title") |
---|
| 821 | def Title(self): |
---|
| 822 | """compose title""" |
---|
[723] | 823 | cid = self.aq_parent.getId() |
---|
[454] | 824 | ce = self.getCourseEntry(cid) |
---|
| 825 | if ce: |
---|
| 826 | return "%s" % ce.Title |
---|
| 827 | return "No course with id %s" % cid |
---|
[152] | 828 | |
---|
[464] | 829 | InitializeClass(StudentCourseResult) |
---|
[454] | 830 | |
---|
[464] | 831 | def addStudentCourseResult(container, id, REQUEST=None, **kw): |
---|
| 832 | """Add a StudentCourseResult.""" |
---|
| 833 | ob = StudentCourseResult(id, **kw) |
---|
[89] | 834 | return CPSBase_adder(container, ob, REQUEST=REQUEST) |
---|
[139] | 835 | ###) |
---|
| 836 | |
---|
[579] | 837 | # Backward Compatibility StudyLevel |
---|
| 838 | |
---|
| 839 | from Products.WAeUP_SRP.Academics import StudyLevel |
---|
| 840 | |
---|
| 841 | from Products.WAeUP_SRP.Academics import addStudyLevel |
---|
| 842 | |
---|