source: WAeUP_SRP/trunk/Students.py @ 1643

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

fixed

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