source: WAeUP_SRP/trunk/Students.py @ 1504

Last change on this file since 1504 was 1448, checked in by Henrik Bettermann, 18 years ago

prepared for import of dummy results

  • Property svn:keywords set to Id
File size: 87.0 KB
RevLine 
[57]1#-*- mode: python; mode: fold -*-
[200]2# $Id: Students.py 1448 2007-02-20 19:51:55Z henrik $
[708]3from string import Template
[45]4from Globals import InitializeClass
5from AccessControl import ClassSecurityInfo
[164]6from AccessControl.SecurityManagement import newSecurityManager
[429]7from zExceptions import BadRequest
[502]8from Products.ZCatalog.ZCatalog import ZCatalog
[47]9from Products.CMFCore.utils import UniqueObject, getToolByName
[45]10from Products.CMFCore.permissions import View
11from Products.CMFCore.permissions import ModifyPortalContent
[154]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
[164]16from Products.CPSCore.CPSMembershipTool import CPSUnrestrictedUser
[361]17from Products.WAeUP_SRP.Academics import makeCertificateCode
[362]18import logging
[971]19import csv,re,os
[362]20import Globals
21p_home = Globals.package_home(globals())
22i_home = Globals.INSTANCE_HOME
[981]23MAX_TRANS = 1000
[966]24import DateTime
[971]25import PIL.Image
26from StringIO import StringIO
[154]27
[958]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): ###(
[723]40    try:
41        return int(s)
42    except:
43        return 0
[422]44
[725]45def getFloat(s):
46    try:
47        return float(s)
48    except:
49        return 0.0
50
[958]51###)
52
[714]53def getStudentByRegNo(self,reg_no): ###(
[502]54    """search student by JAMB Reg No and return StudentFolder"""
55    search = ZCatalog.searchResults(self.portal_catalog,{'meta_type': 'StudentApplication',
[606]56                                  'SearchableText': reg_no,
[502]57                                  })
58    if len(search) < 1:
59        return None
60    return search[0].getObject().aq_parent
61
[714]62###)
63
[1111]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
[1119]75
[361]76class StudentsFolder(CPSDocument): ###(
77    """
78    WAeUP container for the various WAeUP containers data
79    """
[362]80    meta_type = 'StudentsFolder'
[361]81    portal_type = meta_type
82    security = ClassSecurityInfo()
[154]83
[361]84    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsFromCSV")###(
85    def loadFullTimeStudentsFromCSV(self):
86        """load Fulltime Studentdata from CSV values"""
[398]87        import transaction
[708]88        import random
[398]89        tr_count = 0
[361]90        name = 'short_full_time'
91        no_import = False
[395]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')
[1082]95        logger = logging.getLogger('Import.%s' % name)
[361]96        logger.info('Start loading from %s.csv' % name)
[708]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"')
[361]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
[422]106        l = self.portal_catalog({'meta_type': "StudentClearance",})
107        matrics = []
[361]108        for s in l:
[422]109            matrics.append(s.getObject().getContent().matric_no)
[423]110        print matrics
[361]111        l = self.portal_catalog({'meta_type': "Certificate"})
112        certs = {}
113        for c in l:
[727]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()
[361]117        for student in students:
[393]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)
[361]119            sid = student.get('MatricNo')
[396]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
[361]126            certcode = makeCertificateCode(student.get('CourseMajor'))
[727]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():
[393]131                em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
[361]132                logger.info(em)
[393]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)
[361]135                continue
[727]136            certificate_doc = certs[cid].getContent()
[395]137            level = student.get('StudentLevel')
[426]138            try:
[395]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
[422]146            matric_no = student.get('MatricNo')
147            if matric_no not in matrics:
148                matrics.append(matric_no)
[714]149                sid = self.generateStudentId(student.get('Lastname')[0])
[361]150                #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
[714]151                students_folder.invokeFactory('Student', sid)
[426]152                logger.info('%(tr_count)s: Creating Student with ID %(sid)s Matric_no %(matric_no)s ' % vars())
[714]153                student_obj = getattr(self,sid)
[708]154                access_code = "%d" % random.randint(1000000000,9999999999)
[746]155                student_obj.getContent().makeStudentMember(sid,access_code,)
[708]156                pwl_dict = {'student_id': sid,'access_code':access_code}
[714]157                student_obj.invokeFactory('StudentApplication','application')
158                application = student_obj.application
[472]159                da = {'Title': 'Application Data'}
[714]160                student_obj.invokeFactory('StudentPersonal','personal')
[446]161                da['jamb_reg_no'] = student.get('EntryRegNo')
[714]162                personal = student_obj.personal
[708]163                dp = {'Title': 'Personal Data'}
[714]164                student_obj.invokeFactory('StudentClearance','clearance')
165                clearance = student_obj.clearance
[840]166                dc = {'Title': 'Clearance/Eligibility Record'}
[422]167                dc['matric_no'] = matric_no
[714]168                state = student.get('State')
169                lga = student.get('LGA')
170                if state and lga:
171                    lga =  state + ' / ' + lga
172                else:
173                    lga = "None"
[427]174                dc['lga'] = lga
[422]175                dc['nationality'] = student.get('Nationality')
176                dc['email'] = student.get('Emailaddress')
[708]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')
[714]186                application.getContent().edit(mapping=da)
187                personal.getContent().edit(mapping=dp)
188                clearance.getContent().edit(mapping=dc)
[362]189                #
190                # Study Course
191                #
[714]192                student_obj.invokeFactory('StudentStudyCourse','study_course')
193                studycourse = student_obj.study_course
[708]194                dsc = {}
[727]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
[708]208                dsc['study_course'] = certcode
[454]209                css = student.get('CurrentSession') or '2004-2005'
210                cs = int(css.split('-')[0]) - 2000
[714]211                cl = int(student.get('StudentLevel') or '100')/100
[708]212                dsc['entry_session'] = "200%s" % (cs - cl)
213                dsc['clr_ac_pin'] = access_code
[714]214                studycourse.getContent().edit(mapping=dsc)
[364]215                #
216                # Level
217                #
[723]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})
[361]229            else:
[393]230                em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
[361]231                logger.info(em)
[393]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)
[714]234                continue
[398]235            if tr_count > MAX_TRANS:
[422]236                transaction.commit()
237                em = 'Transaction commited\n' % student
[398]238                logger.info(em)
239                tr_count = 0
240            tr_count += 1
[708]241            pwl_dict.update(dc)
242            pwl_dict.update(da)
243            pwl_dict.update(dp)
[714]244            wftool = self.portal_workflow
[708]245            pwlist.append(pwl_template.substitute(pwl_dict))
[714]246            wftool.doActionFor(student_obj,'clear_and_validate')
[742]247            student_obj.manage_setLocalRoles(sid, ['Owner',])
[714]248            wftool.doActionFor(application,'close')
[742]249            application.manage_setLocalRoles(sid, ['Owner',])
[714]250            wftool.doActionFor(clearance,'close')
[742]251            clearance.manage_setLocalRoles(sid, ['Owner',])
[714]252            wftool.doActionFor(personal,'close')
[742]253            personal.manage_setLocalRoles(sid, ['Owner',])
[727]254            wftool.doActionFor(studycourse,'close_for_edit')
[742]255            studycourse.manage_setLocalRoles(sid, ['Owner',])
[714]256        open("%s/import/pwlist-%s.csv" % (i_home,name),"w+").write('\n'.join(pwlist))
[423]257        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
[361]258    ###)
[382]259
[742]260    security.declareProtected(ModifyPortalContent,"loadPumeResultsFromCSV")###(
261    def loadPumeResultsFromCSV(self):
[966]262        """load Fulltime Studentdata from CSV values into pumeresults catalog"""
263        import transaction
264        import random
[1111]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",
[966]277                 'name': "Name",
278                 'score': "Score",
279                 'sex': "Sex",
[1119]280                 'course': "Course",
[966]281                 'faculty': "Faculty",
282                 'department': "Dept",
283                 'course_code_org': "Course Code",
[1111]284                 'status': "Admission Status",
285                 'result_type': None,
[966]286                 }
[1111]287        csv_fields = [f[1] for f in csv_d.items() if f[1]]
[966]288        tr_count = 0
289        total = 0
[1396]290        #name = 'pup_new'
291        name = 'pup_update'
[971]292        update = name.endswith('update')
[966]293        no_import = []
[1111]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]))
[1005]297        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
[1111]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))
[1005]300        no_import_name = "%s/import/%s_not_imported_%s.csv" % (i_home,name,current)
[1111]301        #open(no_import_name,"w").write('\n'.join(no_import))
[1082]302        logger = logging.getLogger('Import.%s' % name)
[966]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])
[1111]312        import_format = ','.join(['"%%(%s)s"' % fn for fn in csv_d.keys()])
[1119]313        eduplicate = '%s,"duplicate"' % format
314        einvalidjamb = '%s,"invalid JambRegNo"' % format
[1005]315        added = 'added ,%s' % format
[1111]316        #from pdb import set_trace;set_trace()
[966]317        for jamb in result:
318            dict = {}
319            for f,fn in csv_d.items():
320                dict[f] = jamb.get(csv_d[f])
[1111]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)
[971]332            if len(res) > 0:
333                if update:
[1005]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
[971]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
[966]356            try:
357                pume.addRecord(**dict)
[1111]358                ok_import.append(import_format % dict)
[966]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:
[1005]366            open(no_import_name,"w+").write('\n'.join(no_import))
[1111]367        open(ok_import_name,"w+").write('\n'.join(ok_import))
[966]368        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
[971]369    ###)
[966]370
[1121]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 = []
[1321]402        s = ','.join(['"%s"' % fn for fn in csv_fields])
[1121]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 = {}
[1386]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'])
[1121]566            catalog_entry['faculty'] = jamb.get(csv_d['faculty'])
567            catalog_entry['department'] = jamb.get(csv_d['department'])
568            catalog_entry['course'] = cert_id
[1386]569            #catalog_entry['level'] = getattr(cert_doc,'start_level')
570            catalog_entry['level'] = '200'
[1121]571            dsc['study_course'] = cert_id
[1386]572            dsc['current_level'] = '200'
573            #dsc['entry_session'] = jamb.get(csv_d['session'])
[1121]574            study_course.getContent().edit(mapping=dsc)
575            self.students_catalog.addRecord(**catalog_entry)
[1124]576            if tr_count > 10:
[1121]577                if len(no_import) > 1:
[1127]578                    open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
[1262]579                             '\n'.join(no_import)+'\n')
[1121]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
[1127]586        open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
[1125]587                                               '\n'.join(no_import))
[1121]588        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
589    ###)
590
[966]591    security.declareProtected(ModifyPortalContent,"createNewStudents")###(
592    def createNewStudents(self):
[742]593        """load Fulltime Studentdata from CSV values"""
594        import transaction
595        import random
[971]596        #from pdb import set_trace
[763]597        wftool = self.portal_workflow
[757]598        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
[971]599        csv_d = {'jamb_reg_no': "RegNumber",
[958]600                 'jamb_lastname': "Name",
[971]601                 'session': "Session",
602                 'pume_tot_score': "PUME SCORE",
603                 'jamb_score': "JambScore",
[958]604                 'jamb_sex': "Sex",
[971]605                 'jamb_state': "State",
[980]606##                 'jamb_first_cos': "AdminCourse",
[971]607                 'faculty': "AdminFaculty",
608                 'course_code': "AdmitCoscode",
609                 'stud_status':"AdmitStatus",
610                 'department': "AdmitDept",
611                 'jamb_lga': "LGA",
612                 'app_email': "email",
[980]613                 'app_mobile': "PhoneNumbers",
[958]614                 }
615        csv_fields = [f[1] for f in csv_d.items()]
616        tr_count = 0
[961]617        total = 0
[958]618        #name = 'pume_results'
[971]619        name = 'Admitted'
[958]620        no_import = []
[1319]621        s = ','.join(['"%s"' % fn for fn in csv_fields])
[971]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
[1121]625        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write('\n'.join(no_import))
[1082]626        logger = logging.getLogger('Import.%s' % name)
[1121]627        logger.info('start loading from %s.csv' % name)
[971]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()
[958]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:
[971]639            jamb['Error'] = "Processing "
640            logger.info(format % jamb)
[958]641            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
642            res = self.portal_catalog({'portal_type': "StudentApplication",
[959]643                                     'SearchableText': jamb_reg_no })
[958]644            if res:
[980]645                em = 'Student with RegNo %s already exists\n' % jamb_reg_no
[958]646                logger.info(em)
[971]647                jamb['Error'] = "Student exists"
[958]648                no_import.append(format % jamb)
649                continue
[971]650            cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
651            if cert_id not in certs.keys():
[958]652                em = 'No Certificate with ID %s \n' % cert_id
653                logger.info(em)
[971]654                jamb['Error'] = "No Certificate %s" % cert_id
655                no_import.append( format % jamb)
[958]656                continue
[1121]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'])
[971]663            cert_doc = certs[cert_id]
664            catalog_entry = {}
665            catalog_entry['jamb_reg_no'] = jamb_reg_no
[958]666            jamb_name = jamb.get(csv_d['jamb_lastname'])
667            jamb_name.replace('>','')
[966]668            jamb_name.replace('<','')
[958]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)
[971]679            catalog_entry['id'] = sid
[958]680            tr_count += 1
[961]681            logger.info('%(total)s+%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
[958]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',
[966]692                         'jamb_score',
[980]693##                         'jamb_first_cos',
[958]694                         'jamb_sex',
[971]695                         'jamb_state',
696                         'jamb_lga',
697                         'app_email',
[980]698                         'app_mobile',
[958]699                         )
700            for f in da_fields:
701                da[f] = jamb.get(csv_d[f])
[971]702            catalog_entry['email'] = jamb.get(csv_d['app_email'])
[958]703            app = student.application
[971]704            app_doc = app.getContent()
[975]705            picture ="%s/import/pictures/%s.jpg" % (i_home,jamb_reg_no)
[971]706            #import pdb;pdb.set_trace()
707            if os.path.exists(picture):
708                file = open(picture)
[974]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()
[971]719                app_doc.manage_addFile('passport',
720                                       file=outfile,
721                                       title="%s.jpg" % jamb_reg_no)
[958]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                         )
[964]730            dp['pume_tot_score'] = jamb.get(csv_d['pume_tot_score']) or "No Option Shaded"
[958]731            pume = student.pume
732            pume.getContent().edit(mapping=dp)
733            #wftool.doActionFor(pume,'close')
734            pume.manage_setLocalRoles(sid, ['Owner',])
[966]735            #student.getContent().createSubObjects()
[975]736            dp = {}
[966]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()
[971]743                dp['middlename'] = ''
[966]744                dp['lastname'] = names[1].capitalize()
745            else:
[971]746                dp['firstname'] = ''
747                dp['middlename'] = ''
[966]748                dp['lastname'] = jamb_name
749            dp['sex'] = jamb.get(csv_d['jamb_sex']) == 'F'
[971]750            catalog_entry['sex'] = dp['sex']
751            catalog_entry['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
[966]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',])
[958]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
[964]764            #
[966]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            #
[964]772            # Study Course
773            #
[958]774            student.invokeFactory('StudentStudyCourse','study_course')
775            study_course = student.study_course
776            dsc = {}
[1386]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'])
[971]790            catalog_entry['faculty'] = jamb.get(csv_d['faculty'])
791            catalog_entry['department'] = jamb.get(csv_d['department'])
792            catalog_entry['course'] = cert_id
[1386]793            #catalog_entry['level'] = getattr(cert_doc,'start_level')
794            catalog_entry['level'] = '100'
[958]795            dsc['study_course'] = cert_id
[1396]796            dsc['entry_level'] = '100'
[1386]797            #dsc['entry_session'] = jamb.get(csv_d['session'])
[958]798            study_course.getContent().edit(mapping=dsc)
[971]799            self.students_catalog.addRecord(**catalog_entry)
[1126]800            if tr_count > 10:
801                if len(no_import) > 0:
[1127]802                    open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
[1264]803                             '\n'.join(no_import) + "\n")
[966]804                    no_import = []
[961]805                em = '%d transactions commited\n' % tr_count
[958]806                transaction.commit()
807                logger.info(em)
[961]808                total += tr_count
[958]809                tr_count = 0
[1127]810        open("%s/import/%s_not_imported.csv" % (i_home,name),"a").write(
[1126]811                                                '\n'.join(no_import))
[958]812        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
813    ###)
814
[1151]815    security.declareProtected(ModifyPortalContent,"importReturningStudents")###(
816    def importReturningStudents(self):
817        """load Returning Studentdata from CSV values"""
[1146]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'
[1151]827        name = 'Returning'
828        table = self.returning_import
[1146]829        no_import = []
830        imported = []
831        logger = logging.getLogger('Import.%s' % name)
832        try:
[1151]833            returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
[1146]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]
[1319]847        #import pdb;pdb.set_trace()
[1151]848        for student in returning:
[1146]849            if start:
850                start = False
851                logger.info('start loading from %s.csv' % name)
[1319]852                s = ','.join(['"%s"' % fn for fn in student.keys()])
[1146]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
[1319]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')
[1168]861            if matric_no == '':
[1252]862                student['Error'] = "Empty matric_no"
[1146]863                no_import.append( format_error % student)
864                continue
[1168]865            if matric_no in regs or self.returning_import(matric_no = matric_no):
[1252]866                student['Error'] = "Duplicate"
[1146]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:
[1252]877                student['Error'] = "Duplicate"
[1146]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(
[1151]886                             '\n'.join(no_import) + '\n')
[1146]887                    no_import = []
888                open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
[1151]889                                            '\n'.join(no_import) + "\n")
890                imported = []
[1146]891                em = '%d transactions commited total %s\n' % (tr_count,total)
892                transaction.commit()
[1168]893                regs = []
[1146]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(
[1252]898                                            '\n'.join(imported))
[1146]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
[1319]904    security.declareProtected(ModifyPortalContent,"fixAllEntryModeForReturning")###(
905    def fixAllEntryModeForReturning(self):
906        "read all Returning*.csv"
907        ipath = "%s/import/" % i_home
908        names = os.listdir(ipath)
909        for name in names:
910            head,tail = os.path.splitext(name)
911            if head.startswith('Returning')\
912               and tail == '.csv'\
913               and name.find('imported') < 0:
914                self.fixEntryModeForReturning(csv_file=head)
[1322]915    ###)
[1386]916
[1319]917    security.declareProtected(ModifyPortalContent,"fixEntryModeForReturning")###(
918    def fixEntryModeForReturning(self,csv_file=None):
919        """load Returning Studentdata from CSV values"""
920        import transaction
921        import random
922        wftool = self.portal_workflow
923        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
924        students_folder = self.portal_url.getPortalObject().campus.students
925        tr_count = 1
926        total = 0
927        if csv_file is None:
928            name = 'Returning'
929        else:
930            name = csv_file
931        table = self.returning_import
932        st_cat = self.students_catalog
933        no_import = []
934        logger = logging.getLogger('getEntryMode.%s' % name)
935        try:
936            returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
937        except:
938            logger.error('Error reading %s.csv' % name)
939            return
940        start = True
941        for student in returning:
942            if start:
943                start = False
944                logger.info('start loading from %s.csv' % name)
[1321]945                s = ','.join(['"%s"' % fn for fn in student.keys()])
[1319]946                no_import.append('%s,"Error"' % s)
947                format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()])
948                format_error = format + ',"%(Error)s"'
949            matric_no = student.get('matric_no')
950            if not matric_no:
951                continue
952            matric_no = matric_no.upper()
953            student['matric_no'] = matric_no
954            if matric_no == '':
955                continue
956            if not table(matric_no = matric_no):
957                student['Error'] = "Not imported yet"
958                no_import.append( format_error % student)
959                continue
960            student_id = None
961            app = None
962            per = None
963            if st_cat(matric_no = matric_no):
964                student_id = st_cat(matric_no = matric_no)[0].id
[1322]965                student_obj = getattr(students_folder,student_id,None)
966                if student_obj:
967                    app = getattr(student_obj,'application',None)
968                    if app is not None:
969                        app_doc = app.getContent()
970                    per = getattr(student_obj,'personal',None)
971                    if per is not None:
972                        per_doc = per.getContent()
[1319]973            student['Mode_of_Entry'] = entry_mode = student.get('Mode of Entry').upper()
974            student['Permanent_Address'] = perm_address = student.get('Permanent Address')
[1322]975            #import pdb;pdb.set_trace()
[1319]976            if not entry_mode:
977                student['Error'] = "'Mode of Entry' empty"
978                no_import.append( format_error % student)
979                continue
980            try:
981                table.modifyRecord(matric_no = matric_no,
982                                   Mode_of_Entry = entry_mode,
983                                   Permanent_Address = perm_address)
984            except KeyError:
985                student['Error'] = "Not found in returning_import"
986                no_import.append( format_error % student)
987                continue
988            if student_id is not None:
989                try:
990                    st_cat.modifyRecord(id = student_id,
991                                        entry_mode=entry_mode)
992                except KeyError:
993                    student['Error'] = "Not found in students_catalog"
994                    no_import.append( format_error % student)
995                    continue
996            if app is not None:
997                da = {}
998                da['entry_mode'] = entry_mode
999                app_doc.edit(mapping=da)
1000            if per is not None:
1001                dp = {}
[1350]1002                dp['perm_address'] = perm_address
[1319]1003                per_doc.edit(mapping=dp)
1004            tr_count += 1
1005            if tr_count > 1000:
1006                if len(no_import) > 0:
1007                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1008                             '\n'.join(no_import) + '\n')
1009                    no_import = []
1010                em = '%d transactions commited total %s\n' % (tr_count,total)
1011                transaction.commit()
1012                regs = []
1013                logger.info(em)
1014                total += tr_count
1015                tr_count = 0
1016        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1017                                                '\n'.join(no_import))
1018        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1019    ###)
[1289]1020
1021    security.declareProtected(ModifyPortalContent,"updateReturningStudents")###(
1022    def updateReturningStudents(self):
1023        """load and overwrite Returning Student Data from CSV values"""
1024        import transaction
1025        import random
1026        #from pdb import set_trace
1027        wftool = self.portal_workflow
1028        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
1029        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1030        tr_count = 1
1031        total = 0
1032        #name = 'pume_results'
1033        name = 'Returning_update'
1034        table = self.returning_import
1035        no_import = []
1036        imported = []
1037        logger = logging.getLogger('Import.%s' % name)
1038        try:
1039            returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1040        except:
1041            logger.error('Error reading %s.csv' % name)
1042            return
1043        l = self.portal_catalog({'meta_type': "Certificate"})
1044        certs = {}
1045        cert_docs = {}
1046        for f in l:
1047            certs[f.getId] = f.getObject().getContent()
1048        start = True
1049        res = table()
1050        regs = []
1051        if len(res) > 0:
1052            regs = [s.matric_no for s in res]
1053        for student in returning:
1054            if start:
1055                start = False
1056                logger.info('start loading from %s.csv' % name)
[1321]1057                s = ','.join(['"%s"' % fn for fn in student.keys()])
[1289]1058                imported.append(s)
1059                no_import.append('%s,"Error"' % s)
1060                format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()])
1061                format_error = format + ',"%(Error)s"'
1062                no_certificate = "no certificate %s" % format
1063            matric_no = student.get('matric_no').upper()
1064            student['matric_no'] = matric_no
1065            if matric_no == '':
1066                student['Error'] = "Empty matric_no"
1067                no_import.append( format_error % student)
1068                continue
1069#            if matric_no in regs or self.returning_import(matric_no = matric_no):
1070#                student['Error'] = "Duplicate"
1071#                no_import.append( format_error % student)
1072#                continue
1073#            cert_id = makeCertificateCode(student.get('Coursemajorcode'))
1074#            if cert_id not in certs.keys():
1075#                student['Error'] = "No Certificate %s" % cert_id
1076#                no_import.append( format_error % student)
1077#                continue
1078            try:
1079                table.modifyRecord(**student)
1080            except KeyError:
1081                #import pdb;pdb.set_trace()
1082                student['Error'] = "no Student found to update"
1083                no_import.append( format_error % student)
1084                continue
[1386]1085            #s = self.students_catalog(matric_no=matric_no)
1086            #if s:
1087            #    level = "%s" % (int(student.get('Level')) + 100)
1088            #    self.students_catalog.modifyRecord(id = s[0].id,
1089            #                                           level=level)
[1289]1090
1091            regs.append(student.get('matric_no'))
1092            imported.append(format % student)
1093            tr_count += 1
1094            if tr_count > 1000:
1095                if len(no_import) > 0:
1096                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1097                             '\n'.join(no_import) + '\n')
1098                    no_import = []
1099                open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1100                                            '\n'.join(no_import) + "\n")
1101                imported = []
1102                em = '%d transactions commited total %s\n' % (tr_count,total)
1103                transaction.commit()
1104                regs = []
1105                logger.info(em)
1106                total += tr_count
1107                tr_count = 0
1108        open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1109                                            '\n'.join(imported))
1110        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1111                                                '\n'.join(no_import))
1112        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1113    ###)
1114
[1146]1115    security.declareProtected(ModifyPortalContent,"importResults")###(
1116    def importResults(self):
[1151]1117        """load Returning Students Results from CSV"""
[1146]1118        import transaction
1119        import random
1120        #from pdb import set_trace
1121        wftool = self.portal_workflow
1122        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
1123        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1124        tr_count = 1
1125        total = 0
1126        #name = 'pume_results'
1127        name = 'Results'
1128        table = self.results_import
1129        no_import = []
1130        imported = []
1131        logger = logging.getLogger('Import.%s' % name)
1132        try:
1133            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1134        except:
1135            logger.error('Error reading %s.csv' % name)
1136            return
1137        l = self.portal_catalog({'meta_type': "Course"})
1138        courses = [f.getId for f in l]
1139        start = True
1140        res = table()
1141        regs = []
1142        if len(res) > 0:
1143            regs = [s.key for s in res]
1144        no_course = []
1145        no_course_list = []
1146        course_count = 0
1147        for result in results:
1148            if start:
1149                start = False
1150                logger.info('start loading from %s.csv' % name)
[1321]1151                s = ','.join(['"%s"' % fn for fn in result.keys()])
[1146]1152                imported.append(s)
1153                no_import.append('%s,"Error"' % s)
1154                format = ','.join(['"%%(%s)s"' % fn for fn in result.keys()])
1155                format_error = format + ',"%(Error)s"'
1156                no_certificate = "no certificate %s" % format
1157            course_id = result.get('CosCode')
[1448]1158            if not course_id:
1159                course_id = 'N/A'
1160                result['CosCode'] = course_id
[1168]1161            matric_no = result.get('matric_no').upper()
1162            result['matric_no'] = matric_no
1163            key = matric_no+course_id
1164            if matric_no == '':
[1252]1165                result['Error'] = "Empty matric_no"
[1146]1166                no_import.append( format_error % result)
1167                continue
[1168]1168            if key in regs or self.results_import(key = key):
[1252]1169                result['Error'] = "Duplicate"
[1146]1170                no_import.append( format_error % result)
1171                continue
1172            if course_id not in courses:
1173                if course_id not in no_course:
1174                    course_count +=1
1175                    no_course.append(course_id)
1176                    no_course_list.append('"%s"' % course_id)
1177                    #result['Error'] = "No Course"
1178                    #logger.info(format_error % result)
1179            result['key'] = key
1180            try:
1181                table.addRecord(**result)
1182            except ValueError:
[1393]1183                #import pdb;pdb.set_trace()
[1252]1184                result['Error'] = "Duplicate"
[1146]1185                no_import.append( format_error % result)
1186                continue
1187            regs.append(key)
1188            imported.append(format % result)
1189            tr_count += 1
1190            if tr_count > 1000:
1191                if len(no_import) > 0:
1192                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
[1151]1193                             '\n'.join(no_import)+'\n')
[1146]1194                    no_import = []
1195                open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
[1151]1196                                            '\n'.join(imported) + '\n')
[1146]1197                imported = []
1198                if no_course_list:
1199                    open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write(
[1151]1200                                            '\n'.join(no_course_list) + '\n')
[1146]1201                    no_course_list = []
1202                em = '%d transactions commited total %s\n courses not found %s' % (tr_count,total,course_count)
1203                transaction.commit()
1204                logger.info(em)
[1168]1205                regs = []
[1146]1206                total += tr_count
1207                tr_count = 0
1208        open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1209                                            '\n'.join(imported))
1210        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1211                                                '\n'.join(no_import))
1212        if no_course_list:
1213            open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write(
1214                                    '\n'.join(no_course_list))
[1393]1215        em = '%d transactions commited total %s\n courses not found %s' % (tr_count,total,course_count)
1216        logger.info(em)
[1146]1217        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1218    ###)
1219
[1065]1220    security.declareProtected(ModifyPortalContent,"updateStudyCourse")###(
1221    def updateStudyCourse(self):
1222        """update StudyCourse from CSV values"""
1223        import transaction
1224        import random
1225        from pdb import set_trace
1226        wftool = self.portal_workflow
1227        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1228        csv_d = {'jamb_reg_no': "RegNumber",
1229                 'jamb_lastname': "Name",
1230                 'session': "Session",
1231                 'pume_tot_score': "PUME SCORE",
1232                 'jamb_score': "JambScore",
1233                 'jamb_sex': "Sex",
1234                 'jamb_state': "State",
1235##                 'jamb_first_cos': "AdminCourse",
1236                 'faculty': "AdminFaculty",
1237                 'course_code': "AdmitCoscode",
1238                 'stud_status':"AdmitStatus",
1239                 'department': "AdmitDept",
1240                 'jamb_lga': "LGA",
1241                 'app_email': "email",
1242                 'app_mobile': "PhoneNumbers",
1243                 }
1244        csv_fields = [f[1] for f in csv_d.items()]
1245        tr_count = 0
1246        total = 0
1247        #name = 'pume_results'
1248        name = 'StudyCourseChange'
1249        no_import = []
[1321]1250        s = ','.join(['"%s"' % fn for fn in csv_fields])
[1065]1251        no_import.append('"Error",%s' % s)
1252        format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
1253        no_certificate = "no certificate %s" % format
1254        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
1255                    '\n'.join(no_import))
[1082]1256        logger = logging.getLogger('Import.%s' % name)
[1065]1257        logger.info('Start loading from %s.csv' % name)
1258        l = self.portal_catalog({'meta_type': "Certificate"})
1259        try:
1260            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1261        except:
1262            logger.error('Error reading %s.csv' % name)
1263            return
1264        for jamb in result:
1265            jamb['Error'] = "Processing "
1266            logger.info(format % jamb)
1267            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
1268            res = self.portal_catalog({'portal_type': "StudentApplication",
1269                                     'SearchableText': jamb_reg_no })
1270            if not res:
1271                em = 'Student with RegNo %s does not exists\n' % jamb_reg_no
1272                logger.info(em)
1273                jamb['Error'] = "Student not exists"
1274                no_import.append(format % jamb)
1275                continue
1276            sid = res[0].getPath().split('/')[-2]
1277            cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
1278            res = self.portal_catalog(portal_type = "Certificate", id = cert_id)
1279            if not res:
1280                em = 'No Certificate with ID %s \n' % cert_id
1281                logger.info(em)
1282                jamb['Error'] = "No Certificate %s" % cert_id
1283                no_import.append( format % jamb)
1284                continue
1285            cert_brain = res[0]
1286            catalog_entry = {}
1287            student = getattr(self,sid)
1288            #
1289            # Study Course
1290            #
1291            study_course = student.study_course
1292            dsc = {}
1293            cert_pl = cert_brain.getPath().split('/')
1294            catalog_entry['id'] = sid
1295            catalog_entry['faculty'] = cert_pl[-4]
1296            catalog_entry['department'] = cert_pl[-3]
1297            catalog_entry['course'] = cert_id
1298            dsc['study_course'] = cert_id
1299            study_course.getContent().edit(mapping=dsc)
1300            self.students_catalog.modifyRecord(**catalog_entry)
1301            if tr_count > 10:
1302                if len(no_import) > 1:
1303                    open("%s/import/%s_not_imported.csv" % (i_home,name),"w+").write(
1304                             '\n'.join(no_import))
1305                    no_import = []
1306                em = '%d transactions commited\n' % tr_count
1307                transaction.commit()
1308                logger.info(em)
1309                total += tr_count
1310                tr_count = 0
1311            tr_count += 1
1312        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1313    ###)
1314
[958]1315    security.declareProtected(ModifyPortalContent,"OLDloadPumeResultsFromCSV")###(
1316    def OLDloadPumeResultsFromCSV(self):
1317        """load Fulltime Studentdata from CSV values"""
1318        import transaction
1319        import random
1320        wftool = self.portal_workflow
1321        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1322        csv_d = {'jamb_reg_no': "JAMBRegno",
1323                 'jamb_lastname': "Name",
[757]1324                 'pume_options': "Options",
1325                 'session': "Session",
1326                 'days': "Days",
1327                 'response': "Responce",
1328                 'wrong': "Wrong",
1329                 'pume_eng_score': "EngScore",
[775]1330                 'pume_gen_score': "GenScore",
[958]1331                 'pume_tot_score': "Score",
[757]1332                 'batch': "Batch",
1333                 'serial': "SerialNo",
1334                 'jamb_score': "JambScore",
1335                 'omitted':"Omitted",
1336                 'search_key': "SearchKey",
1337                 'jamb_sex': "Sex",
1338                 'fac1': "Fac1",
1339                 'fac2': "Fac2",
1340                 'jamb_first_cos': "CourseofStudy",
1341                 'stud_status':"StudStatus",
1342                 'registered': "Registered",
1343                 'jamb_state': "State",
1344                 'eng_fail': "EngFail",
1345                 'gen_fail': "GenFail",
1346                 'un_ans_eng': "UnAnsEng",
1347                 'un_ans_eng': "UnAnsGen",
1348                 'total_ans': "TotalUnAns",
1349                 'dept': "Dept",
1350                 'jamb_second_cos': "Course2",
1351                 'jamb_third_cos': "course3",
1352                 }
1353        csv_fields = [f[1] for f in csv_d.items()]
[742]1354        tr_count = 0
1355        name = 'pume_results'
[757]1356        no_import = []
[1321]1357        s = ','.join(['"%s"' % fn for fn in csv_fields])
[757]1358        no_import.append('%s\n' % s)
[1082]1359        logger = logging.getLogger('Import.%s' % name)
[742]1360        logger.info('Start loading from %s.csv' % name)
1361        try:
[757]1362            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
[742]1363        except:
1364            logger.error('Error reading %s.csv' % name)
1365            return
[757]1366        for jamb in result:
1367            format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
1368            processing = "processing %s" % format
1369            logger.info(processing % jamb)
1370            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
[763]1371            #import pdb;pdb.set_trace()
1372            res = self.portal_catalog({'portal_type': "StudentApplication",
[757]1373                                     'jamb_reg_no': jamb_reg_no })
1374            if res:
1375                em = 'Student with REG-NO %s already exists\n' % jamb_reg_no
[742]1376                logger.info(em)
[757]1377                no_import.append(em)
1378                no_import.append(format % jamb)
[742]1379                continue
[785]1380            cert_id = jamb.get(csv_d['jamb_first_cos']).upper()
1381            res = self.portal_catalog({'portal_type': "Certificate",
1382                                     'id': cert_id })
1383            if len(res) < 1:
1384                em = 'No Certificate with ID %s \n' % cert_id
1385                logger.info(em)
1386                no_import.append(em)
1387                no_import.append(format % jamb)
1388                continue
1389            cert = res[0].getObject()
1390            cert_path = res[0].getPath()
1391            cert_doc = cert.getContent()
[757]1392            jamb_name = jamb.get(csv_d['jamb_lastname'])
1393            jamb_name.replace('>','')
1394            names = jamb_name.split()
1395            letter = names[-1][0].upper()
1396            sid = self.generateStudentId(letter)
1397            not_created = True
1398            while not_created:
1399                try:
1400                    students_folder.invokeFactory('Student', sid)
1401                    not_created = False
1402                except BadRequest:
1403                    sid = self.generateStudentId(letter)
1404            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
1405            student = getattr(self,sid)
1406            student.manage_setLocalRoles(sid, ['Owner',])
[766]1407            student.invokeFactory('StudentClearance','clearance')
[775]1408            #wftool.doActionFor(student.clearance,'open')
[840]1409            dp = {'Title': 'Clearance/Eligibility Record'}
[766]1410            student.clearance.manage_setLocalRoles(sid, ['Owner',])
[763]1411            student.invokeFactory('StudentPume','pume')
1412            dp = {'Title': 'Pume Data'}
[757]1413            student.invokeFactory('StudentApplication','application')
1414            da = {'Title': 'Application Data'}
1415            da["jamb_lastname"] = jamb_name
1416            da_fields = ('jamb_reg_no',
1417                         'jamb_sex',
1418                         'jamb_state',
1419                         'jamb_score',
1420                         'jamb_first_cos',
1421                         'jamb_sex',
1422                         'jamb_state',
1423                         'jamb_first_cos',
1424                         'jamb_second_cos',
1425                         )
[763]1426            for f in da_fields:
1427                da[f] = jamb.get(csv_d[f])
[757]1428            app = student.application
[763]1429            app.getContent().edit(mapping=da)
[757]1430            app.manage_setLocalRoles(sid, ['Owner',])
[780]1431            #wftool.doActionFor(app,'close')
[763]1432            dp_fields = (
1433                         'pume_eng_score',
[764]1434                         'pume_gen_score',
[763]1435                         'pume_tot_score',
1436                         )
1437            for f in dp_fields:
[775]1438                dp[f] = float(jamb.get(csv_d[f]))
[765]1439            pume = student.pume
1440            pume.getContent().edit(mapping=dp)
[780]1441            #wftool.doActionFor(pume,'close')
[763]1442            pume.manage_setLocalRoles(sid, ['Owner',])
[785]1443            #
1444            # Study Course
1445            #
1446            student.invokeFactory('StudentStudyCourse','study_course')
1447            study_course = student.study_course
1448            dsc = {}
1449            from_certificate = ['title',
1450                               'max_elect',
1451                               'max_pass',
1452                               'n_core',
1453                               'nr_years',
1454                               'probation_credits',
1455                               'promotion_credits',
1456                               'start_level',
1457                              ]
1458            for f in from_certificate:
1459                dsc[f] = getattr(cert_doc,f)
1460            cpl = cert_path.split('/')
1461            dsc['faculty'] = cpl[-4]
1462            dsc['department'] = cpl[-3]
1463            dsc['study_course'] = cert_id
1464            dsc['entry_session'] = jamb.get(csv_d['session'])
1465            study_course.getContent().edit(mapping=dsc)
[757]1466            student.getContent().createSubObjects()
[775]1467            if dp['pume_tot_score']>49:
[780]1468                wftool.doActionFor(student,'pume_pass')
[785]1469                wftool.doActionFor(student,'admit')
[775]1470            else:
[780]1471                wftool.doActionFor(student,'pume_fail')
[790]1472                wftool.doActionFor(student,'reject_admission')
[757]1473            if len(no_import) > 1:
1474                open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
1475                     '\n'.join(no_import))
[742]1476        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1477    ###)
1478
[398]1479    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
[396]1480    def loadFullTimeStudentsResultsFromCSV(self):
1481        """load Fulltime Studentdata from CSV values"""
1482        #return
[723]1483        level_wf_actions = {}
1484        level_wf_actions["SUCCESSFUL STUDENT"] = "pass_A"
1485        level_wf_actions["STUDENT WITH CARRYOVER COURSES"] = "pass_B"
[727]1486        level_wf_actions["STUDENT FOR PROBATION"] = "probate_C"
1487        level_wf_actions["STUDENT ON PROBATION/TRANSFER"] = "reject_D"
[398]1488        import transaction
[723]1489        wftool = self.portal_workflow
[398]1490        tr_count = 0
1491        name = 'short_full_time_results_2004_2005'
[396]1492        no_import = False
1493        if not no_import:
1494            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
1495            no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
[1146]1496        logger = logging.getLogger('import.%s' % name)
[396]1497        logger.info('Start loading from %s.csv' % name)
1498        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1499        try:
1500            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1501        except:
1502            logger.error('Error reading %s.csv' % name)
1503            return
[398]1504        l = self.portal_catalog({'meta_type': "Course"})
1505        courses = {}
1506        for c in l:
1507            courses[c.id] = c.getObject()
[723]1508        level_changed = False
1509        student_changed = False
1510        sid = ''
1511        #import pdb;pdb.set_trace()
[396]1512        for result in results:
[723]1513            temp_sid = result.get('Matnumber')
1514            if temp_sid != sid:
1515                student_changed = True
1516                res = self.portal_catalog({'meta_type': "StudentClearance",
1517                                         'SearchableText': temp_sid })
1518                if not res:
1519                    em = 'Student with ID %(Matnumber)s not found\n' % result
1520                    logger.info(em)
1521                    no_import.write(em)
1522                    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)
1523                    continue
1524                elif len(res) > 1:
1525                    em = 'More than one Student with ID %(Matnumber)s found\n' % result
1526                    logger.info(em)
1527                    no_import.write(em)
1528                    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)
1529                    continue
1530                sid = temp_sid
1531                sf = res[0].getObject().aq_parent
1532                sc = getattr(sf,'study_course')
1533                level = ''
1534            else:
1535                student_changed = False
[398]1536            course = result.get('CosCode')
1537            if course not in courses.keys():
1538                em = 'Course with ID %(CosCode)s not found\n' % result
[396]1539                logger.info(em)
1540                no_import.write(em)
[398]1541                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)
[396]1542                continue
[727]1543            course_doc = courses[course].getContent()
[723]1544            temp_level = result.get('Level')
[742]1545            student_id = sf.getId()
1546            result['StudentId'] = student_id
[723]1547            if temp_level != level:
1548                try:
1549                    int(temp_level)
1550                except:
1551                    em = 'Result with ID %(Matnumber)s Course %(CosCode)s Level is empty\n' % result
1552                    logger.info(em)
1553                    no_import.write(em)
1554                    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)
1555                    continue
[727]1556                level_changed = True
1557                if 'dlev' in vars().keys():
[723]1558                    wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
1559                level = temp_level
1560                l = getattr(sc,level,None)
1561                if l is None:
1562                    logger.info('Creating Level %(Level)s for %(StudentId)s %(Matnumber)s' % result)
1563                    sc.invokeFactory('StudentStudyLevel', level)
1564                    l = getattr(sc, level)
[742]1565                    l.manage_setLocalRoles(student_id, ['Owner',])
[723]1566            else:
1567                level_changed = False
1568            cr = getattr(l,course,None)
1569            if cr is None:
1570                logger.info('Creating Course %(CosCode)s for %(StudentId)s %(Matnumber)s in Level %(Level)s' % result)
1571                l.invokeFactory('StudentCourseResult',course)
1572            cr = getattr(l,course)
1573            dcr = {}
[727]1574            from_course = ['title',
1575                           'credits',
1576                           'passmark',
1577                           ]
1578            for f in from_course:
1579                dcr[f] = getattr(course_doc,f)
[454]1580            dlev = {}
[723]1581            dcr['ansbook'] = result.get('Ansbook')
1582            dcr['semester'] = getInt(result.get('Semster'))
1583            dcr['status'] = result.get('CosStuatus')
1584            dcr['score'] = getInt(result.get('Score'))
[454]1585            dlev['session'] = result.get('Session')
[723]1586            dcr['carry_level'] = result.get('CarryLevel')
1587            dcr['grade'] = result.get('Grade')
[725]1588            dcr['weight'] = result.get('Weight')
[454]1589            dlev['verdict'] = result.get('Verdict')
[725]1590            dcr['import_id'] = result.get('id')
1591            gpa = result.get('GPA').replace(',','.')
1592            dlev['imported_gpa'] = getFloat(gpa)
[723]1593            cr.getContent().edit(mapping = dcr)
[742]1594            cr.manage_setLocalRoles(student_id, ['Owner',])
[454]1595            l.getContent().edit(mapping = dlev)
[398]1596            if tr_count > MAX_TRANS:
1597                transaction.commit()
1598                tr_count = 0
1599            tr_count += 1
[723]1600            wftool.doActionFor(cr,'close')
1601        wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
[681]1602        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
[426]1603
[398]1604###)
[396]1605
[457]1606    security.declareProtected(ModifyPortalContent,"loadJAMBFromCSV")###(
1607    def loadJAMBFromCSV(self):
1608        """load JAMB data from CSV values"""
1609        #return
1610        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1611        import transaction
1612        tr_count = 0
[572]1613        name = 'SampleJAMBDataII'
[511]1614        wftool = self.portal_workflow
[457]1615        no_import = False
1616        if not no_import:
1617            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
1618            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')
[1082]1619        logger = logging.getLogger('Import.%s' % name)
[457]1620        logger.info('Start loading from %s.csv' % name)
1621        try:
1622            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1623        except:
1624            logger.error('Error reading %s.csv' % name)
1625            return
1626        for jamb in result:
1627            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)
1628            jamb_reg_no = jamb.get('REG-NO')
[472]1629            res = self.portal_catalog({'meta_type': "StudentApplication",
[457]1630                                     'jamb_reg_no': jamb_reg_no })
1631            if res:
1632                em = 'Student with REG-NO %(REG-NO)s already exists\n' % jamb
1633                logger.info(em)
1634                no_import.write(em)
1635                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)
1636                continue
[511]1637            jamb_name = jamb.get("NAME")
[584]1638            jamb_name.replace('>','')
[511]1639            names = jamb_name.split()
1640            letter = names[-1][0].upper()
[714]1641            sid = self.generateStudentId(letter)
[457]1642            not_created = True
1643            while not_created:
1644                try:
1645                    students_folder.invokeFactory('Student', sid)
1646                    not_created = False
1647                except BadRequest:
[714]1648                    sid = self.generateStudentId(letter)
[457]1649            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
[511]1650            student = getattr(self,sid)
[519]1651            student.manage_setLocalRoles(sid, ['Owner',])
[511]1652            student.invokeFactory('StudentApplication','application')
[472]1653            da = {'Title': 'Application Data'}
[457]1654            da["jamb_reg_no"] = jamb.get("REG-NO")
[511]1655            da["jamb_lastname"] = jamb_name
[457]1656            da["jamb_sex"] = jamb.get("SEX")
1657            da["jamb_state"] = jamb.get("STATE")
1658            da["jamb_lga"] = jamb.get("LGA")
1659            da["jamb_score"] = jamb.get("AGGREGATE")
1660            da["jamb_first_cos"] = jamb.get("COURSE1")
1661            da["jamb_second_cos"] = jamb.get("COURSE2")
1662            da["jamb_first_uni"] = jamb.get("UNIV1")
1663            da["jamb_second_uni"] = jamb.get("UNIV2")
[511]1664            app = student.application
1665            app_doc = app.getContent()
1666            app_doc.edit(mapping=da)
[658]1667            #wftool.doActionFor(app,'open',dest_container=app)
[511]1668            app.manage_setLocalRoles(sid, ['Owner',])
1669            student.getContent().createSubObjects()
[457]1670        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1671    ###)
[426]1672
[472]1673
[511]1674    security.declareProtected(View,"fixOwnership")
1675    def fixOwnership(self):
1676        """fix Ownership"""
1677        for s in self.portal_catalog(meta_type = 'Student'):
1678            student = s.getObject()
1679            sid = s.getId
1680            import pdb;pdb.set_trace()
1681            student.application.manage_setLocalRoles(sid, ['Owner',])
1682            student.personal.manage_setLocalRoles(sid, ['Owner',])
[603]1683
[364]1684    security.declareProtected(View,"Title")
1685    def Title(self):
1686        """compose title"""
[382]1687        return "Student Section"
[361]1688
[714]1689    def generateStudentId(self,letter): ###(
1690        import random
1691        r = random
1692        if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
1693            letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY')
1694        students = self.portal_catalog(meta_type = "StudentsFolder")[-1]
1695        sid = "%c%d" % (letter,r.randint(99999,1000000))
1696        while hasattr(students, sid):
1697            sid = "%c%d" % (letter,r.randint(99999,1000000))
[758]1698        return sid
[714]1699        #return "%c%d" % (r.choice('ABCDEFGHKLMNPQRSTUVWXY'),r.randint(99999,1000000))
1700    ###)
1701
[361]1702InitializeClass(StudentsFolder)
1703
1704def addStudentsFolder(container, id, REQUEST=None, **kw):
1705    """Add a Student."""
1706    ob = StudentsFolder(id, **kw)
1707    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1708###)
1709
[57]1710class Student(CPSDocument): ###(
1711    """
[154]1712    WAeUP Student container for the various student data
[57]1713    """
1714    meta_type = 'Student'
1715    portal_type = meta_type
1716    security = ClassSecurityInfo()
[154]1717
[152]1718    security.declareProtected(View,"Title")
1719    def Title(self):
1720        """compose title"""
[153]1721        reg_nr = self.getId()[1:]
[362]1722        data = getattr(self,'personal',None)
[152]1723        if data:
1724            content = data.getContent()
[1143]1725            return "%s %s %s" % (content.firstname,content.middlename,content.lastname)
[472]1726        data = getattr(self,'application',None)
[464]1727        if data:
1728            content = data.getContent()
1729            return "%s" % (content.jamb_lastname)
[152]1730        return self.title
[154]1731
[511]1732    security.declarePrivate('makeStudentMember') ###(
1733    def makeStudentMember(self,sid,password='uNsEt'):
1734        """make the student a member"""
1735        membership = self.portal_membership
[603]1736        membership.addMember(sid,
[511]1737                             password ,
1738                             roles=('Member',
1739                                     'Student',
[522]1740                                     ),
[511]1741                             domains='',
[904]1742                             properties = {'memberareaCreationFlag': False,
1743                                           'homeless': True},)
[511]1744        member = membership.getMemberById(sid)
1745        self.portal_registration.afterAdd(member, sid, password, None)
1746        self.manage_setLocalRoles(sid, ['Owner',])
1747
1748###)
1749
[764]1750    security.declareProtected(View,'createSubObjects') ###(
1751    def createSubObjects(self):
1752        """make the student a member"""
1753        dp = {'Title': 'Personal Data'}
1754        app_doc = self.application.getContent()
1755        names = app_doc.jamb_lastname.split()
1756        if len(names) == 3:
1757            dp['firstname'] = names[0].capitalize()
1758            dp['middlename'] = names[1].capitalize()
1759            dp['lastname'] = names[2].capitalize()
1760        elif len(names) == 2:
1761            dp['firstname'] = names[0].capitalize()
1762            dp['lastname'] = names[1].capitalize()
1763        else:
1764            dp['lastname'] = app_doc.jamb_lastname
1765        dp['sex'] = app_doc.jamb_sex == 'F'
1766        dp['lga'] = "%s/%s" % (app_doc.jamb_state,app_doc.jamb_lga )
1767        proxy = self.aq_parent
1768        proxy.invokeFactory('StudentPersonal','personal')
1769        per = proxy.personal
1770        per_doc = per.getContent()
1771        per_doc.edit(mapping = dp)
[927]1772        per.manage_setLocalRoles(proxy.getId(), ['Owner',])
[764]1773        #self.portal_workflow.doActionFor(per,'open',dest_container=per)
[603]1774
[511]1775###)
1776
[57]1777InitializeClass(Student)
1778
1779def addStudent(container, id, REQUEST=None, **kw):
1780    """Add a Student."""
1781    ob = Student(id, **kw)
1782    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1783
1784###)
[91]1785
[639]1786class StudentAccommodation(CPSDocument): ###(
1787    """
1788    WAeUP Student container for the various student data
1789    """
1790    meta_type = 'StudentAccommodation'
1791    portal_type = meta_type
1792    security = ClassSecurityInfo()
1793
1794    security.declareProtected(View,"Title")
1795    def Title(self):
1796        """compose title"""
1797        content = self.getContent()
1798        #return "Accommodation Data for %s %s" % (content.firstname,content.lastname)
1799        return "Accommodation Data for Session %s" % content.session
1800
1801
1802InitializeClass(StudentAccommodation)
1803
1804def addStudentAccommodation(container, id, REQUEST=None, **kw):
1805    """Add a Students personal data."""
1806    ob = StudentAccommodation(id, **kw)
1807    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1808
1809###)
1810
[89]1811class StudentPersonal(CPSDocument): ###(
1812    """
[154]1813    WAeUP Student container for the various student data
[89]1814    """
1815    meta_type = 'StudentPersonal'
1816    portal_type = meta_type
1817    security = ClassSecurityInfo()
[152]1818
1819    security.declareProtected(View,"Title")
1820    def Title(self):
1821        """compose title"""
1822        content = self.getContent()
[364]1823        #return "Personal Data for %s %s" % (content.firstname,content.lastname)
1824        return "Personal Data"
[152]1825
[154]1826
[89]1827InitializeClass(StudentPersonal)
1828
1829def addStudentPersonal(container, id, REQUEST=None, **kw):
1830    """Add a Students personal data."""
1831    ob = StudentPersonal(id, **kw)
1832    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1833
1834###)
1835
[423]1836class StudentClearance(CPSDocument): ###(
1837    """
1838    WAeUP Student container for the various student data
1839    """
1840    meta_type = 'StudentClearance'
1841    portal_type = meta_type
1842    security = ClassSecurityInfo()
1843
1844    security.declareProtected(View,"Title")
1845    def Title(self):
1846        """compose title"""
1847        content = self.getContent()
[840]1848        #return "Clearance/Eligibility Record for %s %s" % (content.firstname,content.lastname)
1849        return "Clearance/Eligibility Record"
[423]1850
1851
1852InitializeClass(StudentClearance)
1853
1854def addStudentClearance(container, id, REQUEST=None, **kw):
1855    """Add a Students personal data."""
1856    ob = StudentClearance(id, **kw)
1857    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1858
1859###)
1860
[454]1861class StudentStudyLevel(CPSDocument): ###(
1862    """
1863    WAeUP Student container for the various student data
1864    """
1865    meta_type = 'StudentStudyLevel'
1866    portal_type = meta_type
1867    security = ClassSecurityInfo()
1868
1869    security.declareProtected(View,"Title")
1870    def Title(self):
1871        """compose title"""
1872        return "Level %s" % self.aq_parent.getId()
1873
[723]1874##    security.declarePublic("gpa")
1875##    def gpa(self):
1876##        """calculate the gpa"""
1877##        sum = 0
1878##        course_count = 0
1879##        for sc in self.objectValues():
1880##            result = sc.getContent()
1881##            if not result.grade:
1882##                continue
1883##            res = self.portal_catalog({'meta_type': 'Course',
1884##                                          'id': sc.aq_parent.id})
1885##            if len(res) != 1:
1886##                continue
1887##            course = res[0].getObject().getContent()
1888##            sum += course.credits * ['F','E','D','C','B','A'].index(result.grade)
1889##            course_count += 1
1890##        if course_count:
1891##            return sum/course_count
1892##        return 0.0
[472]1893
[454]1894InitializeClass(StudentStudyLevel)
1895
1896def addStudentStudyLevel(container, id, REQUEST=None, **kw):
1897    """Add a Students personal data."""
1898    ob = StudentStudyLevel(id, **kw)
1899    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1900
1901###)
1902
[362]1903class StudentStudyCourse(CPSDocument): ###(
1904    """
1905    WAeUP Student container for the various student data
1906    """
1907    meta_type = 'StudentStudyCourse'
1908    portal_type = meta_type
1909    security = ClassSecurityInfo()
1910
[364]1911    security.declareProtected(View,"Title")
1912    def Title(self):
1913        """compose title"""
1914        content = self.getContent()
[453]1915        return "Study Course"
[362]1916
1917
1918InitializeClass(StudentStudyCourse)
1919
1920def addStudentStudyCourse(container, id, REQUEST=None, **kw):
1921    """Add a Students personal data."""
1922    ob = StudentStudyCourse(id, **kw)
1923    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1924
1925###)
1926
[472]1927class StudentApplication(CPSDocument): ###(
[179]1928    """
1929    WAeUP Student container for the various student data
1930    """
[472]1931    meta_type = 'StudentApplication'
[179]1932    portal_type = meta_type
1933    security = ClassSecurityInfo()
1934
[181]1935    security.declareProtected(View,"Title")
1936    def Title(self):
1937        """compose title"""
[472]1938        return "Application Data"
[179]1939
[181]1940
[472]1941InitializeClass(StudentApplication)
[179]1942
[472]1943def addStudentApplication(container, id, REQUEST=None, **kw):
[179]1944    """Add a Students eligibility data."""
[472]1945    ob = StudentApplication(id, **kw)
[179]1946    return CPSBase_adder(container, ob, REQUEST=REQUEST)
[763]1947###)
[179]1948
[758]1949class StudentPume(CPSDocument): ###(
1950    """
1951    WAeUP Student container for the various student data
1952    """
1953    meta_type = 'StudentPume'
1954    portal_type = meta_type
1955    security = ClassSecurityInfo()
1956
1957    security.declareProtected(View,"Title")
1958    def Title(self):
1959        """compose title"""
1960        return "PUME Results"
1961
1962
1963InitializeClass(StudentPume)
1964
1965def addStudentPume(container, id, REQUEST=None, **kw):
1966    """Add a Students PUME data."""
1967    ob = StudentPume(id, **kw)
1968    return CPSBase_adder(container, ob, REQUEST=REQUEST)
[179]1969###)
[181]1970
[565]1971##class StudentSemester(CPSDocument): ###(
1972##    """
1973##    WAeUP StudentSemester containing the courses and students
1974##    """
1975##    meta_type = 'StudentSemester'
1976##    portal_type = meta_type
1977##    security = ClassSecurityInfo()
1978##
1979##InitializeClass(StudentSemester)
1980##
1981##def addStudentSemester(container, id, REQUEST=None, **kw):
1982##    """Add a StudentSemester."""
1983##    ob = StudentSemester(id, **kw)
1984##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1985##
1986#####)
[464]1987
[758]1988##class Semester(CPSDocument): ###(
1989##    """
1990##    WAeUP Semester containing the courses and students
1991##    """
1992##    meta_type = 'Semester'
1993##    portal_type = meta_type
1994##    security = ClassSecurityInfo()
1995##
1996##InitializeClass(Semester)
1997##
1998##def addSemester(container, id, REQUEST=None, **kw):
1999##    """Add a Semester."""
2000##    ob = Semester(id, **kw)
2001##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
2002##
2003#####)
[464]2004
2005class StudentCourseResult(CPSDocument): ###(
[89]2006    """
[464]2007    WAeUP StudentCourseResult
[89]2008    """
[464]2009    meta_type = 'StudentCourseResult'
[89]2010    portal_type = meta_type
2011    security = ClassSecurityInfo()
[472]2012
[454]2013    def getCourseEntry(self,cid):
[723]2014        res = self.portal_catalog({'meta_type': "Course",
[454]2015                                           'id': cid})
2016        if res:
2017            return res[-1]
2018        else:
2019            return None
[154]2020
[454]2021    security.declareProtected(View,"Title")
2022    def Title(self):
2023        """compose title"""
[723]2024        cid = self.aq_parent.getId()
[454]2025        ce = self.getCourseEntry(cid)
2026        if ce:
2027            return "%s" % ce.Title
2028        return "No course with id %s" % cid
[152]2029
[464]2030InitializeClass(StudentCourseResult)
[454]2031
[464]2032def addStudentCourseResult(container, id, REQUEST=None, **kw):
2033    """Add a StudentCourseResult."""
2034    ob = StudentCourseResult(id, **kw)
[89]2035    return CPSBase_adder(container, ob, REQUEST=REQUEST)
[139]2036###)
2037
[579]2038# Backward Compatibility StudyLevel
2039
2040from Products.WAeUP_SRP.Academics import StudyLevel
2041
2042from Products.WAeUP_SRP.Academics import addStudyLevel
2043
Note: See TracBrowser for help on using the repository browser.