source: WAeUP_SRP/branches/joachim-event-branch/Students.py @ 1589

Last change on this file since 1589 was 1529, checked in by joachim, 18 years ago

add fixVerdicts

  • Property svn:keywords set to Id
File size: 90.5 KB
Line 
1#-*- mode: python; mode: fold -*-
2# $Id: Students.py 1529 2007-03-08 23:18:47Z joachim $
3from string import Template
4from Globals import InitializeClass
5from AccessControl import ClassSecurityInfo
6from AccessControl.SecurityManagement import newSecurityManager
7from zExceptions import BadRequest
8from Products.ZCatalog.ZCatalog import ZCatalog
9from Products.CMFCore.utils import UniqueObject, getToolByName
10from Products.CMFCore.permissions import View
11from Products.CMFCore.permissions import ModifyPortalContent
12from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder
13#from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument
14from Products.CPSDocument.CPSDocument import CPSDocument
15from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder
16from Products.CPSCore.CPSMembershipTool import CPSUnrestrictedUser
17from Products.WAeUP_SRP.Academics import makeCertificateCode
18import logging
19import csv,re,os
20import Globals
21p_home = Globals.package_home(globals())
22i_home = Globals.INSTANCE_HOME
23MAX_TRANS = 1000
24import DateTime
25# import PIL.Image
26from StringIO import StringIO
27
28def makeCertificateCode(code): ###(
29    code = code.replace('.','')
30    code = code.replace('(','')
31    code = code.replace(')','')
32    code = code.replace('/','')
33    code = code.replace(' ','')
34    code = code.replace('_','')
35    return code
36
37###)
38
39def getInt(s): ###(
40    try:
41        return int(s)
42    except:
43        return 0
44
45def getFloat(s):
46    try:
47        return float(s)
48    except:
49        return 0.0
50
51###)
52
53def getStudentByRegNo(self,reg_no): ###(
54    """search student by JAMB Reg No and return StudentFolder"""
55    search = ZCatalog.searchResults(self.portal_catalog,{'meta_type': 'StudentApplication',
56                                  'SearchableText': reg_no,
57                                  })
58    if len(search) < 1:
59        return None
60    return search[0].getObject().aq_parent
61
62###)
63
64def checkJambNo(jnr):
65    try:
66        if len(jnr) != 10:
67            return False
68    except:
69        return False
70    try:
71        int(jnr[:8])
72        return True
73    except:
74        return False
75
76class StudentsFolder(CPSDocument): ###(
77    """
78    WAeUP container for the various WAeUP containers data
79    """
80    meta_type = 'StudentsFolder'
81    portal_type = meta_type
82    security = ClassSecurityInfo()
83
84    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsFromCSV")###(
85    def loadFullTimeStudentsFromCSV(self):
86        """load Fulltime Studentdata from CSV values"""
87        import transaction
88        import random
89        tr_count = 0
90        name = 'short_full_time'
91        no_import = False
92        if not no_import:
93            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
94            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')
95        logger = logging.getLogger('Import.%s' % name)
96        logger.info('Start loading from %s.csv' % name)
97        pwlist  = []
98        pwlist.append('"student_id","firstname","middlename","lastname","matric_no","jamb_reg_no","access_code"')
99        pwl_template = Template('"$student_id","$firstname","$middlename","$lastname","$matric_no","$jamb_reg_no","$access_code"')
100        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
101        try:
102            students = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
103        except:
104            logger.error('Error reading %s.csv' % name)
105            return
106        l = self.portal_catalog({'meta_type': "StudentClearance",})
107        matrics = []
108        for s in l:
109            matrics.append(s.getObject().getContent().matric_no)
110        print matrics
111        l = self.portal_catalog({'meta_type': "Certificate"})
112        certs = {}
113        for c in l:
114            ca,ac,fa,dep_id,co,certcode = c.relative_path.split('/')
115            cid = "%(dep_id)s_%(certcode)s" % vars()
116            certs[cid] = c.getObject()
117        for student in students:
118            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)
119            sid = student.get('MatricNo')
120            if sid == "":
121                em = 'Empty MatricNo\n'
122                logger.info(em)
123                no_import.write(em)
124                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)
125                continue
126            certcode = makeCertificateCode(student.get('CourseMajor'))
127            dep_id = student.get('Dept')
128            fac_id = student.get('Faculty')
129            cid = "%(dep_id)s_%(certcode)s" % vars()
130            if cid not in certs.keys():
131                em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
132                logger.info(em)
133                no_import.write(em)
134                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)
135                continue
136            certificate_doc = certs[cid].getContent()
137            level = student.get('StudentLevel')
138            try:
139                int(level)
140            except:
141                em = 'Student with ID %(MatricNo)s StudentLevel is empty\n' % student
142                logger.info(em)
143                no_import.write(em)
144                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)
145                continue
146            matric_no = student.get('MatricNo')
147            if matric_no not in matrics:
148                matrics.append(matric_no)
149                sid = self.generateStudentId(student.get('Lastname')[0])
150                #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
151                students_folder.invokeFactory('Student', sid)
152                logger.info('%(tr_count)s: Creating Student with ID %(sid)s Matric_no %(matric_no)s ' % vars())
153                student_obj = getattr(self,sid)
154                access_code = "%d" % random.randint(1000000000,9999999999)
155                student_obj.getContent().makeStudentMember(sid,access_code,)
156                pwl_dict = {'student_id': sid,'access_code':access_code}
157                student_obj.invokeFactory('StudentApplication','application')
158                application = student_obj.application
159                da = {'Title': 'Application Data'}
160                student_obj.invokeFactory('StudentPersonal','personal')
161                da['jamb_reg_no'] = student.get('EntryRegNo')
162                personal = student_obj.personal
163                dp = {'Title': 'Personal Data'}
164                student_obj.invokeFactory('StudentClearance','clearance')
165                clearance = student_obj.clearance
166                dc = {'Title': 'Clearance/Eligibility Record'}
167                dc['matric_no'] = matric_no
168                state = student.get('State')
169                lga = student.get('LGA')
170                if state and lga:
171                    lga =  state + ' / ' + lga
172                else:
173                    lga = "None"
174                dc['lga'] = lga
175                dc['nationality'] = student.get('Nationality')
176                dc['email'] = student.get('Emailaddress')
177                dp['firstname'] = student.get('FirstName')
178                dp['middlename'] = student.get('MiddleName')
179                dp['lastname'] = student.get('Lastname')
180                dp['former_surname'] = student.get('FormerSurname')
181                dp['sex'] = student.get('Sex') == 'F'
182                dp['perm_address'] = student.get('PermanentAddress')
183                dp['perm_city'] = student.get('PermanentAddressCity')
184                dp['campus_address'] = student.get('CampusAddress')
185                dp['phone'] = student.get('PhoneNumber')
186                application.getContent().edit(mapping=da)
187                personal.getContent().edit(mapping=dp)
188                clearance.getContent().edit(mapping=dc)
189                #
190                # Study Course
191                #
192                student_obj.invokeFactory('StudentStudyCourse','study_course')
193                studycourse = student_obj.study_course
194                dsc = {}
195                from_certificate = ['title',
196                                   'max_elect',
197                                   'max_pass',
198                                   'n_core',
199                                   'nr_years',
200                                   'probation_credits',
201                                   'promotion_credits',
202                                   'start_level',
203                                  ]
204                for f in from_certificate:
205                    dsc[f] = getattr(certificate_doc,f)
206                dsc['faculty'] = fac_id
207                dsc['department'] = dep_id
208                dsc['study_course'] = certcode
209                css = student.get('CurrentSession') or '2004-2005'
210                cs = int(css.split('-')[0]) - 2000
211                cl = int(student.get('StudentLevel') or '100')/100
212                dsc['entry_session'] = "200%s" % (cs - cl)
213                dsc['clr_ac_pin'] = access_code
214                studycourse.getContent().edit(mapping=dsc)
215                #
216                # Level
217                #
218##                l = getattr(studycourse,level,None)
219##                if 0 and l is None:
220##                    #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
221##                    logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student)
222##                    studycourse.invokeFactory('StudentStudyLevel', level)
223##                    l = getattr(studycourse, level)
224##                    certificate = certs[certcode]
225##                    cert_level = getattr(certificate,level,None)
226##                    if cert_level is None:
227##                        logger.info('Level %(level)s not in %(certcode)s' % vars())
228##                    l.getContent().edit(mapping={'Title': "Level %s" % level})
229            else:
230                em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
231                logger.info(em)
232                no_import.write(em)
233                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)
234                continue
235            if tr_count > MAX_TRANS:
236                transaction.commit()
237                em = 'Transaction commited\n' % student
238                logger.info(em)
239                tr_count = 0
240            tr_count += 1
241            pwl_dict.update(dc)
242            pwl_dict.update(da)
243            pwl_dict.update(dp)
244            wftool = self.portal_workflow
245            pwlist.append(pwl_template.substitute(pwl_dict))
246            wftool.doActionFor(student_obj,'clear_and_validate')
247            student_obj.manage_setLocalRoles(sid, ['Owner',])
248            wftool.doActionFor(application,'close')
249            application.manage_setLocalRoles(sid, ['Owner',])
250            wftool.doActionFor(clearance,'close')
251            clearance.manage_setLocalRoles(sid, ['Owner',])
252            wftool.doActionFor(personal,'close')
253            personal.manage_setLocalRoles(sid, ['Owner',])
254            wftool.doActionFor(studycourse,'close_for_edit')
255            studycourse.manage_setLocalRoles(sid, ['Owner',])
256        open("%s/import/pwlist-%s.csv" % (i_home,name),"w+").write('\n'.join(pwlist))
257        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
258    ###)
259
260    security.declareProtected(ModifyPortalContent,"loadPumeResultsFromCSV")###(
261    def loadPumeResultsFromCSV(self):
262        """load Fulltime Studentdata from CSV values into pumeresults catalog"""
263        import transaction
264        import random
265##        csv_d = {'jamb_reg_no': "RegNumber", ###(
266##                 'status': "Admission Status",
267##                 'name': "Name",
268##                 'score': "Score",
269##                 'sex': "Sex",
270##                 'faculty': "Faculty",
271##                 'department': "Dept",
272##                 'course': "Course",
273##                 'course_code_org': "Course Code",
274##                 }
275###)
276        csv_d = {'jamb_reg_no': "JAMBRegno",
277                 'name': "Name",
278                 'score': "Score",
279                 'sex': "Sex",
280                 'course': "Course",
281                 'faculty': "Faculty",
282                 'department': "Dept",
283                 'course_code_org': "Course Code",
284                 'status': "Admission Status",
285                 'result_type': None,
286                 }
287        csv_fields = [f[1] for f in csv_d.items() if f[1]]
288        tr_count = 0
289        total = 0
290        #name = 'pup_new'
291        name = 'pup_update'
292        update = name.endswith('update')
293        no_import = []
294        ok_import = []
295        ok_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_d.keys()]))
296        no_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_fields]))
297        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
298        ok_import_name = "%s/import/%s_imported_%s.csv" % (i_home,name,current)
299        #open(ok_import_name,"w").write('\n'.join(no_import))
300        no_import_name = "%s/import/%s_not_imported_%s.csv" % (i_home,name,current)
301        #open(no_import_name,"w").write('\n'.join(no_import))
302        logger = logging.getLogger('Import.%s' % name)
303        starttime = DateTime.now()
304        logger.info('Start loading from %s.csv' % name)
305        try:
306            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
307        except:
308            logger.error('Error reading %s.csv' % name)
309            return
310        pume = self.portal_pumeresults
311        format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
312        import_format = ','.join(['"%%(%s)s"' % fn for fn in csv_d.keys()])
313        eduplicate = '%s,"duplicate"' % format
314        einvalidjamb = '%s,"invalid JambRegNo"' % format
315        added = 'added ,%s' % format
316        #from pdb import set_trace;set_trace()
317        for jamb in result:
318            dict = {}
319            for f,fn in csv_d.items():
320                dict[f] = jamb.get(csv_d[f])
321            dict['result_type'] = 'DE'
322            jnr = jamb.get(csv_d['jamb_reg_no'])
323            if not checkJambNo(jnr):
324                logger.info(einvalidjamb % jamb)
325                dd = {}
326                for f,fn in csv_d.items():
327                    dd[fn] = getattr(data,f)
328                    no_import.append(eduplicate % dd)
329                    no_import.append(eduplicate % jamb)
330                continue
331            res = pume(jamb_reg_no=jnr)
332            if len(res) > 0:
333                if update:
334                    try:
335                        pume.modifyRecord(**dict)
336                    except ValueError:
337                        logger.info(eduplicate % jamb)
338                        continue
339                    except KeyError:
340                        pume.addRecord(**dict)
341                        logger.info(added % jamb)
342                        continue
343                else:
344                    data = res[0]
345                    if data.name != jamb.get(csv_d['name']):
346                        #set_trace()
347                        logger.info(eduplicate % jamb)
348                        #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
349                        #logger.info(em)
350                        dd = {}
351                        for f,fn in csv_d.items():
352                            dd[fn] = getattr(data,f)
353                        no_import.append(eduplicate % dd)
354                        no_import.append(eduplicate % jamb)
355                    continue
356            try:
357                pume.addRecord(**dict)
358                ok_import.append(import_format % dict)
359            except ValueError:
360                logger.info(eduplicate % jamb)
361                #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
362                #logger.info(em)
363                no_import.append(eduplicate % jamb)
364        logger.info('End loading from %s.csv' % name)
365        if len(no_import) > 1:
366            open(no_import_name,"w+").write('\n'.join(no_import))
367        open(ok_import_name,"w+").write('\n'.join(ok_import))
368        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
369    ###)
370
371    security.declareProtected(ModifyPortalContent,"createDEStudents")###(
372    def createDEStudents(self):
373        """load Fulltime Studentdata from CSV values"""
374        import transaction
375        import random
376        #from pdb import set_trace
377        wftool = self.portal_workflow
378        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
379        csv_d = {'jamb_reg_no': "RegNumber",
380                 'jamb_lastname': "Name",
381                 'session': "Session",
382                 'pume_tot_score': "PUDE SCORE",
383                 ##'jamb_score': "JambScore",
384                 'entry_mode': "EntryMode",
385                 'jamb_sex': "Sex",
386                 'jamb_state': "State",
387                 'jamb_first_cos': "AdminCourse",
388                 'faculty': "AdminFaculty",
389                 'course_code': "AdmitCoscode",
390                 'stud_status':"AdmitStatus",
391                 'department': "AdmitDept",
392                 'jamb_lga': "LGA",
393                 'app_email': "email",
394                 'app_mobile': "PhoneNumbers",
395                 }
396        csv_fields = [f[1] for f in csv_d.items()]
397        tr_count = 0
398        total = 0
399        #name = 'pume_results'
400        name = 'DE_Admitted'
401        no_import = []
402        s = ','.join(['"%s"' % fn for fn in csv_fields])
403        no_import.append('"Error",%s' % s)
404        format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
405        no_certificate = "no certificate %s" % format
406        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write('\n'.join(no_import))
407        logger = logging.getLogger('Import.%s' % name)
408        logger.info('start loading from %s.csv' % name)
409        l = self.portal_catalog({'meta_type': "Certificate"})
410        certs = {}
411        cert_docs = {}
412        for f in l:
413            certs[f.getId] = f.getObject().getContent()
414        try:
415            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
416        except:
417            logger.error('Error reading %s.csv' % name)
418            return
419        for jamb in result:
420            jamb['Error'] = "Processing "
421            logger.info(format % jamb)
422            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
423            res = self.portal_catalog({'portal_type': "StudentApplication",
424                                     'SearchableText': jamb_reg_no })
425            if res:
426                em = 'Student with RegNo %s already exists\n' % jamb_reg_no
427                logger.info(em)
428                jamb['Error'] = "Student exists"
429                no_import.append(format % jamb)
430                continue
431            cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
432            if cert_id not in certs.keys():
433                em = 'No Certificate with ID %s \n' % cert_id
434                logger.info(em)
435                jamb['Error'] = "No Certificate %s" % cert_id
436                no_import.append( format % jamb)
437                continue
438            jamb_reg_no =jamb.get(csv_d['jamb_reg_no'])
439            cert_doc = certs[cert_id]
440            catalog_entry = {}
441            catalog_entry['jamb_reg_no'] = jamb_reg_no
442            jamb_name = jamb.get(csv_d['jamb_lastname'])
443            jamb_name.replace('>','')
444            jamb_name.replace('<','')
445            names = jamb_name.split()
446            letter = names[-1][0].upper()
447            sid = self.generateStudentId(letter)
448            not_created = True
449            while not_created:
450                try:
451                    students_folder.invokeFactory('Student', sid)
452                    not_created = False
453                except BadRequest:
454                    sid = self.generateStudentId(letter)
455            catalog_entry['id'] = sid
456            tr_count += 1
457            logger.info('%(total)s+%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
458            student = getattr(self,sid)
459            student.manage_setLocalRoles(sid, ['Owner',])
460            student.invokeFactory('StudentPume','pume')
461            dp = {'Title': 'Pume Data'}
462            student.invokeFactory('StudentApplication','application')
463            da = {'Title': 'Application Data'}
464            da["jamb_lastname"] = jamb_name
465            da_fields = ('jamb_reg_no',
466                         'jamb_sex',
467                         'entry_mode',
468                         #'jamb_score',
469                         'jamb_first_cos',
470                         'jamb_sex',
471                         'jamb_state',
472                         'jamb_lga',
473                         'app_email',
474                         'app_mobile',
475                         )
476            for f in da_fields:
477                da[f] = jamb.get(csv_d[f])
478            catalog_entry['email'] = jamb.get(csv_d['app_email'])
479            app = student.application
480            app_doc = app.getContent()
481            picture ="%s/import/pictures/%s.jpg" % (i_home,jamb_reg_no)
482            #import pdb;pdb.set_trace()
483            if os.path.exists(picture):
484                file = open(picture)
485                if False:
486                    img = PIL.Image.open(file)
487                    img.thumbnail((150,200),
488                                  resample=PIL.Image.ANTIALIAS)
489                    # We now need a buffer to write to. It can't be the same
490                    # as the inbuffer as the PNG writer will write over itself.
491                    outfile = StringIO()
492                    img.save(outfile, format=img.format)
493                else:
494                    outfile = file.read()
495                app_doc.manage_addFile('passport',
496                                       file=outfile,
497                                       title="%s.jpg" % jamb_reg_no)
498            app.getContent().edit(mapping=da)
499            app.manage_setLocalRoles(sid, ['Owner',])
500            #wftool.doActionFor(app,'close')
501            dp_fields = (
502                         #'pume_eng_score',
503                         #'pume_gen_score',
504                         'pume_tot_score',
505                         )
506            dp['pume_tot_score'] = jamb.get(csv_d['pume_tot_score']) or "No Option Shaded"
507            pume = student.pume
508            pume.getContent().edit(mapping=dp)
509            #wftool.doActionFor(pume,'close')
510            pume.manage_setLocalRoles(sid, ['Owner',])
511            #student.getContent().createSubObjects()
512            dp = {}
513            if len(names) == 3:
514                dp['firstname'] = names[0].capitalize()
515                dp['middlename'] = names[1].capitalize()
516                dp['lastname'] = names[2].capitalize()
517            elif len(names) == 2:
518                dp['firstname'] = names[0].capitalize()
519                dp['middlename'] = ''
520                dp['lastname'] = names[1].capitalize()
521            else:
522                dp['firstname'] = ''
523                dp['middlename'] = ''
524                dp['lastname'] = jamb_name
525            dp['sex'] = jamb.get(csv_d['jamb_sex']) == 'F'
526            catalog_entry['sex'] = dp['sex']
527            catalog_entry['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
528            student.invokeFactory('StudentPersonal','personal')
529            per = student.personal
530            per_doc = per.getContent()
531            per_doc.edit(mapping = dp)
532            per.manage_setLocalRoles(sid, ['Owner',])
533            if jamb.get(csv_d['stud_status']) == "Admitted":
534                wftool.doActionFor(student,'pume_pass')
535                wftool.doActionFor(student,'admit')
536            else:
537                wftool.doActionFor(student,'pume_fail')
538                wftool.doActionFor(student,'reject_admission')
539                continue
540            #
541            # Clearance
542            #
543            student.invokeFactory('StudentClearance','clearance')
544            #wftool.doActionFor(student.clearance,'open')
545            dp = {'Title': 'Clearance/Eligibility Record'}
546            student.clearance.manage_setLocalRoles(sid, ['Owner',])
547            #
548            # Study Course
549            #
550            student.invokeFactory('StudentStudyCourse','study_course')
551            study_course = student.study_course
552            dsc = {}
553            #from_certificate = ['title',
554            #                   'max_elect',
555            #                   'max_pass',
556            #                   'n_core',
557            #                   'nr_years',
558            #                   'probation_credits',
559            #                   'promotion_credits',
560            #                   'start_level',
561            #                  ]
562            #for f in from_certificate:
563            #    dsc[f] = getattr(cert_doc,f)
564            #dsc['faculty'] = jamb.get(csv_d['faculty'])
565            #dsc['department'] = jamb.get(csv_d['department'])
566            catalog_entry['faculty'] = jamb.get(csv_d['faculty'])
567            catalog_entry['department'] = jamb.get(csv_d['department'])
568            catalog_entry['course'] = cert_id
569            #catalog_entry['level'] = getattr(cert_doc,'start_level')
570            catalog_entry['level'] = '200'
571            dsc['study_course'] = cert_id
572            dsc['current_level'] = '200'
573            #dsc['entry_session'] = jamb.get(csv_d['session'])
574            study_course.getContent().edit(mapping=dsc)
575            self.students_catalog.addRecord(**catalog_entry)
576            if tr_count > 10:
577                if len(no_import) > 1:
578                    open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
579                             '\n'.join(no_import)+'\n')
580                    no_import = []
581                em = '%d transactions commited\n' % tr_count
582                transaction.commit()
583                logger.info(em)
584                total += tr_count
585                tr_count = 0
586        open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
587                                               '\n'.join(no_import))
588        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
589    ###)
590
591    security.declareProtected(ModifyPortalContent,"createNewStudents")###(
592    def createNewStudents(self):
593        """load Fulltime Studentdata from CSV values"""
594        import transaction
595        import random
596        #from pdb import set_trace
597        wftool = self.portal_workflow
598        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
599        csv_d = {'jamb_reg_no': "RegNumber",
600                 'jamb_lastname': "Name",
601                 'session': "Session",
602                 'pume_tot_score': "PUME SCORE",
603                 'jamb_score': "JambScore",
604                 'jamb_sex': "Sex",
605                 'jamb_state': "State",
606##                 'jamb_first_cos': "AdminCourse",
607                 'faculty': "AdminFaculty",
608                 'course_code': "AdmitCoscode",
609                 'stud_status':"AdmitStatus",
610                 'department': "AdmitDept",
611                 'jamb_lga': "LGA",
612                 'app_email': "email",
613                 'app_mobile': "PhoneNumbers",
614                 }
615        csv_fields = [f[1] for f in csv_d.items()]
616        tr_count = 0
617        total = 0
618        #name = 'pume_results'
619        name = 'Admitted'
620        no_import = []
621        s = ','.join(['"%s"' % fn for fn in csv_fields])
622        no_import.append('"Error",%s' % s)
623        format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
624        no_certificate = "no certificate %s" % format
625        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write('\n'.join(no_import))
626        logger = logging.getLogger('Import.%s' % name)
627        logger.info('start loading from %s.csv' % name)
628        l = self.portal_catalog({'meta_type': "Certificate"})
629        certs = {}
630        cert_docs = {}
631        for f in l:
632            certs[f.getId] = f.getObject().getContent()
633        try:
634            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
635        except:
636            logger.error('Error reading %s.csv' % name)
637            return
638        for jamb in result:
639            jamb['Error'] = "Processing "
640            logger.info(format % jamb)
641            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
642            res = self.portal_catalog({'portal_type': "StudentApplication",
643                                     'SearchableText': jamb_reg_no })
644            if res:
645                em = 'Student with RegNo %s already exists\n' % jamb_reg_no
646                logger.info(em)
647                jamb['Error'] = "Student exists"
648                no_import.append(format % jamb)
649                continue
650            cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
651            if cert_id not in certs.keys():
652                em = 'No Certificate with ID %s \n' % cert_id
653                logger.info(em)
654                jamb['Error'] = "No Certificate %s" % cert_id
655                no_import.append( format % jamb)
656                continue
657            res = self.portal_pumeresults(jamb_reg_no = jamb_reg_no)
658            if len(res) == 1:
659                self.portal_pumeresults.modifyRecord(jamb_reg_no = jamb_reg_no,
660                                                     status = jamb.get(csv_d['stud_status']),
661                                                     )
662            jamb_reg_no =jamb.get(csv_d['jamb_reg_no'])
663            cert_doc = certs[cert_id]
664            catalog_entry = {}
665            catalog_entry['jamb_reg_no'] = jamb_reg_no
666            jamb_name = jamb.get(csv_d['jamb_lastname'])
667            jamb_name.replace('>','')
668            jamb_name.replace('<','')
669            names = jamb_name.split()
670            letter = names[-1][0].upper()
671            sid = self.generateStudentId(letter)
672            not_created = True
673            while not_created:
674                try:
675                    students_folder.invokeFactory('Student', sid)
676                    not_created = False
677                except BadRequest:
678                    sid = self.generateStudentId(letter)
679            catalog_entry['id'] = sid
680            tr_count += 1
681            logger.info('%(total)s+%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
682            student = getattr(self,sid)
683            student.manage_setLocalRoles(sid, ['Owner',])
684            student.invokeFactory('StudentPume','pume')
685            dp = {'Title': 'Pume Data'}
686            student.invokeFactory('StudentApplication','application')
687            da = {'Title': 'Application Data'}
688            da["jamb_lastname"] = jamb_name
689            da_fields = ('jamb_reg_no',
690                         'jamb_sex',
691                         #'jamb_state',
692                         'jamb_score',
693##                         'jamb_first_cos',
694                         'jamb_sex',
695                         'jamb_state',
696                         'jamb_lga',
697                         'app_email',
698                         'app_mobile',
699                         )
700            for f in da_fields:
701                da[f] = jamb.get(csv_d[f])
702            catalog_entry['email'] = jamb.get(csv_d['app_email'])
703            app = student.application
704            app_doc = app.getContent()
705            picture ="%s/import/pictures/%s.jpg" % (i_home,jamb_reg_no)
706            #import pdb;pdb.set_trace()
707            if os.path.exists(picture):
708                file = open(picture)
709                if False:
710                    img = PIL.Image.open(file)
711                    img.thumbnail((150,200),
712                                  resample=PIL.Image.ANTIALIAS)
713                    # We now need a buffer to write to. It can't be the same
714                    # as the inbuffer as the PNG writer will write over itself.
715                    outfile = StringIO()
716                    img.save(outfile, format=img.format)
717                else:
718                    outfile = file.read()
719                app_doc.manage_addFile('passport',
720                                       file=outfile,
721                                       title="%s.jpg" % jamb_reg_no)
722            app.getContent().edit(mapping=da)
723            app.manage_setLocalRoles(sid, ['Owner',])
724            #wftool.doActionFor(app,'close')
725            dp_fields = (
726                         #'pume_eng_score',
727                         #'pume_gen_score',
728                         'pume_tot_score',
729                         )
730            dp['pume_tot_score'] = jamb.get(csv_d['pume_tot_score']) or "No Option Shaded"
731            pume = student.pume
732            pume.getContent().edit(mapping=dp)
733            #wftool.doActionFor(pume,'close')
734            pume.manage_setLocalRoles(sid, ['Owner',])
735            #student.getContent().createSubObjects()
736            dp = {}
737            if len(names) == 3:
738                dp['firstname'] = names[0].capitalize()
739                dp['middlename'] = names[1].capitalize()
740                dp['lastname'] = names[2].capitalize()
741            elif len(names) == 2:
742                dp['firstname'] = names[0].capitalize()
743                dp['middlename'] = ''
744                dp['lastname'] = names[1].capitalize()
745            else:
746                dp['firstname'] = ''
747                dp['middlename'] = ''
748                dp['lastname'] = jamb_name
749            dp['sex'] = jamb.get(csv_d['jamb_sex']) == 'F'
750            catalog_entry['sex'] = dp['sex']
751            catalog_entry['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
752            student.invokeFactory('StudentPersonal','personal')
753            per = student.personal
754            per_doc = per.getContent()
755            per_doc.edit(mapping = dp)
756            per.manage_setLocalRoles(sid, ['Owner',])
757            if jamb.get(csv_d['stud_status']) == "Admitted":
758                wftool.doActionFor(student,'pume_pass')
759                wftool.doActionFor(student,'admit')
760            else:
761                wftool.doActionFor(student,'pume_fail')
762                wftool.doActionFor(student,'reject_admission')
763                continue
764            #
765            # Clearance
766            #
767            student.invokeFactory('StudentClearance','clearance')
768            #wftool.doActionFor(student.clearance,'open')
769            dp = {'Title': 'Clearance/Eligibility Record'}
770            student.clearance.manage_setLocalRoles(sid, ['Owner',])
771            #
772            # Study Course
773            #
774            student.invokeFactory('StudentStudyCourse','study_course')
775            study_course = student.study_course
776            dsc = {}
777            #from_certificate = ['title',
778            #                   'max_elect',
779            #                   'max_pass',
780            #                   'n_core',
781            #                   'nr_years',
782            #                   'probation_credits',
783            #                   'promotion_credits',
784            #                   'start_level',
785            #                  ]
786            #for f in from_certificate:
787            #    dsc[f] = getattr(cert_doc,f)
788            #dsc['faculty'] = jamb.get(csv_d['faculty'])
789            #dsc['department'] = jamb.get(csv_d['department'])
790            catalog_entry['faculty'] = jamb.get(csv_d['faculty'])
791            catalog_entry['department'] = jamb.get(csv_d['department'])
792            catalog_entry['course'] = cert_id
793            #catalog_entry['level'] = getattr(cert_doc,'start_level')
794            catalog_entry['level'] = '100'
795            dsc['study_course'] = cert_id
796            dsc['entry_level'] = '100'
797            #dsc['entry_session'] = jamb.get(csv_d['session'])
798            study_course.getContent().edit(mapping=dsc)
799            self.students_catalog.addRecord(**catalog_entry)
800            if tr_count > 10:
801                if len(no_import) > 0:
802                    open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
803                             '\n'.join(no_import) + "\n")
804                    no_import = []
805                em = '%d transactions commited\n' % tr_count
806                transaction.commit()
807                logger.info(em)
808                total += tr_count
809                tr_count = 0
810        open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
811                                                '\n'.join(no_import))
812        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
813    ###)
814
815    security.declareProtected(ModifyPortalContent,"importReturningStudents")###(
816    def importReturningStudents(self):
817        """load Returning Studentdata from CSV values"""
818        import transaction
819        import random
820        #from pdb import set_trace
821        wftool = self.portal_workflow
822        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
823        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
824        tr_count = 1
825        total = 0
826        #name = 'pume_results'
827        name = 'Returning'
828        table = self.returning_import
829        no_import = []
830        imported = []
831        logger = logging.getLogger('Import.%s' % name)
832        try:
833            returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
834        except:
835            logger.error('Error reading %s.csv' % name)
836            return
837        l = self.portal_catalog({'meta_type': "Certificate"})
838        certs = {}
839        cert_docs = {}
840        for f in l:
841            certs[f.getId] = f.getObject().getContent()
842        start = True
843        res = table()
844        regs = []
845        if len(res) > 0:
846            regs = [s.matric_no for s in res]
847        #import pdb;pdb.set_trace()
848        for student in returning:
849            if start:
850                start = False
851                logger.info('start loading from %s.csv' % name)
852                s = ','.join(['"%s"' % fn for fn in student.keys()])
853                imported.append(s)
854                no_import.append('%s,"Error"' % s)
855                format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()])
856                format_error = format + ',"%(Error)s"'
857                no_certificate = "no certificate %s" % format
858            student['matric_no'] = matric_no = student.get('matric_no').upper()
859            student['Mode_of_Entry'] = entry_mode = student.get('Mode of Entry').upper()
860            student['Permanent_Address'] = perm_address = student.get('Permanent Address')
861            if matric_no == '':
862                student['Error'] = "Empty matric_no"
863                no_import.append( format_error % student)
864                continue
865            if matric_no in regs or self.returning_import(matric_no = matric_no):
866                student['Error'] = "Duplicate"
867                no_import.append( format_error % student)
868                continue
869            cert_id = makeCertificateCode(student.get('Coursemajorcode'))
870            if cert_id not in certs.keys():
871                student['Error'] = "No Certificate %s" % cert_id
872                no_import.append( format_error % student)
873                continue
874            try:
875                table.addRecord(**student)
876            except ValueError:
877                student['Error'] = "Duplicate"
878                no_import.append( format_error % student)
879                continue
880            regs.append(student.get('matric_no'))
881            imported.append(format % student)
882            tr_count += 1
883            if tr_count > 1000:
884                if len(no_import) > 0:
885                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
886                             '\n'.join(no_import) + '\n')
887                    no_import = []
888                open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
889                                            '\n'.join(no_import) + "\n")
890                imported = []
891                em = '%d transactions commited total %s\n' % (tr_count,total)
892                transaction.commit()
893                regs = []
894                logger.info(em)
895                total += tr_count
896                tr_count = 0
897        open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
898                                            '\n'.join(imported))
899        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
900                                                '\n'.join(no_import))
901        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
902    ###)
903
904    security.declareProtected(ModifyPortalContent,"fixVerdicts")###(
905    def fixVerdicts(self,csv_file=None):
906        """fix wrong uploaded verdicts"""
907        import transaction
908        import random
909        wftool = self.portal_workflow
910        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
911        students_folder = self.portal_url.getPortalObject().campus.students
912        tr_count = 1
913        total = 0
914        if csv_file is None:
915            name = 'Verdicts'
916        else:
917            name = csv_file
918        st_cat = self.students_catalog
919        no_import = []
920        verdicts_voc = self.portal_vocabularies.verdicts
921        rverdicts = {}
922        for k,v in verdicts_voc.items():
923            rverdicts[v.upper()] = k
924        logger = logging.getLogger('fixVerdicts')
925        try:
926            verdicts = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
927        except:
928            logger.error('Error reading %s.csv' % name)
929            return
930        start = True
931        #import pdb;pdb.set_trace()
932        for verdict in verdicts:
933            if start:
934                start = False
935                logger.info('start loading from %s.csv' % name)
936                s = ','.join(['"%s"' % fn for fn in verdict.keys()])
937                no_import.append('%s,"Error"' % s)
938                format = ','.join(['"%%(%s)s"' % fn for fn in verdict.keys()])
939                format_error = format + ',"%(Error)s"'
940            matric_no = verdict.get('MAT NO')
941            if not matric_no:
942                continue
943            matric_no = matric_no.upper()
944            if matric_no == '':
945                continue
946            verdict_code = rverdicts.get(verdict.get('CATEGORY'),None)
947            if verdict_code is None:
948                continue
949            sres = st_cat(matric_no = matric_no)
950            if sres:
951                student_id = sres[0].id
952                student_obj = getattr(students_folder,student_id,None)
953                if student_obj:
954                    study_course = getattr(student_obj,'study_course',None)
955                    if study_course is None:
956                        verdict['Error'] = "Student did not yet log in"
957                        no_import.append( format_error % verdict)
958                        continue
959                try:
960                    st_cat.modifyRecord(id = student_id,
961                                    verdict=verdict_code)
962                except KeyError:
963                    student['Error'] = "Not found in students_catalog"
964                    no_import.append( format_error % student)
965                    continue
966                dsc = {}
967                dsc['current_verdict'] = verdict_code
968                study_course.getContent().edit(mapping=dsc)
969            tr_count += 1
970            if tr_count > 1000:
971                if len(no_import) > 0:
972                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
973                             '\n'.join(no_import) + '\n')
974                    no_import = []
975                em = '%d transactions commited total %s\n' % (tr_count,total)
976                transaction.commit()
977                regs = []
978                logger.info(em)
979                total += tr_count
980                tr_count = 0
981        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
982                                                '\n'.join(no_import))
983        total += tr_count
984        em = '%d total transactions commited' % (total)
985        logger.info(em)
986        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
987    ###)
988
989    security.declareProtected(ModifyPortalContent,"fixAllEntryModeForReturning")###(
990    def fixAllEntryModeForReturning(self):
991        "read all Returning*.csv"
992        ipath = "%s/import/" % i_home
993        names = os.listdir(ipath)
994        for name in names:
995            head,tail = os.path.splitext(name)
996            if head.startswith('Returning')\
997               and tail == '.csv'\
998               and name.find('imported') < 0:
999                self.fixEntryModeForReturning(csv_file=head)
1000    ###)
1001
1002    security.declareProtected(ModifyPortalContent,"fixEntryModeForReturning")###(
1003    def fixEntryModeForReturning(self,csv_file=None):
1004        """load Returning Studentdata from CSV values"""
1005        import transaction
1006        import random
1007        wftool = self.portal_workflow
1008        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
1009        students_folder = self.portal_url.getPortalObject().campus.students
1010        tr_count = 1
1011        total = 0
1012        if csv_file is None:
1013            name = 'Returning'
1014        else:
1015            name = csv_file
1016        table = self.returning_import
1017        st_cat = self.students_catalog
1018        no_import = []
1019        logger = logging.getLogger('getEntryMode.%s' % name)
1020        try:
1021            returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1022        except:
1023            logger.error('Error reading %s.csv' % name)
1024            return
1025        start = True
1026        for student in returning:
1027            if start:
1028                start = False
1029                logger.info('start loading from %s.csv' % name)
1030                s = ','.join(['"%s"' % fn for fn in student.keys()])
1031                no_import.append('%s,"Error"' % s)
1032                format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()])
1033                format_error = format + ',"%(Error)s"'
1034            matric_no = student.get('matric_no')
1035            if not matric_no:
1036                continue
1037            matric_no = matric_no.upper()
1038            student['matric_no'] = matric_no
1039            if matric_no == '':
1040                continue
1041            if not table(matric_no = matric_no):
1042                student['Error'] = "Not imported yet"
1043                no_import.append( format_error % student)
1044                continue
1045            student_id = None
1046            app = None
1047            per = None
1048            if st_cat(matric_no = matric_no):
1049                student_id = st_cat(matric_no = matric_no)[0].id
1050                student_obj = getattr(students_folder,student_id,None)
1051                if student_obj:
1052                    app = getattr(student_obj,'application',None)
1053                    if app is not None:
1054                        app_doc = app.getContent()
1055                    per = getattr(student_obj,'personal',None)
1056                    if per is not None:
1057                        per_doc = per.getContent()
1058            student['Mode_of_Entry'] = entry_mode = student.get('Mode of Entry').upper()
1059            student['Permanent_Address'] = perm_address = student.get('Permanent Address')
1060            #import pdb;pdb.set_trace()
1061            if not entry_mode:
1062                student['Error'] = "'Mode of Entry' empty"
1063                no_import.append( format_error % student)
1064                continue
1065            try:
1066                table.modifyRecord(matric_no = matric_no,
1067                                   Mode_of_Entry = entry_mode,
1068                                   Permanent_Address = perm_address)
1069            except KeyError:
1070                student['Error'] = "Not found in returning_import"
1071                no_import.append( format_error % student)
1072                continue
1073            if student_id is not None:
1074                try:
1075                    st_cat.modifyRecord(id = student_id,
1076                                        entry_mode=entry_mode)
1077                except KeyError:
1078                    student['Error'] = "Not found in students_catalog"
1079                    no_import.append( format_error % student)
1080                    continue
1081            if app is not None:
1082                da = {}
1083                da['entry_mode'] = entry_mode
1084                app_doc.edit(mapping=da)
1085            if per is not None:
1086                dp = {}
1087                dp['perm_address'] = perm_address
1088                per_doc.edit(mapping=dp)
1089            tr_count += 1
1090            if tr_count > 1000:
1091                if len(no_import) > 0:
1092                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1093                             '\n'.join(no_import) + '\n')
1094                    no_import = []
1095                em = '%d transactions commited total %s\n' % (tr_count,total)
1096                transaction.commit()
1097                regs = []
1098                logger.info(em)
1099                total += tr_count
1100                tr_count = 0
1101        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1102                                                '\n'.join(no_import))
1103        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1104    ###)
1105
1106    security.declareProtected(ModifyPortalContent,"updateReturningStudents")###(
1107    def updateReturningStudents(self):
1108        """load and overwrite Returning Student Data from CSV values"""
1109        import transaction
1110        import random
1111        #from pdb import set_trace
1112        wftool = self.portal_workflow
1113        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
1114        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1115        tr_count = 1
1116        total = 0
1117        #name = 'pume_results'
1118        name = 'Returning_update'
1119        table = self.returning_import
1120        no_import = []
1121        imported = []
1122        logger = logging.getLogger('Import.%s' % name)
1123        try:
1124            returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1125        except:
1126            logger.error('Error reading %s.csv' % name)
1127            return
1128        l = self.portal_catalog({'meta_type': "Certificate"})
1129        certs = {}
1130        cert_docs = {}
1131        for f in l:
1132            certs[f.getId] = f.getObject().getContent()
1133        start = True
1134        res = table()
1135        regs = []
1136        if len(res) > 0:
1137            regs = [s.matric_no for s in res]
1138        for student in returning:
1139            if start:
1140                start = False
1141                logger.info('start loading from %s.csv' % name)
1142                s = ','.join(['"%s"' % fn for fn in student.keys()])
1143                imported.append(s)
1144                no_import.append('%s,"Error"' % s)
1145                format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()])
1146                format_error = format + ',"%(Error)s"'
1147                no_certificate = "no certificate %s" % format
1148            matric_no = student.get('matric_no').upper()
1149            student['matric_no'] = matric_no
1150            if matric_no == '':
1151                student['Error'] = "Empty matric_no"
1152                no_import.append( format_error % student)
1153                continue
1154#            if matric_no in regs or self.returning_import(matric_no = matric_no):
1155#                student['Error'] = "Duplicate"
1156#                no_import.append( format_error % student)
1157#                continue
1158#            cert_id = makeCertificateCode(student.get('Coursemajorcode'))
1159#            if cert_id not in certs.keys():
1160#                student['Error'] = "No Certificate %s" % cert_id
1161#                no_import.append( format_error % student)
1162#                continue
1163            try:
1164                table.modifyRecord(**student)
1165            except KeyError:
1166                #import pdb;pdb.set_trace()
1167                student['Error'] = "no Student found to update"
1168                no_import.append( format_error % student)
1169                continue
1170            #s = self.students_catalog(matric_no=matric_no)
1171            #if s:
1172            #    level = "%s" % (int(student.get('Level')) + 100)
1173            #    self.students_catalog.modifyRecord(id = s[0].id,
1174            #                                           level=level)
1175
1176            regs.append(student.get('matric_no'))
1177            imported.append(format % student)
1178            tr_count += 1
1179            if tr_count > 1000:
1180                if len(no_import) > 0:
1181                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1182                             '\n'.join(no_import) + '\n')
1183                    no_import = []
1184                open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1185                                            '\n'.join(no_import) + "\n")
1186                imported = []
1187                em = '%d transactions commited total %s\n' % (tr_count,total)
1188                transaction.commit()
1189                regs = []
1190                logger.info(em)
1191                total += tr_count
1192                tr_count = 0
1193        open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1194                                            '\n'.join(imported))
1195        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1196                                                '\n'.join(no_import))
1197        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1198    ###)
1199
1200    security.declareProtected(ModifyPortalContent,"importResults")###(
1201    def importResults(self):
1202        """load Returning Students Results from CSV"""
1203        import transaction
1204        import random
1205        #from pdb import set_trace
1206        wftool = self.portal_workflow
1207        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
1208        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1209        tr_count = 1
1210        total = 0
1211        #name = 'pume_results'
1212        name = 'Results'
1213        table = self.results_import
1214        no_import = []
1215        imported = []
1216        logger = logging.getLogger('Import.%s' % name)
1217        try:
1218            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1219        except:
1220            logger.error('Error reading %s.csv' % name)
1221            return
1222        l = self.portal_catalog({'meta_type': "Course"})
1223        courses = [f.getId for f in l]
1224        start = True
1225        res = table()
1226        regs = []
1227        if len(res) > 0:
1228            regs = [s.key for s in res]
1229        no_course = []
1230        no_course_list = []
1231        course_count = 0
1232        for result in results:
1233            if start:
1234                start = False
1235                logger.info('start loading from %s.csv' % name)
1236                s = ','.join(['"%s"' % fn for fn in result.keys()])
1237                imported.append(s)
1238                no_import.append('%s,"Error"' % s)
1239                format = ','.join(['"%%(%s)s"' % fn for fn in result.keys()])
1240                format_error = format + ',"%(Error)s"'
1241                no_certificate = "no certificate %s" % format
1242            course_id = result.get('CosCode')
1243            if not course_id:
1244                course_id = 'N/A'
1245                result['CosCode'] = course_id
1246            matric_no = result.get('matric_no').upper()
1247            result['matric_no'] = matric_no
1248            key = matric_no+course_id
1249            if matric_no == '':
1250                result['Error'] = "Empty matric_no"
1251                no_import.append( format_error % result)
1252                continue
1253            if key in regs or self.results_import(key = key):
1254                result['Error'] = "Duplicate"
1255                no_import.append( format_error % result)
1256                continue
1257            if course_id not in courses:
1258                if course_id not in no_course:
1259                    course_count +=1
1260                    no_course.append(course_id)
1261                    no_course_list.append('"%s"' % course_id)
1262                    #result['Error'] = "No Course"
1263                    #logger.info(format_error % result)
1264            result['key'] = key
1265            try:
1266                table.addRecord(**result)
1267            except ValueError:
1268                #import pdb;pdb.set_trace()
1269                result['Error'] = "Duplicate"
1270                no_import.append( format_error % result)
1271                continue
1272            regs.append(key)
1273            imported.append(format % result)
1274            tr_count += 1
1275            if tr_count > 1000:
1276                if len(no_import) > 0:
1277                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1278                             '\n'.join(no_import)+'\n')
1279                    no_import = []
1280                open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1281                                            '\n'.join(imported) + '\n')
1282                imported = []
1283                if no_course_list:
1284                    open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write(
1285                                            '\n'.join(no_course_list) + '\n')
1286                    no_course_list = []
1287                em = '%d transactions commited total %s\n courses not found %s' % (tr_count,total,course_count)
1288                transaction.commit()
1289                logger.info(em)
1290                regs = []
1291                total += tr_count
1292                tr_count = 0
1293        open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1294                                            '\n'.join(imported))
1295        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1296                                                '\n'.join(no_import))
1297        if no_course_list:
1298            open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write(
1299                                    '\n'.join(no_course_list))
1300        em = '%d transactions commited total %s\n courses not found %s' % (tr_count,total,course_count)
1301        logger.info(em)
1302        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1303    ###)
1304
1305    security.declareProtected(ModifyPortalContent,"updateStudyCourse")###(
1306    def updateStudyCourse(self):
1307        """update StudyCourse from CSV values"""
1308        import transaction
1309        import random
1310        from pdb import set_trace
1311        wftool = self.portal_workflow
1312        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1313        csv_d = {'jamb_reg_no': "RegNumber",
1314                 'jamb_lastname': "Name",
1315                 'session': "Session",
1316                 'pume_tot_score': "PUME SCORE",
1317                 'jamb_score': "JambScore",
1318                 'jamb_sex': "Sex",
1319                 'jamb_state': "State",
1320##                 'jamb_first_cos': "AdminCourse",
1321                 'faculty': "AdminFaculty",
1322                 'course_code': "AdmitCoscode",
1323                 'stud_status':"AdmitStatus",
1324                 'department': "AdmitDept",
1325                 'jamb_lga': "LGA",
1326                 'app_email': "email",
1327                 'app_mobile': "PhoneNumbers",
1328                 }
1329        csv_fields = [f[1] for f in csv_d.items()]
1330        tr_count = 0
1331        total = 0
1332        #name = 'pume_results'
1333        name = 'StudyCourseChange'
1334        no_import = []
1335        s = ','.join(['"%s"' % fn for fn in csv_fields])
1336        no_import.append('"Error",%s' % s)
1337        format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
1338        no_certificate = "no certificate %s" % format
1339        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
1340                    '\n'.join(no_import))
1341        logger = logging.getLogger('Import.%s' % name)
1342        logger.info('Start loading from %s.csv' % name)
1343        l = self.portal_catalog({'meta_type': "Certificate"})
1344        try:
1345            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1346        except:
1347            logger.error('Error reading %s.csv' % name)
1348            return
1349        for jamb in result:
1350            jamb['Error'] = "Processing "
1351            logger.info(format % jamb)
1352            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
1353            res = self.portal_catalog({'portal_type': "StudentApplication",
1354                                     'SearchableText': jamb_reg_no })
1355            if not res:
1356                em = 'Student with RegNo %s does not exists\n' % jamb_reg_no
1357                logger.info(em)
1358                jamb['Error'] = "Student not exists"
1359                no_import.append(format % jamb)
1360                continue
1361            sid = res[0].getPath().split('/')[-2]
1362            cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
1363            res = self.portal_catalog(portal_type = "Certificate", id = cert_id)
1364            if not res:
1365                em = 'No Certificate with ID %s \n' % cert_id
1366                logger.info(em)
1367                jamb['Error'] = "No Certificate %s" % cert_id
1368                no_import.append( format % jamb)
1369                continue
1370            cert_brain = res[0]
1371            catalog_entry = {}
1372            student = getattr(self,sid)
1373            #
1374            # Study Course
1375            #
1376            study_course = student.study_course
1377            dsc = {}
1378            cert_pl = cert_brain.getPath().split('/')
1379            catalog_entry['id'] = sid
1380            catalog_entry['faculty'] = cert_pl[-4]
1381            catalog_entry['department'] = cert_pl[-3]
1382            catalog_entry['course'] = cert_id
1383            dsc['study_course'] = cert_id
1384            study_course.getContent().edit(mapping=dsc)
1385            self.students_catalog.modifyRecord(**catalog_entry)
1386            if tr_count > 10:
1387                if len(no_import) > 1:
1388                    open("%s/import/%s_not_imported.csv" % (i_home,name),"w+").write(
1389                             '\n'.join(no_import))
1390                    no_import = []
1391                em = '%d transactions commited\n' % tr_count
1392                transaction.commit()
1393                logger.info(em)
1394                total += tr_count
1395                tr_count = 0
1396            tr_count += 1
1397        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1398    ###)
1399
1400    security.declareProtected(ModifyPortalContent,"OLDloadPumeResultsFromCSV")###(
1401    def OLDloadPumeResultsFromCSV(self):
1402        """load Fulltime Studentdata from CSV values"""
1403        import transaction
1404        import random
1405        wftool = self.portal_workflow
1406        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1407        csv_d = {'jamb_reg_no': "JAMBRegno",
1408                 'jamb_lastname': "Name",
1409                 'pume_options': "Options",
1410                 'session': "Session",
1411                 'days': "Days",
1412                 'response': "Responce",
1413                 'wrong': "Wrong",
1414                 'pume_eng_score': "EngScore",
1415                 'pume_gen_score': "GenScore",
1416                 'pume_tot_score': "Score",
1417                 'batch': "Batch",
1418                 'serial': "SerialNo",
1419                 'jamb_score': "JambScore",
1420                 'omitted':"Omitted",
1421                 'search_key': "SearchKey",
1422                 'jamb_sex': "Sex",
1423                 'fac1': "Fac1",
1424                 'fac2': "Fac2",
1425                 'jamb_first_cos': "CourseofStudy",
1426                 'stud_status':"StudStatus",
1427                 'registered': "Registered",
1428                 'jamb_state': "State",
1429                 'eng_fail': "EngFail",
1430                 'gen_fail': "GenFail",
1431                 'un_ans_eng': "UnAnsEng",
1432                 'un_ans_eng': "UnAnsGen",
1433                 'total_ans': "TotalUnAns",
1434                 'dept': "Dept",
1435                 'jamb_second_cos': "Course2",
1436                 'jamb_third_cos': "course3",
1437                 }
1438        csv_fields = [f[1] for f in csv_d.items()]
1439        tr_count = 0
1440        name = 'pume_results'
1441        no_import = []
1442        s = ','.join(['"%s"' % fn for fn in csv_fields])
1443        no_import.append('%s\n' % s)
1444        logger = logging.getLogger('Import.%s' % name)
1445        logger.info('Start loading from %s.csv' % name)
1446        try:
1447            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1448        except:
1449            logger.error('Error reading %s.csv' % name)
1450            return
1451        for jamb in result:
1452            format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
1453            processing = "processing %s" % format
1454            logger.info(processing % jamb)
1455            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
1456            #import pdb;pdb.set_trace()
1457            res = self.portal_catalog({'portal_type': "StudentApplication",
1458                                     'jamb_reg_no': jamb_reg_no })
1459            if res:
1460                em = 'Student with REG-NO %s already exists\n' % jamb_reg_no
1461                logger.info(em)
1462                no_import.append(em)
1463                no_import.append(format % jamb)
1464                continue
1465            cert_id = jamb.get(csv_d['jamb_first_cos']).upper()
1466            res = self.portal_catalog({'portal_type': "Certificate",
1467                                     'id': cert_id })
1468            if len(res) < 1:
1469                em = 'No Certificate with ID %s \n' % cert_id
1470                logger.info(em)
1471                no_import.append(em)
1472                no_import.append(format % jamb)
1473                continue
1474            cert = res[0].getObject()
1475            cert_path = res[0].getPath()
1476            cert_doc = cert.getContent()
1477            jamb_name = jamb.get(csv_d['jamb_lastname'])
1478            jamb_name.replace('>','')
1479            names = jamb_name.split()
1480            letter = names[-1][0].upper()
1481            sid = self.generateStudentId(letter)
1482            not_created = True
1483            while not_created:
1484                try:
1485                    students_folder.invokeFactory('Student', sid)
1486                    not_created = False
1487                except BadRequest:
1488                    sid = self.generateStudentId(letter)
1489            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
1490            student = getattr(self,sid)
1491            student.manage_setLocalRoles(sid, ['Owner',])
1492            student.invokeFactory('StudentClearance','clearance')
1493            #wftool.doActionFor(student.clearance,'open')
1494            dp = {'Title': 'Clearance/Eligibility Record'}
1495            student.clearance.manage_setLocalRoles(sid, ['Owner',])
1496            student.invokeFactory('StudentPume','pume')
1497            dp = {'Title': 'Pume Data'}
1498            student.invokeFactory('StudentApplication','application')
1499            da = {'Title': 'Application Data'}
1500            da["jamb_lastname"] = jamb_name
1501            da_fields = ('jamb_reg_no',
1502                         'jamb_sex',
1503                         'jamb_state',
1504                         'jamb_score',
1505                         'jamb_first_cos',
1506                         'jamb_sex',
1507                         'jamb_state',
1508                         'jamb_first_cos',
1509                         'jamb_second_cos',
1510                         )
1511            for f in da_fields:
1512                da[f] = jamb.get(csv_d[f])
1513            app = student.application
1514            app.getContent().edit(mapping=da)
1515            app.manage_setLocalRoles(sid, ['Owner',])
1516            #wftool.doActionFor(app,'close')
1517            dp_fields = (
1518                         'pume_eng_score',
1519                         'pume_gen_score',
1520                         'pume_tot_score',
1521                         )
1522            for f in dp_fields:
1523                dp[f] = float(jamb.get(csv_d[f]))
1524            pume = student.pume
1525            pume.getContent().edit(mapping=dp)
1526            #wftool.doActionFor(pume,'close')
1527            pume.manage_setLocalRoles(sid, ['Owner',])
1528            #
1529            # Study Course
1530            #
1531            student.invokeFactory('StudentStudyCourse','study_course')
1532            study_course = student.study_course
1533            dsc = {}
1534            from_certificate = ['title',
1535                               'max_elect',
1536                               'max_pass',
1537                               'n_core',
1538                               'nr_years',
1539                               'probation_credits',
1540                               'promotion_credits',
1541                               'start_level',
1542                              ]
1543            for f in from_certificate:
1544                dsc[f] = getattr(cert_doc,f)
1545            cpl = cert_path.split('/')
1546            dsc['faculty'] = cpl[-4]
1547            dsc['department'] = cpl[-3]
1548            dsc['study_course'] = cert_id
1549            dsc['entry_session'] = jamb.get(csv_d['session'])
1550            study_course.getContent().edit(mapping=dsc)
1551            student.getContent().createSubObjects()
1552            if dp['pume_tot_score']>49:
1553                wftool.doActionFor(student,'pume_pass')
1554                wftool.doActionFor(student,'admit')
1555            else:
1556                wftool.doActionFor(student,'pume_fail')
1557                wftool.doActionFor(student,'reject_admission')
1558            if len(no_import) > 1:
1559                open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
1560                     '\n'.join(no_import))
1561        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1562    ###)
1563
1564    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
1565    def loadFullTimeStudentsResultsFromCSV(self):
1566        """load Fulltime Studentdata from CSV values"""
1567        #return
1568        level_wf_actions = {}
1569        level_wf_actions["SUCCESSFUL STUDENT"] = "pass_A"
1570        level_wf_actions["STUDENT WITH CARRYOVER COURSES"] = "pass_B"
1571        level_wf_actions["STUDENT FOR PROBATION"] = "probate_C"
1572        level_wf_actions["STUDENT ON PROBATION/TRANSFER"] = "reject_D"
1573        import transaction
1574        wftool = self.portal_workflow
1575        tr_count = 0
1576        name = 'short_full_time_results_2004_2005'
1577        no_import = False
1578        if not no_import:
1579            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
1580            no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
1581        logger = logging.getLogger('import.%s' % name)
1582        logger.info('Start loading from %s.csv' % name)
1583        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1584        try:
1585            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1586        except:
1587            logger.error('Error reading %s.csv' % name)
1588            return
1589        l = self.portal_catalog({'meta_type': "Course"})
1590        courses = {}
1591        for c in l:
1592            courses[c.id] = c.getObject()
1593        level_changed = False
1594        student_changed = False
1595        sid = ''
1596        #import pdb;pdb.set_trace()
1597        for result in results:
1598            temp_sid = result.get('Matnumber')
1599            if temp_sid != sid:
1600                student_changed = True
1601                res = self.portal_catalog({'meta_type': "StudentClearance",
1602                                         'SearchableText': temp_sid })
1603                if not res:
1604                    em = 'Student with ID %(Matnumber)s not found\n' % result
1605                    logger.info(em)
1606                    no_import.write(em)
1607                    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)
1608                    continue
1609                elif len(res) > 1:
1610                    em = 'More than one Student with ID %(Matnumber)s found\n' % result
1611                    logger.info(em)
1612                    no_import.write(em)
1613                    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)
1614                    continue
1615                sid = temp_sid
1616                sf = res[0].getObject().aq_parent
1617                sc = getattr(sf,'study_course')
1618                level = ''
1619            else:
1620                student_changed = False
1621            course = result.get('CosCode')
1622            if course not in courses.keys():
1623                em = 'Course with ID %(CosCode)s not found\n' % result
1624                logger.info(em)
1625                no_import.write(em)
1626                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)
1627                continue
1628            course_doc = courses[course].getContent()
1629            temp_level = result.get('Level')
1630            student_id = sf.getId()
1631            result['StudentId'] = student_id
1632            if temp_level != level:
1633                try:
1634                    int(temp_level)
1635                except:
1636                    em = 'Result with ID %(Matnumber)s Course %(CosCode)s Level is empty\n' % result
1637                    logger.info(em)
1638                    no_import.write(em)
1639                    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)
1640                    continue
1641                level_changed = True
1642                if 'dlev' in vars().keys():
1643                    wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
1644                level = temp_level
1645                l = getattr(sc,level,None)
1646                if l is None:
1647                    logger.info('Creating Level %(Level)s for %(StudentId)s %(Matnumber)s' % result)
1648                    sc.invokeFactory('StudentStudyLevel', level)
1649                    l = getattr(sc, level)
1650                    l.manage_setLocalRoles(student_id, ['Owner',])
1651            else:
1652                level_changed = False
1653            cr = getattr(l,course,None)
1654            if cr is None:
1655                logger.info('Creating Course %(CosCode)s for %(StudentId)s %(Matnumber)s in Level %(Level)s' % result)
1656                l.invokeFactory('StudentCourseResult',course)
1657            cr = getattr(l,course)
1658            dcr = {}
1659            from_course = ['title',
1660                           'credits',
1661                           'passmark',
1662                           ]
1663            for f in from_course:
1664                dcr[f] = getattr(course_doc,f)
1665            dlev = {}
1666            dcr['ansbook'] = result.get('Ansbook')
1667            dcr['semester'] = getInt(result.get('Semster'))
1668            dcr['status'] = result.get('CosStuatus')
1669            dcr['score'] = getInt(result.get('Score'))
1670            dlev['session'] = result.get('Session')
1671            dcr['carry_level'] = result.get('CarryLevel')
1672            dcr['grade'] = result.get('Grade')
1673            dcr['weight'] = result.get('Weight')
1674            dlev['verdict'] = result.get('Verdict')
1675            dcr['import_id'] = result.get('id')
1676            gpa = result.get('GPA').replace(',','.')
1677            dlev['imported_gpa'] = getFloat(gpa)
1678            cr.getContent().edit(mapping = dcr)
1679            cr.manage_setLocalRoles(student_id, ['Owner',])
1680            l.getContent().edit(mapping = dlev)
1681            if tr_count > MAX_TRANS:
1682                transaction.commit()
1683                tr_count = 0
1684            tr_count += 1
1685            wftool.doActionFor(cr,'close')
1686        wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
1687        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1688
1689###)
1690
1691    security.declareProtected(ModifyPortalContent,"loadJAMBFromCSV")###(
1692    def loadJAMBFromCSV(self):
1693        """load JAMB data from CSV values"""
1694        #return
1695        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1696        import transaction
1697        tr_count = 0
1698        name = 'SampleJAMBDataII'
1699        wftool = self.portal_workflow
1700        no_import = False
1701        if not no_import:
1702            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
1703            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')
1704        logger = logging.getLogger('Import.%s' % name)
1705        logger.info('Start loading from %s.csv' % name)
1706        try:
1707            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1708        except:
1709            logger.error('Error reading %s.csv' % name)
1710            return
1711        for jamb in result:
1712            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)
1713            jamb_reg_no = jamb.get('REG-NO')
1714            res = self.portal_catalog({'meta_type': "StudentApplication",
1715                                     'jamb_reg_no': jamb_reg_no })
1716            if res:
1717                em = 'Student with REG-NO %(REG-NO)s already exists\n' % jamb
1718                logger.info(em)
1719                no_import.write(em)
1720                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)
1721                continue
1722            jamb_name = jamb.get("NAME")
1723            jamb_name.replace('>','')
1724            names = jamb_name.split()
1725            letter = names[-1][0].upper()
1726            sid = self.generateStudentId(letter)
1727            not_created = True
1728            while not_created:
1729                try:
1730                    students_folder.invokeFactory('Student', sid)
1731                    not_created = False
1732                except BadRequest:
1733                    sid = self.generateStudentId(letter)
1734            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
1735            student = getattr(self,sid)
1736            student.manage_setLocalRoles(sid, ['Owner',])
1737            student.invokeFactory('StudentApplication','application')
1738            da = {'Title': 'Application Data'}
1739            da["jamb_reg_no"] = jamb.get("REG-NO")
1740            da["jamb_lastname"] = jamb_name
1741            da["jamb_sex"] = jamb.get("SEX")
1742            da["jamb_state"] = jamb.get("STATE")
1743            da["jamb_lga"] = jamb.get("LGA")
1744            da["jamb_score"] = jamb.get("AGGREGATE")
1745            da["jamb_first_cos"] = jamb.get("COURSE1")
1746            da["jamb_second_cos"] = jamb.get("COURSE2")
1747            da["jamb_first_uni"] = jamb.get("UNIV1")
1748            da["jamb_second_uni"] = jamb.get("UNIV2")
1749            app = student.application
1750            app_doc = app.getContent()
1751            app_doc.edit(mapping=da)
1752            #wftool.doActionFor(app,'open',dest_container=app)
1753            app.manage_setLocalRoles(sid, ['Owner',])
1754            student.getContent().createSubObjects()
1755        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1756    ###)
1757
1758 
1759    security.declareProtected(View,"fixOwnership")
1760    def fixOwnership(self):
1761        """fix Ownership"""
1762        for s in self.portal_catalog(meta_type = 'Student'):
1763            student = s.getObject()
1764            sid = s.getId
1765            import pdb;pdb.set_trace()
1766            student.application.manage_setLocalRoles(sid, ['Owner',])
1767            student.personal.manage_setLocalRoles(sid, ['Owner',])
1768
1769    security.declareProtected(View,"Title")
1770    def Title(self):
1771        """compose title"""
1772        return "Student Section"
1773
1774    def generateStudentId(self,letter): ###(
1775        import random
1776        r = random
1777        if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
1778            letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY')
1779        students = self.portal_catalog(meta_type = "StudentsFolder")[-1]
1780        sid = "%c%d" % (letter,r.randint(99999,1000000))
1781        while hasattr(students, sid):
1782            sid = "%c%d" % (letter,r.randint(99999,1000000))
1783        return sid
1784        #return "%c%d" % (r.choice('ABCDEFGHKLMNPQRSTUVWXY'),r.randint(99999,1000000))
1785    ###)
1786
1787InitializeClass(StudentsFolder)
1788
1789def addStudentsFolder(container, id, REQUEST=None, **kw):
1790    """Add a Student."""
1791    ob = StudentsFolder(id, **kw)
1792    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1793###)
1794
1795class Student(CPSDocument): ###(
1796    """
1797    WAeUP Student container for the various student data
1798    """
1799    meta_type = 'Student'
1800    portal_type = meta_type
1801    security = ClassSecurityInfo()
1802
1803    security.declareProtected(View,"Title")
1804    def Title(self):
1805        """compose title"""
1806        reg_nr = self.getId()[1:]
1807        data = getattr(self,'personal',None)
1808        if data:
1809            content = data.getContent()
1810            return "%s %s %s" % (content.firstname,content.middlename,content.lastname)
1811        data = getattr(self,'application',None)
1812        if data:
1813            content = data.getContent()
1814            return "%s" % (content.jamb_lastname)
1815        return self.title
1816
1817    security.declarePrivate('makeStudentMember') ###(
1818    def makeStudentMember(self,sid,password='uNsEt'):
1819        """make the student a member"""
1820        membership = self.portal_membership
1821        membership.addMember(sid,
1822                             password ,
1823                             roles=('Member',
1824                                     'Student',
1825                                     ),
1826                             domains='',
1827                             properties = {'memberareaCreationFlag': False,
1828                                           'homeless': True},)
1829        member = membership.getMemberById(sid)
1830        self.portal_registration.afterAdd(member, sid, password, None)
1831        self.manage_setLocalRoles(sid, ['Owner',])
1832
1833###)
1834
1835    security.declareProtected(View,'createSubObjects') ###(
1836    def createSubObjects(self):
1837        """make the student a member"""
1838        dp = {'Title': 'Personal Data'}
1839        app_doc = self.application.getContent()
1840        names = app_doc.jamb_lastname.split()
1841        if len(names) == 3:
1842            dp['firstname'] = names[0].capitalize()
1843            dp['middlename'] = names[1].capitalize()
1844            dp['lastname'] = names[2].capitalize()
1845        elif len(names) == 2:
1846            dp['firstname'] = names[0].capitalize()
1847            dp['lastname'] = names[1].capitalize()
1848        else:
1849            dp['lastname'] = app_doc.jamb_lastname
1850        dp['sex'] = app_doc.jamb_sex == 'F'
1851        dp['lga'] = "%s/%s" % (app_doc.jamb_state,app_doc.jamb_lga )
1852        proxy = self.aq_parent
1853        proxy.invokeFactory('StudentPersonal','personal')
1854        per = proxy.personal
1855        per_doc = per.getContent()
1856        per_doc.edit(mapping = dp)
1857        per.manage_setLocalRoles(proxy.getId(), ['Owner',])
1858        #self.portal_workflow.doActionFor(per,'open',dest_container=per)
1859
1860###)
1861
1862InitializeClass(Student)
1863
1864def addStudent(container, id, REQUEST=None, **kw):
1865    """Add a Student."""
1866    ob = Student(id, **kw)
1867    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1868
1869###)
1870
1871class StudentAccommodation(CPSDocument): ###(
1872    """
1873    WAeUP Student container for the various student data
1874    """
1875    meta_type = 'StudentAccommodation'
1876    portal_type = meta_type
1877    security = ClassSecurityInfo()
1878
1879    security.declareProtected(View,"Title")
1880    def Title(self):
1881        """compose title"""
1882        content = self.getContent()
1883        #return "Accommodation Data for %s %s" % (content.firstname,content.lastname)
1884        return "Accommodation Data for Session %s" % content.session
1885
1886
1887InitializeClass(StudentAccommodation)
1888
1889def addStudentAccommodation(container, id, REQUEST=None, **kw):
1890    """Add a Students personal data."""
1891    ob = StudentAccommodation(id, **kw)
1892    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1893
1894###)
1895
1896class StudentPersonal(CPSDocument): ###(
1897    """
1898    WAeUP Student container for the various student data
1899    """
1900    meta_type = 'StudentPersonal'
1901    portal_type = meta_type
1902    security = ClassSecurityInfo()
1903
1904    security.declareProtected(View,"Title")
1905    def Title(self):
1906        """compose title"""
1907        content = self.getContent()
1908        #return "Personal Data for %s %s" % (content.firstname,content.lastname)
1909        return "Personal Data"
1910
1911
1912InitializeClass(StudentPersonal)
1913
1914def addStudentPersonal(container, id, REQUEST=None, **kw):
1915    """Add a Students personal data."""
1916    ob = StudentPersonal(id, **kw)
1917    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1918
1919###)
1920
1921class StudentClearance(CPSDocument): ###(
1922    """
1923    WAeUP Student container for the various student data
1924    """
1925    meta_type = 'StudentClearance'
1926    portal_type = meta_type
1927    security = ClassSecurityInfo()
1928
1929    security.declareProtected(View,"Title")
1930    def Title(self):
1931        """compose title"""
1932        content = self.getContent()
1933        #return "Clearance/Eligibility Record for %s %s" % (content.firstname,content.lastname)
1934        return "Clearance/Eligibility Record"
1935
1936
1937InitializeClass(StudentClearance)
1938
1939def addStudentClearance(container, id, REQUEST=None, **kw):
1940    """Add a Students personal data."""
1941    ob = StudentClearance(id, **kw)
1942    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1943
1944###)
1945
1946class StudentStudyLevel(CPSDocument): ###(
1947    """
1948    WAeUP Student container for the various student data
1949    """
1950    meta_type = 'StudentStudyLevel'
1951    portal_type = meta_type
1952    security = ClassSecurityInfo()
1953
1954    security.declareProtected(View,"Title")
1955    def Title(self):
1956        """compose title"""
1957        return "Level %s" % self.aq_parent.getId()
1958
1959##    security.declarePublic("gpa")
1960##    def gpa(self):
1961##        """calculate the gpa"""
1962##        sum = 0
1963##        course_count = 0
1964##        for sc in self.objectValues():
1965##            result = sc.getContent()
1966##            if not result.grade:
1967##                continue
1968##            res = self.portal_catalog({'meta_type': 'Course',
1969##                                          'id': sc.aq_parent.id})
1970##            if len(res) != 1:
1971##                continue
1972##            course = res[0].getObject().getContent()
1973##            sum += course.credits * ['F','E','D','C','B','A'].index(result.grade)
1974##            course_count += 1
1975##        if course_count:
1976##            return sum/course_count
1977##        return 0.0
1978
1979InitializeClass(StudentStudyLevel)
1980
1981def addStudentStudyLevel(container, id, REQUEST=None, **kw):
1982    """Add a Students personal data."""
1983    ob = StudentStudyLevel(id, **kw)
1984    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1985
1986###)
1987
1988class StudentStudyCourse(CPSDocument): ###(
1989    """
1990    WAeUP Student container for the various student data
1991    """
1992    meta_type = 'StudentStudyCourse'
1993    portal_type = meta_type
1994    security = ClassSecurityInfo()
1995
1996    security.declareProtected(View,"Title")
1997    def Title(self):
1998        """compose title"""
1999        content = self.getContent()
2000        return "Study Course"
2001
2002
2003InitializeClass(StudentStudyCourse)
2004
2005def addStudentStudyCourse(container, id, REQUEST=None, **kw):
2006    """Add a Students personal data."""
2007    ob = StudentStudyCourse(id, **kw)
2008    return CPSBase_adder(container, ob, REQUEST=REQUEST)
2009
2010###)
2011
2012class StudentApplication(CPSDocument): ###(
2013    """
2014    WAeUP Student container for the various student data
2015    """
2016    meta_type = 'StudentApplication'
2017    portal_type = meta_type
2018    security = ClassSecurityInfo()
2019
2020    security.declareProtected(View,"Title")
2021    def Title(self):
2022        """compose title"""
2023        return "Application Data"
2024
2025
2026InitializeClass(StudentApplication)
2027
2028def addStudentApplication(container, id, REQUEST=None, **kw):
2029    """Add a Students eligibility data."""
2030    ob = StudentApplication(id, **kw)
2031    return CPSBase_adder(container, ob, REQUEST=REQUEST)
2032###)
2033
2034class StudentPume(CPSDocument): ###(
2035    """
2036    WAeUP Student container for the various student data
2037    """
2038    meta_type = 'StudentPume'
2039    portal_type = meta_type
2040    security = ClassSecurityInfo()
2041
2042    security.declareProtected(View,"Title")
2043    def Title(self):
2044        """compose title"""
2045        return "PUME Results"
2046
2047
2048InitializeClass(StudentPume)
2049
2050def addStudentPume(container, id, REQUEST=None, **kw):
2051    """Add a Students PUME data."""
2052    ob = StudentPume(id, **kw)
2053    return CPSBase_adder(container, ob, REQUEST=REQUEST)
2054###)
2055
2056##class StudentSemester(CPSDocument): ###(
2057##    """
2058##    WAeUP StudentSemester containing the courses and students
2059##    """
2060##    meta_type = 'StudentSemester'
2061##    portal_type = meta_type
2062##    security = ClassSecurityInfo()
2063##
2064##InitializeClass(StudentSemester)
2065##
2066##def addStudentSemester(container, id, REQUEST=None, **kw):
2067##    """Add a StudentSemester."""
2068##    ob = StudentSemester(id, **kw)
2069##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
2070##
2071#####)
2072
2073##class Semester(CPSDocument): ###(
2074##    """
2075##    WAeUP Semester containing the courses and students
2076##    """
2077##    meta_type = 'Semester'
2078##    portal_type = meta_type
2079##    security = ClassSecurityInfo()
2080##
2081##InitializeClass(Semester)
2082##
2083##def addSemester(container, id, REQUEST=None, **kw):
2084##    """Add a Semester."""
2085##    ob = Semester(id, **kw)
2086##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
2087##
2088#####)
2089
2090class StudentCourseResult(CPSDocument): ###(
2091    """
2092    WAeUP StudentCourseResult
2093    """
2094    meta_type = 'StudentCourseResult'
2095    portal_type = meta_type
2096    security = ClassSecurityInfo()
2097
2098    def getCourseEntry(self,cid):
2099        res = self.portal_catalog({'meta_type': "Course",
2100                                           'id': cid})
2101        if res:
2102            return res[-1]
2103        else:
2104            return None
2105
2106    security.declareProtected(View,"Title")
2107    def Title(self):
2108        """compose title"""
2109        cid = self.aq_parent.getId()
2110        ce = self.getCourseEntry(cid)
2111        if ce:
2112            return "%s" % ce.Title
2113        return "No course with id %s" % cid
2114
2115InitializeClass(StudentCourseResult)
2116
2117def addStudentCourseResult(container, id, REQUEST=None, **kw):
2118    """Add a StudentCourseResult."""
2119    ob = StudentCourseResult(id, **kw)
2120    return CPSBase_adder(container, ob, REQUEST=REQUEST)
2121###)
2122
2123# Backward Compatibility StudyLevel
2124
2125from Products.WAeUP_SRP.Academics import StudyLevel
2126
2127from Products.WAeUP_SRP.Academics import addStudyLevel
2128
Note: See TracBrowser for help on using the repository browser.