source: WAeUP_SRP/branches/uli/Students.unused_methods.txt @ 7657

Last change on this file since 7657 was 1593, checked in by uli, 18 years ago

Merged current trunk into uli-branch.

File size: 32.9 KB
Line 
1    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsFromCSV")###(
2    def loadFullTimeStudentsFromCSV(self):
3        """load Fulltime Studentdata from CSV values"""
4        import transaction
5        import random
6        tr_count = 0
7        name = 'short_full_time'
8        no_import = False
9        if not no_import:
10            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
11            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')
12        logger = logging.getLogger('Import.%s' % name)
13        logger.info('Start loading from %s.csv' % name)
14        pwlist  = []
15        pwlist.append('"student_id","firstname","middlename","lastname","matric_no","jamb_reg_no","access_code"')
16        pwl_template = Template('"$student_id","$firstname","$middlename","$lastname","$matric_no","$jamb_reg_no","$access_code"')
17        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
18        try:
19            students = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
20        except:
21            logger.error('Error reading %s.csv' % name)
22            return
23        l = self.portal_catalog({'meta_type': "StudentClearance",})
24        matrics = []
25        for s in l:
26            matrics.append(s.getObject().getContent().matric_no)
27        print matrics
28        l = self.portal_catalog({'meta_type': "Certificate"})
29        certs = {}
30        for c in l:
31            ca,ac,fa,dep_id,co,certcode = c.relative_path.split('/')
32            cid = "%(dep_id)s_%(certcode)s" % vars()
33            certs[cid] = c.getObject()
34        for student in students:
35            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)
36            sid = student.get('MatricNo')
37            if sid == "":
38                em = 'Empty MatricNo\n'
39                logger.info(em)
40                no_import.write(em)
41                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)
42                continue
43            certcode = makeCertificateCode(student.get('CourseMajor'))
44            dep_id = student.get('Dept')
45            fac_id = student.get('Faculty')
46            cid = "%(dep_id)s_%(certcode)s" % vars()
47            if cid not in certs.keys():
48                em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
49                logger.info(em)
50                no_import.write(em)
51                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)
52                continue
53            certificate_doc = certs[cid].getContent()
54            level = student.get('StudentLevel')
55            try:
56                int(level)
57            except:
58                em = 'Student with ID %(MatricNo)s StudentLevel is empty\n' % student
59                logger.info(em)
60                no_import.write(em)
61                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)
62                continue
63            matric_no = student.get('MatricNo')
64            if matric_no not in matrics:
65                matrics.append(matric_no)
66                sid = self.generateStudentId(student.get('Lastname')[0])
67                #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
68                students_folder.invokeFactory('Student', sid)
69                logger.info('%(tr_count)s: Creating Student with ID %(sid)s Matric_no %(matric_no)s ' % vars())
70                student_obj = getattr(self,sid)
71                access_code = "%d" % random.randint(1000000000,9999999999)
72                student_obj.getContent().makeStudentMember(sid,access_code,)
73                pwl_dict = {'student_id': sid,'access_code':access_code}
74                student_obj.invokeFactory('StudentApplication','application')
75                application = student_obj.application
76                da = {'Title': 'Application Data'}
77                student_obj.invokeFactory('StudentPersonal','personal')
78                da['jamb_reg_no'] = student.get('EntryRegNo')
79                personal = student_obj.personal
80                dp = {'Title': 'Personal Data'}
81                student_obj.invokeFactory('StudentClearance','clearance')
82                clearance = student_obj.clearance
83                dc = {'Title': 'Clearance/Eligibility Record'}
84                dc['matric_no'] = matric_no
85                state = student.get('State')
86                lga = student.get('LGA')
87                if state and lga:
88                    lga =  state + ' / ' + lga
89                else:
90                    lga = "None"
91                dc['lga'] = lga
92                dc['nationality'] = student.get('Nationality')
93                dc['email'] = student.get('Emailaddress')
94                dp['firstname'] = student.get('FirstName')
95                dp['middlename'] = student.get('MiddleName')
96                dp['lastname'] = student.get('Lastname')
97                dp['former_surname'] = student.get('FormerSurname')
98                dp['sex'] = student.get('Sex') == 'F'
99                dp['perm_address'] = student.get('PermanentAddress')
100                dp['perm_city'] = student.get('PermanentAddressCity')
101                dp['campus_address'] = student.get('CampusAddress')
102                dp['phone'] = student.get('PhoneNumber')
103                application.getContent().edit(mapping=da)
104                personal.getContent().edit(mapping=dp)
105                clearance.getContent().edit(mapping=dc)
106                #
107                # Study Course
108                #
109                student_obj.invokeFactory('StudentStudyCourse','study_course')
110                studycourse = student_obj.study_course
111                dsc = {}
112                from_certificate = ['title',
113                                   'max_elect',
114                                   'max_pass',
115                                   'n_core',
116                                   'nr_years',
117                                   'probation_credits',
118                                   'promotion_credits',
119                                   'start_level',
120                                  ]
121                for f in from_certificate:
122                    dsc[f] = getattr(certificate_doc,f)
123                dsc['faculty'] = fac_id
124                dsc['department'] = dep_id
125                dsc['study_course'] = certcode
126                css = student.get('CurrentSession') or '2004-2005'
127                cs = int(css.split('-')[0]) - 2000
128                cl = int(student.get('StudentLevel') or '100')/100
129                dsc['entry_session'] = "200%s" % (cs - cl)
130                dsc['clr_ac_pin'] = access_code
131                studycourse.getContent().edit(mapping=dsc)
132                #
133                # Level
134                #
135##                l = getattr(studycourse,level,None)
136##                if 0 and l is None:
137##                    #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
138##                    logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student)
139##                    studycourse.invokeFactory('StudentStudyLevel', level)
140##                    l = getattr(studycourse, level)
141##                    certificate = certs[certcode]
142##                    cert_level = getattr(certificate,level,None)
143##                    if cert_level is None:
144##                        logger.info('Level %(level)s not in %(certcode)s' % vars())
145##                    l.getContent().edit(mapping={'Title': "Level %s" % level})
146            else:
147                em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
148                logger.info(em)
149                no_import.write(em)
150                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)
151                continue
152            if tr_count > MAX_TRANS:
153                transaction.commit()
154                em = 'Transaction commited\n' % student
155                logger.info(em)
156                tr_count = 0
157            tr_count += 1
158            pwl_dict.update(dc)
159            pwl_dict.update(da)
160            pwl_dict.update(dp)
161            wftool = self.portal_workflow
162            pwlist.append(pwl_template.substitute(pwl_dict))
163            wftool.doActionFor(student_obj,'clear_and_validate')
164            student_obj.manage_setLocalRoles(sid, ['Owner',])
165            wftool.doActionFor(application,'close')
166            application.manage_setLocalRoles(sid, ['Owner',])
167            wftool.doActionFor(clearance,'close')
168            clearance.manage_setLocalRoles(sid, ['Owner',])
169            wftool.doActionFor(personal,'close')
170            personal.manage_setLocalRoles(sid, ['Owner',])
171            wftool.doActionFor(studycourse,'close_for_edit')
172            studycourse.manage_setLocalRoles(sid, ['Owner',])
173        open("%s/import/pwlist-%s.csv" % (i_home,name),"w+").write('\n'.join(pwlist))
174        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
175    ###)
176
177    security.declareProtected(ModifyPortalContent,"loadPumeResultsFromCSV")###(
178    def loadPumeResultsFromCSV(self):
179        """load Fulltime Studentdata from CSV values into pumeresults catalog"""
180        import transaction
181        import random
182##        csv_d = {'jamb_reg_no': "RegNumber", ###(
183##                 'status': "Admission Status",
184##                 'name': "Name",
185##                 'score': "Score",
186##                 'sex': "Sex",
187##                 'faculty': "Faculty",
188##                 'department': "Dept",
189##                 'course': "Course",
190##                 'course_code_org': "Course Code",
191##                 }
192###)
193        csv_d = {'jamb_reg_no': "JAMBRegno",
194                 'name': "Name",
195                 'score': "Score",
196                 'sex': "Sex",
197                 'course': "Course",
198                 'faculty': "Faculty",
199                 'department': "Dept",
200                 'course_code_org': "Course Code",
201                 'status': "Admission Status",
202                 'result_type': None,
203                 }
204        csv_fields = [f[1] for f in csv_d.items() if f[1]]
205        tr_count = 0
206        total = 0
207        #name = 'pup_new'
208        name = 'pup_update'
209        update = name.endswith('update')
210        no_import = []
211        ok_import = []
212        ok_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_d.keys()]))
213        no_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_fields]))
214        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
215        ok_import_name = "%s/import/%s_imported_%s.csv" % (i_home,name,current)
216        #open(ok_import_name,"w").write('\n'.join(no_import))
217        no_import_name = "%s/import/%s_not_imported_%s.csv" % (i_home,name,current)
218        #open(no_import_name,"w").write('\n'.join(no_import))
219        logger = logging.getLogger('Import.%s' % name)
220        starttime = DateTime.now()
221        logger.info('Start loading from %s.csv' % name)
222        try:
223            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
224        except:
225            logger.error('Error reading %s.csv' % name)
226            return
227        pume = self.portal_pumeresults
228        format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
229        import_format = ','.join(['"%%(%s)s"' % fn for fn in csv_d.keys()])
230        eduplicate = '%s,"duplicate"' % format
231        einvalidjamb = '%s,"invalid JambRegNo"' % format
232        added = 'added ,%s' % format
233        #from pdb import set_trace;set_trace()
234        for jamb in result:
235            dict = {}
236            for f,fn in csv_d.items():
237                dict[f] = jamb.get(csv_d[f])
238            dict['result_type'] = 'DE'
239            jnr = jamb.get(csv_d['jamb_reg_no'])
240            if not checkJambNo(jnr):
241                logger.info(einvalidjamb % jamb)
242                dd = {}
243                for f,fn in csv_d.items():
244                    dd[fn] = getattr(data,f)
245                    no_import.append(eduplicate % dd)
246                    no_import.append(eduplicate % jamb)
247                continue
248            res = pume(jamb_reg_no=jnr)
249            if len(res) > 0:
250                if update:
251                    try:
252                        pume.modifyRecord(**dict)
253                    except ValueError:
254                        logger.info(eduplicate % jamb)
255                        continue
256                    except KeyError:
257                        pume.addRecord(**dict)
258                        logger.info(added % jamb)
259                        continue
260                else:
261                    data = res[0]
262                    if data.name != jamb.get(csv_d['name']):
263                        #set_trace()
264                        logger.info(eduplicate % jamb)
265                        #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
266                        #logger.info(em)
267                        dd = {}
268                        for f,fn in csv_d.items():
269                            dd[fn] = getattr(data,f)
270                        no_import.append(eduplicate % dd)
271                        no_import.append(eduplicate % jamb)
272                    continue
273            try:
274                pume.addRecord(**dict)
275                ok_import.append(import_format % dict)
276            except ValueError:
277                logger.info(eduplicate % jamb)
278                #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
279                #logger.info(em)
280                no_import.append(eduplicate % jamb)
281        logger.info('End loading from %s.csv' % name)
282        if len(no_import) > 1:
283            open(no_import_name,"w+").write('\n'.join(no_import))
284        open(ok_import_name,"w+").write('\n'.join(ok_import))
285        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
286    ###)
287
288
289    security.declareProtected(ModifyPortalContent,"OLDloadPumeResultsFromCSV")###(
290    def OLDloadPumeResultsFromCSV(self):
291        """load Fulltime Studentdata from CSV values"""
292        import transaction
293        import random
294        wftool = self.portal_workflow
295        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
296        csv_d = {'jamb_reg_no': "JAMBRegno",
297                 'jamb_lastname': "Name",
298                 'pume_options': "Options",
299                 'session': "Session",
300                 'days': "Days",
301                 'response': "Responce",
302                 'wrong': "Wrong",
303                 'pume_eng_score': "EngScore",
304                 'pume_gen_score': "GenScore",
305                 'pume_tot_score': "Score",
306                 'batch': "Batch",
307                 'serial': "SerialNo",
308                 'jamb_score': "JambScore",
309                 'omitted':"Omitted",
310                 'search_key': "SearchKey",
311                 'jamb_sex': "Sex",
312                 'fac1': "Fac1",
313                 'fac2': "Fac2",
314                 'jamb_first_cos': "CourseofStudy",
315                 'stud_status':"StudStatus",
316                 'registered': "Registered",
317                 'jamb_state': "State",
318                 'eng_fail': "EngFail",
319                 'gen_fail': "GenFail",
320                 'un_ans_eng': "UnAnsEng",
321                 'un_ans_eng': "UnAnsGen",
322                 'total_ans': "TotalUnAns",
323                 'dept': "Dept",
324                 'jamb_second_cos': "Course2",
325                 'jamb_third_cos': "course3",
326                 }
327        csv_fields = [f[1] for f in csv_d.items()]
328        tr_count = 0
329        name = 'pume_results'
330        no_import = []
331        s = ','.join(['"%s"' % fn for fn in csv_fields])
332        no_import.append('%s\n' % s)
333        logger = logging.getLogger('Import.%s' % name)
334        logger.info('Start loading from %s.csv' % name)
335        try:
336            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
337        except:
338            logger.error('Error reading %s.csv' % name)
339            return
340        for jamb in result:
341            format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
342            processing = "processing %s" % format
343            logger.info(processing % jamb)
344            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
345            #import pdb;pdb.set_trace()
346            res = self.portal_catalog({'portal_type': "StudentApplication",
347                                     'jamb_reg_no': jamb_reg_no })
348            if res:
349                em = 'Student with REG-NO %s already exists\n' % jamb_reg_no
350                logger.info(em)
351                no_import.append(em)
352                no_import.append(format % jamb)
353                continue
354            cert_id = jamb.get(csv_d['jamb_first_cos']).upper()
355            res = self.portal_catalog({'portal_type': "Certificate",
356                                     'id': cert_id })
357            if len(res) < 1:
358                em = 'No Certificate with ID %s \n' % cert_id
359                logger.info(em)
360                no_import.append(em)
361                no_import.append(format % jamb)
362                continue
363            cert = res[0].getObject()
364            cert_path = res[0].getPath()
365            cert_doc = cert.getContent()
366            jamb_name = jamb.get(csv_d['jamb_lastname'])
367            jamb_name.replace('>','')
368            names = jamb_name.split()
369            letter = names[-1][0].upper()
370            sid = self.generateStudentId(letter)
371            not_created = True
372            while not_created:
373                try:
374                    students_folder.invokeFactory('Student', sid)
375                    not_created = False
376                except BadRequest:
377                    sid = self.generateStudentId(letter)
378            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
379            student = getattr(self,sid)
380            student.manage_setLocalRoles(sid, ['Owner',])
381            student.invokeFactory('StudentClearance','clearance')
382            #wftool.doActionFor(student.clearance,'open')
383            dp = {'Title': 'Clearance/Eligibility Record'}
384            student.clearance.manage_setLocalRoles(sid, ['Owner',])
385            student.invokeFactory('StudentPume','pume')
386            dp = {'Title': 'Pume Data'}
387            student.invokeFactory('StudentApplication','application')
388            da = {'Title': 'Application Data'}
389            da["jamb_lastname"] = jamb_name
390            da_fields = ('jamb_reg_no',
391                         'jamb_sex',
392                         'jamb_state',
393                         'jamb_score',
394                         'jamb_first_cos',
395                         'jamb_sex',
396                         'jamb_state',
397                         'jamb_first_cos',
398                         'jamb_second_cos',
399                         )
400            for f in da_fields:
401                da[f] = jamb.get(csv_d[f])
402            app = student.application
403            app.getContent().edit(mapping=da)
404            app.manage_setLocalRoles(sid, ['Owner',])
405            #wftool.doActionFor(app,'close')
406            dp_fields = (
407                         'pume_eng_score',
408                         'pume_gen_score',
409                         'pume_tot_score',
410                         )
411            for f in dp_fields:
412                dp[f] = float(jamb.get(csv_d[f]))
413            pume = student.pume
414            pume.getContent().edit(mapping=dp)
415            #wftool.doActionFor(pume,'close')
416            pume.manage_setLocalRoles(sid, ['Owner',])
417            #
418            # Study Course
419            #
420            student.invokeFactory('StudentStudyCourse','study_course')
421            study_course = student.study_course
422            dsc = {}
423            from_certificate = ['title',
424                               'max_elect',
425                               'max_pass',
426                               'n_core',
427                               'nr_years',
428                               'probation_credits',
429                               'promotion_credits',
430                               'start_level',
431                              ]
432            for f in from_certificate:
433                dsc[f] = getattr(cert_doc,f)
434            cpl = cert_path.split('/')
435            dsc['faculty'] = cpl[-4]
436            dsc['department'] = cpl[-3]
437            dsc['study_course'] = cert_id
438            dsc['entry_session'] = jamb.get(csv_d['session'])
439            study_course.getContent().edit(mapping=dsc)
440            student.getContent().createSubObjects()
441            if dp['pume_tot_score']>49:
442                wftool.doActionFor(student,'pume_pass')
443                wftool.doActionFor(student,'admit')
444            else:
445                wftool.doActionFor(student,'pume_fail')
446                wftool.doActionFor(student,'reject_admission')
447            if len(no_import) > 1:
448                open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
449                     '\n'.join(no_import))
450        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
451    ###)
452
453    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
454    def loadFullTimeStudentsResultsFromCSV(self):
455        """load Fulltime Studentdata from CSV values"""
456        #return
457        level_wf_actions = {}
458        level_wf_actions["SUCCESSFUL STUDENT"] = "pass_A"
459        level_wf_actions["STUDENT WITH CARRYOVER COURSES"] = "pass_B"
460        level_wf_actions["STUDENT FOR PROBATION"] = "probate_C"
461        level_wf_actions["STUDENT ON PROBATION/TRANSFER"] = "reject_D"
462        import transaction
463        wftool = self.portal_workflow
464        tr_count = 0
465        name = 'short_full_time_results_2004_2005'
466        no_import = False
467        if not no_import:
468            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
469            no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
470        logger = logging.getLogger('import.%s' % name)
471        logger.info('Start loading from %s.csv' % name)
472        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
473        try:
474            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
475        except:
476            logger.error('Error reading %s.csv' % name)
477            return
478        l = self.portal_catalog({'meta_type': "Course"})
479        courses = {}
480        for c in l:
481            courses[c.id] = c.getObject()
482        level_changed = False
483        student_changed = False
484        sid = ''
485        #import pdb;pdb.set_trace()
486        for result in results:
487            temp_sid = result.get('Matnumber')
488            if temp_sid != sid:
489                student_changed = True
490                res = self.portal_catalog({'meta_type': "StudentClearance",
491                                         'SearchableText': temp_sid })
492                if not res:
493                    em = 'Student with ID %(Matnumber)s not found\n' % result
494                    logger.info(em)
495                    no_import.write(em)
496                    no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
497                    continue
498                elif len(res) > 1:
499                    em = 'More than one Student with ID %(Matnumber)s found\n' % result
500                    logger.info(em)
501                    no_import.write(em)
502                    no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
503                    continue
504                sid = temp_sid
505                sf = res[0].getObject().aq_parent
506                sc = getattr(sf,'study_course')
507                level = ''
508            else:
509                student_changed = False
510            course = result.get('CosCode')
511            if course not in courses.keys():
512                em = 'Course with ID %(CosCode)s not found\n' % result
513                logger.info(em)
514                no_import.write(em)
515                no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
516                continue
517            course_doc = courses[course].getContent()
518            temp_level = result.get('Level')
519            student_id = sf.getId()
520            result['StudentId'] = student_id
521            if temp_level != level:
522                try:
523                    int(temp_level)
524                except:
525                    em = 'Result with ID %(Matnumber)s Course %(CosCode)s Level is empty\n' % result
526                    logger.info(em)
527                    no_import.write(em)
528                    no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
529                    continue
530                level_changed = True
531                if 'dlev' in vars().keys():
532                    wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
533                level = temp_level
534                l = getattr(sc,level,None)
535                if l is None:
536                    logger.info('Creating Level %(Level)s for %(StudentId)s %(Matnumber)s' % result)
537                    sc.invokeFactory('StudentStudyLevel', level)
538                    l = getattr(sc, level)
539                    l.manage_setLocalRoles(student_id, ['Owner',])
540            else:
541                level_changed = False
542            cr = getattr(l,course,None)
543            if cr is None:
544                logger.info('Creating Course %(CosCode)s for %(StudentId)s %(Matnumber)s in Level %(Level)s' % result)
545                l.invokeFactory('StudentCourseResult',course)
546            cr = getattr(l,course)
547            dcr = {}
548            from_course = ['title',
549                           'credits',
550                           'passmark',
551                           ]
552            for f in from_course:
553                dcr[f] = getattr(course_doc,f)
554            dlev = {}
555            dcr['ansbook'] = result.get('Ansbook')
556            dcr['semester'] = getInt(result.get('Semster'))
557            dcr['status'] = result.get('CosStuatus')
558            dcr['score'] = getInt(result.get('Score'))
559            dlev['session'] = result.get('Session')
560            dcr['carry_level'] = result.get('CarryLevel')
561            dcr['grade'] = result.get('Grade')
562            dcr['weight'] = result.get('Weight')
563            dlev['verdict'] = result.get('Verdict')
564            dcr['import_id'] = result.get('id')
565            gpa = result.get('GPA').replace(',','.')
566            dlev['imported_gpa'] = getFloat(gpa)
567            cr.getContent().edit(mapping = dcr)
568            cr.manage_setLocalRoles(student_id, ['Owner',])
569            l.getContent().edit(mapping = dlev)
570            if tr_count > MAX_TRANS:
571                transaction.commit()
572                tr_count = 0
573            tr_count += 1
574            wftool.doActionFor(cr,'close')
575        wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
576        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
577
578###)
579
580    security.declareProtected(ModifyPortalContent,"loadJAMBFromCSV")###(
581    def loadJAMBFromCSV(self):
582        """load JAMB data from CSV values"""
583        #return
584        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
585        import transaction
586        tr_count = 0
587        name = 'SampleJAMBDataII'
588        wftool = self.portal_workflow
589        no_import = False
590        if not no_import:
591            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
592            no_import.write('REG-NO,NAME,SEX,STATE,LGA,ENG-SCORE,SUBJ1,SUBJ1-SCORE,SUBJ2,SUBJ2-SCORE,SUBJ3,SUBJ3-SCORE,AGGREGATE,UNIV1,FACULTY1,COURSE1,UNIV2,FACULTY2,COURSE2')
593        logger = logging.getLogger('Import.%s' % name)
594        logger.info('Start loading from %s.csv' % name)
595        try:
596            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
597        except:
598            logger.error('Error reading %s.csv' % name)
599            return
600        for jamb in result:
601            logger.info('processing %(REG-NO)s,%(NAME)s,%(SEX)s,%(STATE)s,%(LGA)s,%(ENG-SCORE)s,%(SUBJ1)s,%(SUBJ1-SCORE)s,%(SUBJ2)s,%(SUBJ2-SCORE)s,%(SUBJ3)s,%(SUBJ3-SCORE)s,%(AGGREGATE)s,%(UNIV1)s,%(FACULTY1)s,%(COURSE1)s,%(UNIV2)s,%(FACULTY2)s,%(COURSE2)s\n' % jamb)
602            jamb_reg_no = jamb.get('REG-NO')
603            res = self.portal_catalog({'meta_type': "StudentApplication",
604                                     'jamb_reg_no': jamb_reg_no })
605            if res:
606                em = 'Student with REG-NO %(REG-NO)s already exists\n' % jamb
607                logger.info(em)
608                no_import.write(em)
609                no_import.write('%(REG-NO)s,%(NAME)s,%(SEX)s,%(STATE)s,%(LGA)s,%(ENG-SCORE)s,%(SUBJ1)s,%(SUBJ1-SCORE)s,%(SUBJ2)s,%(SUBJ2-SCORE)s,%(SUBJ3)s,%(SUBJ3-SCORE)s,%(AGGREGATE)s,%(UNIV1)s,%(FACULTY1)s,%(COURSE1)s,%(UNIV2)s,%(FACULTY2)s,%(COURSE2)s\n' % jamb)
610                continue
611            jamb_name = jamb.get("NAME")
612            jamb_name.replace('>','')
613            names = jamb_name.split()
614            letter = names[-1][0].upper()
615            sid = self.generateStudentId(letter)
616            not_created = True
617            while not_created:
618                try:
619                    students_folder.invokeFactory('Student', sid)
620                    not_created = False
621                except BadRequest:
622                    sid = self.generateStudentId(letter)
623            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
624            student = getattr(self,sid)
625            student.manage_setLocalRoles(sid, ['Owner',])
626            student.invokeFactory('StudentApplication','application')
627            da = {'Title': 'Application Data'}
628            da["jamb_reg_no"] = jamb.get("REG-NO")
629            da["jamb_lastname"] = jamb_name
630            da["jamb_sex"] = jamb.get("SEX")
631            da["jamb_state"] = jamb.get("STATE")
632            da["jamb_lga"] = jamb.get("LGA")
633            da["jamb_score"] = jamb.get("AGGREGATE")
634            da["jamb_first_cos"] = jamb.get("COURSE1")
635            da["jamb_second_cos"] = jamb.get("COURSE2")
636            da["jamb_first_uni"] = jamb.get("UNIV1")
637            da["jamb_second_uni"] = jamb.get("UNIV2")
638            app = student.application
639            app_doc = app.getContent()
640            app_doc.edit(mapping=da)
641            #wftool.doActionFor(app,'open',dest_container=app)
642            app.manage_setLocalRoles(sid, ['Owner',])
643            student.getContent().createSubObjects()
644        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
645    ###)
Note: See TracBrowser for help on using the repository browser.