#-*- 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 csv,re import logging import Globals p_home = Globals.package_home(globals()) i_home = Globals.INSTANCE_HOME class AcademicsFolder(CPSDocument): ###( """ WAeUP AcademicsFolder containing StudyCourses """ meta_type = 'AcademicsFolder' portal_type = meta_type security = ClassSecurityInfo() ## security.declareProtected(View,"Title") ## def Title(self): ## """compose title""" ## return "AcademicsFolder of %s" % (self.title) security.declareProtected(View,"loadFacultiesFromCSV")###( def loadFacultiesFromCSV(self): """install Universityspecific Faculies from CSV values""" #return name = 'faculty' 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']} f.getContent().edit(mapping=d) else: if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") no_import.write('"Session","FacultyCode","Description","CollegeCode","FacultyKey","Status","degree_grade","Bankcode"\n') logger.info('Faculty with ID %(FacultyCode)s %(Description)s already exists' % faculty) no_import.write('"%(Session)s","%(FacultyCode)s","%(Description)s","%(CollegeCode)s","%(FacultyKey)s","%(Status)s","%(degree_grade)s","%(Bankcode)s"\n' % faculty) return self.getContent().temporary_view_all() ###) 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.academics.temporary_view_all() return self.temporary_view_all() ###) security.declareProtected(ModifyPortalContent,"loadDepartmentsFromCSV")###( def loadDepartmentsFromCSV(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": #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep) 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) return self.academics.temporary_view_all() return self.temporary_view_all() ###) security.declareProtected(ModifyPortalContent,"loadCoursesFromCSV")###( def loadCoursesFromCSV(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 %(CourseKey)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","CourseKey","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","%(CourseKey)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 course_id.startswith('CHEM'): ## tid = course_id.replace('CHEM','CHE') ## logger.info("invalid course_code %(course_id)s renaming to %(tid)s" % vars()) ## course_id = tid ## elif course_id.startswith('DAIC'): ## tid = course_id.replace('DAIC','DAC') ## logger.info("invalid course_code %(course_id)s renaming to %(tid)s" % vars()) ## course_id = tid ## elif course_id.startswith('DAIM'): ## tid = course_id.replace('DAIM','DAM') ## logger.info("invalid course_code %(course_id)s renaming to %(tid)s" % vars()) ## course_id = tid elif len(course_id) != 6: 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","CourseKey","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","%(CourseKey)s","%(Category)s","%(AdmStatus)s"\n' % course) continue c = getattr(dept,course_id,None) if c is None: logger.info('Creating Course %(CourseCode)s %(Description)s in Department %(Dept)s' % course) dept.invokeFactory('Course', course_id) c = getattr(dept,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') 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.academics.temporary_view_all() return self.temporary_view_all() ###) security.declareProtected(ModifyPortalContent,"loadCertificatesFromCSV")###( def loadCertificatesFromCSV(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] c = getattr(dep,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) dep.invokeFactory('Certificate', certificate_id) c = getattr(dep,certificate_id) dict = {'Title': certificate['Description']} code = certificate.get('CertCode') code = code.replace('.','') code = code.replace('(','') code = code.replace(')','') code = code.replace('/','') code = code.replace(' ','') code = code.replace('_','') 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') c.getContent().edit(mapping=dict) return self.academics.temporary_view_all() return self.temporary_view_all() ###) security.declareProtected(ModifyPortalContent,"loadCertificateCoursesFromCSV")###( def loadCertificateCoursesFromCSV(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"})] for cert_course in cert_courses: logger.info('Processing %(CosCode)s %(CertCode)s %(CoreKey)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: #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","CoreKey","Session","Level","Core","Elective","Mandatory","AdmStatus","Dept","Semester"\n') no_import.write(em) no_import.write('"%(CosCode)s","%(CertCode)s","%(CoreKey)s","%(Session)s","%(Level)s","%(Core)s","%(Elective)s","%(Mandatory)s","%(AdmStatus)s","%(Dept)s","%(Semester)s"\n' % cert_course) continue certificate = certificate[-1].getObject() 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","CoreKey","Session","Level","Core","Elective","Mandatory","AdmStatus","Dept","Semester"\n') no_import.write(em) no_import.write('"%(CosCode)s","%(CertCode)s","%(CoreKey)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","CoreKey","Session","Level","Core","Elective","Mandatory","AdmStatus","Dept","Semester"\n') no_import.write(em) no_import.write('"%(CosCode)s","%(CertCode)s","%(CoreKey)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.invokeFactory('Semester','first') l.invokeFactory('Semester','second') certificate.orderObjects('id') first_s = getattr(l,'first') second_s = getattr(l,'second') 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","CoreKey","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","%(CoreKey)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.academics.edit(mapping=dict) ###) 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 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""" ce = self.getCourseEntry(self.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) 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) 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,"Title") ## def Title(self): ## """compose title""" ## reg_nr = self.getId()[1:] ## return "Department of %s" % (self.title) InitializeClass(Department) def addDepartment(container, id, REQUEST=None, **kw): """Add a Department.""" ob = Department(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) 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) ###) class CourseTicket(CPSDocument): ###( """ WAeUP CourseTicket """ meta_type = 'CourseTicket' portal_type = meta_type security = ClassSecurityInfo() InitializeClass(CourseTicket) def addCourseTicket(container, id, REQUEST=None, **kw): """Add a CourseTicket.""" ob = CourseTicket(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###)