source: WAeUP_SRP/trunk/Students.py @ 1321

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

no import headline fixed

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