source: WAeUP_SRP/trunk/Students.py @ 1442

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

auf pup_update gewechselt

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