#-*- 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 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() security.declareProtected(View,"Title") def Title(self): """compose title""" return "Academic Section" security.declareProtected(ModifyPortalContent,"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']} else: d = {} ## 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) 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.academics.academics_contents() ###) 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": 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.academics.academics_contents() ###) 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 %(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.academics.academics_contents() ###) 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] 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.academics.academics_contents() ###) 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"})] 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.academics.academics_contents() ###) 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.upper()) 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""" itype = self.portal_vocabularies.institution_types_voc.get(self.institution_type,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,"Title") ## def Title(self): ## """compose title""" ## reg_nr = self.getId()[1:] ## return "Department of %s" % (self.title) security.declareProtected(View,"LongTitle") def LongTitle(self): """compose long_title""" if not hasattr(self,'institution_type'): self.institution_type = "department" itype = self.portal_vocabularies.institution_types_voc.get(self.institution_type,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) ###) 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) ###)