source: WAeUP_SRP/trunk/Students.py @ 1388

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

Datenimport für study_course angepasst

bitte prüfen!

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