Index: WAeUP_SRP/trunk/Students.py
===================================================================
--- WAeUP_SRP/trunk/Students.py	(revision 745)
+++ WAeUP_SRP/trunk/Students.py	(revision 746)
@@ -70,4 +70,180 @@
         pwlist.append('"student_id","firstname","middlename","lastname","matric_no","jamb_reg_no","access_code"')
         pwl_template = Template('"$student_id","$firstname","$middlename","$lastname","$matric_no","$jamb_reg_no","$access_code"')
+        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
+        try:
+            students = 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': "StudentClearance",})
+        matrics = []
+        for s in l:
+            matrics.append(s.getObject().getContent().matric_no)
+        print matrics
+        l = self.portal_catalog({'meta_type': "Certificate"})
+        certs = {}
+        for c in l:
+            ca,ac,fa,dep_id,co,certcode = c.relative_path.split('/')
+            cid = "%(dep_id)s_%(certcode)s" % vars()
+            certs[cid] = c.getObject()
+        for student in students:
+            logger.info('processing "%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
+            sid = student.get('MatricNo')
+            if sid == "":
+                em = 'Empty MatricNo\n'
+                logger.info(em)
+                no_import.write(em)
+                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
+                continue
+            certcode = makeCertificateCode(student.get('CourseMajor'))
+            dep_id = student.get('Dept')
+            fac_id = student.get('Faculty')
+            cid = "%(dep_id)s_%(certcode)s" % vars()
+            if cid not in certs.keys():
+                em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
+                logger.info(em)
+                no_import.write(em)
+                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
+                continue
+            certificate_doc = certs[cid].getContent()
+            level = student.get('StudentLevel')
+            try:
+                int(level)
+            except:
+                em = 'Student with ID %(MatricNo)s StudentLevel is empty\n' % student
+                logger.info(em)
+                no_import.write(em)
+                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
+                continue
+            matric_no = student.get('MatricNo')
+            if matric_no not in matrics:
+                matrics.append(matric_no)
+                sid = self.generateStudentId(student.get('Lastname')[0])
+                #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
+                students_folder.invokeFactory('Student', sid)
+                logger.info('%(tr_count)s: Creating Student with ID %(sid)s Matric_no %(matric_no)s ' % vars())
+                student_obj = getattr(self,sid)
+                access_code = "%d" % random.randint(1000000000,9999999999)
+                student_obj.getContent().makeStudentMember(sid,access_code,)
+                pwl_dict = {'student_id': sid,'access_code':access_code}
+                student_obj.invokeFactory('StudentApplication','application')
+                application = student_obj.application
+                da = {'Title': 'Application Data'}
+                student_obj.invokeFactory('StudentPersonal','personal')
+                da['jamb_reg_no'] = student.get('EntryRegNo')
+                personal = student_obj.personal
+                dp = {'Title': 'Personal Data'}
+                student_obj.invokeFactory('StudentClearance','clearance')
+                clearance = student_obj.clearance
+                dc = {'Title': 'Clearance Data'}
+                dc['matric_no'] = matric_no
+                state = student.get('State')
+                lga = student.get('LGA')
+                if state and lga:
+                    lga =  state + ' / ' + lga
+                else:
+                    lga = "None"
+                dc['lga'] = lga
+                dc['nationality'] = student.get('Nationality')
+                dc['email'] = student.get('Emailaddress')
+                dp['firstname'] = student.get('FirstName')
+                dp['middlename'] = student.get('MiddleName')
+                dp['lastname'] = student.get('Lastname')
+                dp['former_surname'] = student.get('FormerSurname')
+                dp['sex'] = student.get('Sex') == 'F'
+                dp['perm_address'] = student.get('PermanentAddress')
+                dp['perm_city'] = student.get('PermanentAddressCity')
+                dp['campus_address'] = student.get('CampusAddress')
+                dp['phone'] = student.get('PhoneNumber')
+                application.getContent().edit(mapping=da)
+                personal.getContent().edit(mapping=dp)
+                clearance.getContent().edit(mapping=dc)
+                #
+                # Study Course
+                #
+                student_obj.invokeFactory('StudentStudyCourse','study_course')
+                studycourse = student_obj.study_course
+                dsc = {}
+                from_certificate = ['title',
+                                   'max_elect',
+                                   'max_pass',
+                                   'n_core',
+                                   'nr_years',
+                                   'probation_credits',
+                                   'promotion_credits',
+                                   'start_level',
+                                  ]
+                for f in from_certificate:
+                    dsc[f] = getattr(certificate_doc,f)
+                dsc['faculty'] = fac_id
+                dsc['department'] = dep_id
+                dsc['study_course'] = certcode
+                css = student.get('CurrentSession') or '2004-2005'
+                cs = int(css.split('-')[0]) - 2000
+                cl = int(student.get('StudentLevel') or '100')/100
+                dsc['entry_session'] = "200%s" % (cs - cl)
+                dsc['clr_ac_pin'] = access_code
+                studycourse.getContent().edit(mapping=dsc)
+                #
+                # Level
+                #
+##                l = getattr(studycourse,level,None)
+##                if 0 and l is None:
+##                    #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
+##                    logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student)
+##                    studycourse.invokeFactory('StudentStudyLevel', level)
+##                    l = getattr(studycourse, level)
+##                    certificate = certs[certcode]
+##                    cert_level = getattr(certificate,level,None)
+##                    if cert_level is None:
+##                        logger.info('Level %(level)s not in %(certcode)s' % vars())
+##                    l.getContent().edit(mapping={'Title': "Level %s" % level})
+            else:
+                em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
+                logger.info(em)
+                no_import.write(em)
+                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
+                continue
+            if tr_count > MAX_TRANS:
+                transaction.commit()
+                em = 'Transaction commited\n' % student
+                logger.info(em)
+                tr_count = 0
+            tr_count += 1
+            pwl_dict.update(dc)
+            pwl_dict.update(da)
+            pwl_dict.update(dp)
+            wftool = self.portal_workflow
+            pwlist.append(pwl_template.substitute(pwl_dict))
+            wftool.doActionFor(student_obj,'clear_and_validate')
+            student_obj.manage_setLocalRoles(sid, ['Owner',])
+            wftool.doActionFor(application,'close')
+            application.manage_setLocalRoles(sid, ['Owner',])
+            wftool.doActionFor(clearance,'close')
+            clearance.manage_setLocalRoles(sid, ['Owner',])
+            wftool.doActionFor(personal,'close')
+            personal.manage_setLocalRoles(sid, ['Owner',])
+            wftool.doActionFor(studycourse,'close_for_edit')
+            studycourse.manage_setLocalRoles(sid, ['Owner',])
+        open("%s/import/pwlist-%s.csv" % (i_home,name),"w+").write('\n'.join(pwlist))
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
+    ###)
+
+    security.declareProtected(ModifyPortalContent,"loadPumeResultsFromCSV")###(
+    def loadPumeResultsFromCSV(self):
+        """load Fulltime Studentdata from CSV values"""
+        import transaction
+        import random
+        tr_count = 0
+        name = 'pume_results'
+        no_import = False
+        if not no_import:
+            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
+            no_import.write('"MatricNo","EntryRegNo","CurrentSession","StudentLevel","fullname","FirstName","MiddleName","Lastname","FormerSurname","Sex","Nationality","State","LGA","PermanentAddress","PermanentAddressCity","CampusAddress","PhoneNumber","Emailaddress","Mode","CourseMajor","Faculty","Dept"\n')
+        logger = logging.getLogger('%s_import' % name)
+        logger.info('Start loading from %s.csv' % name)
+        pwlist  = []
+        pwlist.append('"student_id","firstname","middlename","lastname","jamb_reg_no","access_code"')
+        pwl_template = Template('"$student_id","$firstname","$middlename","$lastname","$jamb_reg_no","$access_code"')
         students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
         try:
@@ -230,180 +406,4 @@
     ###)
 
-    security.declareProtected(ModifyPortalContent,"loadPumeResultsFromCSV")###(
-    def loadPumeResultsFromCSV(self):
-        """load Fulltime Studentdata from CSV values"""
-        import transaction
-        import random
-        tr_count = 0
-        name = 'pume_results'
-        no_import = False
-        if not no_import:
-            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
-            no_import.write('"MatricNo","EntryRegNo","CurrentSession","StudentLevel","fullname","FirstName","MiddleName","Lastname","FormerSurname","Sex","Nationality","State","LGA","PermanentAddress","PermanentAddressCity","CampusAddress","PhoneNumber","Emailaddress","Mode","CourseMajor","Faculty","Dept"\n')
-        logger = logging.getLogger('%s_import' % name)
-        logger.info('Start loading from %s.csv' % name)
-        pwlist  = []
-        pwlist.append('"student_id","firstname","middlename","lastname","jamb_reg_no","access_code"')
-        pwl_template = Template('"$student_id","$firstname","$middlename","$lastname","$jamb_reg_no","$access_code"')
-        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
-        try:
-            students = 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': "StudentClearance",})
-        matrics = []
-        for s in l:
-            matrics.append(s.getObject().getContent().matric_no)
-        print matrics
-        l = self.portal_catalog({'meta_type': "Certificate"})
-        certs = {}
-        for c in l:
-            ca,ac,fa,dep_id,co,certcode = c.relative_path.split('/')
-            cid = "%(dep_id)s_%(certcode)s" % vars()
-            certs[cid] = c.getObject()
-        for student in students:
-            logger.info('processing "%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
-            sid = student.get('MatricNo')
-            if sid == "":
-                em = 'Empty MatricNo\n'
-                logger.info(em)
-                no_import.write(em)
-                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
-                continue
-            certcode = makeCertificateCode(student.get('CourseMajor'))
-            dep_id = student.get('Dept')
-            fac_id = student.get('Faculty')
-            cid = "%(dep_id)s_%(certcode)s" % vars()
-            if cid not in certs.keys():
-                em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
-                logger.info(em)
-                no_import.write(em)
-                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
-                continue
-            certificate_doc = certs[cid].getContent()
-            level = student.get('StudentLevel')
-            try:
-                int(level)
-            except:
-                em = 'Student with ID %(MatricNo)s StudentLevel is empty\n' % student
-                logger.info(em)
-                no_import.write(em)
-                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
-                continue
-            matric_no = student.get('MatricNo')
-            if matric_no not in matrics:
-                matrics.append(matric_no)
-                sid = self.generateStudentId(student.get('Lastname')[0])
-                #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
-                students_folder.invokeFactory('Student', sid)
-                logger.info('%(tr_count)s: Creating Student with ID %(sid)s Matric_no %(matric_no)s ' % vars())
-                student_obj = getattr(self,sid)
-                access_code = "%d" % random.randint(1000000000,9999999999)
-                student_obj.getContent().addMember(sid,access_code ,)
-                pwl_dict = {'student_id': sid,'access_code':access_code}
-                student_obj.invokeFactory('StudentApplication','application')
-                application = student_obj.application
-                da = {'Title': 'Application Data'}
-                student_obj.invokeFactory('StudentPersonal','personal')
-                da['jamb_reg_no'] = student.get('EntryRegNo')
-                personal = student_obj.personal
-                dp = {'Title': 'Personal Data'}
-                student_obj.invokeFactory('StudentClearance','clearance')
-                clearance = student_obj.clearance
-                dc = {'Title': 'Clearance Data'}
-                dc['matric_no'] = matric_no
-                state = student.get('State')
-                lga = student.get('LGA')
-                if state and lga:
-                    lga =  state + ' / ' + lga
-                else:
-                    lga = "None"
-                dc['lga'] = lga
-                dc['nationality'] = student.get('Nationality')
-                dc['email'] = student.get('Emailaddress')
-                dp['firstname'] = student.get('FirstName')
-                dp['middlename'] = student.get('MiddleName')
-                dp['lastname'] = student.get('Lastname')
-                dp['former_surname'] = student.get('FormerSurname')
-                dp['sex'] = student.get('Sex') == 'F'
-                dp['perm_address'] = student.get('PermanentAddress')
-                dp['perm_city'] = student.get('PermanentAddressCity')
-                dp['campus_address'] = student.get('CampusAddress')
-                dp['phone'] = student.get('PhoneNumber')
-                application.getContent().edit(mapping=da)
-                personal.getContent().edit(mapping=dp)
-                clearance.getContent().edit(mapping=dc)
-                #
-                # Study Course
-                #
-                student_obj.invokeFactory('StudentStudyCourse','study_course')
-                studycourse = student_obj.study_course
-                dsc = {}
-                from_certificate = ['title',
-                                   'max_elect',
-                                   'max_pass',
-                                   'n_core',
-                                   'nr_years',
-                                   'probation_credits',
-                                   'promotion_credits',
-                                   'start_level',
-                                  ]
-                for f in from_certificate:
-                    dsc[f] = getattr(certificate_doc,f)
-                dsc['faculty'] = fac_id
-                dsc['department'] = dep_id
-                dsc['study_course'] = certcode
-                css = student.get('CurrentSession') or '2004-2005'
-                cs = int(css.split('-')[0]) - 2000
-                cl = int(student.get('StudentLevel') or '100')/100
-                dsc['entry_session'] = "200%s" % (cs - cl)
-                dsc['clr_ac_pin'] = access_code
-                studycourse.getContent().edit(mapping=dsc)
-                #
-                # Level
-                #
-##                l = getattr(studycourse,level,None)
-##                if 0 and l is None:
-##                    #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
-##                    logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student)
-##                    studycourse.invokeFactory('StudentStudyLevel', level)
-##                    l = getattr(studycourse, level)
-##                    certificate = certs[certcode]
-##                    cert_level = getattr(certificate,level,None)
-##                    if cert_level is None:
-##                        logger.info('Level %(level)s not in %(certcode)s' % vars())
-##                    l.getContent().edit(mapping={'Title': "Level %s" % level})
-            else:
-                em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
-                logger.info(em)
-                no_import.write(em)
-                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
-                continue
-            if tr_count > MAX_TRANS:
-                transaction.commit()
-                em = 'Transaction commited\n' % student
-                logger.info(em)
-                tr_count = 0
-            tr_count += 1
-            pwl_dict.update(dc)
-            pwl_dict.update(da)
-            pwl_dict.update(dp)
-            wftool = self.portal_workflow
-            pwlist.append(pwl_template.substitute(pwl_dict))
-            wftool.doActionFor(student_obj,'clear_and_validate')
-            student_obj.manage_setLocalRoles(sid, ['Owner',])
-            wftool.doActionFor(application,'close')
-            application.manage_setLocalRoles(sid, ['Owner',])
-            wftool.doActionFor(clearance,'close')
-            clearance.manage_setLocalRoles(sid, ['Owner',])
-            wftool.doActionFor(personal,'close')
-            personal.manage_setLocalRoles(sid, ['Owner',])
-            wftool.doActionFor(studycourse,'close_for_edit')
-            studycourse.manage_setLocalRoles(sid, ['Owner',])
-        open("%s/import/pwlist-%s.csv" % (i_home,name),"w+").write('\n'.join(pwlist))
-        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
-    ###)
-
     security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
     def loadFullTimeStudentsResultsFromCSV(self):
