source: WAeUP_SRP/branches/regebro-noskins/Students.py @ 7657

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

CouseAdviser? role modifications

  • Property svn:keywords set to Id
File size: 87.0 KB
Line 
1#-*- mode: python; mode: fold -*-
2# $Id: Students.py 1515 2007-03-03 11:45:58Z 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
25# import 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            if not course_id:
1159                course_id = 'N/A'
1160                result['CosCode'] = course_id
1161            matric_no = result.get('matric_no').upper()
1162            result['matric_no'] = matric_no
1163            key = matric_no+course_id
1164            if matric_no == '':
1165                result['Error'] = "Empty matric_no"
1166                no_import.append( format_error % result)
1167                continue
1168            if key in regs or self.results_import(key = key):
1169                result['Error'] = "Duplicate"
1170                no_import.append( format_error % result)
1171                continue
1172            if course_id not in courses:
1173                if course_id not in no_course:
1174                    course_count +=1
1175                    no_course.append(course_id)
1176                    no_course_list.append('"%s"' % course_id)
1177                    #result['Error'] = "No Course"
1178                    #logger.info(format_error % result)
1179            result['key'] = key
1180            try:
1181                table.addRecord(**result)
1182            except ValueError:
1183                #import pdb;pdb.set_trace()
1184                result['Error'] = "Duplicate"
1185                no_import.append( format_error % result)
1186                continue
1187            regs.append(key)
1188            imported.append(format % result)
1189            tr_count += 1
1190            if tr_count > 1000:
1191                if len(no_import) > 0:
1192                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1193                             '\n'.join(no_import)+'\n')
1194                    no_import = []
1195                open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1196                                            '\n'.join(imported) + '\n')
1197                imported = []
1198                if no_course_list:
1199                    open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write(
1200                                            '\n'.join(no_course_list) + '\n')
1201                    no_course_list = []
1202                em = '%d transactions commited total %s\n courses not found %s' % (tr_count,total,course_count)
1203                transaction.commit()
1204                logger.info(em)
1205                regs = []
1206                total += tr_count
1207                tr_count = 0
1208        open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1209                                            '\n'.join(imported))
1210        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1211                                                '\n'.join(no_import))
1212        if no_course_list:
1213            open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write(
1214                                    '\n'.join(no_course_list))
1215        em = '%d transactions commited total %s\n courses not found %s' % (tr_count,total,course_count)
1216        logger.info(em)
1217        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1218    ###)
1219
1220    security.declareProtected(ModifyPortalContent,"updateStudyCourse")###(
1221    def updateStudyCourse(self):
1222        """update StudyCourse from CSV values"""
1223        import transaction
1224        import random
1225        from pdb import set_trace
1226        wftool = self.portal_workflow
1227        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1228        csv_d = {'jamb_reg_no': "RegNumber",
1229                 'jamb_lastname': "Name",
1230                 'session': "Session",
1231                 'pume_tot_score': "PUME SCORE",
1232                 'jamb_score': "JambScore",
1233                 'jamb_sex': "Sex",
1234                 'jamb_state': "State",
1235##                 'jamb_first_cos': "AdminCourse",
1236                 'faculty': "AdminFaculty",
1237                 'course_code': "AdmitCoscode",
1238                 'stud_status':"AdmitStatus",
1239                 'department': "AdmitDept",
1240                 'jamb_lga': "LGA",
1241                 'app_email': "email",
1242                 'app_mobile': "PhoneNumbers",
1243                 }
1244        csv_fields = [f[1] for f in csv_d.items()]
1245        tr_count = 0
1246        total = 0
1247        #name = 'pume_results'
1248        name = 'StudyCourseChange'
1249        no_import = []
1250        s = ','.join(['"%s"' % fn for fn in csv_fields])
1251        no_import.append('"Error",%s' % s)
1252        format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
1253        no_certificate = "no certificate %s" % format
1254        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
1255                    '\n'.join(no_import))
1256        logger = logging.getLogger('Import.%s' % name)
1257        logger.info('Start loading from %s.csv' % name)
1258        l = self.portal_catalog({'meta_type': "Certificate"})
1259        try:
1260            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1261        except:
1262            logger.error('Error reading %s.csv' % name)
1263            return
1264        for jamb in result:
1265            jamb['Error'] = "Processing "
1266            logger.info(format % jamb)
1267            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
1268            res = self.portal_catalog({'portal_type': "StudentApplication",
1269                                     'SearchableText': jamb_reg_no })
1270            if not res:
1271                em = 'Student with RegNo %s does not exists\n' % jamb_reg_no
1272                logger.info(em)
1273                jamb['Error'] = "Student not exists"
1274                no_import.append(format % jamb)
1275                continue
1276            sid = res[0].getPath().split('/')[-2]
1277            cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
1278            res = self.portal_catalog(portal_type = "Certificate", id = cert_id)
1279            if not res:
1280                em = 'No Certificate with ID %s \n' % cert_id
1281                logger.info(em)
1282                jamb['Error'] = "No Certificate %s" % cert_id
1283                no_import.append( format % jamb)
1284                continue
1285            cert_brain = res[0]
1286            catalog_entry = {}
1287            student = getattr(self,sid)
1288            #
1289            # Study Course
1290            #
1291            study_course = student.study_course
1292            dsc = {}
1293            cert_pl = cert_brain.getPath().split('/')
1294            catalog_entry['id'] = sid
1295            catalog_entry['faculty'] = cert_pl[-4]
1296            catalog_entry['department'] = cert_pl[-3]
1297            catalog_entry['course'] = cert_id
1298            dsc['study_course'] = cert_id
1299            study_course.getContent().edit(mapping=dsc)
1300            self.students_catalog.modifyRecord(**catalog_entry)
1301            if tr_count > 10:
1302                if len(no_import) > 1:
1303                    open("%s/import/%s_not_imported.csv" % (i_home,name),"w+").write(
1304                             '\n'.join(no_import))
1305                    no_import = []
1306                em = '%d transactions commited\n' % tr_count
1307                transaction.commit()
1308                logger.info(em)
1309                total += tr_count
1310                tr_count = 0
1311            tr_count += 1
1312        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1313    ###)
1314
1315    security.declareProtected(ModifyPortalContent,"OLDloadPumeResultsFromCSV")###(
1316    def OLDloadPumeResultsFromCSV(self):
1317        """load Fulltime Studentdata from CSV values"""
1318        import transaction
1319        import random
1320        wftool = self.portal_workflow
1321        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1322        csv_d = {'jamb_reg_no': "JAMBRegno",
1323                 'jamb_lastname': "Name",
1324                 'pume_options': "Options",
1325                 'session': "Session",
1326                 'days': "Days",
1327                 'response': "Responce",
1328                 'wrong': "Wrong",
1329                 'pume_eng_score': "EngScore",
1330                 'pume_gen_score': "GenScore",
1331                 'pume_tot_score': "Score",
1332                 'batch': "Batch",
1333                 'serial': "SerialNo",
1334                 'jamb_score': "JambScore",
1335                 'omitted':"Omitted",
1336                 'search_key': "SearchKey",
1337                 'jamb_sex': "Sex",
1338                 'fac1': "Fac1",
1339                 'fac2': "Fac2",
1340                 'jamb_first_cos': "CourseofStudy",
1341                 'stud_status':"StudStatus",
1342                 'registered': "Registered",
1343                 'jamb_state': "State",
1344                 'eng_fail': "EngFail",
1345                 'gen_fail': "GenFail",
1346                 'un_ans_eng': "UnAnsEng",
1347                 'un_ans_eng': "UnAnsGen",
1348                 'total_ans': "TotalUnAns",
1349                 'dept': "Dept",
1350                 'jamb_second_cos': "Course2",
1351                 'jamb_third_cos': "course3",
1352                 }
1353        csv_fields = [f[1] for f in csv_d.items()]
1354        tr_count = 0
1355        name = 'pume_results'
1356        no_import = []
1357        s = ','.join(['"%s"' % fn for fn in csv_fields])
1358        no_import.append('%s\n' % s)
1359        logger = logging.getLogger('Import.%s' % name)
1360        logger.info('Start loading from %s.csv' % name)
1361        try:
1362            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1363        except:
1364            logger.error('Error reading %s.csv' % name)
1365            return
1366        for jamb in result:
1367            format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
1368            processing = "processing %s" % format
1369            logger.info(processing % jamb)
1370            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
1371            #import pdb;pdb.set_trace()
1372            res = self.portal_catalog({'portal_type': "StudentApplication",
1373                                     'jamb_reg_no': jamb_reg_no })
1374            if res:
1375                em = 'Student with REG-NO %s already exists\n' % jamb_reg_no
1376                logger.info(em)
1377                no_import.append(em)
1378                no_import.append(format % jamb)
1379                continue
1380            cert_id = jamb.get(csv_d['jamb_first_cos']).upper()
1381            res = self.portal_catalog({'portal_type': "Certificate",
1382                                     'id': cert_id })
1383            if len(res) < 1:
1384                em = 'No Certificate with ID %s \n' % cert_id
1385                logger.info(em)
1386                no_import.append(em)
1387                no_import.append(format % jamb)
1388                continue
1389            cert = res[0].getObject()
1390            cert_path = res[0].getPath()
1391            cert_doc = cert.getContent()
1392            jamb_name = jamb.get(csv_d['jamb_lastname'])
1393            jamb_name.replace('>','')
1394            names = jamb_name.split()
1395            letter = names[-1][0].upper()
1396            sid = self.generateStudentId(letter)
1397            not_created = True
1398            while not_created:
1399                try:
1400                    students_folder.invokeFactory('Student', sid)
1401                    not_created = False
1402                except BadRequest:
1403                    sid = self.generateStudentId(letter)
1404            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
1405            student = getattr(self,sid)
1406            student.manage_setLocalRoles(sid, ['Owner',])
1407            student.invokeFactory('StudentClearance','clearance')
1408            #wftool.doActionFor(student.clearance,'open')
1409            dp = {'Title': 'Clearance/Eligibility Record'}
1410            student.clearance.manage_setLocalRoles(sid, ['Owner',])
1411            student.invokeFactory('StudentPume','pume')
1412            dp = {'Title': 'Pume Data'}
1413            student.invokeFactory('StudentApplication','application')
1414            da = {'Title': 'Application Data'}
1415            da["jamb_lastname"] = jamb_name
1416            da_fields = ('jamb_reg_no',
1417                         'jamb_sex',
1418                         'jamb_state',
1419                         'jamb_score',
1420                         'jamb_first_cos',
1421                         'jamb_sex',
1422                         'jamb_state',
1423                         'jamb_first_cos',
1424                         'jamb_second_cos',
1425                         )
1426            for f in da_fields:
1427                da[f] = jamb.get(csv_d[f])
1428            app = student.application
1429            app.getContent().edit(mapping=da)
1430            app.manage_setLocalRoles(sid, ['Owner',])
1431            #wftool.doActionFor(app,'close')
1432            dp_fields = (
1433                         'pume_eng_score',
1434                         'pume_gen_score',
1435                         'pume_tot_score',
1436                         )
1437            for f in dp_fields:
1438                dp[f] = float(jamb.get(csv_d[f]))
1439            pume = student.pume
1440            pume.getContent().edit(mapping=dp)
1441            #wftool.doActionFor(pume,'close')
1442            pume.manage_setLocalRoles(sid, ['Owner',])
1443            #
1444            # Study Course
1445            #
1446            student.invokeFactory('StudentStudyCourse','study_course')
1447            study_course = student.study_course
1448            dsc = {}
1449            from_certificate = ['title',
1450                               'max_elect',
1451                               'max_pass',
1452                               'n_core',
1453                               'nr_years',
1454                               'probation_credits',
1455                               'promotion_credits',
1456                               'start_level',
1457                              ]
1458            for f in from_certificate:
1459                dsc[f] = getattr(cert_doc,f)
1460            cpl = cert_path.split('/')
1461            dsc['faculty'] = cpl[-4]
1462            dsc['department'] = cpl[-3]
1463            dsc['study_course'] = cert_id
1464            dsc['entry_session'] = jamb.get(csv_d['session'])
1465            study_course.getContent().edit(mapping=dsc)
1466            student.getContent().createSubObjects()
1467            if dp['pume_tot_score']>49:
1468                wftool.doActionFor(student,'pume_pass')
1469                wftool.doActionFor(student,'admit')
1470            else:
1471                wftool.doActionFor(student,'pume_fail')
1472                wftool.doActionFor(student,'reject_admission')
1473            if len(no_import) > 1:
1474                open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
1475                     '\n'.join(no_import))
1476        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1477    ###)
1478
1479    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
1480    def loadFullTimeStudentsResultsFromCSV(self):
1481        """load Fulltime Studentdata from CSV values"""
1482        #return
1483        level_wf_actions = {}
1484        level_wf_actions["SUCCESSFUL STUDENT"] = "pass_A"
1485        level_wf_actions["STUDENT WITH CARRYOVER COURSES"] = "pass_B"
1486        level_wf_actions["STUDENT FOR PROBATION"] = "probate_C"
1487        level_wf_actions["STUDENT ON PROBATION/TRANSFER"] = "reject_D"
1488        import transaction
1489        wftool = self.portal_workflow
1490        tr_count = 0
1491        name = 'short_full_time_results_2004_2005'
1492        no_import = False
1493        if not no_import:
1494            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
1495            no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
1496        logger = logging.getLogger('import.%s' % name)
1497        logger.info('Start loading from %s.csv' % name)
1498        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1499        try:
1500            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1501        except:
1502            logger.error('Error reading %s.csv' % name)
1503            return
1504        l = self.portal_catalog({'meta_type': "Course"})
1505        courses = {}
1506        for c in l:
1507            courses[c.id] = c.getObject()
1508        level_changed = False
1509        student_changed = False
1510        sid = ''
1511        #import pdb;pdb.set_trace()
1512        for result in results:
1513            temp_sid = result.get('Matnumber')
1514            if temp_sid != sid:
1515                student_changed = True
1516                res = self.portal_catalog({'meta_type': "StudentClearance",
1517                                         'SearchableText': temp_sid })
1518                if not res:
1519                    em = 'Student with ID %(Matnumber)s not found\n' % result
1520                    logger.info(em)
1521                    no_import.write(em)
1522                    no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
1523                    continue
1524                elif len(res) > 1:
1525                    em = 'More than one Student with ID %(Matnumber)s found\n' % result
1526                    logger.info(em)
1527                    no_import.write(em)
1528                    no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
1529                    continue
1530                sid = temp_sid
1531                sf = res[0].getObject().aq_parent
1532                sc = getattr(sf,'study_course')
1533                level = ''
1534            else:
1535                student_changed = False
1536            course = result.get('CosCode')
1537            if course not in courses.keys():
1538                em = 'Course with ID %(CosCode)s not found\n' % result
1539                logger.info(em)
1540                no_import.write(em)
1541                no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
1542                continue
1543            course_doc = courses[course].getContent()
1544            temp_level = result.get('Level')
1545            student_id = sf.getId()
1546            result['StudentId'] = student_id
1547            if temp_level != level:
1548                try:
1549                    int(temp_level)
1550                except:
1551                    em = 'Result with ID %(Matnumber)s Course %(CosCode)s Level is empty\n' % result
1552                    logger.info(em)
1553                    no_import.write(em)
1554                    no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
1555                    continue
1556                level_changed = True
1557                if 'dlev' in vars().keys():
1558                    wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
1559                level = temp_level
1560                l = getattr(sc,level,None)
1561                if l is None:
1562                    logger.info('Creating Level %(Level)s for %(StudentId)s %(Matnumber)s' % result)
1563                    sc.invokeFactory('StudentStudyLevel', level)
1564                    l = getattr(sc, level)
1565                    l.manage_setLocalRoles(student_id, ['Owner',])
1566            else:
1567                level_changed = False
1568            cr = getattr(l,course,None)
1569            if cr is None:
1570                logger.info('Creating Course %(CosCode)s for %(StudentId)s %(Matnumber)s in Level %(Level)s' % result)
1571                l.invokeFactory('StudentCourseResult',course)
1572            cr = getattr(l,course)
1573            dcr = {}
1574            from_course = ['title',
1575                           'credits',
1576                           'passmark',
1577                           ]
1578            for f in from_course:
1579                dcr[f] = getattr(course_doc,f)
1580            dlev = {}
1581            dcr['ansbook'] = result.get('Ansbook')
1582            dcr['semester'] = getInt(result.get('Semster'))
1583            dcr['status'] = result.get('CosStuatus')
1584            dcr['score'] = getInt(result.get('Score'))
1585            dlev['session'] = result.get('Session')
1586            dcr['carry_level'] = result.get('CarryLevel')
1587            dcr['grade'] = result.get('Grade')
1588            dcr['weight'] = result.get('Weight')
1589            dlev['verdict'] = result.get('Verdict')
1590            dcr['import_id'] = result.get('id')
1591            gpa = result.get('GPA').replace(',','.')
1592            dlev['imported_gpa'] = getFloat(gpa)
1593            cr.getContent().edit(mapping = dcr)
1594            cr.manage_setLocalRoles(student_id, ['Owner',])
1595            l.getContent().edit(mapping = dlev)
1596            if tr_count > MAX_TRANS:
1597                transaction.commit()
1598                tr_count = 0
1599            tr_count += 1
1600            wftool.doActionFor(cr,'close')
1601        wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
1602        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1603
1604###)
1605
1606    security.declareProtected(ModifyPortalContent,"loadJAMBFromCSV")###(
1607    def loadJAMBFromCSV(self):
1608        """load JAMB data from CSV values"""
1609        #return
1610        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1611        import transaction
1612        tr_count = 0
1613        name = 'SampleJAMBDataII'
1614        wftool = self.portal_workflow
1615        no_import = False
1616        if not no_import:
1617            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
1618            no_import.write('REG-NO,NAME,SEX,STATE,LGA,ENG-SCORE,SUBJ1,SUBJ1-SCORE,SUBJ2,SUBJ2-SCORE,SUBJ3,SUBJ3-SCORE,AGGREGATE,UNIV1,FACULTY1,COURSE1,UNIV2,FACULTY2,COURSE2')
1619        logger = logging.getLogger('Import.%s' % name)
1620        logger.info('Start loading from %s.csv' % name)
1621        try:
1622            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1623        except:
1624            logger.error('Error reading %s.csv' % name)
1625            return
1626        for jamb in result:
1627            logger.info('processing %(REG-NO)s,%(NAME)s,%(SEX)s,%(STATE)s,%(LGA)s,%(ENG-SCORE)s,%(SUBJ1)s,%(SUBJ1-SCORE)s,%(SUBJ2)s,%(SUBJ2-SCORE)s,%(SUBJ3)s,%(SUBJ3-SCORE)s,%(AGGREGATE)s,%(UNIV1)s,%(FACULTY1)s,%(COURSE1)s,%(UNIV2)s,%(FACULTY2)s,%(COURSE2)s\n' % jamb)
1628            jamb_reg_no = jamb.get('REG-NO')
1629            res = self.portal_catalog({'meta_type': "StudentApplication",
1630                                     'jamb_reg_no': jamb_reg_no })
1631            if res:
1632                em = 'Student with REG-NO %(REG-NO)s already exists\n' % jamb
1633                logger.info(em)
1634                no_import.write(em)
1635                no_import.write('%(REG-NO)s,%(NAME)s,%(SEX)s,%(STATE)s,%(LGA)s,%(ENG-SCORE)s,%(SUBJ1)s,%(SUBJ1-SCORE)s,%(SUBJ2)s,%(SUBJ2-SCORE)s,%(SUBJ3)s,%(SUBJ3-SCORE)s,%(AGGREGATE)s,%(UNIV1)s,%(FACULTY1)s,%(COURSE1)s,%(UNIV2)s,%(FACULTY2)s,%(COURSE2)s\n' % jamb)
1636                continue
1637            jamb_name = jamb.get("NAME")
1638            jamb_name.replace('>','')
1639            names = jamb_name.split()
1640            letter = names[-1][0].upper()
1641            sid = self.generateStudentId(letter)
1642            not_created = True
1643            while not_created:
1644                try:
1645                    students_folder.invokeFactory('Student', sid)
1646                    not_created = False
1647                except BadRequest:
1648                    sid = self.generateStudentId(letter)
1649            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
1650            student = getattr(self,sid)
1651            student.manage_setLocalRoles(sid, ['Owner',])
1652            student.invokeFactory('StudentApplication','application')
1653            da = {'Title': 'Application Data'}
1654            da["jamb_reg_no"] = jamb.get("REG-NO")
1655            da["jamb_lastname"] = jamb_name
1656            da["jamb_sex"] = jamb.get("SEX")
1657            da["jamb_state"] = jamb.get("STATE")
1658            da["jamb_lga"] = jamb.get("LGA")
1659            da["jamb_score"] = jamb.get("AGGREGATE")
1660            da["jamb_first_cos"] = jamb.get("COURSE1")
1661            da["jamb_second_cos"] = jamb.get("COURSE2")
1662            da["jamb_first_uni"] = jamb.get("UNIV1")
1663            da["jamb_second_uni"] = jamb.get("UNIV2")
1664            app = student.application
1665            app_doc = app.getContent()
1666            app_doc.edit(mapping=da)
1667            #wftool.doActionFor(app,'open',dest_container=app)
1668            app.manage_setLocalRoles(sid, ['Owner',])
1669            student.getContent().createSubObjects()
1670        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1671    ###)
1672
1673
1674    security.declareProtected(View,"fixOwnership")
1675    def fixOwnership(self):
1676        """fix Ownership"""
1677        for s in self.portal_catalog(meta_type = 'Student'):
1678            student = s.getObject()
1679            sid = s.getId
1680            import pdb;pdb.set_trace()
1681            student.application.manage_setLocalRoles(sid, ['Owner',])
1682            student.personal.manage_setLocalRoles(sid, ['Owner',])
1683
1684    security.declareProtected(View,"Title")
1685    def Title(self):
1686        """compose title"""
1687        return "Student Section"
1688
1689    def generateStudentId(self,letter): ###(
1690        import random
1691        r = random
1692        if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
1693            letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY')
1694        students = self.portal_catalog(meta_type = "StudentsFolder")[-1]
1695        sid = "%c%d" % (letter,r.randint(99999,1000000))
1696        while hasattr(students, sid):
1697            sid = "%c%d" % (letter,r.randint(99999,1000000))
1698        return sid
1699        #return "%c%d" % (r.choice('ABCDEFGHKLMNPQRSTUVWXY'),r.randint(99999,1000000))
1700    ###)
1701
1702InitializeClass(StudentsFolder)
1703
1704def addStudentsFolder(container, id, REQUEST=None, **kw):
1705    """Add a Student."""
1706    ob = StudentsFolder(id, **kw)
1707    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1708###)
1709
1710class Student(CPSDocument): ###(
1711    """
1712    WAeUP Student container for the various student data
1713    """
1714    meta_type = 'Student'
1715    portal_type = meta_type
1716    security = ClassSecurityInfo()
1717
1718    security.declareProtected(View,"Title")
1719    def Title(self):
1720        """compose title"""
1721        reg_nr = self.getId()[1:]
1722        data = getattr(self,'personal',None)
1723        if data:
1724            content = data.getContent()
1725            return "%s %s %s" % (content.firstname,content.middlename,content.lastname)
1726        data = getattr(self,'application',None)
1727        if data:
1728            content = data.getContent()
1729            return "%s" % (content.jamb_lastname)
1730        return self.title
1731
1732    security.declarePrivate('makeStudentMember') ###(
1733    def makeStudentMember(self,sid,password='uNsEt'):
1734        """make the student a member"""
1735        membership = self.portal_membership
1736        membership.addMember(sid,
1737                             password ,
1738                             roles=('Member',
1739                                     'Student',
1740                                     ),
1741                             domains='',
1742                             properties = {'memberareaCreationFlag': False,
1743                                           'homeless': True},)
1744        member = membership.getMemberById(sid)
1745        self.portal_registration.afterAdd(member, sid, password, None)
1746        self.manage_setLocalRoles(sid, ['Owner',])
1747
1748###)
1749
1750    security.declareProtected(View,'createSubObjects') ###(
1751    def createSubObjects(self):
1752        """make the student a member"""
1753        dp = {'Title': 'Personal Data'}
1754        app_doc = self.application.getContent()
1755        names = app_doc.jamb_lastname.split()
1756        if len(names) == 3:
1757            dp['firstname'] = names[0].capitalize()
1758            dp['middlename'] = names[1].capitalize()
1759            dp['lastname'] = names[2].capitalize()
1760        elif len(names) == 2:
1761            dp['firstname'] = names[0].capitalize()
1762            dp['lastname'] = names[1].capitalize()
1763        else:
1764            dp['lastname'] = app_doc.jamb_lastname
1765        dp['sex'] = app_doc.jamb_sex == 'F'
1766        dp['lga'] = "%s/%s" % (app_doc.jamb_state,app_doc.jamb_lga )
1767        proxy = self.aq_parent
1768        proxy.invokeFactory('StudentPersonal','personal')
1769        per = proxy.personal
1770        per_doc = per.getContent()
1771        per_doc.edit(mapping = dp)
1772        per.manage_setLocalRoles(proxy.getId(), ['Owner',])
1773        #self.portal_workflow.doActionFor(per,'open',dest_container=per)
1774
1775###)
1776
1777InitializeClass(Student)
1778
1779def addStudent(container, id, REQUEST=None, **kw):
1780    """Add a Student."""
1781    ob = Student(id, **kw)
1782    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1783
1784###)
1785
1786class StudentAccommodation(CPSDocument): ###(
1787    """
1788    WAeUP Student container for the various student data
1789    """
1790    meta_type = 'StudentAccommodation'
1791    portal_type = meta_type
1792    security = ClassSecurityInfo()
1793
1794    security.declareProtected(View,"Title")
1795    def Title(self):
1796        """compose title"""
1797        content = self.getContent()
1798        #return "Accommodation Data for %s %s" % (content.firstname,content.lastname)
1799        return "Accommodation Data for Session %s" % content.session
1800
1801
1802InitializeClass(StudentAccommodation)
1803
1804def addStudentAccommodation(container, id, REQUEST=None, **kw):
1805    """Add a Students personal data."""
1806    ob = StudentAccommodation(id, **kw)
1807    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1808
1809###)
1810
1811class StudentPersonal(CPSDocument): ###(
1812    """
1813    WAeUP Student container for the various student data
1814    """
1815    meta_type = 'StudentPersonal'
1816    portal_type = meta_type
1817    security = ClassSecurityInfo()
1818
1819    security.declareProtected(View,"Title")
1820    def Title(self):
1821        """compose title"""
1822        content = self.getContent()
1823        #return "Personal Data for %s %s" % (content.firstname,content.lastname)
1824        return "Personal Data"
1825
1826
1827InitializeClass(StudentPersonal)
1828
1829def addStudentPersonal(container, id, REQUEST=None, **kw):
1830    """Add a Students personal data."""
1831    ob = StudentPersonal(id, **kw)
1832    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1833
1834###)
1835
1836class StudentClearance(CPSDocument): ###(
1837    """
1838    WAeUP Student container for the various student data
1839    """
1840    meta_type = 'StudentClearance'
1841    portal_type = meta_type
1842    security = ClassSecurityInfo()
1843
1844    security.declareProtected(View,"Title")
1845    def Title(self):
1846        """compose title"""
1847        content = self.getContent()
1848        #return "Clearance/Eligibility Record for %s %s" % (content.firstname,content.lastname)
1849        return "Clearance/Eligibility Record"
1850
1851
1852InitializeClass(StudentClearance)
1853
1854def addStudentClearance(container, id, REQUEST=None, **kw):
1855    """Add a Students personal data."""
1856    ob = StudentClearance(id, **kw)
1857    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1858
1859###)
1860
1861class StudentStudyLevel(CPSDocument): ###(
1862    """
1863    WAeUP Student container for the various student data
1864    """
1865    meta_type = 'StudentStudyLevel'
1866    portal_type = meta_type
1867    security = ClassSecurityInfo()
1868
1869    security.declareProtected(View,"Title")
1870    def Title(self):
1871        """compose title"""
1872        return "Level %s" % self.aq_parent.getId()
1873
1874##    security.declarePublic("gpa")
1875##    def gpa(self):
1876##        """calculate the gpa"""
1877##        sum = 0
1878##        course_count = 0
1879##        for sc in self.objectValues():
1880##            result = sc.getContent()
1881##            if not result.grade:
1882##                continue
1883##            res = self.portal_catalog({'meta_type': 'Course',
1884##                                          'id': sc.aq_parent.id})
1885##            if len(res) != 1:
1886##                continue
1887##            course = res[0].getObject().getContent()
1888##            sum += course.credits * ['F','E','D','C','B','A'].index(result.grade)
1889##            course_count += 1
1890##        if course_count:
1891##            return sum/course_count
1892##        return 0.0
1893
1894InitializeClass(StudentStudyLevel)
1895
1896def addStudentStudyLevel(container, id, REQUEST=None, **kw):
1897    """Add a Students personal data."""
1898    ob = StudentStudyLevel(id, **kw)
1899    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1900
1901###)
1902
1903class StudentStudyCourse(CPSDocument): ###(
1904    """
1905    WAeUP Student container for the various student data
1906    """
1907    meta_type = 'StudentStudyCourse'
1908    portal_type = meta_type
1909    security = ClassSecurityInfo()
1910
1911    security.declareProtected(View,"Title")
1912    def Title(self):
1913        """compose title"""
1914        content = self.getContent()
1915        return "Study Course"
1916
1917
1918InitializeClass(StudentStudyCourse)
1919
1920def addStudentStudyCourse(container, id, REQUEST=None, **kw):
1921    """Add a Students personal data."""
1922    ob = StudentStudyCourse(id, **kw)
1923    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1924
1925###)
1926
1927class StudentApplication(CPSDocument): ###(
1928    """
1929    WAeUP Student container for the various student data
1930    """
1931    meta_type = 'StudentApplication'
1932    portal_type = meta_type
1933    security = ClassSecurityInfo()
1934
1935    security.declareProtected(View,"Title")
1936    def Title(self):
1937        """compose title"""
1938        return "Application Data"
1939
1940
1941InitializeClass(StudentApplication)
1942
1943def addStudentApplication(container, id, REQUEST=None, **kw):
1944    """Add a Students eligibility data."""
1945    ob = StudentApplication(id, **kw)
1946    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1947###)
1948
1949class StudentPume(CPSDocument): ###(
1950    """
1951    WAeUP Student container for the various student data
1952    """
1953    meta_type = 'StudentPume'
1954    portal_type = meta_type
1955    security = ClassSecurityInfo()
1956
1957    security.declareProtected(View,"Title")
1958    def Title(self):
1959        """compose title"""
1960        return "PUME Results"
1961
1962
1963InitializeClass(StudentPume)
1964
1965def addStudentPume(container, id, REQUEST=None, **kw):
1966    """Add a Students PUME data."""
1967    ob = StudentPume(id, **kw)
1968    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1969###)
1970
1971##class StudentSemester(CPSDocument): ###(
1972##    """
1973##    WAeUP StudentSemester containing the courses and students
1974##    """
1975##    meta_type = 'StudentSemester'
1976##    portal_type = meta_type
1977##    security = ClassSecurityInfo()
1978##
1979##InitializeClass(StudentSemester)
1980##
1981##def addStudentSemester(container, id, REQUEST=None, **kw):
1982##    """Add a StudentSemester."""
1983##    ob = StudentSemester(id, **kw)
1984##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1985##
1986#####)
1987
1988##class Semester(CPSDocument): ###(
1989##    """
1990##    WAeUP Semester containing the courses and students
1991##    """
1992##    meta_type = 'Semester'
1993##    portal_type = meta_type
1994##    security = ClassSecurityInfo()
1995##
1996##InitializeClass(Semester)
1997##
1998##def addSemester(container, id, REQUEST=None, **kw):
1999##    """Add a Semester."""
2000##    ob = Semester(id, **kw)
2001##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
2002##
2003#####)
2004
2005class StudentCourseResult(CPSDocument): ###(
2006    """
2007    WAeUP StudentCourseResult
2008    """
2009    meta_type = 'StudentCourseResult'
2010    portal_type = meta_type
2011    security = ClassSecurityInfo()
2012
2013    def getCourseEntry(self,cid):
2014        res = self.portal_catalog({'meta_type': "Course",
2015                                           'id': cid})
2016        if res:
2017            return res[-1]
2018        else:
2019            return None
2020
2021    security.declareProtected(View,"Title")
2022    def Title(self):
2023        """compose title"""
2024        cid = self.aq_parent.getId()
2025        ce = self.getCourseEntry(cid)
2026        if ce:
2027            return "%s" % ce.Title
2028        return "No course with id %s" % cid
2029
2030InitializeClass(StudentCourseResult)
2031
2032def addStudentCourseResult(container, id, REQUEST=None, **kw):
2033    """Add a StudentCourseResult."""
2034    ob = StudentCourseResult(id, **kw)
2035    return CPSBase_adder(container, ob, REQUEST=REQUEST)
2036###)
2037
2038# Backward Compatibility StudyLevel
2039
2040from Products.WAeUP_SRP.Academics import StudyLevel
2041
2042from Products.WAeUP_SRP.Academics import addStudyLevel
2043
Note: See TracBrowser for help on using the repository browser.