#-*- 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('Academics.AcademicsFolder.importLocalRoles') 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('Academics.AcademicsFolder.loadFacultiesFromCSV') 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('Academics.AcademicsFolder.loadDepartmentsFromCSV') 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 not_allowed =re.compile('[^A-Z0-9]') wf = self.portal_workflow name = 'Courses' no_import = False logger = logging.getLogger('Academics.AcademicsFolder.loadCoursesFromCSV') 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 imported = [] not_imported = [] heading = ','.join(['"%s"' % fn for fn in fields]) imported.append(heading) not_imported.append(heading) #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) course_id = course.get('code') if not_allowed.search(course_id): msg = ',"Invalid Course Code"' #logger.info(msg) #not_imported.append('"%s",' % msg) not_imported.append(format % course + msg) continue depid = course.get('department_code').upper() if depid in deps.keys(): dept= deps.get(depid) else: msg = ',"Department not found"' #logger.info(msg) #not_imported.append('"%s",' % msg) not_imported.append(format % course + msg) continue if course_id in course_list: msg = ',"Multiple Course Code"' #logger.info(msg) #not_imported.append('"%s",' % msg) not_imported.append(format % course + msg) continue level = "%c00" % course_id[-3] course['level'] = level cf = dept.courses c = getattr(cf,course_id,None) #cd_fields = (('code','code'), # ('faculty','faculty_code'), # ('department','department_code'), # ('title','title'), # ('credits','credits'), # ('level','level'), # ('semester','semester'), # ) #cd = {} #for t,f in cd_fields: # cd[t] = course[f] #import pdb;pdb.set_trace() if c is None: logger.info('Creating Course %(code)s %(title)s in Department %(department_code)s' % course) cf.invokeFactory('Course', course_id) course_list.append(course_id) imported.append(format % course) c = getattr(cf,course_id) #self.courses_catalog.addRecord(**cd) #else: # self.courses_catalog.modifyRecord(**cd) 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') current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S") if len(not_imported) > 1: open("%s/import/%s_%s_not_imported.csv" % (i_home,name,current),"w")\ .write('\n'.join(not_imported)) open("%s/import/%s_%s_imported.csv" % (i_home,name,current),"w")\ .write('\n'.join(imported)) 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('Academics.AcademicsFolder.loadCertificatesFromCSV') 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('Academics.AcademicsFolder.loadCertificateCoursesFromCSV') name = 'CertificateCourses' wf = self.portal_workflow no_import_list = [] 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() pid = "%(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() pid = "%(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 StudyLevel %(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,"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')) ###) def exportLocalRoles(self,portal_type): ###( name = '%sLocalRoles' % portal_type logger = logging.getLogger('Academics.AcademicsFolder.exportLocalRoles') 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/export/%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('Academics.AcademicsFolder.exportFacultiesToCSV') 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/export/%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('Academics.AcademicsFolder.exportDepartmentsToCSV') 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/export/%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('Academics.AcademicsFolder.exportCoursesToCSV') 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/export/%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('Academics.AcademicsFolder.exportCertificatesToCSV') 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['study_mode'] = getattr(certificate_d,'study_mode',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/export/%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('Academics.AcademicsFolder.exportCertificateCoursesToCSV') 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","core_or_elective"' dataline = '"%(code)s","%(review_state)s","%(faculty_code)s","%(department_code)s",' dataline += '"%(certificate_code)s","%(level)s","%(core_or_elective)s"' export.append(heading) 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 course in level.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/export/%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""" return self.portal_vocabularies.levels.get(self.aq_parent.getId()) # 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) ###)