source: WAeUP_SRP/trunk/Students.py @ 1302

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

reallocation repair kit

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