#-*- mode: python; mode: fold -*- from Globals import InitializeClass from AccessControl import ClassSecurityInfo from Products.CMFCore.utils import UniqueObject, getToolByName from Products.CMFCore.permissions import View from Products.CMFCore.permissions import ModifyPortalContent from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder #from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument from Products.CPSDocument.CPSDocument import CPSDocument #from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder #from Products.CPSCore.CPSBase import CPSBaseBTreeDocument as BaseBTreeDocument #from Products.CMFCore.DirectoryView import registerDirectory #registerDirectory('skins', globals()) #registerDirectory('skins/waeup_default', globals()) #registerDirectory('skins/waeup_faculty', globals()) import DateTime import csv,re import logging import Globals p_home = Globals.package_home(globals()) i_home = Globals.INSTANCE_HOME def makeCertificateCode(code): ###( code = code.replace('.','') code = code.replace('(','') code = code.replace(')','') code = code.replace('/','') code = code.replace(' ','') code = code.replace('_','') return code ###) class AcademicsFolder(CPSDocument): ###( """ WAeUP AcademicsFolder containing StudyCourses """ meta_type = 'AcademicsFolder' portal_type = meta_type security = ClassSecurityInfo() use_catalog_for_folder_contents = True security.declareProtected(View,"Title") def Title(self): """compose title""" return "Academic Section" def importLocalRoles(self,portal_type): ###( name = '%sLocalRoles' % portal_type logger = logging.getLogger('%s_import' % name) current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") logger.info('Start importing %(name)s to %(name)s_%(current)s.csv' % vars()) export = [] export.append('"code","users"') pm = self.portal_membership #import pdb;pdb.set_trace() try: imp_roles = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) except: logger.error('Error reading %s.csv' % name) return odict = {} for f in self.portal_catalog({'meta_type': portal_type}): odict[f.getId] = f.getObject() for role in imp_roles: obj_id = role['code'] if obj_id in odict.keys(): roles_dict = eval(role['users']) for r in roles_dict.keys(): if r.startswith('user'): member_id = r.split(':')[1] odict[obj_id].manage_setLocalRoles(member_id, roles_dict[r]) logger.info('importing role for %s %s %s' % (obj_id, member_id,roles_dict[r])) ###) security.declareProtected(ModifyPortalContent,"loadFacultiesFromCSV")###( def loadFacultiesFromCSV(self): """install Universityspecific Faculies from CSV values""" #return if self.REQUEST.get('localroles'): self.importLocalRoles('Faculty') return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) name = 'Faculties' no_import = False logger = logging.getLogger('%s_csv_import' % name) logger.info('Start loading from %s.csv' % name) academics = self.portal_catalog({'meta_type': 'AcademicsFolder'})[-1].getObject() try: faculties = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) except: logger.error('Error reading %s.csv' % name) return l = self.portal_catalog({'meta_type': "Faculty"}) facs = {} for f in l: facs[f.id] = f.getObject() fields = ("code", "review_state", "title", "title_prefix", "college_code", "degree_grade", "bank_code", ) format = ','.join(['"%%(%s)s"' % fn for fn in fields]) for faculty in faculties: processing = "processing %s" % format logger.info(processing % faculty) fid = faculty.get('code') f = facs.get(fid,None) if f is None: #self.log('Creating Faculty %(id)s = %(Title)s' % faculty) logger.info('Creating Faculty with ID %(code)s %(title)s' % faculty) academics.invokeFactory('Faculty', fid) f = getattr(self,fid) d = {'Title': faculty.get('title')} else: d = {} f.getContent().edit(mapping=faculty) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"loadDepartmentsFromCSV")###( def loadDepartmentsFromCSV(self): """install Universityspecific Faculies from CSV values""" #return if self.REQUEST.get('localroles'): self.importLocalRoles('Department') return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) name = 'Departments' no_import = False logger = logging.getLogger('loaddepartments') fields = ("code", "review_state", "title", "title_prefix", "faculty_code", ) format = ','.join(['"%%(%s)s"' % fn for fn in fields]) try: deps = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) except: logger.error('Error reading %s.csv' % name) return no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") heading = ','.join(['"%s"' % fn for fn in fields]) no_import.write('%s\n' % heading) l = self.portal_catalog({'meta_type': "Faculty"}) facs = {} for f in l: facs[f.id] = f.getObject() for dep in deps: processing = "processing %s" % format logger.info(processing % dep) fid = dep['faculty_code'] f = facs.get(fid,None) if f is None: logger.info( "No Faculty with ID: %s" % fid) no_import.write( "No Faculty with ID: %s\n" % fid) no_import.write(format % dep + "\n") else: did = dep.get('code') d = getattr(f,did,None) if d is None or d.portal_type == "Faculty": logger.info('Creating Department %(code)s = %(title)s' % dep) f.invokeFactory('Department', did) d = getattr(f,did) d.invokeFactory('CoursesFolder','courses') courses = getattr(d,'courses') dict = {'Title': 'Courses'} courses.getContent().edit(mapping=dict) d.invokeFactory('CertificatesFolder','certificates') certificates = getattr(d,'certificates') dict = {'Title': 'Certificates'} certificates.getContent().edit(mapping=dict) d.getContent().edit(mapping=dep) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"loadCoursesFromCSV")###( def loadCoursesFromCSV(self): """install Universityspecific Courses from CSV values""" #return wf = self.portal_workflow name = 'Courses' no_import = False logger = logging.getLogger('loadcourses') fields = ("code", "review_state", "title", "faculty_code", "department_code", "credits", "org_code", "passmark", "semester", "session", ) format = ','.join(['"%%(%s)s"' % fn for fn in fields]) try: courses = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) except: logger.error('Error reading %s.csv' % name) return no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") heading = ','.join(['"%s"' % fn for fn in fields]) no_import.write('%s\n' % heading) l = self.portal_catalog({'meta_type': "Faculty"}) facs = {} for f in l: facs[f.id] = f.getObject() dl = self.portal_catalog({'meta_type': "Department"}) deps = {} for d in dl: deps[d.id] = d.getObject() cl = self.portal_catalog({'meta_type': "Course"}) course_list = [ c.id for c in cl] for course in courses: processing = "processing %s" % format logger.info(processing % course) depid = course.get('department_code').upper() if depid in deps.keys(): dept= deps.get(depid) else: msg = "Department %(department_code)s for Course %(code)s not found" % course logger.info(msg) no_import.write(msg) no_import.write(format % course + "\n") continue course_id = course.get('code') cf = dept.courses c = getattr(cf,course_id,None) if c is None: logger.info('Creating Course %(code)s %(title)s in Department %(department_code)s' % course) cf.invokeFactory('Course', course_id) c = getattr(cf,course_id) c.getContent().edit(mapping=course) review_state = course.get('review_state') if review_state == "checked" and wf.getInfoFor(c,'review_state',None) != 'checked': self.portal_workflow.doActionFor(c,'approve') return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"loadCertificatesFromCSV")###( def loadCertificatesFromCSV(self): """install Universityspecific Certificates from CSV values""" #return name = 'Certificates' wf = self.portal_workflow no_import_list = [] logger = logging.getLogger('loadcertificates') fields = ("code", "review_state", "title", "faculty_code", "department_code", "category", "end_level", "m_prefix", "max_elect", "max_pass", "n_core", "nr_years", "original_code", "probation_credits", "promotion_credits", "start_level", "admin_status", ) format = ','.join(['"%%(%s)s"' % fn for fn in fields]) try: courses = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) except: logger.error('Error reading %s.csv' % name) return try: certificates = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) except: logger.error('Error reading %s.csv' % name) return f_ids = [f.id for f in self.portal_catalog({'meta_type': "Faculty"})] dl = self.portal_catalog({'meta_type': "Department"}) deps = {} for d in dl: deps[d.id] = d.getObject() for certificate in certificates: processing = "processing %s" % format logger.info(processing % certificate) depid = certificate.get('department_code') facid = certificate.get('faculty_code') if facid not in f_ids: msg = 'Faculty %(faculty_code)s for %(code)s %(title)s not found' % certificate logger.info(msg) no_import_list.append(msg) no_import_list.append(format % certificate + "\n") continue if not deps.has_key(depid): msg = 'Department %(department_code)s for %(code)s %(title)s not found' % certificate logger.info(msg) no_import_list.append(msg) no_import_list.append(format % certificate + "\n") continue dep = deps[depid] cf= dep.certificates code = certificate.get('code') certificate_id = code c = getattr(cf,certificate_id,None) if c is None: logger.info('Creating certificate %(code)s %(title)s in Department %(department_code)s' % certificate) cf.invokeFactory('Certificate', certificate_id) c = getattr(cf,certificate_id) c.getContent().edit(mapping=certificate) review_state = c.get('review_state') if review_state == "checked" and wf.getInfoFor(c,'review_state',None) != 'checked': self.portal_workflow.doActionFor(c,'approve') if no_import_list: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") heading = ','.join(['"%s"' % fn for fn in fields]) no_import.write('%s\n' % heading) for line in no_import_list: no_import.write(line) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"loadCertificateCoursesFromCSV")###( def loadCertificateCoursesFromCSV(self): """install Certificate Courses from CSV values""" #return logger = logging.getLogger('loadcertificatecourses') name = 'CertificateCourses' wf = self.portal_workflow no_import_list = [] logger = logging.getLogger('loadcertificatecourses') fields = ("code", "review_state", "faculty_code", "department_code", "certificate_code", "level", "semester", "core_or_elective", ) format = ','.join(['"%%(%s)s"' % fn for fn in fields]) try: cert_courses = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) except: logger.error('Error reading %s.csv' % name) return d_ids = [d.id for d in self.portal_catalog({'meta_type': "Department"})] c_ids = [c.id for c in self.portal_catalog({'meta_type': "Course"})] l = self.portal_catalog({'meta_type': "Certificate"}) certs = {} for f in l: ca,ac,fa,depid,co,code = f.relative_path.split('/') pid = "%(depid)s_%(code)s" % vars() certs[pid] = f.getObject() for cert_course in cert_courses: processing = "processing %s" % format logger.info(processing % cert_course) depid = cert_course.get('department_code') course_code = cert_course.get('code') code = cert_course.get('certificate_code') pid = "%(depid)s_%(code)s" % vars() if not pid in certs.keys(): em = 'CertCode %(certificate_code)s for %(code)s not found\n' % cert_course logger.info(em) #no_import_list.append(em) no_import_list.append(format % cert_course + ',"Certificate"\n') continue certificate = certs[pid] certificate_code = certificate.getId() if course_code not in c_ids: em = 'Course %(code)s for %(certificate_code)s Level %(level)s not found in Courses\n' % cert_course logger.info(em) #no_import_list.append(em) no_import_list.append(format % cert_course + ',"Course"\n') continue if depid not in d_ids: em = 'Department %(department_code)s for %(certificate_code)s not found\n' % cert_course logger.info(em) #no_import_list.append(em) no_import_list.append(format % cert_course + ',"Department"\n') continue level = cert_course.get('level') l = getattr(certificate,level,None) if l is None: logger.info('Creating Level %(level)s in certificate %(certificate_code)s' % cert_course) certificate.invokeFactory('StudyLevel', level) l = getattr(certificate, level) l.getContent().edit(mapping={'Title': "Level %s" % level}) certificate.orderObjects('id') if hasattr(l,course_code): msg = 'Duplicate %(code)s in Level %(level)s' % cert_course logger.info(msg) #no_import_list.append(msg + "\n") no_import_list.append(format % cert_course + ',"duplicate"\n') continue l.invokeFactory('CertificateCourse',course_code) logger.info('Creating CertificateCourse %(code)s in certificate %(certificate_code)s Level %(level)s' % cert_course) cc = getattr(l,course_code) semester = 'first' try: sem = int(cert_course.get('semester')) cert_course['semester'] = ('first','second')[sem - 1] except: pass cert_course['core_or_elective'] = cert_course['core_or_elective'] in ("TRUE","True") cc.getContent().edit(mapping=cert_course) review_state = cc.get('review_state') if review_state == "checked" and wf.getInfoFor(cc,'review_state',None) != 'checked': self.portal_workflow.doActionFor(cc,'approve') if no_import_list: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") heading = ','.join(['"%s"' % fn for fn in fields]) no_import.write('%s\n' % heading) for line in no_import_list: no_import.write(line) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"loadFacultiesFromOrgCSV")###( def loadFacultiesFromOrgCSV(self): """install Universityspecific Faculies from CSV values""" #return name = 'Faculties' no_import = False logger = logging.getLogger('%s_import' % name) logger.info('Start loading from %s.csv' % name) academics = self.portal_catalog({'meta_type': 'AcademicsFolder'})[-1].getObject() try: faculties = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) except: logger.error('Error reading %s.csv' % name) return l = self.portal_catalog({'meta_type': "Faculty"}) facs = {} for f in l: facs[f.id] = f.getObject() for faculty in faculties: logger.info('processing %(Session)s %(FacultyCode)s %(Description)s %(CollegeCode)s %(FacultyKey)s %(Status)s %(degree_grade)s %(Bankcode)s' % faculty) fid = faculty['FacultyCode'] f = facs.get(fid,None) if f is None: #self.log('Creating Faculty %(id)s = %(Title)s' % faculty) logger.info('Creating Faculty with ID %(FacultyCode)s %(Description)s' % faculty) academics.invokeFactory('Faculty', fid) f = getattr(self,fid) d = {'Title': faculty['Description']} else: d = {} d['bank_code'] = faculty.get("Bankcode") d["degree_grade"] = faculty.get("degree_grade") d['institution_type'] = 'faculty' d['Description'] = '' d['college_code'] = faculty.get('CollegeCode') f.getContent().edit(mapping=d) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"yamlDumpFaculties")###( def yamlDumpFaculties(self): """dump Faculies to Yaml""" #return import yaml logger = logging.getLogger('dumpfaculties') logger.info('Start dumping Faculties') academics = self.portal_catalog({'meta_type': 'AcademicsFolder'})[-1].getObject() l = self.portal_catalog({'meta_type': "Faculty"}) facs = {} for f in l: facs[f.id] = f.getObject() for fid in facs.keys(): faculty = facs.get(fid).aq_self logger.info('dumping %s %s ' % (faculty.id, faculty.title)) print yaml.dump(faculty) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"loadDepartmentsFromOrgCSV")###( def loadDepartmentsFromOrgCSV(self): """install Universityspecific Faculies from CSV values""" #return name = 'Departments' no_import = False logger = logging.getLogger('loaddepartments') try: deps = csv.DictReader(open("%s/import/departments.csv" % i_home,"rb")) except: logger.error('Error reading departments.csv') return l = self.portal_catalog({'meta_type': "Faculty"}) facs = {} for f in l: facs[f.id] = f.getObject() for dep in deps: logger.info('Processing %(Session)s %(DeptCode)s %(Description)s %(FacultyCode)s' % dep) fid = dep['FacultyCode'] f = facs.get(fid,None) if f is None: logger.info( "No Faculty with ID: %s" % fid) if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") no_import.write('"Session","DeptCode","Description","FacultyCode"\n') no_import.write( "No Faculty with ID: %s\n" % fid) no_import.write('"%(Session)s","%(DeptCode)s","%(Description)s","%(FacultyCode)s"\n' % dep) else: did = dep.get('DeptCode') d = getattr(f,did,None) if d is None or d.portal_type == "Faculty": logger.info('Creating Department %(DeptCode)s = %(Description)s' % dep) f.invokeFactory('Department', did) d = getattr(f,did) dict = {'Title': dep['Description']} d.getContent().edit(mapping=dict) d.invokeFactory('CoursesFolder','courses') courses = getattr(d,'courses') dict = {'Title': 'Courses'} courses.getContent().edit(mapping=dict) d.invokeFactory('CertificatesFolder','certificates') certificates = getattr(d,'certificates') dict = {'Title': 'Certificates'} certificates.getContent().edit(mapping=dict) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"loadCoursesFromOrgCSV")###( def loadCoursesFromOrgCSV(self): """install Universityspecific Courses from CSV values""" #return name = 'Courses' no_import = False logger = logging.getLogger('loadcourses') try: courses = csv.DictReader(open("%s/import/courses.csv" % i_home,"rb")) except: logger.error('Error reading courses.csv') return l = self.portal_catalog({'meta_type': "Faculty"}) facs = {} for f in l: facs[f.id] = f.getObject() dl = self.portal_catalog({'meta_type': "Department"}) deps = {} for d in dl: deps[d.id] = d.getObject() cl = self.portal_catalog({'meta_type': "Course"}) course_list = [ c.id for c in cl] for course in courses: logger.info('Processing %(CourseCode)s %(Description)s %(Credits)s %(Dept)s %(Semester)s %(Session)s %(PassMark)s %(Category)s %(AdmStatus)s' % course) ## if course.get("FORMERCODE").endswith('BITS'): ## continue depid = course.get('Dept').upper() if depid in deps.keys(): dept= deps.get(depid) ## elif depid in facs.keys(): ## dept= facs.get(depid) else: logger.info("Dep %(Dept)s for Course %(CourseCode)s not found" % course) if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") no_import.write('"CourseCode","Description","Credits","Dept","Semester","Session","PassMark","Category","AdmStatus"\n') no_import.write("Dep %(Dept)s for Course %(CourseCode)s not found\n" % course) no_import.write('"%(CourseCode)s","%(Description)s","%(Credits)s","%(Dept)s","%(Semester)s","%(Session)s","%(PassMark)s","%(Category)s","%(AdmStatus)s"\n' % course) continue course_id = ''.join(re.split('\W+',course.get('CourseCode'))) if len(course_id) == 3: course_id = "%s000" % course_id elif len(course_id) > 10: logger.info("invalid course_code %(CourseCode)s" % course) if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") no_import.write('"CourseCode","Description","Credits","Dept","Semester","Session","PassMark","Category","AdmStatus"\n') no_import.write("invalid course_code %(CourseCode)s\n" % course) no_import.write('"%(CourseCode)s","%(Description)s","%(Credits)s","%(Dept)s","%(Semester)s","%(Session)s","%(PassMark)s","%(Category)s","%(AdmStatus)s"\n' % course) continue courses = dept.courses c = getattr(courses,course_id,None) if c is None: logger.info('Creating Course %(CourseCode)s %(Description)s in Department %(Dept)s' % course) courses.invokeFactory('Course', course_id) c = getattr(courses,course_id) dict = {'Title': course['Description']} dict['code'] = course_id dict['org_code'] = course.get('CourseCode') dict['credits'] = course.get('Credits') dict['semester'] = course.get('Semester') dict['session'] = course.get('Session') dict['category'] = course.get('Category') dict['adm_status'] = course.get('AdmStatus') dict['former_code'] = course.get('FORMERCODE') pm = course.get('PassMark') if pm.find(',') > -1: pm.replace(',','.') elif pm == "": pm = "0.0" try: dict['passmark'] = int(float(pm)) except: dict['passmark'] = 0 c.getContent().edit(mapping=dict) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"loadCertificatesFromOrgCSV")###( def loadCertificatesFromOrgCSV(self): """install Universityspecific Certificates from CSV values""" #return name = 'Certificates' no_import = False logger = logging.getLogger('loadcertificates') try: certificates = csv.DictReader(open("%s/import/certificates.csv" % i_home,"rb")) except: logger.error('Error reading certificates.csv') return f_ids = [f.id for f in self.portal_catalog({'meta_type': "Faculty"})] #d_ids = [d.id for d in self.portal_catalog({'meta_type': "Department"})] dl = self.portal_catalog({'meta_type': "Department"}) deps = {} for d in dl: deps[d.id] = d.getObject() for certificate in certificates: logger.info('Processing %(CertCode)s %(Description)s %(Faculty)s %(MaxPass)s %(MaxLoad)s %(session)s %(PromotionCredits)s %(Probationcredits)s %(StartLevel)s %(endLevel)s %(Nyears)s %(Ncore)s %(MaxElect)s %(MPREFIX)s %(Dept)s %(Admstatus)s %(category)s' % certificate) depid = certificate.get('Dept') facid = certificate.get('Faculty') if facid not in f_ids: logger.info('Faculty %(Faculty)s for %(CertCode)s %(Description)s not found' % certificate) if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") no_import.write('"CertCode","Description","Faculty","MaxPass","MaxLoad","session","PromotionCredits","Probationcredits","StartLevel","endLevel","Nyears","Ncore","MaxElect","MPREFIX","Dept","Admstatus","category"\n') no_import.write('Faculty %(Faculty)s for %(CertCode)s %(Description)s not found\n' % certificate) no_import.write('"%(CertCode)s","%(Description)s","%(Faculty)s","%(MaxPass)s","%(MaxLoad)s","%(session)s","%(PromotionCredits)s","%(Probationcredits)s","%(StartLevel)s","%(endLevel)s","%(Nyears)s","%(Ncore)s","%(MaxElect)s","%(MPREFIX)s","%(Dept)s","%(Admstatus)s","%(category)s"\n' % certificate) continue if not deps.has_key(depid): logger.info('Department %(Dept)s for %(CertCode)s %(Description)s not found' % certificate) if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") no_import.write('"CertCode","Description","Faculty","MaxPass","MaxLoad","session","PromotionCredits","Probationcredits","StartLevel","endLevel","Nyears","Ncore","MaxElect","MPREFIX","Dept","Admstatus","category"\n') no_import.write('Department %(Dept)s for %(CertCode)s %(Description)s not found\n' % certificate) no_import.write('"%(CertCode)s","%(Description)s","%(Faculty)s","%(MaxPass)s","%(MaxLoad)s","%(session)s","%(PromotionCredits)s","%(Probationcredits)s","%(StartLevel)s","%(endLevel)s","%(Nyears)s","%(Ncore)s","%(MaxElect)s","%(MPREFIX)s","%(Dept)s","%(Admstatus)s","%(category)s"\n' % certificate) continue #certificate_id = "%(category)s_%(Admstatus)s_%(Dept)s" % certificate dep = deps[depid] certificates = dep.certificates code = makeCertificateCode(certificate.get('CertCode')) certificate_id = code c = getattr(certificates,certificate_id,None) if c is None: #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep) logger.info('Creating certificate %(CertCode)s %(Description)s in Department %(Dept)s' % certificate) certificates.invokeFactory('Certificate', certificate_id) c = getattr(certificates,certificate_id) dict = {'Title': certificate['Description']} dict['code'] = code dict['faculty'] = certificate.get('Faculty') dict['department'] = certificate.get('Dept') dict['max_pass'] = certificate.get('MaxPass') dict['max_load'] = certificate.get('MaxLoad') dict['admin_status'] = certificate.get('Admstatus') dict['category'] = certificate.get('category') dict['m_prefix'] = certificate.get('MPREFIX') dict['nr_years'] = int(certificate.get('Nyears')) nc = certificate.get('Ncore','1') try: dict['n_core'] = int(nc) except: dict['n_core'] = 1 dict['start_level'] = certificate.get('StartLevel') dict['end_level'] = certificate.get('endLevel') dict['promotion_credits'] = certificate.get('PromotionCredits') dict['probation_credits'] = certificate.get('ProbationCredits') else: dict = {} dict['original_code'] = certificate.get('CertCode') print c.getContent().edit(mapping=dict) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"loadCertificateCoursesFromOrgCSV")###( def loadCertificateCoursesFromOrgCSV(self): """install Certificate Courses from CSV values""" #return logger = logging.getLogger('loadcertificatecourses') name = 'Certificate_courses' no_import = False try: cert_courses = csv.DictReader(open("%s/import/course_level_courses.csv" % i_home,"rb")) except: logger.error('Error reading course_level_courses.csv') return d_ids = [d.id for d in self.portal_catalog({'meta_type': "Department"})] c_ids = [c.id for c in self.portal_catalog({'meta_type': "Course"})] l = self.portal_catalog({'meta_type': "Certificate"}) certs = {} for f in l: certs[f.id] = f.getObject() for cert_course in cert_courses: logger.info('Processing %(CosCode)s %(CertCode)s %(Session)s %(Level)s %(Core)s %(Elective)s %(Mandatory)s %(AdmStatus)s %(Dept)s %(Semester)s' % cert_course) depid = cert_course.get('Dept') course_code = cert_course.get('CosCode') code = cert_course.get('CertCode') code = code.replace('.','') code = code.replace('(','') code = code.replace(')','') code = code.replace('/','') code = code.replace(' ','') code = code.replace('_','') ## if cert_course.get('Session') != '2002/2003': ## continue ## certificate = self.portal_catalog({'meta_type': "Certificate", ## 'SearchableText': code}) ## if not certificate: if not code in certs.keys(): #print code em = 'CertCode %(CertCode)s for %(CosCode)s not found\n' % cert_course logger.info(em) if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") no_import.write('"CosCode","CertCode","Session","Level","Core","Elective","Mandatory","AdmStatus","Dept","Semester"\n') no_import.write(em) no_import.write('"%(CosCode)s","%(CertCode)s","%(Session)s","%(Level)s","%(Core)s","%(Elective)s","%(Mandatory)s","%(AdmStatus)s","%(Dept)s","%(Semester)s"\n' % cert_course) continue certificate = certs[code] certificate_code = certificate.getId() if course_code not in c_ids: em = 'CorseCode %(CosCode)s for %(CertCode)s not found in Courses\n' % cert_course logger.info(em) if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") no_import.write('"CosCode","CertCode","Session","Level","Core","Elective","Mandatory","AdmStatus","Dept","Semester"\n') no_import.write(em) no_import.write('"%(CosCode)s","%(CertCode)s","%(Session)s","%(Level)s","%(Core)s","%(Elective)s","%(Mandatory)s","%(AdmStatus)s","%(Dept)s","%(Semester)s"\n' % cert_course) continue if depid not in d_ids: em = 'Department %(Dept)s for %(CertCode)s not found\n' % cert_course logger.info(em) if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") no_import.write('"CosCode","CertCode","Session","Level","Core","Elective","Mandatory","AdmStatus","Dept","Semester"\n') no_import.write(em) no_import.write('"%(CosCode)s","%(CertCode)s","%(Session)s","%(Level)s","%(Core)s","%(Elective)s","%(Mandatory)s","%(AdmStatus)s","%(Dept)s","%(Semester)s"\n' % cert_course) continue level = cert_course.get('Level') l = getattr(certificate,level,None) if l is None: #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep) logger.info('Creating Level %(Level)s in certificate %(CertCode)s' % cert_course) certificate.invokeFactory('StudyLevel', level) l = getattr(certificate, level) l.getContent().edit(mapping={'Title': "Level %s" % level}) l.invokeFactory('Semester','first') l.invokeFactory('Semester','second') certificate.orderObjects('id') first_s = getattr(l,'first') first_s.getContent().edit(mapping={'Title': 'First Semester'}) second_s = getattr(l,'second') second_s.getContent().edit(mapping={'Title': 'Second Semester'}) if cert_course.get('Semester') == '1': semester = first_s else: semester = second_s if hasattr(semester,course_code): logger.info('Duplicate %(CosCode)s in Level %(Level)s' % cert_course) if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") no_import.write('"CosCode","CertCode","Session","Level","Core","Elective","Mandatory","AdmStatus","Dept","Semester"\n') ## no_import.write('Duplicate %(CosCode)s in Level %(Level)s' % cert_course) ## no_import.write('"%(CosCode)s","%(CertCode)s","%(Session)s","%(Level)s","%(Core)s","%(Elective)s","%(Mandatory)s","%(AdmStatus)s","%(Dept)s","%(Semester)s"\n' % cert_course) continue semester.invokeFactory('CertificateCourse',course_code) cc = getattr(semester,course_code) dict = {} dict['code'] = cert_course.get('CosCode') dict['certificate_code'] = code dict['certificate_code_org'] = cert_course.get('CertCode') dict['department'] = cert_course.get('Dept') dict['admin_status'] = cert_course.get('Admstatus') dict['session'] = cert_course.get('Session') if cert_course.get('Core') != '': dict['core_or_elective'] = True else: dict['core_or_elective'] = False dict['level'] = cert_course.get('Level') cc.getContent().edit(mapping=dict) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) def exportLocalRoles(self,portal_type): ###( name = '%sLocalRoles' % portal_type logger = logging.getLogger('%s_export' % name) current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") logger.info('Start exporting %(name)s to %(name)s_%(current)s.csv' % vars()) objects = [f.getObject() for f in self.portal_catalog({'meta_type': portal_type})] export = [] export.append('"code","users"') #import pdb;pdb.set_trace() for obj in objects: lr = {} for (username, roles) in obj.get_local_roles(): lr[ 'user:' + username ] = [x for x in roles] for (groupname, roles) in obj.get_local_group_roles(): lr[ 'group:' + group ] = [x for x in roles] logger.info('exporting %s %s ' % (obj.id, lr)) export.append('"%s","%s"' % (obj.getId(),lr)) open("%s/import/%s-%s.csv" % (i_home,name,current),"w+").write('\n'.join(export)) ###) security.declareProtected(ModifyPortalContent,"exportFacultiesToCSV")###( def exportFacultiesToCSV(self): """export Faculies to CSV""" #return if self.REQUEST.get('localroles'): self.exportLocalRoles('Faculty') return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) name = 'Faculties' no_import = False logger = logging.getLogger('%s_export' % name) current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") logger.info('Start exporting %(name)s to %(name)s_%(current)s.csv' % vars()) objects = [f.getObject() for f in self.portal_catalog({'meta_type': 'Faculty'})] export = [] export.append('"code","review_state","title","title_prefix","college_code","degree_grade","bank_code"') for obj in objects: logger.info('exporting %s %s ' % (obj.id, obj.title)) obj_d = obj.getContent() di = {} di['code'] = obj_d.code di['title'] = obj_d.title di['title_prefix'] = obj_d.title_prefix di['college_code'] = obj_d.college_code di['degree_grade'] = obj_d.degree_grade di['bank_code'] = obj_d.bank_code di['code'] = di['code'] or obj.id di['review_state'] = self.portal_workflow.getInfoFor(obj,'review_state','no_state') export.append('"%(code)s","%(review_state)s","%(title)s","%(title_prefix)s","%(college_code)s","%(degree_grade)s","%(bank_code)s"' % di) open("%s/import/%s-%s.csv" % (i_home,name,current),"w+").write('\n'.join(export)) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"exportDepartmentsToCSV")###( def exportDepartmentsToCSV(self): """export Faculies to CSV""" #return if self.REQUEST.get('localroles'): self.exportLocalRoles('Department') return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) name = 'Departments' no_import = False logger = logging.getLogger('%s_export' % name) current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") logger.info('Start exporting %(name)s to %(name)s_%(current)s.csv' % vars()) faculties = [f.getObject() for f in self.portal_catalog({'meta_type': 'Faculty'})] export = [] export.append('"code","review_state","title","title_prefix","faculty_code"') #import pdb;pdb.set_trace() for faculty in faculties: di = {} di['faculty_code'] = faculty.id for department in faculty.objectValues(): department_d = department.getContent() try: di['code'] = department.getId() di['title'] = department_d.title di['title_prefix'] = department_d.title_prefix di['review_state'] = self.portal_workflow.getInfoFor(department,'review_state','no_state') export.append('"%(code)s","%(review_state)s","%(title)s","%(title_prefix)s","%(faculty_code)s"' % di) logger.info('exporting %s %s ' % (department.id, department.title)) except: logger.info('could not export %s %s ' % (department.id, department.title)) continue open("%s/import/%s-%s.csv" % (i_home,name,current),"w+").write('\n'.join(export)) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"exportCoursesToCSV")###( def exportCoursesToCSV(self): """export Courses to CSV""" #return name = 'Courses' no_import = False logger = logging.getLogger('%s_export' % name) current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") logger.info('Start exporting %(name)s to %(name)s_%(current)s.csv' % vars()) faculties = [f.getObject() for f in self.portal_catalog({'meta_type': 'Faculty'})] export = [] heading = '"code","review_state","title","faculty_code","department_code",' heading += '"credits","org_code","passmark","semester","session"' dataline = '"%(code)s","%(review_state)s","%(title)s","%(faculty_code)s","%(department_code)s",' dataline += '"%(credits)s","%(org_code)s","%(passmark)s","%(semester)s","%(session)s"' export.append(heading) #import pdb;pdb.set_trace() fields = ("title", "credits", "org_code", "passmark", "semester", "session", ) for faculty in faculties: di = {} di['faculty_code'] = faculty.id for department in faculty.objectValues(): di['department_code'] = department.id for course in department.courses.objectValues(): di['code'] = course.getId() course_d = course.getContent() for f in fields: di[f] = getattr(course_d,f,None) di['review_state'] = self.portal_workflow.getInfoFor(course,'review_state','no_state') export.append(dataline % di) logger.info('exporting %s %s ' % (course.id, course.title)) continue open("%s/import/%s-%s.csv" % (i_home,name,current),"w+").write('\n'.join(export)) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"exportCertificatesToCSV")###( def exportCertificatesToCSV(self): """export Certificates to CSV""" #return name = 'Certificates' no_import = False logger = logging.getLogger('%s_export' % name) current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") logger.info('Start exporting %(name)s to %(name)s_%(current)s.csv' % vars()) faculties = [f.getObject() for f in self.portal_catalog({'meta_type': 'Faculty'})] export = [] heading = '"code","review_state","title","faculty_code","department_code",' heading += '"category","end_level","m_prefix","max_elect","max_pass",' heading += '"n_core","nr_years","original_code","probation_credits",' heading += '"promotion_credits","start_level","admin_status"' dataline = '"%(code)s","%(review_state)s","%(title)s","%(faculty_code)s","%(department_code)s",' dataline += '"%(category)s","%(end_level)s","%(m_prefix)s","%(max_elect)s","%(max_pass)s",' dataline += '"%(n_core)s","%(nr_years)s","%(original_code)s","%(probation_credits)s",' dataline += '"%(promotion_credits)s","%(start_level)s","%(admin_status)s"' export.append(heading) #import pdb;pdb.set_trace() for faculty in faculties: di = {} di['faculty_code'] = faculty.id for department in faculty.objectValues(): di['department_code'] = department.id for certificate in department.certificates.objectValues(): certificate_d = certificate.getContent() di['code'] = certificate.id #di['code'] = getattr(certificate_d,'code',None) di['title'] = getattr(certificate_d,'title',None) di['category'] = getattr(certificate_d,'category',None) di['admin_status'] = getattr(certificate_d,'admin_status',None) di['end_level'] = getattr(certificate_d,'end_level',None) di['m_prefix'] = getattr(certificate_d,'m_prefix',None) di['max_elect'] = getattr(certificate_d,'max_elect',None) di['max_load'] = getattr(certificate_d,'max_load',None) di['max_pass'] = getattr(certificate_d,'max_pass',None) di['n_core'] = getattr(certificate_d,'n_core',None) di['nr_years'] = getattr(certificate_d,'nr_years',None) di['original_code'] = getattr(certificate_d,'original_code',None) di['probation_credits'] = getattr(certificate_d,'probation_credits',None) di['promotion_credits'] = getattr(certificate_d,'promotion_credits',None) di['start_level'] = getattr(certificate_d,'start_level',None) di['code'] = di['code'] or certificate.id di['review_state'] = self.portal_workflow.getInfoFor(certificate,'review_state','no_state') try: export.append(dataline % di) logger.info('exporting %s %s ' % (certificate.id, certificate.title)) except: logger.info('could not export %s %s ' % (certificate.id, certificate.title)) continue open("%s/import/%s-%s.csv" % (i_home,name,current),"w+").write('\n'.join(export)) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) security.declareProtected(ModifyPortalContent,"exportCertificateCoursesToCSV")###( def exportCertificateCoursesToCSV(self): """export CertificateCourses to CSV""" #return name = 'CertificateCourses' no_import = False logger = logging.getLogger('%s_export' % name) current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") logger.info('Start exporting %(name)s to %(name)s_%(current)s.csv' % vars()) faculties = [f.getObject() for f in self.portal_catalog({'meta_type': 'Faculty'})] export = [] heading = '"code","review_state","faculty_code","department_code",' heading += '"certificate_code","level","semester","core_or_elective"' dataline = '"%(code)s","%(review_state)s","%(faculty_code)s","%(department_code)s",' dataline += '"%(certificate_code)s","%(level)s","%(semester)s","%(core_or_elective)s"' export.append(heading) #import pdb;pdb.set_trace() semesters = ['first','second'] for faculty in faculties: di = {} di['faculty_code'] = faculty.id for department in faculty.objectValues(): di['department_code'] = department.id for certificate in department.certificates.objectValues(): di['certificate_code'] = certificate.id for level in certificate.objectValues(): di['level'] = level.id for sem in semesters: semester = getattr(level,sem,None) if semester is not None: di['semester'] = semesters.index(sem) + 1 for course in semester.objectValues(): di['code'] = course.getId() course_d = course.getContent() di['core_or_elective'] = getattr(course_d,'core_or_elective',None) di['review_state'] = self.portal_workflow.getInfoFor(course,'review_state','no_state') try: export.append(dataline % di) logger.info('exporting %s %s ' % (certificate.id, certificate.title)) except: logger.info('could not export %s %s ' % (certificate.id, certificate.title)) continue open("%s/import/%s-%s.csv" % (i_home,name,current),"w+").write('\n'.join(export)) return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1')) ###) InitializeClass(AcademicsFolder) def addAcademicsFolder(container, id, REQUEST=None, **kw): """Add a AcademicsFolder.""" ob = AcademicsFolder(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class Certificate(CPSDocument): ###( """ WAeUP Certificate """ meta_type = 'Certificate' portal_type = meta_type security = ClassSecurityInfo() def __init__(self, id, **kw): CPSDocument.__init__(self, id, **kw) ## security.declareProtected(View,"Title") ## def Title(self): ## """compose title""" ## return "Certificate of %s" % (self.title) InitializeClass(Certificate) def addCertificate(container, id, REQUEST=None, **kw): """Add a Certificate.""" ob = Certificate(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class StudyLevel(CPSDocument): ###( """ WAeUP StudyLevel containing the courses and students """ meta_type = 'StudyLevel' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") def Title(self): """compose title""" try: return "Level %s" % self.aq_parent.getId() except: return "no Title for %s" % self.getId() InitializeClass(StudyLevel) def addStudyLevel(container, id, REQUEST=None, **kw): """Add a StudyLevel.""" ob = StudyLevel(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class CertificateCourse(CPSDocument): ###( """ WAeUP CertificateCourse """ meta_type = 'CertificateCourse' portal_type = meta_type security = ClassSecurityInfo() def getCourseEntry(self,cid): res = self.portal_catalog({'meta_type': "Course", 'id': cid}) if res: return res[-1] else: return None security.declareProtected(View,"Title") def Title(self): """compose title""" #import pdb;pdb.set_trace() ce = self.getCourseEntry(self.aq_parent.getId()) #print self.id, self.aq_parent.id if ce: return "%s" % ce.Title return "No such course" security.declareProtected(View,"credits") def credits(self): """credits from course""" ce = self.getCourseEntry(self.id) if ce: return "%s" % ce.credits return "0" security.declareProtected(View,"passmark") def passmark(self): """passmark from course""" ce = self.getCourseEntry(self.id) if ce is not None and hasattr(ce,"passmark"): return ce.passmark security.declareProtected(View,"coursepath") def coursepath(self): """coursepath from course""" ce = self.getCourseEntry(self.id) if ce: return ce.getPath() return "?" InitializeClass(CertificateCourse) def addCertificateCourse(container, id, REQUEST=None, **kw): """Add a CertificateCourse.""" ob = CertificateCourse(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class Faculty(CPSDocument): ###( """ WAeUP Faculty containing Departments """ meta_type = 'Faculty' portal_type = meta_type security = ClassSecurityInfo() ## def __init__(self, id, **kw): ## CPSDocument.__init__(self, id, **kw) security.declareProtected(View,"Title") def Title(self): """compose title""" return "%s" % (self.title) security.declareProtected(View,"LongTitle") def LongTitle(self): """compose long_title""" prefix = getattr(self,'title_prefix',None) if prefix is None: prefix = getattr(self,'institution_type','faculty') self.getContent().edit(mapping = {'title_prefix': prefix}) itype = self.portal_vocabularies.institution_types_voc.get(prefix,default="Faculty of") return "%s %s" % (itype,self.title) InitializeClass(Faculty) def addFaculty(container, id, REQUEST=None, **kw): """Add a Faculty.""" ob = Faculty(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class Department(CPSDocument): ###( """ WAeUP Department containing the courses and students """ meta_type = 'Department' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"LongTitle") def LongTitle(self): """compose long_title""" prefix = getattr(self,'title_prefix',None) if prefix is None: prefix = getattr(self,'institution_type','department') self.getContent().edit(mapping = {'title_prefix': prefix}) itype = self.portal_vocabularies.institution_types_voc.get(prefix,default="Department of") return "%s %s" % (itype,self.title) InitializeClass(Department) def addDepartment(container, id, REQUEST=None, **kw): """Add a Department.""" object = Department(id, **kw) id = object.getId() container._setObject(id, object) ## dep = getattr(container,id).getEditableContent() #getContent() ## dep.invokeFactory('CoursesFolder','Courses') ## o = getattr(dep,'Courses') ## dict = {'Title': 'Courses'} ## o.getContent().edit(mapping=dict) ## dep.invokeFactory('CertificatesFolder','Certificates') ## o = getattr(dep,'Certificates') ## dict = {'Title': 'Certificates'} ## o.geetContent().edit(mapping=dict) if REQUEST is not None: url = container.absolute_url() REQUEST.RESPONSE.redirect('%s/manage_main' % url) ###) class Course(CPSDocument): ###( """ WAeUP Course """ meta_type = 'Course' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") def Title(self): """compose title""" return self.title InitializeClass(Course) def addCourse(container, id, REQUEST=None, **kw): """Add a Course.""" ob = Course(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###)