source: WAeUP_SRP/trunk/Students.py @ 1366

Last change on this file since 1366 was 1350, checked in by joachim, 18 years ago

added Patch for VocalbulariesTool?, which fixed the loading of to many objects.

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