source: WAeUP_SRP/trunk/Students.py @ 1124

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