source: WAeUP_SRP/trunk/Students.py @ 1011

Last change on this file since 1011 was 1005, checked in by joachim, 18 years ago

pume import check for duplicate on import

  • Property svn:keywords set to Id
File size: 53.5 KB
Line 
1#-*- mode: python; mode: fold -*-
2# $Id: Students.py 1005 2006-12-07 09:29:29Z 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
64class StudentsFolder(CPSDocument): ###(
65    """
66    WAeUP container for the various WAeUP containers data
67    """
68    meta_type = 'StudentsFolder'
69    portal_type = meta_type
70    security = ClassSecurityInfo()
71
72    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsFromCSV")###(
73    def loadFullTimeStudentsFromCSV(self):
74        """load Fulltime Studentdata from CSV values"""
75        import transaction
76        import random
77        tr_count = 0
78        name = 'short_full_time'
79        no_import = False
80        if not no_import:
81            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
82            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')
83        logger = logging.getLogger('%s_import' % name)
84        logger.info('Start loading from %s.csv' % name)
85        pwlist  = []
86        pwlist.append('"student_id","firstname","middlename","lastname","matric_no","jamb_reg_no","access_code"')
87        pwl_template = Template('"$student_id","$firstname","$middlename","$lastname","$matric_no","$jamb_reg_no","$access_code"')
88        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
89        try:
90            students = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
91        except:
92            logger.error('Error reading %s.csv' % name)
93            return
94        l = self.portal_catalog({'meta_type': "StudentClearance",})
95        matrics = []
96        for s in l:
97            matrics.append(s.getObject().getContent().matric_no)
98        print matrics
99        l = self.portal_catalog({'meta_type': "Certificate"})
100        certs = {}
101        for c in l:
102            ca,ac,fa,dep_id,co,certcode = c.relative_path.split('/')
103            cid = "%(dep_id)s_%(certcode)s" % vars()
104            certs[cid] = c.getObject()
105        for student in students:
106            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)
107            sid = student.get('MatricNo')
108            if sid == "":
109                em = 'Empty MatricNo\n'
110                logger.info(em)
111                no_import.write(em)
112                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)
113                continue
114            certcode = makeCertificateCode(student.get('CourseMajor'))
115            dep_id = student.get('Dept')
116            fac_id = student.get('Faculty')
117            cid = "%(dep_id)s_%(certcode)s" % vars()
118            if cid not in certs.keys():
119                em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
120                logger.info(em)
121                no_import.write(em)
122                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)
123                continue
124            certificate_doc = certs[cid].getContent()
125            level = student.get('StudentLevel')
126            try:
127                int(level)
128            except:
129                em = 'Student with ID %(MatricNo)s StudentLevel is empty\n' % student
130                logger.info(em)
131                no_import.write(em)
132                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)
133                continue
134            matric_no = student.get('MatricNo')
135            if matric_no not in matrics:
136                matrics.append(matric_no)
137                sid = self.generateStudentId(student.get('Lastname')[0])
138                #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
139                students_folder.invokeFactory('Student', sid)
140                logger.info('%(tr_count)s: Creating Student with ID %(sid)s Matric_no %(matric_no)s ' % vars())
141                student_obj = getattr(self,sid)
142                access_code = "%d" % random.randint(1000000000,9999999999)
143                student_obj.getContent().makeStudentMember(sid,access_code,)
144                pwl_dict = {'student_id': sid,'access_code':access_code}
145                student_obj.invokeFactory('StudentApplication','application')
146                application = student_obj.application
147                da = {'Title': 'Application Data'}
148                student_obj.invokeFactory('StudentPersonal','personal')
149                da['jamb_reg_no'] = student.get('EntryRegNo')
150                personal = student_obj.personal
151                dp = {'Title': 'Personal Data'}
152                student_obj.invokeFactory('StudentClearance','clearance')
153                clearance = student_obj.clearance
154                dc = {'Title': 'Clearance/Eligibility Record'}
155                dc['matric_no'] = matric_no
156                state = student.get('State')
157                lga = student.get('LGA')
158                if state and lga:
159                    lga =  state + ' / ' + lga
160                else:
161                    lga = "None"
162                dc['lga'] = lga
163                dc['nationality'] = student.get('Nationality')
164                dc['email'] = student.get('Emailaddress')
165                dp['firstname'] = student.get('FirstName')
166                dp['middlename'] = student.get('MiddleName')
167                dp['lastname'] = student.get('Lastname')
168                dp['former_surname'] = student.get('FormerSurname')
169                dp['sex'] = student.get('Sex') == 'F'
170                dp['perm_address'] = student.get('PermanentAddress')
171                dp['perm_city'] = student.get('PermanentAddressCity')
172                dp['campus_address'] = student.get('CampusAddress')
173                dp['phone'] = student.get('PhoneNumber')
174                application.getContent().edit(mapping=da)
175                personal.getContent().edit(mapping=dp)
176                clearance.getContent().edit(mapping=dc)
177                #
178                # Study Course
179                #
180                student_obj.invokeFactory('StudentStudyCourse','study_course')
181                studycourse = student_obj.study_course
182                dsc = {}
183                from_certificate = ['title',
184                                   'max_elect',
185                                   'max_pass',
186                                   'n_core',
187                                   'nr_years',
188                                   'probation_credits',
189                                   'promotion_credits',
190                                   'start_level',
191                                  ]
192                for f in from_certificate:
193                    dsc[f] = getattr(certificate_doc,f)
194                dsc['faculty'] = fac_id
195                dsc['department'] = dep_id
196                dsc['study_course'] = certcode
197                css = student.get('CurrentSession') or '2004-2005'
198                cs = int(css.split('-')[0]) - 2000
199                cl = int(student.get('StudentLevel') or '100')/100
200                dsc['entry_session'] = "200%s" % (cs - cl)
201                dsc['clr_ac_pin'] = access_code
202                studycourse.getContent().edit(mapping=dsc)
203                #
204                # Level
205                #
206##                l = getattr(studycourse,level,None)
207##                if 0 and l is None:
208##                    #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
209##                    logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student)
210##                    studycourse.invokeFactory('StudentStudyLevel', level)
211##                    l = getattr(studycourse, level)
212##                    certificate = certs[certcode]
213##                    cert_level = getattr(certificate,level,None)
214##                    if cert_level is None:
215##                        logger.info('Level %(level)s not in %(certcode)s' % vars())
216##                    l.getContent().edit(mapping={'Title': "Level %s" % level})
217            else:
218                em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
219                logger.info(em)
220                no_import.write(em)
221                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)
222                continue
223            if tr_count > MAX_TRANS:
224                transaction.commit()
225                em = 'Transaction commited\n' % student
226                logger.info(em)
227                tr_count = 0
228            tr_count += 1
229            pwl_dict.update(dc)
230            pwl_dict.update(da)
231            pwl_dict.update(dp)
232            wftool = self.portal_workflow
233            pwlist.append(pwl_template.substitute(pwl_dict))
234            wftool.doActionFor(student_obj,'clear_and_validate')
235            student_obj.manage_setLocalRoles(sid, ['Owner',])
236            wftool.doActionFor(application,'close')
237            application.manage_setLocalRoles(sid, ['Owner',])
238            wftool.doActionFor(clearance,'close')
239            clearance.manage_setLocalRoles(sid, ['Owner',])
240            wftool.doActionFor(personal,'close')
241            personal.manage_setLocalRoles(sid, ['Owner',])
242            wftool.doActionFor(studycourse,'close_for_edit')
243            studycourse.manage_setLocalRoles(sid, ['Owner',])
244        open("%s/import/pwlist-%s.csv" % (i_home,name),"w+").write('\n'.join(pwlist))
245        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
246    ###)
247
248    security.declareProtected(ModifyPortalContent,"loadPumeResultsFromCSV")###(
249    def loadPumeResultsFromCSV(self):
250        """load Fulltime Studentdata from CSV values into pumeresults catalog"""
251        import transaction
252        import random
253        from pdb import set_trace
254        csv_d = {'jamb_reg_no': "RegNumber",
255                 'status': "Admission Status",
256                 'name': "Name",
257                 'score': "Score",
258                 'sex': "Sex",
259                 'faculty': "Faculty",
260                 'department': "Dept",
261                 'course': "Course",
262                 'course_code_org': "Course Code",
263                 }
264        csv_fields = [f[1] for f in csv_d.items()]
265        tr_count = 0
266        total = 0
267        #name = 'pup_new'
268        name = 'pup_update'
269        update = name.endswith('update')
270        no_import = []
271        s = ','.join(['"%s"' % fn for fn in csv_fields])
272        no_import.append('%s' % s)
273        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
274        no_import_name = "%s/import/%s_not_imported_%s.csv" % (i_home,name,current)
275        open(no_import_name,"w").write('\n'.join(no_import))
276        no_import = []
277        logger = logging.getLogger('%s_import' % name)
278        starttime = DateTime.now()
279        logger.info('Start loading from %s.csv' % name)
280        try:
281            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
282        except:
283            logger.error('Error reading %s.csv' % name)
284            return
285        pume = self.portal_pumeresults
286        format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
287        eduplicate = '"dupplicate",%s' % format
288        added = 'added ,%s' % format
289        for jamb in result:
290            dict = {}
291            #set_trace()
292            for f,fn in csv_d.items():
293                dict[f] = jamb.get(csv_d[f])
294            res = pume(jamb_reg_no=jamb.get(csv_d['jamb_reg_no']))
295            if len(res) > 0:
296                if update:
297                    try:
298                        pume.modifyRecord(**dict)
299                    except ValueError:
300                        logger.info(eduplicate % jamb)
301                        continue
302                    except KeyError:
303                        pume.addRecord(**dict)
304                        logger.info(added % jamb)
305                        continue
306                else:
307                    data = res[0]
308                    if data.name != jamb.get(csv_d['name']):
309                        #set_trace()
310                        logger.info(eduplicate % jamb)
311                        #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
312                        #logger.info(em)
313                        dd = {}
314                        for f,fn in csv_d.items():
315                            dd[fn] = getattr(data,f)
316                        no_import.append(eduplicate % dd)
317                        no_import.append(eduplicate % jamb)
318                    continue
319            try:
320                pume.addRecord(**dict)
321            except ValueError:
322                logger.info(eduplicate % jamb)
323                #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
324                #logger.info(em)
325                no_import.append(eduplicate % jamb)
326        logger.info('End loading from %s.csv' % name)
327        if len(no_import) > 1:
328            open(no_import_name,"w+").write('\n'.join(no_import))
329        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
330    ###)
331
332    security.declareProtected(ModifyPortalContent,"createNewStudents")###(
333    def createNewStudents(self):
334        """load Fulltime Studentdata from CSV values"""
335        import transaction
336        import random
337        #from pdb import set_trace
338        wftool = self.portal_workflow
339        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
340        csv_d = {'jamb_reg_no': "RegNumber",
341                 'jamb_lastname': "Name",
342                 'session': "Session",
343                 'pume_tot_score': "PUME SCORE",
344                 'jamb_score': "JambScore",
345                 'jamb_sex': "Sex",
346                 'jamb_state': "State",
347##                 'jamb_first_cos': "AdminCourse",
348                 'faculty': "AdminFaculty",
349                 'course_code': "AdmitCoscode",
350                 'stud_status':"AdmitStatus",
351                 'department': "AdmitDept",
352                 'jamb_lga': "LGA",
353                 'app_email': "email",
354                 'app_mobile': "PhoneNumbers",
355                 }
356        csv_fields = [f[1] for f in csv_d.items()]
357        tr_count = 0
358        total = 0
359        #name = 'pume_results'
360        name = 'Admitted'
361        no_import = []
362        s = ','.join(['"(%s)"' % fn for fn in csv_fields])
363        no_import.append('"Error",%s' % s)
364        format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
365        no_certificate = "no certificate %s" % format
366        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
367                    '\n'.join(no_import))
368        logger = logging.getLogger('%s_import' % name)
369        logger.info('Start loading from %s.csv' % name)
370        l = self.portal_catalog({'meta_type': "Certificate"})
371        certs = {}
372        cert_docs = {}
373        for f in l:
374            certs[f.getId] = f.getObject().getContent()
375        try:
376            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
377        except:
378            logger.error('Error reading %s.csv' % name)
379            return
380        for jamb in result:
381            jamb['Error'] = "Processing "
382            logger.info(format % jamb)
383            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
384            res = self.portal_catalog({'portal_type': "StudentApplication",
385                                     'SearchableText': jamb_reg_no })
386            if res:
387                em = 'Student with RegNo %s already exists\n' % jamb_reg_no
388                logger.info(em)
389                jamb['Error'] = "Student exists"
390                no_import.append(format % jamb)
391                continue
392            cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
393            if cert_id not in certs.keys():
394                em = 'No Certificate with ID %s \n' % cert_id
395                logger.info(em)
396                jamb['Error'] = "No Certificate %s" % cert_id
397                no_import.append( format % jamb)
398                continue
399            cert_doc = certs[cert_id]
400            catalog_entry = {}
401            catalog_entry['jamb_reg_no'] = jamb_reg_no
402            jamb_name = jamb.get(csv_d['jamb_lastname'])
403            jamb_name.replace('>','')
404            jamb_name.replace('<','')
405            names = jamb_name.split()
406            letter = names[-1][0].upper()
407            sid = self.generateStudentId(letter)
408            not_created = True
409            while not_created:
410                try:
411                    students_folder.invokeFactory('Student', sid)
412                    not_created = False
413                except BadRequest:
414                    sid = self.generateStudentId(letter)
415            catalog_entry['id'] = sid
416            tr_count += 1
417            logger.info('%(total)s+%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
418            student = getattr(self,sid)
419            student.manage_setLocalRoles(sid, ['Owner',])
420            student.invokeFactory('StudentPume','pume')
421            dp = {'Title': 'Pume Data'}
422            student.invokeFactory('StudentApplication','application')
423            da = {'Title': 'Application Data'}
424            da["jamb_lastname"] = jamb_name
425            da_fields = ('jamb_reg_no',
426                         'jamb_sex',
427                         #'jamb_state',
428                         'jamb_score',
429##                         'jamb_first_cos',
430                         'jamb_sex',
431                         'jamb_state',
432                         'jamb_lga',
433                         'app_email',
434                         'app_mobile',
435                         )
436            for f in da_fields:
437                da[f] = jamb.get(csv_d[f])
438            catalog_entry['email'] = jamb.get(csv_d['app_email'])
439            app = student.application
440            app_doc = app.getContent()
441            picture ="%s/import/pictures/%s.jpg" % (i_home,jamb_reg_no)
442            #import pdb;pdb.set_trace()
443            if os.path.exists(picture):
444                file = open(picture)
445                if False:
446                    img = PIL.Image.open(file)
447                    img.thumbnail((150,200),
448                                  resample=PIL.Image.ANTIALIAS)
449                    # We now need a buffer to write to. It can't be the same
450                    # as the inbuffer as the PNG writer will write over itself.
451                    outfile = StringIO()
452                    img.save(outfile, format=img.format)
453                else:
454                    outfile = file.read()
455                app_doc.manage_addFile('passport',
456                                       file=outfile,
457                                       title="%s.jpg" % jamb_reg_no)
458            app.getContent().edit(mapping=da)
459            app.manage_setLocalRoles(sid, ['Owner',])
460            #wftool.doActionFor(app,'close')
461            dp_fields = (
462                         #'pume_eng_score',
463                         #'pume_gen_score',
464                         'pume_tot_score',
465                         )
466            dp['pume_tot_score'] = jamb.get(csv_d['pume_tot_score']) or "No Option Shaded"
467            pume = student.pume
468            pume.getContent().edit(mapping=dp)
469            #wftool.doActionFor(pume,'close')
470            pume.manage_setLocalRoles(sid, ['Owner',])
471            #student.getContent().createSubObjects()
472            dp = {}
473            if len(names) == 3:
474                dp['firstname'] = names[0].capitalize()
475                dp['middlename'] = names[1].capitalize()
476                dp['lastname'] = names[2].capitalize()
477            elif len(names) == 2:
478                dp['firstname'] = names[0].capitalize()
479                dp['middlename'] = ''
480                dp['lastname'] = names[1].capitalize()
481            else:
482                dp['firstname'] = ''
483                dp['middlename'] = ''
484                dp['lastname'] = jamb_name
485            dp['sex'] = jamb.get(csv_d['jamb_sex']) == 'F'
486            catalog_entry['sex'] = dp['sex']
487            catalog_entry['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
488            student.invokeFactory('StudentPersonal','personal')
489            per = student.personal
490            per_doc = per.getContent()
491            per_doc.edit(mapping = dp)
492            per.manage_setLocalRoles(sid, ['Owner',])
493            if jamb.get(csv_d['stud_status']) == "Admitted":
494                wftool.doActionFor(student,'pume_pass')
495                wftool.doActionFor(student,'admit')
496            else:
497                wftool.doActionFor(student,'pume_fail')
498                wftool.doActionFor(student,'reject_admission')
499                continue
500            #
501            # Clearance
502            #
503            student.invokeFactory('StudentClearance','clearance')
504            #wftool.doActionFor(student.clearance,'open')
505            dp = {'Title': 'Clearance/Eligibility Record'}
506            student.clearance.manage_setLocalRoles(sid, ['Owner',])
507            #
508            # Study Course
509            #
510            student.invokeFactory('StudentStudyCourse','study_course')
511            study_course = student.study_course
512            dsc = {}
513            from_certificate = ['title',
514                               'max_elect',
515                               'max_pass',
516                               'n_core',
517                               'nr_years',
518                               'probation_credits',
519                               'promotion_credits',
520                               'start_level',
521                              ]
522            for f in from_certificate:
523                dsc[f] = getattr(cert_doc,f)
524            dsc['faculty'] = jamb.get(csv_d['faculty'])
525            dsc['department'] = jamb.get(csv_d['department'])
526            catalog_entry['faculty'] = jamb.get(csv_d['faculty'])
527            catalog_entry['department'] = jamb.get(csv_d['department'])
528            catalog_entry['course'] = cert_id
529            catalog_entry['level'] = getattr(cert_doc,'start_level')
530            dsc['study_course'] = cert_id
531            dsc['entry_session'] = jamb.get(csv_d['session'])
532            study_course.getContent().edit(mapping=dsc)
533            self.students_catalog.addRecord(**catalog_entry)
534            if tr_count > MAX_TRANS:
535                if len(no_import) > 1:
536                    open("%s/import/%s_not_imported.csv" % (i_home,name),"w+").write(
537                             '\n'.join(no_import))
538                    no_import = []
539                em = '%d transactions commited\n' % tr_count
540                transaction.commit()
541                logger.info(em)
542                total += tr_count
543                tr_count = 0
544        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
545    ###)
546
547    security.declareProtected(ModifyPortalContent,"OLDloadPumeResultsFromCSV")###(
548    def OLDloadPumeResultsFromCSV(self):
549        """load Fulltime Studentdata from CSV values"""
550        import transaction
551        import random
552        wftool = self.portal_workflow
553        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
554        csv_d = {'jamb_reg_no': "JAMBRegno",
555                 'jamb_lastname': "Name",
556                 'pume_options': "Options",
557                 'session': "Session",
558                 'days': "Days",
559                 'response': "Responce",
560                 'wrong': "Wrong",
561                 'pume_eng_score': "EngScore",
562                 'pume_gen_score': "GenScore",
563                 'pume_tot_score': "Score",
564                 'batch': "Batch",
565                 'serial': "SerialNo",
566                 'jamb_score': "JambScore",
567                 'omitted':"Omitted",
568                 'search_key': "SearchKey",
569                 'jamb_sex': "Sex",
570                 'fac1': "Fac1",
571                 'fac2': "Fac2",
572                 'jamb_first_cos': "CourseofStudy",
573                 'stud_status':"StudStatus",
574                 'registered': "Registered",
575                 'jamb_state': "State",
576                 'eng_fail': "EngFail",
577                 'gen_fail': "GenFail",
578                 'un_ans_eng': "UnAnsEng",
579                 'un_ans_eng': "UnAnsGen",
580                 'total_ans': "TotalUnAns",
581                 'dept': "Dept",
582                 'jamb_second_cos': "Course2",
583                 'jamb_third_cos': "course3",
584                 }
585        csv_fields = [f[1] for f in csv_d.items()]
586        tr_count = 0
587        name = 'pume_results'
588        no_import = []
589        s = ','.join(['"(%s)"' % fn for fn in csv_fields])
590        no_import.append('%s\n' % s)
591        logger = logging.getLogger('%s_import' % name)
592        logger.info('Start loading from %s.csv' % name)
593        try:
594            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
595        except:
596            logger.error('Error reading %s.csv' % name)
597            return
598        for jamb in result:
599            format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
600            processing = "processing %s" % format
601            logger.info(processing % jamb)
602            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
603            #import pdb;pdb.set_trace()
604            res = self.portal_catalog({'portal_type': "StudentApplication",
605                                     'jamb_reg_no': jamb_reg_no })
606            if res:
607                em = 'Student with REG-NO %s already exists\n' % jamb_reg_no
608                logger.info(em)
609                no_import.append(em)
610                no_import.append(format % jamb)
611                continue
612            cert_id = jamb.get(csv_d['jamb_first_cos']).upper()
613            res = self.portal_catalog({'portal_type': "Certificate",
614                                     'id': cert_id })
615            if len(res) < 1:
616                em = 'No Certificate with ID %s \n' % cert_id
617                logger.info(em)
618                no_import.append(em)
619                no_import.append(format % jamb)
620                continue
621            cert = res[0].getObject()
622            cert_path = res[0].getPath()
623            cert_doc = cert.getContent()
624            jamb_name = jamb.get(csv_d['jamb_lastname'])
625            jamb_name.replace('>','')
626            names = jamb_name.split()
627            letter = names[-1][0].upper()
628            sid = self.generateStudentId(letter)
629            not_created = True
630            while not_created:
631                try:
632                    students_folder.invokeFactory('Student', sid)
633                    not_created = False
634                except BadRequest:
635                    sid = self.generateStudentId(letter)
636            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
637            student = getattr(self,sid)
638            student.manage_setLocalRoles(sid, ['Owner',])
639            student.invokeFactory('StudentClearance','clearance')
640            #wftool.doActionFor(student.clearance,'open')
641            dp = {'Title': 'Clearance/Eligibility Record'}
642            student.clearance.manage_setLocalRoles(sid, ['Owner',])
643            student.invokeFactory('StudentPume','pume')
644            dp = {'Title': 'Pume Data'}
645            student.invokeFactory('StudentApplication','application')
646            da = {'Title': 'Application Data'}
647            da["jamb_lastname"] = jamb_name
648            da_fields = ('jamb_reg_no',
649                         'jamb_sex',
650                         'jamb_state',
651                         'jamb_score',
652                         'jamb_first_cos',
653                         'jamb_sex',
654                         'jamb_state',
655                         'jamb_first_cos',
656                         'jamb_second_cos',
657                         )
658            for f in da_fields:
659                da[f] = jamb.get(csv_d[f])
660            app = student.application
661            app.getContent().edit(mapping=da)
662            app.manage_setLocalRoles(sid, ['Owner',])
663            #wftool.doActionFor(app,'close')
664            dp_fields = (
665                         'pume_eng_score',
666                         'pume_gen_score',
667                         'pume_tot_score',
668                         )
669            for f in dp_fields:
670                dp[f] = float(jamb.get(csv_d[f]))
671            pume = student.pume
672            pume.getContent().edit(mapping=dp)
673            #wftool.doActionFor(pume,'close')
674            pume.manage_setLocalRoles(sid, ['Owner',])
675            #
676            # Study Course
677            #
678            student.invokeFactory('StudentStudyCourse','study_course')
679            study_course = student.study_course
680            dsc = {}
681            from_certificate = ['title',
682                               'max_elect',
683                               'max_pass',
684                               'n_core',
685                               'nr_years',
686                               'probation_credits',
687                               'promotion_credits',
688                               'start_level',
689                              ]
690            for f in from_certificate:
691                dsc[f] = getattr(cert_doc,f)
692            cpl = cert_path.split('/')
693            dsc['faculty'] = cpl[-4]
694            dsc['department'] = cpl[-3]
695            dsc['study_course'] = cert_id
696            dsc['entry_session'] = jamb.get(csv_d['session'])
697            study_course.getContent().edit(mapping=dsc)
698            student.getContent().createSubObjects()
699            if dp['pume_tot_score']>49:
700                wftool.doActionFor(student,'pume_pass')
701                wftool.doActionFor(student,'admit')
702            else:
703                wftool.doActionFor(student,'pume_fail')
704                wftool.doActionFor(student,'reject_admission')
705            if len(no_import) > 1:
706                open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
707                     '\n'.join(no_import))
708        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
709    ###)
710
711    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
712    def loadFullTimeStudentsResultsFromCSV(self):
713        """load Fulltime Studentdata from CSV values"""
714        #return
715        level_wf_actions = {}
716        level_wf_actions["SUCCESSFUL STUDENT"] = "pass_A"
717        level_wf_actions["STUDENT WITH CARRYOVER COURSES"] = "pass_B"
718        level_wf_actions["STUDENT FOR PROBATION"] = "probate_C"
719        level_wf_actions["STUDENT ON PROBATION/TRANSFER"] = "reject_D"
720        import transaction
721        wftool = self.portal_workflow
722        tr_count = 0
723        name = 'short_full_time_results_2004_2005'
724        no_import = False
725        if not no_import:
726            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
727            no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
728        logger = logging.getLogger('%s_import' % name)
729        logger.info('Start loading from %s.csv' % name)
730        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
731        try:
732            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
733        except:
734            logger.error('Error reading %s.csv' % name)
735            return
736        l = self.portal_catalog({'meta_type': "Course"})
737        courses = {}
738        for c in l:
739            courses[c.id] = c.getObject()
740        level_changed = False
741        student_changed = False
742        sid = ''
743        #import pdb;pdb.set_trace()
744        for result in results:
745            temp_sid = result.get('Matnumber')
746            if temp_sid != sid:
747                student_changed = True
748                res = self.portal_catalog({'meta_type': "StudentClearance",
749                                         'SearchableText': temp_sid })
750                if not res:
751                    em = 'Student with ID %(Matnumber)s not found\n' % result
752                    logger.info(em)
753                    no_import.write(em)
754                    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)
755                    continue
756                elif len(res) > 1:
757                    em = 'More than one Student with ID %(Matnumber)s found\n' % result
758                    logger.info(em)
759                    no_import.write(em)
760                    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)
761                    continue
762                sid = temp_sid
763                sf = res[0].getObject().aq_parent
764                sc = getattr(sf,'study_course')
765                level = ''
766            else:
767                student_changed = False
768            course = result.get('CosCode')
769            if course not in courses.keys():
770                em = 'Course with ID %(CosCode)s not found\n' % result
771                logger.info(em)
772                no_import.write(em)
773                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)
774                continue
775            course_doc = courses[course].getContent()
776            temp_level = result.get('Level')
777            student_id = sf.getId()
778            result['StudentId'] = student_id
779            if temp_level != level:
780                try:
781                    int(temp_level)
782                except:
783                    em = 'Result with ID %(Matnumber)s Course %(CosCode)s Level is empty\n' % result
784                    logger.info(em)
785                    no_import.write(em)
786                    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)
787                    continue
788                level_changed = True
789                if 'dlev' in vars().keys():
790                    wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
791                level = temp_level
792                l = getattr(sc,level,None)
793                if l is None:
794                    logger.info('Creating Level %(Level)s for %(StudentId)s %(Matnumber)s' % result)
795                    sc.invokeFactory('StudentStudyLevel', level)
796                    l = getattr(sc, level)
797                    l.manage_setLocalRoles(student_id, ['Owner',])
798            else:
799                level_changed = False
800            cr = getattr(l,course,None)
801            if cr is None:
802                logger.info('Creating Course %(CosCode)s for %(StudentId)s %(Matnumber)s in Level %(Level)s' % result)
803                l.invokeFactory('StudentCourseResult',course)
804            cr = getattr(l,course)
805            dcr = {}
806            from_course = ['title',
807                           'credits',
808                           'passmark',
809                           ]
810            for f in from_course:
811                dcr[f] = getattr(course_doc,f)
812            dlev = {}
813            dcr['ansbook'] = result.get('Ansbook')
814            dcr['semester'] = getInt(result.get('Semster'))
815            dcr['status'] = result.get('CosStuatus')
816            dcr['score'] = getInt(result.get('Score'))
817            dlev['session'] = result.get('Session')
818            dcr['carry_level'] = result.get('CarryLevel')
819            dcr['grade'] = result.get('Grade')
820            dcr['weight'] = result.get('Weight')
821            dlev['verdict'] = result.get('Verdict')
822            dcr['import_id'] = result.get('id')
823            gpa = result.get('GPA').replace(',','.')
824            dlev['imported_gpa'] = getFloat(gpa)
825            cr.getContent().edit(mapping = dcr)
826            cr.manage_setLocalRoles(student_id, ['Owner',])
827            l.getContent().edit(mapping = dlev)
828            if tr_count > MAX_TRANS:
829                transaction.commit()
830                tr_count = 0
831            tr_count += 1
832            wftool.doActionFor(cr,'close')
833        wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
834        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
835
836###)
837
838    security.declareProtected(ModifyPortalContent,"loadJAMBFromCSV")###(
839    def loadJAMBFromCSV(self):
840        """load JAMB data from CSV values"""
841        #return
842        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
843        import transaction
844        tr_count = 0
845        name = 'SampleJAMBDataII'
846        wftool = self.portal_workflow
847        no_import = False
848        if not no_import:
849            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
850            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')
851        logger = logging.getLogger('%s_import' % name)
852        logger.info('Start loading from %s.csv' % name)
853        try:
854            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
855        except:
856            logger.error('Error reading %s.csv' % name)
857            return
858        for jamb in result:
859            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)
860            jamb_reg_no = jamb.get('REG-NO')
861            res = self.portal_catalog({'meta_type': "StudentApplication",
862                                     'jamb_reg_no': jamb_reg_no })
863            if res:
864                em = 'Student with REG-NO %(REG-NO)s already exists\n' % jamb
865                logger.info(em)
866                no_import.write(em)
867                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)
868                continue
869            jamb_name = jamb.get("NAME")
870            jamb_name.replace('>','')
871            names = jamb_name.split()
872            letter = names[-1][0].upper()
873            sid = self.generateStudentId(letter)
874            not_created = True
875            while not_created:
876                try:
877                    students_folder.invokeFactory('Student', sid)
878                    not_created = False
879                except BadRequest:
880                    sid = self.generateStudentId(letter)
881            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
882            student = getattr(self,sid)
883            student.manage_setLocalRoles(sid, ['Owner',])
884            student.invokeFactory('StudentApplication','application')
885            da = {'Title': 'Application Data'}
886            da["jamb_reg_no"] = jamb.get("REG-NO")
887            da["jamb_lastname"] = jamb_name
888            da["jamb_sex"] = jamb.get("SEX")
889            da["jamb_state"] = jamb.get("STATE")
890            da["jamb_lga"] = jamb.get("LGA")
891            da["jamb_score"] = jamb.get("AGGREGATE")
892            da["jamb_first_cos"] = jamb.get("COURSE1")
893            da["jamb_second_cos"] = jamb.get("COURSE2")
894            da["jamb_first_uni"] = jamb.get("UNIV1")
895            da["jamb_second_uni"] = jamb.get("UNIV2")
896            app = student.application
897            app_doc = app.getContent()
898            app_doc.edit(mapping=da)
899            #wftool.doActionFor(app,'open',dest_container=app)
900            app.manage_setLocalRoles(sid, ['Owner',])
901            student.getContent().createSubObjects()
902        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
903    ###)
904
905
906    security.declareProtected(View,"fixOwnership")
907    def fixOwnership(self):
908        """fix Ownership"""
909        for s in self.portal_catalog(meta_type = 'Student'):
910            student = s.getObject()
911            sid = s.getId
912            import pdb;pdb.set_trace()
913            student.application.manage_setLocalRoles(sid, ['Owner',])
914            student.personal.manage_setLocalRoles(sid, ['Owner',])
915
916    security.declareProtected(View,"Title")
917    def Title(self):
918        """compose title"""
919        return "Student Section"
920
921    def generateStudentId(self,letter): ###(
922        import random
923        r = random
924        if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
925            letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY')
926        students = self.portal_catalog(meta_type = "StudentsFolder")[-1]
927        sid = "%c%d" % (letter,r.randint(99999,1000000))
928        while hasattr(students, sid):
929            sid = "%c%d" % (letter,r.randint(99999,1000000))
930        return sid
931        #return "%c%d" % (r.choice('ABCDEFGHKLMNPQRSTUVWXY'),r.randint(99999,1000000))
932    ###)
933
934InitializeClass(StudentsFolder)
935
936def addStudentsFolder(container, id, REQUEST=None, **kw):
937    """Add a Student."""
938    ob = StudentsFolder(id, **kw)
939    return CPSBase_adder(container, ob, REQUEST=REQUEST)
940###)
941
942class Student(CPSDocument): ###(
943    """
944    WAeUP Student container for the various student data
945    """
946    meta_type = 'Student'
947    portal_type = meta_type
948    security = ClassSecurityInfo()
949
950    security.declareProtected(View,"Title")
951    def Title(self):
952        """compose title"""
953        reg_nr = self.getId()[1:]
954        data = getattr(self,'personal',None)
955        if data:
956            content = data.getContent()
957            return "%s %s" % (content.firstname,content.lastname)
958        data = getattr(self,'application',None)
959        if data:
960            content = data.getContent()
961            return "%s" % (content.jamb_lastname)
962        return self.title
963
964    security.declarePrivate('makeStudentMember') ###(
965    def makeStudentMember(self,sid,password='uNsEt'):
966        """make the student a member"""
967        membership = self.portal_membership
968        membership.addMember(sid,
969                             password ,
970                             roles=('Member',
971                                     'Student',
972                                     ),
973                             domains='',
974                             properties = {'memberareaCreationFlag': False,
975                                           'homeless': True},)
976        member = membership.getMemberById(sid)
977        self.portal_registration.afterAdd(member, sid, password, None)
978        self.manage_setLocalRoles(sid, ['Owner',])
979
980###)
981
982    security.declareProtected(View,'createSubObjects') ###(
983    def createSubObjects(self):
984        """make the student a member"""
985        dp = {'Title': 'Personal Data'}
986        app_doc = self.application.getContent()
987        names = app_doc.jamb_lastname.split()
988        if len(names) == 3:
989            dp['firstname'] = names[0].capitalize()
990            dp['middlename'] = names[1].capitalize()
991            dp['lastname'] = names[2].capitalize()
992        elif len(names) == 2:
993            dp['firstname'] = names[0].capitalize()
994            dp['lastname'] = names[1].capitalize()
995        else:
996            dp['lastname'] = app_doc.jamb_lastname
997        dp['sex'] = app_doc.jamb_sex == 'F'
998        dp['lga'] = "%s/%s" % (app_doc.jamb_state,app_doc.jamb_lga )
999        proxy = self.aq_parent
1000        proxy.invokeFactory('StudentPersonal','personal')
1001        per = proxy.personal
1002        per_doc = per.getContent()
1003        per_doc.edit(mapping = dp)
1004        per.manage_setLocalRoles(proxy.getId(), ['Owner',])
1005        #self.portal_workflow.doActionFor(per,'open',dest_container=per)
1006
1007###)
1008
1009InitializeClass(Student)
1010
1011def addStudent(container, id, REQUEST=None, **kw):
1012    """Add a Student."""
1013    ob = Student(id, **kw)
1014    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1015
1016###)
1017
1018class StudentAccommodation(CPSDocument): ###(
1019    """
1020    WAeUP Student container for the various student data
1021    """
1022    meta_type = 'StudentAccommodation'
1023    portal_type = meta_type
1024    security = ClassSecurityInfo()
1025
1026    security.declareProtected(View,"Title")
1027    def Title(self):
1028        """compose title"""
1029        content = self.getContent()
1030        #return "Accommodation Data for %s %s" % (content.firstname,content.lastname)
1031        return "Accommodation Data for Session %s" % content.session
1032
1033
1034InitializeClass(StudentAccommodation)
1035
1036def addStudentAccommodation(container, id, REQUEST=None, **kw):
1037    """Add a Students personal data."""
1038    ob = StudentAccommodation(id, **kw)
1039    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1040
1041###)
1042
1043class StudentPersonal(CPSDocument): ###(
1044    """
1045    WAeUP Student container for the various student data
1046    """
1047    meta_type = 'StudentPersonal'
1048    portal_type = meta_type
1049    security = ClassSecurityInfo()
1050
1051    security.declareProtected(View,"Title")
1052    def Title(self):
1053        """compose title"""
1054        content = self.getContent()
1055        #return "Personal Data for %s %s" % (content.firstname,content.lastname)
1056        return "Personal Data"
1057
1058
1059InitializeClass(StudentPersonal)
1060
1061def addStudentPersonal(container, id, REQUEST=None, **kw):
1062    """Add a Students personal data."""
1063    ob = StudentPersonal(id, **kw)
1064    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1065
1066###)
1067
1068class StudentClearance(CPSDocument): ###(
1069    """
1070    WAeUP Student container for the various student data
1071    """
1072    meta_type = 'StudentClearance'
1073    portal_type = meta_type
1074    security = ClassSecurityInfo()
1075
1076    security.declareProtected(View,"Title")
1077    def Title(self):
1078        """compose title"""
1079        content = self.getContent()
1080        #return "Clearance/Eligibility Record for %s %s" % (content.firstname,content.lastname)
1081        return "Clearance/Eligibility Record"
1082
1083
1084InitializeClass(StudentClearance)
1085
1086def addStudentClearance(container, id, REQUEST=None, **kw):
1087    """Add a Students personal data."""
1088    ob = StudentClearance(id, **kw)
1089    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1090
1091###)
1092
1093class StudentStudyLevel(CPSDocument): ###(
1094    """
1095    WAeUP Student container for the various student data
1096    """
1097    meta_type = 'StudentStudyLevel'
1098    portal_type = meta_type
1099    security = ClassSecurityInfo()
1100
1101    security.declareProtected(View,"Title")
1102    def Title(self):
1103        """compose title"""
1104        return "Level %s" % self.aq_parent.getId()
1105
1106##    security.declarePublic("gpa")
1107##    def gpa(self):
1108##        """calculate the gpa"""
1109##        sum = 0
1110##        course_count = 0
1111##        for sc in self.objectValues():
1112##            result = sc.getContent()
1113##            if not result.grade:
1114##                continue
1115##            res = self.portal_catalog({'meta_type': 'Course',
1116##                                          'id': sc.aq_parent.id})
1117##            if len(res) != 1:
1118##                continue
1119##            course = res[0].getObject().getContent()
1120##            sum += course.credits * ['F','E','D','C','B','A'].index(result.grade)
1121##            course_count += 1
1122##        if course_count:
1123##            return sum/course_count
1124##        return 0.0
1125
1126InitializeClass(StudentStudyLevel)
1127
1128def addStudentStudyLevel(container, id, REQUEST=None, **kw):
1129    """Add a Students personal data."""
1130    ob = StudentStudyLevel(id, **kw)
1131    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1132
1133###)
1134
1135class StudentStudyCourse(CPSDocument): ###(
1136    """
1137    WAeUP Student container for the various student data
1138    """
1139    meta_type = 'StudentStudyCourse'
1140    portal_type = meta_type
1141    security = ClassSecurityInfo()
1142
1143    security.declareProtected(View,"Title")
1144    def Title(self):
1145        """compose title"""
1146        content = self.getContent()
1147        return "Study Course"
1148
1149
1150InitializeClass(StudentStudyCourse)
1151
1152def addStudentStudyCourse(container, id, REQUEST=None, **kw):
1153    """Add a Students personal data."""
1154    ob = StudentStudyCourse(id, **kw)
1155    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1156
1157###)
1158
1159class StudentApplication(CPSDocument): ###(
1160    """
1161    WAeUP Student container for the various student data
1162    """
1163    meta_type = 'StudentApplication'
1164    portal_type = meta_type
1165    security = ClassSecurityInfo()
1166
1167    security.declareProtected(View,"Title")
1168    def Title(self):
1169        """compose title"""
1170        return "Application Data"
1171
1172
1173InitializeClass(StudentApplication)
1174
1175def addStudentApplication(container, id, REQUEST=None, **kw):
1176    """Add a Students eligibility data."""
1177    ob = StudentApplication(id, **kw)
1178    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1179###)
1180
1181
1182class StudentPume(CPSDocument): ###(
1183    """
1184    WAeUP Student container for the various student data
1185    """
1186    meta_type = 'StudentPume'
1187    portal_type = meta_type
1188    security = ClassSecurityInfo()
1189
1190    security.declareProtected(View,"Title")
1191    def Title(self):
1192        """compose title"""
1193        return "PUME Results"
1194
1195
1196InitializeClass(StudentPume)
1197
1198def addStudentPume(container, id, REQUEST=None, **kw):
1199    """Add a Students PUME data."""
1200    ob = StudentPume(id, **kw)
1201    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1202###)
1203
1204##class StudentSemester(CPSDocument): ###(
1205##    """
1206##    WAeUP StudentSemester containing the courses and students
1207##    """
1208##    meta_type = 'StudentSemester'
1209##    portal_type = meta_type
1210##    security = ClassSecurityInfo()
1211##
1212##InitializeClass(StudentSemester)
1213##
1214##def addStudentSemester(container, id, REQUEST=None, **kw):
1215##    """Add a StudentSemester."""
1216##    ob = StudentSemester(id, **kw)
1217##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1218##
1219#####)
1220
1221##class Semester(CPSDocument): ###(
1222##    """
1223##    WAeUP Semester containing the courses and students
1224##    """
1225##    meta_type = 'Semester'
1226##    portal_type = meta_type
1227##    security = ClassSecurityInfo()
1228##
1229##InitializeClass(Semester)
1230##
1231##def addSemester(container, id, REQUEST=None, **kw):
1232##    """Add a Semester."""
1233##    ob = Semester(id, **kw)
1234##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1235##
1236#####)
1237
1238class StudentCourseResult(CPSDocument): ###(
1239    """
1240    WAeUP StudentCourseResult
1241    """
1242    meta_type = 'StudentCourseResult'
1243    portal_type = meta_type
1244    security = ClassSecurityInfo()
1245
1246    def getCourseEntry(self,cid):
1247        res = self.portal_catalog({'meta_type': "Course",
1248                                           'id': cid})
1249        if res:
1250            return res[-1]
1251        else:
1252            return None
1253
1254    security.declareProtected(View,"Title")
1255    def Title(self):
1256        """compose title"""
1257        cid = self.aq_parent.getId()
1258        ce = self.getCourseEntry(cid)
1259        if ce:
1260            return "%s" % ce.Title
1261        return "No course with id %s" % cid
1262
1263InitializeClass(StudentCourseResult)
1264
1265def addStudentCourseResult(container, id, REQUEST=None, **kw):
1266    """Add a StudentCourseResult."""
1267    ob = StudentCourseResult(id, **kw)
1268    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1269###)
1270
1271# Backward Compatibility StudyLevel
1272
1273from Products.WAeUP_SRP.Academics import StudyLevel
1274
1275from Products.WAeUP_SRP.Academics import addStudyLevel
1276
Note: See TracBrowser for help on using the repository browser.