source: WAeUP_SRP/trunk/Students.py @ 1118

Last change on this file since 1118 was 1111, checked in by joachim, 18 years ago

pude import

  • Property svn:keywords set to Id
File size: 59.0 KB
Line 
1#-*- mode: python; mode: fold -*-
2# $Id: Students.py 1111 2006-12-21 11:21:54Z 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 Code",
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 = '"dupplicate",%s' % format
314        einvalidjamb = '"invalid JambNr",%s' % 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,"createNewStudents")###(
372    def createNewStudents(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': "PUME SCORE",
383                 'jamb_score': "JambScore",
384                 'jamb_sex': "Sex",
385                 'jamb_state': "State",
386##                 'jamb_first_cos': "AdminCourse",
387                 'faculty': "AdminFaculty",
388                 'course_code': "AdmitCoscode",
389                 'stud_status':"AdmitStatus",
390                 'department': "AdmitDept",
391                 'jamb_lga': "LGA",
392                 'app_email': "email",
393                 'app_mobile': "PhoneNumbers",
394                 }
395        csv_fields = [f[1] for f in csv_d.items()]
396        tr_count = 0
397        total = 0
398        #name = 'pume_results'
399        name = 'Admitted'
400        no_import = []
401        s = ','.join(['"(%s)"' % fn for fn in csv_fields])
402        no_import.append('"Error",%s' % s)
403        format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
404        no_certificate = "no certificate %s" % format
405        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
406                    '\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            cert_doc = certs[cert_id]
439            catalog_entry = {}
440            catalog_entry['jamb_reg_no'] = jamb_reg_no
441            jamb_name = jamb.get(csv_d['jamb_lastname'])
442            jamb_name.replace('>','')
443            jamb_name.replace('<','')
444            names = jamb_name.split()
445            letter = names[-1][0].upper()
446            sid = self.generateStudentId(letter)
447            not_created = True
448            while not_created:
449                try:
450                    students_folder.invokeFactory('Student', sid)
451                    not_created = False
452                except BadRequest:
453                    sid = self.generateStudentId(letter)
454            catalog_entry['id'] = sid
455            tr_count += 1
456            logger.info('%(total)s+%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
457            student = getattr(self,sid)
458            student.manage_setLocalRoles(sid, ['Owner',])
459            student.invokeFactory('StudentPume','pume')
460            dp = {'Title': 'Pume Data'}
461            student.invokeFactory('StudentApplication','application')
462            da = {'Title': 'Application Data'}
463            da["jamb_lastname"] = jamb_name
464            da_fields = ('jamb_reg_no',
465                         'jamb_sex',
466                         #'jamb_state',
467                         'jamb_score',
468##                         'jamb_first_cos',
469                         'jamb_sex',
470                         'jamb_state',
471                         'jamb_lga',
472                         'app_email',
473                         'app_mobile',
474                         )
475            for f in da_fields:
476                da[f] = jamb.get(csv_d[f])
477            catalog_entry['email'] = jamb.get(csv_d['app_email'])
478            app = student.application
479            app_doc = app.getContent()
480            picture ="%s/import/pictures/%s.jpg" % (i_home,jamb_reg_no)
481            #import pdb;pdb.set_trace()
482            if os.path.exists(picture):
483                file = open(picture)
484                if False:
485                    img = PIL.Image.open(file)
486                    img.thumbnail((150,200),
487                                  resample=PIL.Image.ANTIALIAS)
488                    # We now need a buffer to write to. It can't be the same
489                    # as the inbuffer as the PNG writer will write over itself.
490                    outfile = StringIO()
491                    img.save(outfile, format=img.format)
492                else:
493                    outfile = file.read()
494                app_doc.manage_addFile('passport',
495                                       file=outfile,
496                                       title="%s.jpg" % jamb_reg_no)
497            app.getContent().edit(mapping=da)
498            app.manage_setLocalRoles(sid, ['Owner',])
499            #wftool.doActionFor(app,'close')
500            dp_fields = (
501                         #'pume_eng_score',
502                         #'pume_gen_score',
503                         'pume_tot_score',
504                         )
505            dp['pume_tot_score'] = jamb.get(csv_d['pume_tot_score']) or "No Option Shaded"
506            pume = student.pume
507            pume.getContent().edit(mapping=dp)
508            #wftool.doActionFor(pume,'close')
509            pume.manage_setLocalRoles(sid, ['Owner',])
510            #student.getContent().createSubObjects()
511            dp = {}
512            if len(names) == 3:
513                dp['firstname'] = names[0].capitalize()
514                dp['middlename'] = names[1].capitalize()
515                dp['lastname'] = names[2].capitalize()
516            elif len(names) == 2:
517                dp['firstname'] = names[0].capitalize()
518                dp['middlename'] = ''
519                dp['lastname'] = names[1].capitalize()
520            else:
521                dp['firstname'] = ''
522                dp['middlename'] = ''
523                dp['lastname'] = jamb_name
524            dp['sex'] = jamb.get(csv_d['jamb_sex']) == 'F'
525            catalog_entry['sex'] = dp['sex']
526            catalog_entry['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
527            student.invokeFactory('StudentPersonal','personal')
528            per = student.personal
529            per_doc = per.getContent()
530            per_doc.edit(mapping = dp)
531            per.manage_setLocalRoles(sid, ['Owner',])
532            if jamb.get(csv_d['stud_status']) == "Admitted":
533                wftool.doActionFor(student,'pume_pass')
534                wftool.doActionFor(student,'admit')
535            else:
536                wftool.doActionFor(student,'pume_fail')
537                wftool.doActionFor(student,'reject_admission')
538                continue
539            #
540            # Clearance
541            #
542            student.invokeFactory('StudentClearance','clearance')
543            #wftool.doActionFor(student.clearance,'open')
544            dp = {'Title': 'Clearance/Eligibility Record'}
545            student.clearance.manage_setLocalRoles(sid, ['Owner',])
546            #
547            # Study Course
548            #
549            student.invokeFactory('StudentStudyCourse','study_course')
550            study_course = student.study_course
551            dsc = {}
552            from_certificate = ['title',
553                               'max_elect',
554                               'max_pass',
555                               'n_core',
556                               'nr_years',
557                               'probation_credits',
558                               'promotion_credits',
559                               'start_level',
560                              ]
561            for f in from_certificate:
562                dsc[f] = getattr(cert_doc,f)
563            dsc['faculty'] = jamb.get(csv_d['faculty'])
564            dsc['department'] = jamb.get(csv_d['department'])
565            catalog_entry['faculty'] = jamb.get(csv_d['faculty'])
566            catalog_entry['department'] = jamb.get(csv_d['department'])
567            catalog_entry['course'] = cert_id
568            catalog_entry['level'] = getattr(cert_doc,'start_level')
569            dsc['study_course'] = cert_id
570            dsc['entry_session'] = jamb.get(csv_d['session'])
571            study_course.getContent().edit(mapping=dsc)
572            self.students_catalog.addRecord(**catalog_entry)
573            if tr_count > MAX_TRANS:
574                if len(no_import) > 1:
575                    open("%s/import/%s_not_imported.csv" % (i_home,name),"w+").write(
576                             '\n'.join(no_import))
577                    no_import = []
578                em = '%d transactions commited\n' % tr_count
579                transaction.commit()
580                logger.info(em)
581                total += tr_count
582                tr_count = 0
583        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
584    ###)
585
586    security.declareProtected(ModifyPortalContent,"updateStudyCourse")###(
587    def updateStudyCourse(self):
588        """update StudyCourse from CSV values"""
589        import transaction
590        import random
591        from pdb import set_trace
592        wftool = self.portal_workflow
593        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
594        csv_d = {'jamb_reg_no': "RegNumber",
595                 'jamb_lastname': "Name",
596                 'session': "Session",
597                 'pume_tot_score': "PUME SCORE",
598                 'jamb_score': "JambScore",
599                 'jamb_sex': "Sex",
600                 'jamb_state': "State",
601##                 'jamb_first_cos': "AdminCourse",
602                 'faculty': "AdminFaculty",
603                 'course_code': "AdmitCoscode",
604                 'stud_status':"AdmitStatus",
605                 'department': "AdmitDept",
606                 'jamb_lga': "LGA",
607                 'app_email': "email",
608                 'app_mobile': "PhoneNumbers",
609                 }
610        csv_fields = [f[1] for f in csv_d.items()]
611        tr_count = 0
612        total = 0
613        #name = 'pume_results'
614        name = 'StudyCourseChange'
615        no_import = []
616        s = ','.join(['"(%s)"' % fn for fn in csv_fields])
617        no_import.append('"Error",%s' % s)
618        format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
619        no_certificate = "no certificate %s" % format
620        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
621                    '\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        try:
626            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
627        except:
628            logger.error('Error reading %s.csv' % name)
629            return
630        for jamb in result:
631            jamb['Error'] = "Processing "
632            logger.info(format % jamb)
633            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
634            res = self.portal_catalog({'portal_type': "StudentApplication",
635                                     'SearchableText': jamb_reg_no })
636            if not res:
637                em = 'Student with RegNo %s does not exists\n' % jamb_reg_no
638                logger.info(em)
639                jamb['Error'] = "Student not exists"
640                no_import.append(format % jamb)
641                continue
642            sid = res[0].getPath().split('/')[-2]
643            cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
644            res = self.portal_catalog(portal_type = "Certificate", id = cert_id)
645            if not res:
646                em = 'No Certificate with ID %s \n' % cert_id
647                logger.info(em)
648                jamb['Error'] = "No Certificate %s" % cert_id
649                no_import.append( format % jamb)
650                continue
651            cert_brain = res[0]
652            catalog_entry = {}
653            student = getattr(self,sid)
654            #
655            # Study Course
656            #
657            study_course = student.study_course
658            dsc = {}
659            cert_pl = cert_brain.getPath().split('/')
660            catalog_entry['id'] = sid
661            catalog_entry['faculty'] = cert_pl[-4]
662            catalog_entry['department'] = cert_pl[-3]
663            catalog_entry['course'] = cert_id
664            dsc['study_course'] = cert_id
665            study_course.getContent().edit(mapping=dsc)
666            self.students_catalog.modifyRecord(**catalog_entry)
667            if tr_count > 10:
668                if len(no_import) > 1:
669                    open("%s/import/%s_not_imported.csv" % (i_home,name),"w+").write(
670                             '\n'.join(no_import))
671                    no_import = []
672                em = '%d transactions commited\n' % tr_count
673                transaction.commit()
674                logger.info(em)
675                total += tr_count
676                tr_count = 0
677            tr_count += 1
678        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
679    ###)
680
681    security.declareProtected(ModifyPortalContent,"OLDloadPumeResultsFromCSV")###(
682    def OLDloadPumeResultsFromCSV(self):
683        """load Fulltime Studentdata from CSV values"""
684        import transaction
685        import random
686        wftool = self.portal_workflow
687        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
688        csv_d = {'jamb_reg_no': "JAMBRegno",
689                 'jamb_lastname': "Name",
690                 'pume_options': "Options",
691                 'session': "Session",
692                 'days': "Days",
693                 'response': "Responce",
694                 'wrong': "Wrong",
695                 'pume_eng_score': "EngScore",
696                 'pume_gen_score': "GenScore",
697                 'pume_tot_score': "Score",
698                 'batch': "Batch",
699                 'serial': "SerialNo",
700                 'jamb_score': "JambScore",
701                 'omitted':"Omitted",
702                 'search_key': "SearchKey",
703                 'jamb_sex': "Sex",
704                 'fac1': "Fac1",
705                 'fac2': "Fac2",
706                 'jamb_first_cos': "CourseofStudy",
707                 'stud_status':"StudStatus",
708                 'registered': "Registered",
709                 'jamb_state': "State",
710                 'eng_fail': "EngFail",
711                 'gen_fail': "GenFail",
712                 'un_ans_eng': "UnAnsEng",
713                 'un_ans_eng': "UnAnsGen",
714                 'total_ans': "TotalUnAns",
715                 'dept': "Dept",
716                 'jamb_second_cos': "Course2",
717                 'jamb_third_cos': "course3",
718                 }
719        csv_fields = [f[1] for f in csv_d.items()]
720        tr_count = 0
721        name = 'pume_results'
722        no_import = []
723        s = ','.join(['"(%s)"' % fn for fn in csv_fields])
724        no_import.append('%s\n' % s)
725        logger = logging.getLogger('Import.%s' % name)
726        logger.info('Start loading from %s.csv' % name)
727        try:
728            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
729        except:
730            logger.error('Error reading %s.csv' % name)
731            return
732        for jamb in result:
733            format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
734            processing = "processing %s" % format
735            logger.info(processing % jamb)
736            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
737            #import pdb;pdb.set_trace()
738            res = self.portal_catalog({'portal_type': "StudentApplication",
739                                     'jamb_reg_no': jamb_reg_no })
740            if res:
741                em = 'Student with REG-NO %s already exists\n' % jamb_reg_no
742                logger.info(em)
743                no_import.append(em)
744                no_import.append(format % jamb)
745                continue
746            cert_id = jamb.get(csv_d['jamb_first_cos']).upper()
747            res = self.portal_catalog({'portal_type': "Certificate",
748                                     'id': cert_id })
749            if len(res) < 1:
750                em = 'No Certificate with ID %s \n' % cert_id
751                logger.info(em)
752                no_import.append(em)
753                no_import.append(format % jamb)
754                continue
755            cert = res[0].getObject()
756            cert_path = res[0].getPath()
757            cert_doc = cert.getContent()
758            jamb_name = jamb.get(csv_d['jamb_lastname'])
759            jamb_name.replace('>','')
760            names = jamb_name.split()
761            letter = names[-1][0].upper()
762            sid = self.generateStudentId(letter)
763            not_created = True
764            while not_created:
765                try:
766                    students_folder.invokeFactory('Student', sid)
767                    not_created = False
768                except BadRequest:
769                    sid = self.generateStudentId(letter)
770            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
771            student = getattr(self,sid)
772            student.manage_setLocalRoles(sid, ['Owner',])
773            student.invokeFactory('StudentClearance','clearance')
774            #wftool.doActionFor(student.clearance,'open')
775            dp = {'Title': 'Clearance/Eligibility Record'}
776            student.clearance.manage_setLocalRoles(sid, ['Owner',])
777            student.invokeFactory('StudentPume','pume')
778            dp = {'Title': 'Pume Data'}
779            student.invokeFactory('StudentApplication','application')
780            da = {'Title': 'Application Data'}
781            da["jamb_lastname"] = jamb_name
782            da_fields = ('jamb_reg_no',
783                         'jamb_sex',
784                         'jamb_state',
785                         'jamb_score',
786                         'jamb_first_cos',
787                         'jamb_sex',
788                         'jamb_state',
789                         'jamb_first_cos',
790                         'jamb_second_cos',
791                         )
792            for f in da_fields:
793                da[f] = jamb.get(csv_d[f])
794            app = student.application
795            app.getContent().edit(mapping=da)
796            app.manage_setLocalRoles(sid, ['Owner',])
797            #wftool.doActionFor(app,'close')
798            dp_fields = (
799                         'pume_eng_score',
800                         'pume_gen_score',
801                         'pume_tot_score',
802                         )
803            for f in dp_fields:
804                dp[f] = float(jamb.get(csv_d[f]))
805            pume = student.pume
806            pume.getContent().edit(mapping=dp)
807            #wftool.doActionFor(pume,'close')
808            pume.manage_setLocalRoles(sid, ['Owner',])
809            #
810            # Study Course
811            #
812            student.invokeFactory('StudentStudyCourse','study_course')
813            study_course = student.study_course
814            dsc = {}
815            from_certificate = ['title',
816                               'max_elect',
817                               'max_pass',
818                               'n_core',
819                               'nr_years',
820                               'probation_credits',
821                               'promotion_credits',
822                               'start_level',
823                              ]
824            for f in from_certificate:
825                dsc[f] = getattr(cert_doc,f)
826            cpl = cert_path.split('/')
827            dsc['faculty'] = cpl[-4]
828            dsc['department'] = cpl[-3]
829            dsc['study_course'] = cert_id
830            dsc['entry_session'] = jamb.get(csv_d['session'])
831            study_course.getContent().edit(mapping=dsc)
832            student.getContent().createSubObjects()
833            if dp['pume_tot_score']>49:
834                wftool.doActionFor(student,'pume_pass')
835                wftool.doActionFor(student,'admit')
836            else:
837                wftool.doActionFor(student,'pume_fail')
838                wftool.doActionFor(student,'reject_admission')
839            if len(no_import) > 1:
840                open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
841                     '\n'.join(no_import))
842        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
843    ###)
844
845    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
846    def loadFullTimeStudentsResultsFromCSV(self):
847        """load Fulltime Studentdata from CSV values"""
848        #return
849        level_wf_actions = {}
850        level_wf_actions["SUCCESSFUL STUDENT"] = "pass_A"
851        level_wf_actions["STUDENT WITH CARRYOVER COURSES"] = "pass_B"
852        level_wf_actions["STUDENT FOR PROBATION"] = "probate_C"
853        level_wf_actions["STUDENT ON PROBATION/TRANSFER"] = "reject_D"
854        import transaction
855        wftool = self.portal_workflow
856        tr_count = 0
857        name = 'short_full_time_results_2004_2005'
858        no_import = False
859        if not no_import:
860            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
861            no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
862        logger = logging.getLogger('Ímport.%s' % name)
863        logger.info('Start loading from %s.csv' % name)
864        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
865        try:
866            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
867        except:
868            logger.error('Error reading %s.csv' % name)
869            return
870        l = self.portal_catalog({'meta_type': "Course"})
871        courses = {}
872        for c in l:
873            courses[c.id] = c.getObject()
874        level_changed = False
875        student_changed = False
876        sid = ''
877        #import pdb;pdb.set_trace()
878        for result in results:
879            temp_sid = result.get('Matnumber')
880            if temp_sid != sid:
881                student_changed = True
882                res = self.portal_catalog({'meta_type': "StudentClearance",
883                                         'SearchableText': temp_sid })
884                if not res:
885                    em = 'Student with ID %(Matnumber)s not found\n' % result
886                    logger.info(em)
887                    no_import.write(em)
888                    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)
889                    continue
890                elif len(res) > 1:
891                    em = 'More than one Student with ID %(Matnumber)s found\n' % result
892                    logger.info(em)
893                    no_import.write(em)
894                    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)
895                    continue
896                sid = temp_sid
897                sf = res[0].getObject().aq_parent
898                sc = getattr(sf,'study_course')
899                level = ''
900            else:
901                student_changed = False
902            course = result.get('CosCode')
903            if course not in courses.keys():
904                em = 'Course with ID %(CosCode)s not found\n' % result
905                logger.info(em)
906                no_import.write(em)
907                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)
908                continue
909            course_doc = courses[course].getContent()
910            temp_level = result.get('Level')
911            student_id = sf.getId()
912            result['StudentId'] = student_id
913            if temp_level != level:
914                try:
915                    int(temp_level)
916                except:
917                    em = 'Result with ID %(Matnumber)s Course %(CosCode)s Level is empty\n' % result
918                    logger.info(em)
919                    no_import.write(em)
920                    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)
921                    continue
922                level_changed = True
923                if 'dlev' in vars().keys():
924                    wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
925                level = temp_level
926                l = getattr(sc,level,None)
927                if l is None:
928                    logger.info('Creating Level %(Level)s for %(StudentId)s %(Matnumber)s' % result)
929                    sc.invokeFactory('StudentStudyLevel', level)
930                    l = getattr(sc, level)
931                    l.manage_setLocalRoles(student_id, ['Owner',])
932            else:
933                level_changed = False
934            cr = getattr(l,course,None)
935            if cr is None:
936                logger.info('Creating Course %(CosCode)s for %(StudentId)s %(Matnumber)s in Level %(Level)s' % result)
937                l.invokeFactory('StudentCourseResult',course)
938            cr = getattr(l,course)
939            dcr = {}
940            from_course = ['title',
941                           'credits',
942                           'passmark',
943                           ]
944            for f in from_course:
945                dcr[f] = getattr(course_doc,f)
946            dlev = {}
947            dcr['ansbook'] = result.get('Ansbook')
948            dcr['semester'] = getInt(result.get('Semster'))
949            dcr['status'] = result.get('CosStuatus')
950            dcr['score'] = getInt(result.get('Score'))
951            dlev['session'] = result.get('Session')
952            dcr['carry_level'] = result.get('CarryLevel')
953            dcr['grade'] = result.get('Grade')
954            dcr['weight'] = result.get('Weight')
955            dlev['verdict'] = result.get('Verdict')
956            dcr['import_id'] = result.get('id')
957            gpa = result.get('GPA').replace(',','.')
958            dlev['imported_gpa'] = getFloat(gpa)
959            cr.getContent().edit(mapping = dcr)
960            cr.manage_setLocalRoles(student_id, ['Owner',])
961            l.getContent().edit(mapping = dlev)
962            if tr_count > MAX_TRANS:
963                transaction.commit()
964                tr_count = 0
965            tr_count += 1
966            wftool.doActionFor(cr,'close')
967        wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
968        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
969
970###)
971
972    security.declareProtected(ModifyPortalContent,"loadJAMBFromCSV")###(
973    def loadJAMBFromCSV(self):
974        """load JAMB data from CSV values"""
975        #return
976        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
977        import transaction
978        tr_count = 0
979        name = 'SampleJAMBDataII'
980        wftool = self.portal_workflow
981        no_import = False
982        if not no_import:
983            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
984            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')
985        logger = logging.getLogger('Import.%s' % name)
986        logger.info('Start loading from %s.csv' % name)
987        try:
988            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
989        except:
990            logger.error('Error reading %s.csv' % name)
991            return
992        for jamb in result:
993            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)
994            jamb_reg_no = jamb.get('REG-NO')
995            res = self.portal_catalog({'meta_type': "StudentApplication",
996                                     'jamb_reg_no': jamb_reg_no })
997            if res:
998                em = 'Student with REG-NO %(REG-NO)s already exists\n' % jamb
999                logger.info(em)
1000                no_import.write(em)
1001                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)
1002                continue
1003            jamb_name = jamb.get("NAME")
1004            jamb_name.replace('>','')
1005            names = jamb_name.split()
1006            letter = names[-1][0].upper()
1007            sid = self.generateStudentId(letter)
1008            not_created = True
1009            while not_created:
1010                try:
1011                    students_folder.invokeFactory('Student', sid)
1012                    not_created = False
1013                except BadRequest:
1014                    sid = self.generateStudentId(letter)
1015            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
1016            student = getattr(self,sid)
1017            student.manage_setLocalRoles(sid, ['Owner',])
1018            student.invokeFactory('StudentApplication','application')
1019            da = {'Title': 'Application Data'}
1020            da["jamb_reg_no"] = jamb.get("REG-NO")
1021            da["jamb_lastname"] = jamb_name
1022            da["jamb_sex"] = jamb.get("SEX")
1023            da["jamb_state"] = jamb.get("STATE")
1024            da["jamb_lga"] = jamb.get("LGA")
1025            da["jamb_score"] = jamb.get("AGGREGATE")
1026            da["jamb_first_cos"] = jamb.get("COURSE1")
1027            da["jamb_second_cos"] = jamb.get("COURSE2")
1028            da["jamb_first_uni"] = jamb.get("UNIV1")
1029            da["jamb_second_uni"] = jamb.get("UNIV2")
1030            app = student.application
1031            app_doc = app.getContent()
1032            app_doc.edit(mapping=da)
1033            #wftool.doActionFor(app,'open',dest_container=app)
1034            app.manage_setLocalRoles(sid, ['Owner',])
1035            student.getContent().createSubObjects()
1036        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1037    ###)
1038
1039
1040    security.declareProtected(View,"fixOwnership")
1041    def fixOwnership(self):
1042        """fix Ownership"""
1043        for s in self.portal_catalog(meta_type = 'Student'):
1044            student = s.getObject()
1045            sid = s.getId
1046            import pdb;pdb.set_trace()
1047            student.application.manage_setLocalRoles(sid, ['Owner',])
1048            student.personal.manage_setLocalRoles(sid, ['Owner',])
1049
1050    security.declareProtected(View,"Title")
1051    def Title(self):
1052        """compose title"""
1053        return "Student Section"
1054
1055    def generateStudentId(self,letter): ###(
1056        import random
1057        r = random
1058        if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
1059            letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY')
1060        students = self.portal_catalog(meta_type = "StudentsFolder")[-1]
1061        sid = "%c%d" % (letter,r.randint(99999,1000000))
1062        while hasattr(students, sid):
1063            sid = "%c%d" % (letter,r.randint(99999,1000000))
1064        return sid
1065        #return "%c%d" % (r.choice('ABCDEFGHKLMNPQRSTUVWXY'),r.randint(99999,1000000))
1066    ###)
1067
1068InitializeClass(StudentsFolder)
1069
1070def addStudentsFolder(container, id, REQUEST=None, **kw):
1071    """Add a Student."""
1072    ob = StudentsFolder(id, **kw)
1073    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1074###)
1075
1076class Student(CPSDocument): ###(
1077    """
1078    WAeUP Student container for the various student data
1079    """
1080    meta_type = 'Student'
1081    portal_type = meta_type
1082    security = ClassSecurityInfo()
1083
1084    security.declareProtected(View,"Title")
1085    def Title(self):
1086        """compose title"""
1087        reg_nr = self.getId()[1:]
1088        data = getattr(self,'personal',None)
1089        if data:
1090            content = data.getContent()
1091            return "%s %s" % (content.firstname,content.lastname)
1092        data = getattr(self,'application',None)
1093        if data:
1094            content = data.getContent()
1095            return "%s" % (content.jamb_lastname)
1096        return self.title
1097
1098    security.declarePrivate('makeStudentMember') ###(
1099    def makeStudentMember(self,sid,password='uNsEt'):
1100        """make the student a member"""
1101        membership = self.portal_membership
1102        membership.addMember(sid,
1103                             password ,
1104                             roles=('Member',
1105                                     'Student',
1106                                     ),
1107                             domains='',
1108                             properties = {'memberareaCreationFlag': False,
1109                                           'homeless': True},)
1110        member = membership.getMemberById(sid)
1111        self.portal_registration.afterAdd(member, sid, password, None)
1112        self.manage_setLocalRoles(sid, ['Owner',])
1113
1114###)
1115
1116    security.declareProtected(View,'createSubObjects') ###(
1117    def createSubObjects(self):
1118        """make the student a member"""
1119        dp = {'Title': 'Personal Data'}
1120        app_doc = self.application.getContent()
1121        names = app_doc.jamb_lastname.split()
1122        if len(names) == 3:
1123            dp['firstname'] = names[0].capitalize()
1124            dp['middlename'] = names[1].capitalize()
1125            dp['lastname'] = names[2].capitalize()
1126        elif len(names) == 2:
1127            dp['firstname'] = names[0].capitalize()
1128            dp['lastname'] = names[1].capitalize()
1129        else:
1130            dp['lastname'] = app_doc.jamb_lastname
1131        dp['sex'] = app_doc.jamb_sex == 'F'
1132        dp['lga'] = "%s/%s" % (app_doc.jamb_state,app_doc.jamb_lga )
1133        proxy = self.aq_parent
1134        proxy.invokeFactory('StudentPersonal','personal')
1135        per = proxy.personal
1136        per_doc = per.getContent()
1137        per_doc.edit(mapping = dp)
1138        per.manage_setLocalRoles(proxy.getId(), ['Owner',])
1139        #self.portal_workflow.doActionFor(per,'open',dest_container=per)
1140
1141###)
1142
1143InitializeClass(Student)
1144
1145def addStudent(container, id, REQUEST=None, **kw):
1146    """Add a Student."""
1147    ob = Student(id, **kw)
1148    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1149
1150###)
1151
1152class StudentAccommodation(CPSDocument): ###(
1153    """
1154    WAeUP Student container for the various student data
1155    """
1156    meta_type = 'StudentAccommodation'
1157    portal_type = meta_type
1158    security = ClassSecurityInfo()
1159
1160    security.declareProtected(View,"Title")
1161    def Title(self):
1162        """compose title"""
1163        content = self.getContent()
1164        #return "Accommodation Data for %s %s" % (content.firstname,content.lastname)
1165        return "Accommodation Data for Session %s" % content.session
1166
1167
1168InitializeClass(StudentAccommodation)
1169
1170def addStudentAccommodation(container, id, REQUEST=None, **kw):
1171    """Add a Students personal data."""
1172    ob = StudentAccommodation(id, **kw)
1173    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1174
1175###)
1176
1177class StudentPersonal(CPSDocument): ###(
1178    """
1179    WAeUP Student container for the various student data
1180    """
1181    meta_type = 'StudentPersonal'
1182    portal_type = meta_type
1183    security = ClassSecurityInfo()
1184
1185    security.declareProtected(View,"Title")
1186    def Title(self):
1187        """compose title"""
1188        content = self.getContent()
1189        #return "Personal Data for %s %s" % (content.firstname,content.lastname)
1190        return "Personal Data"
1191
1192
1193InitializeClass(StudentPersonal)
1194
1195def addStudentPersonal(container, id, REQUEST=None, **kw):
1196    """Add a Students personal data."""
1197    ob = StudentPersonal(id, **kw)
1198    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1199
1200###)
1201
1202class StudentClearance(CPSDocument): ###(
1203    """
1204    WAeUP Student container for the various student data
1205    """
1206    meta_type = 'StudentClearance'
1207    portal_type = meta_type
1208    security = ClassSecurityInfo()
1209
1210    security.declareProtected(View,"Title")
1211    def Title(self):
1212        """compose title"""
1213        content = self.getContent()
1214        #return "Clearance/Eligibility Record for %s %s" % (content.firstname,content.lastname)
1215        return "Clearance/Eligibility Record"
1216
1217
1218InitializeClass(StudentClearance)
1219
1220def addStudentClearance(container, id, REQUEST=None, **kw):
1221    """Add a Students personal data."""
1222    ob = StudentClearance(id, **kw)
1223    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1224
1225###)
1226
1227class StudentStudyLevel(CPSDocument): ###(
1228    """
1229    WAeUP Student container for the various student data
1230    """
1231    meta_type = 'StudentStudyLevel'
1232    portal_type = meta_type
1233    security = ClassSecurityInfo()
1234
1235    security.declareProtected(View,"Title")
1236    def Title(self):
1237        """compose title"""
1238        return "Level %s" % self.aq_parent.getId()
1239
1240##    security.declarePublic("gpa")
1241##    def gpa(self):
1242##        """calculate the gpa"""
1243##        sum = 0
1244##        course_count = 0
1245##        for sc in self.objectValues():
1246##            result = sc.getContent()
1247##            if not result.grade:
1248##                continue
1249##            res = self.portal_catalog({'meta_type': 'Course',
1250##                                          'id': sc.aq_parent.id})
1251##            if len(res) != 1:
1252##                continue
1253##            course = res[0].getObject().getContent()
1254##            sum += course.credits * ['F','E','D','C','B','A'].index(result.grade)
1255##            course_count += 1
1256##        if course_count:
1257##            return sum/course_count
1258##        return 0.0
1259
1260InitializeClass(StudentStudyLevel)
1261
1262def addStudentStudyLevel(container, id, REQUEST=None, **kw):
1263    """Add a Students personal data."""
1264    ob = StudentStudyLevel(id, **kw)
1265    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1266
1267###)
1268
1269class StudentStudyCourse(CPSDocument): ###(
1270    """
1271    WAeUP Student container for the various student data
1272    """
1273    meta_type = 'StudentStudyCourse'
1274    portal_type = meta_type
1275    security = ClassSecurityInfo()
1276
1277    security.declareProtected(View,"Title")
1278    def Title(self):
1279        """compose title"""
1280        content = self.getContent()
1281        return "Study Course"
1282
1283
1284InitializeClass(StudentStudyCourse)
1285
1286def addStudentStudyCourse(container, id, REQUEST=None, **kw):
1287    """Add a Students personal data."""
1288    ob = StudentStudyCourse(id, **kw)
1289    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1290
1291###)
1292
1293class StudentApplication(CPSDocument): ###(
1294    """
1295    WAeUP Student container for the various student data
1296    """
1297    meta_type = 'StudentApplication'
1298    portal_type = meta_type
1299    security = ClassSecurityInfo()
1300
1301    security.declareProtected(View,"Title")
1302    def Title(self):
1303        """compose title"""
1304        return "Application Data"
1305
1306
1307InitializeClass(StudentApplication)
1308
1309def addStudentApplication(container, id, REQUEST=None, **kw):
1310    """Add a Students eligibility data."""
1311    ob = StudentApplication(id, **kw)
1312    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1313###)
1314
1315
1316class StudentPume(CPSDocument): ###(
1317    """
1318    WAeUP Student container for the various student data
1319    """
1320    meta_type = 'StudentPume'
1321    portal_type = meta_type
1322    security = ClassSecurityInfo()
1323
1324    security.declareProtected(View,"Title")
1325    def Title(self):
1326        """compose title"""
1327        return "PUME Results"
1328
1329
1330InitializeClass(StudentPume)
1331
1332def addStudentPume(container, id, REQUEST=None, **kw):
1333    """Add a Students PUME data."""
1334    ob = StudentPume(id, **kw)
1335    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1336###)
1337
1338##class StudentSemester(CPSDocument): ###(
1339##    """
1340##    WAeUP StudentSemester containing the courses and students
1341##    """
1342##    meta_type = 'StudentSemester'
1343##    portal_type = meta_type
1344##    security = ClassSecurityInfo()
1345##
1346##InitializeClass(StudentSemester)
1347##
1348##def addStudentSemester(container, id, REQUEST=None, **kw):
1349##    """Add a StudentSemester."""
1350##    ob = StudentSemester(id, **kw)
1351##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1352##
1353#####)
1354
1355##class Semester(CPSDocument): ###(
1356##    """
1357##    WAeUP Semester containing the courses and students
1358##    """
1359##    meta_type = 'Semester'
1360##    portal_type = meta_type
1361##    security = ClassSecurityInfo()
1362##
1363##InitializeClass(Semester)
1364##
1365##def addSemester(container, id, REQUEST=None, **kw):
1366##    """Add a Semester."""
1367##    ob = Semester(id, **kw)
1368##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1369##
1370#####)
1371
1372class StudentCourseResult(CPSDocument): ###(
1373    """
1374    WAeUP StudentCourseResult
1375    """
1376    meta_type = 'StudentCourseResult'
1377    portal_type = meta_type
1378    security = ClassSecurityInfo()
1379
1380    def getCourseEntry(self,cid):
1381        res = self.portal_catalog({'meta_type': "Course",
1382                                           'id': cid})
1383        if res:
1384            return res[-1]
1385        else:
1386            return None
1387
1388    security.declareProtected(View,"Title")
1389    def Title(self):
1390        """compose title"""
1391        cid = self.aq_parent.getId()
1392        ce = self.getCourseEntry(cid)
1393        if ce:
1394            return "%s" % ce.Title
1395        return "No course with id %s" % cid
1396
1397InitializeClass(StudentCourseResult)
1398
1399def addStudentCourseResult(container, id, REQUEST=None, **kw):
1400    """Add a StudentCourseResult."""
1401    ob = StudentCourseResult(id, **kw)
1402    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1403###)
1404
1405# Backward Compatibility StudyLevel
1406
1407from Products.WAeUP_SRP.Academics import StudyLevel
1408
1409from Products.WAeUP_SRP.Academics import addStudyLevel
1410
Note: See TracBrowser for help on using the repository browser.