Index: /WAeUP_SRP/trunk/Students.py
===================================================================
--- /WAeUP_SRP/trunk/Students.py	(revision 957)
+++ /WAeUP_SRP/trunk/Students.py	(revision 958)
@@ -23,5 +23,16 @@
 MAX_TRANS = 1000
 
-def getInt(s):
+def makeCertificateCode(code): ###(
+    code = code.replace('.','')
+    code = code.replace('(','')
+    code = code.replace(')','')
+    code = code.replace('/','')
+    code = code.replace(' ','')
+    code = code.replace('_','')
+    return code
+
+###)
+
+def getInt(s): ###(
     try:
         return int(s)
@@ -34,4 +45,6 @@
     except:
         return 0.0
+
+###)
 
 def getStudentByRegNo(self,reg_no): ###(
@@ -235,8 +248,186 @@
         import transaction
         import random
+        from pdb import set_trace
         wftool = self.portal_workflow
         students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
-        csv_d = {'jamb_reg_no': "Matnumber",
-                 'jamb_lastname': "Surname",
+        csv_d = {'jamb_reg_no': "JAMBRegno",
+                 'jamb_lastname': "Name",
+                 #'pume_options': "Options",
+                 #'session': "Session",
+                 #'days': "Days",
+                 #'response': "Responce",
+                 #'wrong': "Wrong",
+                 #'pume_eng_score': "EngScore",
+                 #'pume_gen_score': "GenScore",
+                 'pume_tot_score': "Score",
+                 #'batch': "Batch",
+                 #'serial': "SerialNo",
+                 #'jamb_score': "JambScore",
+                 #'omitted':"Omitted",
+                 #'search_key': "SearchKey",
+                 'jamb_sex': "Sex",
+                 'fac1': "Faculty",
+                 'dep': "Dept",
+                 #'fac2': "Fac2",
+                 'jamb_first_cos': "Course Code",
+                 'stud_status':"Admission Status",
+                 #'registered': "Registered",
+                 #'jamb_state': "State",
+                 #'eng_fail': "EngFail",
+                 #'gen_fail': "GenFail",
+                 #'un_ans_eng': "UnAnsEng",
+                 #'un_ans_eng': "UnAnsGen",
+                 #'total_ans': "TotalUnAns",
+                 #'dept': "Dept",
+                 #'jamb_second_cos': "Course2",
+                 #'jamb_third_cos': "course3",
+                 }
+        csv_fields = [f[1] for f in csv_d.items()]
+        tr_count = 0
+        #name = 'pume_results'
+        name = 'pup'
+        no_import = []
+        s = ','.join(['"(%s)"' % fn for fn in csv_fields])
+        no_import.append('%s\n' % s)
+        logger = logging.getLogger('%s_import' % name)
+        logger.info('Start loading from %s.csv' % name)
+        #set_trace()
+        try:
+            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
+        except:
+            logger.error('Error reading %s.csv' % name)
+            return
+        for jamb in result:
+            format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
+            processing = "processing %s" % format
+            logger.info(processing % jamb)
+            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
+            #import pdb;pdb.set_trace()
+            res = self.portal_catalog({'portal_type': "StudentApplication",
+                                     'jamb_reg_no': jamb_reg_no })
+            if res:
+                em = 'Student with REG-NO %s already exists\n' % jamb_reg_no
+                logger.info(em)
+                no_import.append(em)
+                no_import.append(format % jamb)
+                continue
+            cert_id = makeCertificateCode(jamb.get(csv_d['jamb_first_cos']))
+            res = self.portal_catalog({'portal_type': "Certificate",
+                                     'id': cert_id })
+            if len(res) < 1:
+                em = 'No Certificate with ID %s \n' % cert_id
+                logger.info(em)
+                no_import.append(em)
+                no_import.append(format % jamb)
+                continue
+            cert = res[0].getObject()
+            cert_path = res[0].getPath()
+            cert_doc = cert.getContent()
+            jamb_name = jamb.get(csv_d['jamb_lastname'])
+            jamb_name.replace('>','')
+            names = jamb_name.split()
+            letter = names[-1][0].upper()
+            sid = self.generateStudentId(letter)
+            not_created = True
+            while not_created:
+                try:
+                    students_folder.invokeFactory('Student', sid)
+                    not_created = False
+                except BadRequest:
+                    sid = self.generateStudentId(letter)
+            tr_count += 1
+            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
+            student = getattr(self,sid)
+            student.manage_setLocalRoles(sid, ['Owner',])
+            student.invokeFactory('StudentClearance','clearance')
+            #wftool.doActionFor(student.clearance,'open')
+            dp = {'Title': 'Clearance/Eligibility Record'}
+            student.clearance.manage_setLocalRoles(sid, ['Owner',])
+            student.invokeFactory('StudentPume','pume')
+            dp = {'Title': 'Pume Data'}
+            student.invokeFactory('StudentApplication','application')
+            da = {'Title': 'Application Data'}
+            da["jamb_lastname"] = jamb_name
+            da_fields = ('jamb_reg_no',
+                         'jamb_sex',
+                         #'jamb_state',
+                         #'jamb_score',
+                         'jamb_first_cos',
+                         'jamb_sex',
+                         #'jamb_state',
+                         #'jamb_first_cos',
+                         #'jamb_second_cos',
+                         )
+            for f in da_fields:
+                da[f] = jamb.get(csv_d[f])
+            app = student.application
+            app.getContent().edit(mapping=da)
+            app.manage_setLocalRoles(sid, ['Owner',])
+            #wftool.doActionFor(app,'close')
+            dp_fields = (
+                         #'pume_eng_score',
+                         #'pume_gen_score',
+                         'pume_tot_score',
+                         )
+            for f in dp_fields:
+                try:
+                    dp[f] = float(jamb.get(csv_d[f]))
+                except ValueError:
+                    dp[f] = 0.0
+            pume = student.pume
+            pume.getContent().edit(mapping=dp)
+            #wftool.doActionFor(pume,'close')
+            pume.manage_setLocalRoles(sid, ['Owner',])
+            #
+            # Study Course
+            #
+            if jamb.get(csv_d['stud_status']) == "Admitted":
+                wftool.doActionFor(student,'pume_pass')
+                wftool.doActionFor(student,'admit')
+            else:
+                wftool.doActionFor(student,'pume_fail')
+                wftool.doActionFor(student,'reject_admission')
+                continue
+            student.invokeFactory('StudentStudyCourse','study_course')
+            study_course = student.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(cert_doc,f)
+            cpl = cert_path.split('/')
+            dsc['faculty'] = cpl[-4]
+            dsc['department'] = cpl[-3]
+            dsc['study_course'] = cert_id
+            dsc['entry_session'] = "2006/2007" #jamb.get(csv_d['session'])
+            study_course.getContent().edit(mapping=dsc)
+            student.getContent().createSubObjects()
+            if tr_count > MAX_TRANS:
+                transaction.commit()
+                em = '%d transactions commited\n' % tr_count
+                logger.info(em)
+                tr_count = 0
+        if len(no_import) > 1:
+            open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
+                     '\n'.join(no_import))
+        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
+    ###)
+
+    security.declareProtected(ModifyPortalContent,"OLDloadPumeResultsFromCSV")###(
+    def OLDloadPumeResultsFromCSV(self):
+        """load Fulltime Studentdata from CSV values"""
+        import transaction
+        import random
+        wftool = self.portal_workflow
+        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
+        csv_d = {'jamb_reg_no': "JAMBRegno",
+                 'jamb_lastname': "Name",
                  'pume_options': "Options",
                  'session': "Session",
@@ -246,5 +437,5 @@
                  'pume_eng_score': "EngScore",
                  'pume_gen_score': "GenScore",
-                 'pume_tot_score': "Right",
+                 'pume_tot_score': "Score",
                  'batch': "Batch",
                  'serial': "SerialNo",
