Index: WAeUP_SRP/trunk/Academics.py
===================================================================
--- WAeUP_SRP/trunk/Academics.py	(revision 458)
+++ WAeUP_SRP/trunk/Academics.py	(revision 526)
@@ -17,4 +17,5 @@
 #registerDirectory('skins/waeup_faculty', globals())
 
+import DateTime
 import csv,re
 import logging
@@ -76,9 +77,4 @@
             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")
@@ -87,5 +83,5 @@
             d['college_code'] = faculty.get('CollegeCode')
             f.getContent().edit(mapping=d)
-        return self.academics.academics_contents()
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
     ###)
 
@@ -106,6 +102,5 @@
             logger.info('dumping %s %s ' % (faculty.id, faculty.title))
             print yaml.dump(faculty)
-        return self.academics.temporary_view_all()
-        return self.temporary_view_all()
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
 
     ###)
@@ -155,5 +150,5 @@
                     dict = {'Title': 'Certificates'}
                     certificates.getContent().edit(mapping=dict)
-        return self.academics.academics_contents()
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
     ###)
 
@@ -233,5 +228,5 @@
                 dict['passmark'] = 0
             c.getContent().edit(mapping=dict)
-        return self.academics.academics_contents()
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
     ###)
 
@@ -310,5 +305,5 @@
             print
             c.getContent().edit(mapping=dict)
-        return self.academics.academics_contents()
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
     ###)
 
@@ -419,6 +414,219 @@
             dict['level'] = cert_course.get('Level')
             cc.getContent().edit(mapping=dict)
-        return self.academics.academics_contents()
-    ###)
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
+    ###)
+
+    security.declareProtected(ModifyPortalContent,"exportFacultiesToCSV")###(
+    def exportFacultiesToCSV(self):
+        """export Faculies to CSV"""
+        #return
+        name = 'Faculty'
+        no_import = False
+        logger = logging.getLogger('%s_export' % name)
+        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
+        logger.info('Start exporting %(name)s to %(name)s_%(current)s.csv' % vars())
+        objects = [f.getObject() for f in self.portal_catalog({'meta_type': name})]
+        export = []
+        export.append('"code","review_state","title","title_prefix","college_code","degree_grade","bank_code"')
+        for obj in objects:
+            logger.info('exporting %s %s ' % (obj.id, obj.title))
+            obj_d = obj.getContent()
+            di = {}
+            di['code'] = obj_d.code
+            di['title'] = obj_d.title
+            di['title_prefix'] = obj_d.title_prefix
+            di['college_code'] = obj_d.college_code
+            di['degree_grade'] = obj_d.degree_grade
+            di['bank_code'] = obj_d.bank_code
+            di['code'] = di['code'] or obj.id
+            di['review_state'] = self.portal_workflow.getInfoFor(obj,'review_state','no_state')
+            export.append('"%(code)s","%(review_state)s","%(title)s","%(title_prefix)s","%(college_code)s","%(degree_grade)s","%(bank_code)s"' % di)
+        open("%s/import/%s-%s.csv" % (i_home,name,current),"w+").write('\n'.join(export))
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
+
+    ###)
+
+    security.declareProtected(ModifyPortalContent,"exportDepartmentsToCSV")###(
+    def exportDepartmentsToCSV(self):
+        """export Faculies to CSV"""
+        #return
+        import copy
+        name = 'Department'
+        no_import = False
+        logger = logging.getLogger('%s_export' % name)
+        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
+        logger.info('Start exporting %(name)s to %(name)s_%(current)s.csv' % vars())
+        faculties = [f.getObject() for f in self.portal_catalog({'meta_type': 'Faculty'})]
+        export = []
+        export.append('"code","review_state","title","title_prefix","faculty_code"')
+        #import pdb;pdb.set_trace()
+        for faculty in faculties:
+            di = {}
+            di['faculty_code'] = faculty.id
+            for department in faculty.objectValues():
+                department_d = department.getContent()
+                try:
+                    di['code'] = department_d.code
+                    di['title'] = department_d.title
+                    di['title_prefix'] = department_d.title_prefix
+                    di['code'] = di['code'] or department.id
+                    di['review_state'] = self.portal_workflow.getInfoFor(department,'review_state','no_state')
+                    export.append('"%(code)s","%(review_state)s","%(title)s","%(title_prefix)s","%(faculty_code)s""' % di)
+                    logger.info('exporting %s %s ' % (department.id, department.title))
+                except:
+                    logger.info('could not export %s %s ' % (department.id, department.title))
+                    continue
+        open("%s/import/%s-%s.csv" % (i_home,name,current),"w+").write('\n'.join(export))
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
+    ###)
+
+    security.declareProtected(ModifyPortalContent,"exportCoursesToCSV")###(
+    def exportCoursesToCSV(self):
+        """export Courses to CSV"""
+        #return
+        name = 'Course'
+        no_import = False
+        logger = logging.getLogger('%s_export' % name)
+        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
+        logger.info('Start exporting %(name)s to %(name)s_%(current)s.csv' % vars())
+        faculties = [f.getObject() for f in self.portal_catalog({'meta_type': 'Faculty'})]
+        export = []
+        heading = '"code","review_state","title","faculty_code","department_code",'
+        heading += '"credits","org_code","passmark","semester","session"'
+        dataline = '"%(code)s","%(review_state)s","%(title)s","%(faculty_code)s","%(department_code)s",'
+        dataline += '"%(credits)s","%(org_code)s","%(passmark)s","%(semester)s","%(session)s"'
+        export.append(heading)
+        #import pdb;pdb.set_trace()
+        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(): 
+                    course_d = course.getContent()
+                    try:
+                        di['code'] = course_d.code
+                        di['title'] = course_d.title
+                        di['category'] = course_d.category
+                        di['adm_status'] = course_d.adm_status
+                        di['credits'] = course_d.credits
+                        di['former_code'] = course_d.former_code
+                        di['org_code'] = course_d.org_code
+                        di['passmark'] = course_d.passmark
+                        di['semester'] = course_d.semester
+                        di['session'] = course_d.session
+                        di['code'] = di['code'] or course.id
+                        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))
+                    except:
+                        logger.info('could not export %s %s ' % (course.id, course.title))
+                    continue
+        open("%s/import/%s-%s.csv" % (i_home,name,current),"w+").write('\n'.join(export))
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
+    ###)
+
+    security.declareProtected(ModifyPortalContent,"exportCertificatesToCSV")###(
+    def exportCertificatesToCSV(self):
+        """export Certificates to CSV"""
+        #return
+        name = 'Certificate'
+        no_import = False
+        logger = logging.getLogger('%s_export' % name)
+        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
+        logger.info('Start exporting %(name)s to %(name)s_%(current)s.csv' % vars())
+        faculties = [f.getObject() for f in self.portal_catalog({'meta_type': 'Faculty'})]
+        export = []
+        heading = '"code","review_state","title","faculty_code","department_code",'
+        heading += '"category","end_level","m_prefix","max_elect","max_pass",'
+        heading += '"n_core","nr_years","original_code","probation_credits",'
+        heading += '"promotion_credits","start_level","admin_status"'
+        dataline = '"(code)s","%(review_state)s","%(title)s","%(faculty_code)s","%(department_code)s",'
+        dataline += '"%(category)s","%(end_level)s","%(m_prefix)s","%(max_elect)s","%(max_pass)s",'
+        dataline += '"%(n_core)s","%(nr_years)s","%(original_code)s","%(probation_credits)s",'
+        dataline += '"%(promotion_credits)s","%(start_level)s","%(admin_status)s"'
+        export.append(heading)
+        #import pdb;pdb.set_trace()
+        for faculty in faculties:
+            di = {}
+            di['faculty_code'] = faculty.id
+            for department in faculty.objectValues():
+                di['department_code'] = department.id
+                for certificate in department.certificates.objectValues(): 
+                    certificate_d = certificate.getContent()
+                    di['code'] = certificate_d.get('code',None)
+                    di['title'] = certificate_d.get('title',None)
+                    di['category'] = certificate_d.get('category',None)
+                    di['admin_status'] = certificate_d.get('admin_status',None)
+                    di['end_level'] = certificate_d.get('end_level',None)
+                    di['m_prefix'] = certificate_d.get('m_prefix',None)
+                    di['max_elect'] = certificate_d.get('max_elect',None)
+                    di['max_load'] = certificate_d.get('max_load',None)
+                    di['max_pass'] = certificate_d.get('max_pass',None)
+                    di['n_core'] = certificate_d.get('n_core',None)
+                    di['nr_years'] = certificate_d.get('nr_years',None)
+                    di['original_code'] = certificate_d.get('original_code',None)
+                    di['probation_credits'] = certificate_d.get('probation_credits',None)
+                    di['promotion_credits'] = certificate_d.get('promotion_credits',None)
+                    di['start_level'] = certificate_d.get('start_level',None)
+                    di['code'] = di['code'] or certificate.id
+                    di['review_state'] = self.portal_workflow.getInfoFor(certificate,'review_state','no_state')
+                    try:
+                        export.append(dataline % di)
+                        logger.info('exporting %s %s ' % (certificate.id, certificate.title))
+                    except:
+                        logger.info('could not export %s %s ' % (certificate.id, certificate.title))
+                    continue
+        open("%s/import/%s-%s.csv" % (i_home,name,current),"w+").write('\n'.join(export))
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
+    ###)
+
+    security.declareProtected(ModifyPortalContent,"exportCertificateCoursesToCSV")###(
+    def exportCertificateCoursesToCSV(self):
+        """export CertificateCourses to CSV"""
+        #return
+        name = 'CertificateCourse'
+        no_import = False
+        logger = logging.getLogger('%s_export' % name)
+        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
+        logger.info('Start exporting %(name)s to %(name)s_%(current)s.csv' % vars())
+        faculties = [f.getObject() for f in self.portal_catalog({'meta_type': 'Faculty'})]
+        export = []
+        heading = '"code","review_state","faculty_code","department_code",'
+        heading += '"certificate_code","level","semester","core_or_elective"'
+        dataline = '"%(code)s","%(review_state)s","%(faculty_code)s","%(department_code)s",'
+        dataline += '"%(certificate_code)s","%(level)s","%(semester)s","%(core_or_elective)s"' 
+        export.append(heading)
+        #import pdb;pdb.set_trace()
+        semesters = ['first','second']
+        for faculty in faculties:
+            di = {}
+            di['faculty_code'] = faculty.id
+            for department in faculty.objectValues():
+                di['department_code'] = department.id
+                for certificate in department.certificates.objectValues(): 
+                    di['certificate_code'] = certificate.id
+                    for level in certificate.objectValues():
+                        di['level'] = level.id
+                        for sem in semesters:
+                            semester = getattr(level,sem,None)
+                            if semester is not None:
+                                di['semester'] = semesters.index(sem) + 1 
+                                for course in semester.objectValues():
+                                    course_d = course.getContent()
+                                    di['code'] = course_d.get('code',None)
+                                    di['core_or_elective'] = course_d.get('core_or_elective',None)
+                                    di['code'] = di['code'] or course.id
+                                    di['review_state'] = self.portal_workflow.getInfoFor(course,'review_state','no_state')
+                                    try:
+                                        export.append(dataline % di)
+                                        logger.info('exporting %s %s ' % (certificate.id, certificate.title))
+                                    except:
+                                        logger.info('could not export %s %s ' % (certificate.id, certificate.title))
+                                        continue
+        open("%s/import/%s-%s.csv" % (i_home,name,current),"w+").write('\n'.join(export))
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
+    ###)
+
 
 InitializeClass(AcademicsFolder)
