#-*- mode: python; mode: fold -*- # $Id: exportimport.py 31640 2006-01-15 19:22:29Z ogrisel $ """WAeUP Tool XML Adapter. An XML adapter tells the GenericSetup machinery howto im- / export persistent configuration that is relative to a specific CMF component such as our WAeUP Tool. GenericSetup uses the Zope3 interfaces and components machinery to find the right XML adapter for the right object. This is why we flagged our waeup tool with the `implements(IWAeUPTool)` class attribute and made an adapter association in the `configure.zcml` file. """ import csv,re # Zope3 component architecture from zope.component import adapts from zope.interface import implements # Standard GenericSetup base classes and functions from Products.GenericSetup.utils import exportObjects from Products.GenericSetup.utils import importObjects from Products.GenericSetup.utils import XMLAdapterBase from Products.GenericSetup.utils import PropertyManagerHelpers from Products.CMFCore.utils import getToolByName # GenericSetup multi adapts a class that implement IWAeUP and a particular # ISetupEnvironment to and IBody (piece of XML configuration). from Products.GenericSetup.interfaces import IBody from Products.GenericSetup.interfaces import ISetupEnviron from Products.WAeUP.interfaces import IWAeUPTool import Globals p_home = Globals.package_home(globals()) i_home = Globals.INSTANCE_HOME TOOL = 'portal_waeup' NAME = 'waeup' # The exportWAeUP and importWAeUP methods are called by the specific # im- / export steps defined in the corresponding XML files in the # WAeUP/profiles/default/ directory. def installFaculties( academics): ###( """install Universityspecific Faculies with Departments""" faculties = [ ###( {'id': 'agriculture', ###( 'Title': 'Agriculture', 'departments': [ ## { 'id': 'dep1', ###( ## 'Title': 'One', ## }, ], },###) ###) {'id': 'arts', ###( 'Title': 'Arts', 'departments': [ ## { 'id': 'dep1', ###( ## 'Title': 'One', ## }, ], },###) ###) {'id': 'BasicMedicalSciences', ###( 'Title': 'Basic Medical Sciences', 'departments': [ ## { 'id': 'dep1', ###( ## 'Title': 'One', ## }, ], },###) ###) {'id': 'science', ###( 'Title': 'Science', 'departments': [ { 'id': 'bio', ###( 'Title': 'Biochemistry', }, { 'id': 'bot', 'Title': 'Botany', }, { 'id': 'che', 'Title': 'Chemistry', }, { 'id': 'com', 'Title': 'Computer Science', }, { 'id': 'geo', 'Title': 'Geologie', }, { 'id': 'mat', 'Title': 'Mathematics', }, { 'id': 'mic', 'Title': 'Microbiology', }, { 'id': 'opt', 'Title': 'Optometry', }, { 'id': 'phy', 'Title': 'Physics', }, { 'id': 'zoo', 'Title': 'Zoology', }, ], },###) ]###) ###) for faculty in faculties: fid = faculty['id'] f = getattr(academics,fid,None) if f is None: #self.log('Creating Faculty %(id)s = %(Title)s' % faculty) academics.invokeFactory('Faculty', fid) f = getattr(academics,fid) f.getContent().edit(mapping=faculty) for department in faculty['departments']: #self.log('Checking Department %(id)s = %(Title)s' % department) did = department['id'] d = getattr(f,did,None) if d is None: #self.log('Creating Department %(id)s = %(Title)s' % department) f.invokeFactory('Department', did) d = getattr(f,did) d.getContent().edit(mapping=department) ###) def loadFacultiesFromCSV(academics,site,context): ###( """install Universityspecific Faculies from CSV values""" #return logger = context.getLogger('loadfaculties') logger.info('Start loading Faculties') try: faculties = csv.DictReader(open("%s/import/faculty.csv" % i_home,"rb")) except: return l = site.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(academics,fid) d = {'Title': faculty['Description']} f.getContent().edit(mapping=d) ###) def loadDepartmentsFromCSV(academics,site,context): ###( """install Universityspecific Faculies from CSV values""" #return logger = context.getLogger('loaddepartments') try: deps = csv.DictReader(open("%s/import/departments.csv" % i_home,"rb")) except: return l = site.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) 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) ###) def loadCoursesFromCSV(academics,site,context): ###( """install Universityspecific Courses from CSV values""" #return logger = context.getLogger('loadcourses') try: courses = csv.DictReader(open("%s/import/courses.csv" % i_home,"rb")) except: return l = site.portal_catalog({'meta_type': "Faculty"}) facs = {} for f in l: facs[f.id] = f.getObject() dl = site.portal_catalog({'meta_type': "Department"}) deps = {} for d in dl: deps[d.id] = d.getObject() cl = site.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 %(FORMERCODE)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) continue course_id = ''.join(re.split('\W+',course.get('CourseCode'))) if len(course_id) == 3: course_id = "%s000" % course_id elif len(course_id) != 6: logger.info("invalid course_code %(CourseCode)s" % course) #print course_id,course.get('CourseCode'),course.get('Description') continue ## if course_id in course_list: ## continue c = getattr(dept,course_id,None) if c is None: #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep) 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') dict['passmark'] = course.get('PassMark') c.getContent().edit(mapping=dict) ###) def loadCertificatesFromCSV(site,context): ###( """install Universityspecific Certificates from CSV values""" #return logger = context.getLogger('loadcertificates') try: certificates = csv.DictReader(open("%s/import/certificates.csv" % i_home,"rb")) except: return f_ids = [f.id for f in site.portal_catalog({'meta_type': "Faculty"})] #d_ids = [d.id for d in site.portal_catalog({'meta_type': "Department"})] dl = site.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) continue if not deps.has_key(depid): logger.info('Department %(Dept)s for %(CertCode)s %(Description)s not found' % 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) ###) def oldloadCertificatesFromCSV(certfolder,site,context): ###( """install Universityspecific Certificates from CSV values""" logger = context.getLogger('loadcertificates') certificates = csv.DictReader(open("%s/import/certificates.csv" % i_home,"rb")) f_ids = [f.id for f in site.portal_catalog({'meta_type': "Faculty"})] d_ids = [d.id for d in site.portal_catalog({'meta_type': "Department"})] for certificate in certificates: 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) continue if depid not in d_ids: logger.info('Department %(Dept)s for %(CertCode)s %(Description)s not found' % certificate) continue certificate_id = "%(category)s_%(Admstatus)s_%(Dept)s" % certificate c = getattr(certfolder,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) certfolder.invokeFactory('Certificate', certificate_id) c = getattr(certfolder,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) ###) def loadCertificateCoursesFromCSV(site,context): ###( """install Certificate Courses from CSV values""" #return logger = context.getLogger('loadcertificatecourses') try: cert_courses = csv.DictReader(open("%s/import/course_level_courses.csv" % i_home,"rb")) except: return d_ids = [d.id for d in site.portal_catalog({'meta_type': "Department"})] 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') 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 = site.portal_catalog({'meta_type': "Certificate", 'SearchableText': code}) if not certificate: print code logger.info('CertCode %(CertCode)s for %(CosCode)s not found' % cert_course) continue certificate = certificate[-1].getObject() certificate_code = certificate.getId() if depid not in d_ids: logger.info('Department %(Dept)s for %(CertCode)s not found' % cert_course) continue course_code = cert_course.get('CosCode') 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') 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) 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) ###) def old_loadCertificateCoursesFromCSV(certfolder,site,context): ###( """install Certificate Courses from CSV values""" return logger = context.getLogger('loadcertificatecourses') cert_courses = csv.DictReader(open("%s/import/course_level_courses.csv" % i_home,"rb")) d_ids = [d.id for d in site.portal_catalog({'meta_type': "Department"})] for cert_course in cert_courses: depid = cert_course.get('Dept') 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 = site.portal_catalog({'meta_type': "Certificate", 'SearchableText': code}) if not certificate: print code logger.info('CertCode %(CertCode)s for %(CosCode)s not found' % cert_course) continue certificate = certificate[-1].getObject() certificate_code = certificate.getId() if depid not in d_ids: logger.info('Department %(Dept)s for %(CertCode)s not found' % cert_course) continue course_code = cert_course.get('CosCode') 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') 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) 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) ###) def setupStructure(site,context): ###( sections = getattr(site,'sections') portal = getattr(sections,'uniportal',None) if portal is None: sections.invokeFactory('University','uniportal') portal = getattr(site,'uniportal',None) portal.getContent().edit(mapping={'Title':SRPP_TITLE}) students = getattr(portal,'students',None) if students is None: portal.invokeFactory('StudentsFolder','students') students = getattr(portal,'students').getContent() students.edit(mapping={'Title':'Students'}) academics = getattr(portal,'academics',None) if academics is None: portal.invokeFactory('AcademicsFolder','academics') academics = getattr(portal,'academics') academics.getContent().edit(mapping={'Title':'Academics'}) loadFacultiesFromCSV(academics,site,context) loadDepartmentsFromCSV(academics,site,context) loadCoursesFromCSV(academics,site,context) ## certificates = getattr(academics,'certificates',None) ## if certificates is None: ## academics.invokeFactory('SCFolder','certificates') ## certificates = getattr(academics,'certificates') ## certificates.getContent().edit(mapping={'Title':'Certificates'}) loadCertificatesFromCSV(site,context) loadCertificateCoursesFromCSV(site,context) if not hasattr(portal,'accommodation'): portal.invokeFactory('AccoFolder','accommodation') accommodation = getattr(portal,'accommodation').getContent() accommodation.edit(mapping={'Title':'Accommodation'}) ###) def exportWAeUP(context): """Export our WAeUP tool configuration """ site = context.getSite() tool = getToolByName(site, TOOL, None) if tool is None: logger = context.getLogger(NAME) logger.info("Nothing to export.") return exportObjects(tool, '', context) def importWAeUP(context): """Import WAeUP tool configuration """ site = context.getSite() setupStructure(site,context) #import pdb; pdb.set_trace() pass #site = context.getSite() #tool = getToolByName(site, TOOL) #importObjects(tool, '', context) # This the XMLAdapter itself. It encodes the im- / export logic that is specific # to our tool. `im- / exportObjects` functions will find it thanks to the zope # components machinery and the associations made in the configure.zcml file. class WAeUPXMLAdapter(XMLAdapterBase, PropertyManagerHelpers): """XML importer and exporter for the WAeUP tool. Hence this XMLAdapter is really simple. To get more complete examples of what XMLAdapters are meant to do, please have a look at the CPSSkins.exportimport.py or CPSDirectory.exportimport.py files, for instance. """ adapts(IWAeUPTool, ISetupEnviron) implements(IBody) _LOGGER_ID = NAME name = NAME def _exportNode(self): """Export the object as a DOM node. """ node = self._getObjectNode('object') node.appendChild(self._extractProperties()) self._logger.info("WAeUP tool exported.") return node def _importNode(self, node): """Import the object from the DOM node. """ if self.environ.shouldPurge(): self._purgeProperties() self._initProperties(node) self._logger.info("WAeUP tool imported.")