source: WAeUP_SRP/branches/joachim-event-branch/Students.py @ 12407

Last change on this file since 12407 was 1597, checked in by joachim, 18 years ago

merged changes 1588:1596 from trunk to joachim-event-branch

  • Property svn:keywords set to Id
File size: 63.8 KB
RevLine 
[57]1#-*- mode: python; mode: fold -*-
[200]2# $Id: Students.py 1597 2007-03-19 21:59:41Z joachim $
[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
[1596]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
[1596]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))
[1596]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:
[1596]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))
[1596]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")
[1596]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 = []
[1596]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
[1596]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
[1596]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
[1596]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
[1596]685                st_cat.modifyRecord(id = student_id,
[1529]686                                    verdict=verdict_code)
[1596]687
[1529]688                dsc = {}
689                dsc['current_verdict'] = verdict_code
690                study_course.getContent().edit(mapping=dsc)
[1596]691            else:
692                verdict['Error'] = "Not found in students_catalog"
693                no_import.append( format_error % verdict)
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
[1596]715    security.declareProtected(ModifyPortalContent,"fixAllNames")###(
716    def fixAllNames(self):
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
733            response_write(response,"%d) %s: %s" % (count + total,student_entry.id,old_new))
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")###(
742    def fixName(self,student_brain, student_entry):
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
748        new_student = student_entry.jamb_reg_no.startswith('6')
749        student_obj = student_brain.getObject()
750        personal = getattr(student_obj,'personal',None)
751        invalid = ''
752        if personal is None:
753            return '"%s", "Returning: %s","%s"' % (invalid,student_entry.name,"not logged in")
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()]
764                if len(new_names) > 3:
765                    old_first = new_names[0]
766                    old_last = new_names[1]
767                    old_middle = ' '.join(new_names[2:])
768                elif len(new_names) == 3:
769                    old_first = new_names[0]
770                    old_last = new_names[1]
771                    old_middle = new_names[2]
772                elif len(new_names) == 2:
773                    old_first = new_names[0]
774                    old_last = new_names[1]
775                    old_middle = ''
776                elif len(new_names) == 1:
777                    old_last = new_names[0]
778                    old_first = ''
779                    old_middle = ''
780            if old_last:
781                new_first = old_last
782            if old_middle:
783                new_middle = old_middle
784            if old_first:
785                new_last = old_first
786                   
787            if old_first.find('<') != -1 or\
788               old_first.find('>') != -1 or\
789               old_middle.find('<') != -1 or\
790               old_middle.find('>') != -1 or\
791               old_last.find('<') != -1 or\
792               old_last.find('>') != -1:
793                   invalid = "invalid characters"
794        else:
795            if old_first and old_middle and old_last:
796                new_first = old_first.capitalize()
797                new_middle = old_middle.capitalize()
798                new_last = old_last.capitalize()
799            elif old_first and not old_middle and old_last:
800                new_names = [n.capitalize() for n in old_first.split()]
801                if len(new_names) > 1:
802                    new_first = new_names[0]
803                    new_middle = ' '.join(new_names[1:])
804                else:
805                    new_first = old_first.capitalize()
806                    new_middle = ''
807                new_last = old_last.capitalize()
808            elif not old_first and not old_middle and old_last:
809                new_first = ''
810                new_middle = ''
811                new_last = old_last.capitalize()
812            else:
813                new_first = ''
814                new_middle = ''
815                new_last = ''
816        name = "%(new_last)s %(new_first)s %(new_middle)s" % vars()
817        if new_student:
818            text = "New"
819        else:
820            text = "Returning"
821        old_new = '"%s", "%s: %s","%s"' % (invalid,text,
822                                           student_entry.name,
823                                           name)
824        if not invalid:
825            self.students_catalog.modifyRecord(id = student_id,
826                                      name_fixed = fix,
827                                      name = name)
828            per_doc.edit(mapping = {'firstname' : new_first,
829                                'middlename' : new_middle,
830                                'lastname' : new_last,
831                                })
832        return old_new
833    ###)
834
[1319]835    security.declareProtected(ModifyPortalContent,"fixAllEntryModeForReturning")###(
836    def fixAllEntryModeForReturning(self):
837        "read all Returning*.csv"
838        ipath = "%s/import/" % i_home
839        names = os.listdir(ipath)
840        for name in names:
841            head,tail = os.path.splitext(name)
842            if head.startswith('Returning')\
843               and tail == '.csv'\
844               and name.find('imported') < 0:
845                self.fixEntryModeForReturning(csv_file=head)
[1322]846    ###)
[1386]847
[1319]848    security.declareProtected(ModifyPortalContent,"fixEntryModeForReturning")###(
849    def fixEntryModeForReturning(self,csv_file=None):
850        """load Returning Studentdata from CSV values"""
851        import transaction
852        import random
853        wftool = self.portal_workflow
854        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
855        students_folder = self.portal_url.getPortalObject().campus.students
856        tr_count = 1
857        total = 0
858        if csv_file is None:
859            name = 'Returning'
860        else:
861            name = csv_file
862        table = self.returning_import
863        st_cat = self.students_catalog
864        no_import = []
[1596]865        logger = logging.getLogger('Students.StudentsFolder.fixEntryModeForReturning')
[1319]866        try:
867            returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
868        except:
869            logger.error('Error reading %s.csv' % name)
870            return
871        start = True
872        for student in returning:
873            if start:
874                start = False
[1596]875                logger.info('Start loading from %s.csv' % name)
[1321]876                s = ','.join(['"%s"' % fn for fn in student.keys()])
[1319]877                no_import.append('%s,"Error"' % s)
878                format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()])
879                format_error = format + ',"%(Error)s"'
880            matric_no = student.get('matric_no')
881            if not matric_no:
882                continue
883            matric_no = matric_no.upper()
884            student['matric_no'] = matric_no
885            if matric_no == '':
886                continue
887            if not table(matric_no = matric_no):
888                student['Error'] = "Not imported yet"
889                no_import.append( format_error % student)
890                continue
891            student_id = None
892            app = None
893            per = None
894            if st_cat(matric_no = matric_no):
895                student_id = st_cat(matric_no = matric_no)[0].id
[1322]896                student_obj = getattr(students_folder,student_id,None)
897                if student_obj:
898                    app = getattr(student_obj,'application',None)
899                    if app is not None:
900                        app_doc = app.getContent()
901                    per = getattr(student_obj,'personal',None)
902                    if per is not None:
903                        per_doc = per.getContent()
[1319]904            student['Mode_of_Entry'] = entry_mode = student.get('Mode of Entry').upper()
905            student['Permanent_Address'] = perm_address = student.get('Permanent Address')
[1322]906            #import pdb;pdb.set_trace()
[1319]907            if not entry_mode:
908                student['Error'] = "'Mode of Entry' empty"
909                no_import.append( format_error % student)
910                continue
911            try:
912                table.modifyRecord(matric_no = matric_no,
913                                   Mode_of_Entry = entry_mode,
914                                   Permanent_Address = perm_address)
915            except KeyError:
916                student['Error'] = "Not found in returning_import"
917                no_import.append( format_error % student)
918                continue
919            if student_id is not None:
920                try:
921                    st_cat.modifyRecord(id = student_id,
922                                        entry_mode=entry_mode)
923                except KeyError:
924                    student['Error'] = "Not found in students_catalog"
925                    no_import.append( format_error % student)
926                    continue
927            if app is not None:
928                da = {}
929                da['entry_mode'] = entry_mode
930                app_doc.edit(mapping=da)
931            if per is not None:
932                dp = {}
[1350]933                dp['perm_address'] = perm_address
[1319]934                per_doc.edit(mapping=dp)
935            tr_count += 1
936            if tr_count > 1000:
937                if len(no_import) > 0:
938                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
939                             '\n'.join(no_import) + '\n')
940                    no_import = []
941                em = '%d transactions commited total %s\n' % (tr_count,total)
942                transaction.commit()
943                regs = []
944                logger.info(em)
945                total += tr_count
946                tr_count = 0
947        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
948                                                '\n'.join(no_import))
949        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
950    ###)
[1289]951
952    security.declareProtected(ModifyPortalContent,"updateReturningStudents")###(
953    def updateReturningStudents(self):
954        """load and overwrite Returning Student Data from CSV values"""
955        import transaction
956        import random
957        #from pdb import set_trace
958        wftool = self.portal_workflow
959        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
960        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
961        tr_count = 1
962        total = 0
963        #name = 'pume_results'
964        name = 'Returning_update'
965        table = self.returning_import
966        no_import = []
967        imported = []
[1596]968        logger = logging.getLogger('Students.StudentsFolder.updateReturningStudents')
[1289]969        try:
970            returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
971        except:
972            logger.error('Error reading %s.csv' % name)
973            return
974        l = self.portal_catalog({'meta_type': "Certificate"})
975        certs = {}
976        cert_docs = {}
977        for f in l:
978            certs[f.getId] = f.getObject().getContent()
979        start = True
980        res = table()
981        regs = []
982        if len(res) > 0:
983            regs = [s.matric_no for s in res]
984        for student in returning:
985            if start:
986                start = False
[1596]987                logger.info('Start loading from %s.csv' % name)
[1321]988                s = ','.join(['"%s"' % fn for fn in student.keys()])
[1289]989                imported.append(s)
990                no_import.append('%s,"Error"' % s)
991                format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()])
992                format_error = format + ',"%(Error)s"'
993                no_certificate = "no certificate %s" % format
994            matric_no = student.get('matric_no').upper()
995            student['matric_no'] = matric_no
996            if matric_no == '':
997                student['Error'] = "Empty matric_no"
998                no_import.append( format_error % student)
999                continue
1000#            if matric_no in regs or self.returning_import(matric_no = matric_no):
1001#                student['Error'] = "Duplicate"
1002#                no_import.append( format_error % student)
1003#                continue
1004#            cert_id = makeCertificateCode(student.get('Coursemajorcode'))
1005#            if cert_id not in certs.keys():
1006#                student['Error'] = "No Certificate %s" % cert_id
1007#                no_import.append( format_error % student)
1008#                continue
1009            try:
1010                table.modifyRecord(**student)
1011            except KeyError:
1012                #import pdb;pdb.set_trace()
1013                student['Error'] = "no Student found to update"
1014                no_import.append( format_error % student)
1015                continue
[1386]1016            #s = self.students_catalog(matric_no=matric_no)
1017            #if s:
1018            #    level = "%s" % (int(student.get('Level')) + 100)
1019            #    self.students_catalog.modifyRecord(id = s[0].id,
1020            #                                           level=level)
[1289]1021
1022            regs.append(student.get('matric_no'))
1023            imported.append(format % student)
1024            tr_count += 1
1025            if tr_count > 1000:
1026                if len(no_import) > 0:
1027                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1028                             '\n'.join(no_import) + '\n')
1029                    no_import = []
1030                open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1031                                            '\n'.join(no_import) + "\n")
1032                imported = []
1033                em = '%d transactions commited total %s\n' % (tr_count,total)
1034                transaction.commit()
1035                regs = []
1036                logger.info(em)
1037                total += tr_count
1038                tr_count = 0
1039        open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1040                                            '\n'.join(imported))
1041        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1042                                                '\n'.join(no_import))
1043        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1044    ###)
1045
[1146]1046    security.declareProtected(ModifyPortalContent,"importResults")###(
1047    def importResults(self):
[1151]1048        """load Returning Students Results from CSV"""
[1146]1049        import transaction
1050        import random
1051        #from pdb import set_trace
1052        wftool = self.portal_workflow
1053        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
1054        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1055        tr_count = 1
1056        total = 0
1057        #name = 'pume_results'
1058        name = 'Results'
1059        table = self.results_import
1060        no_import = []
1061        imported = []
[1596]1062        logger = logging.getLogger('Students.StudentsFolder.importResults')
[1146]1063        try:
1064            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1065        except:
1066            logger.error('Error reading %s.csv' % name)
1067            return
1068        l = self.portal_catalog({'meta_type': "Course"})
1069        courses = [f.getId for f in l]
1070        start = True
1071        res = table()
1072        regs = []
1073        if len(res) > 0:
1074            regs = [s.key for s in res]
1075        no_course = []
1076        no_course_list = []
1077        course_count = 0
1078        for result in results:
1079            if start:
1080                start = False
[1596]1081                logger.info('Start loading from %s.csv' % name)
[1321]1082                s = ','.join(['"%s"' % fn for fn in result.keys()])
[1146]1083                imported.append(s)
1084                no_import.append('%s,"Error"' % s)
1085                format = ','.join(['"%%(%s)s"' % fn for fn in result.keys()])
1086                format_error = format + ',"%(Error)s"'
1087                no_certificate = "no certificate %s" % format
1088            course_id = result.get('CosCode')
[1448]1089            if not course_id:
1090                course_id = 'N/A'
1091                result['CosCode'] = course_id
[1168]1092            matric_no = result.get('matric_no').upper()
1093            result['matric_no'] = matric_no
1094            key = matric_no+course_id
1095            if matric_no == '':
[1252]1096                result['Error'] = "Empty matric_no"
[1146]1097                no_import.append( format_error % result)
1098                continue
[1168]1099            if key in regs or self.results_import(key = key):
[1252]1100                result['Error'] = "Duplicate"
[1146]1101                no_import.append( format_error % result)
1102                continue
1103            if course_id not in courses:
1104                if course_id not in no_course:
1105                    course_count +=1
1106                    no_course.append(course_id)
1107                    no_course_list.append('"%s"' % course_id)
1108                    #result['Error'] = "No Course"
1109                    #logger.info(format_error % result)
1110            result['key'] = key
1111            try:
1112                table.addRecord(**result)
1113            except ValueError:
[1393]1114                #import pdb;pdb.set_trace()
[1252]1115                result['Error'] = "Duplicate"
[1146]1116                no_import.append( format_error % result)
1117                continue
1118            regs.append(key)
1119            imported.append(format % result)
1120            tr_count += 1
1121            if tr_count > 1000:
1122                if len(no_import) > 0:
1123                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
[1151]1124                             '\n'.join(no_import)+'\n')
[1146]1125                    no_import = []
1126                open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
[1151]1127                                            '\n'.join(imported) + '\n')
[1146]1128                imported = []
1129                if no_course_list:
1130                    open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write(
[1151]1131                                            '\n'.join(no_course_list) + '\n')
[1146]1132                    no_course_list = []
1133                em = '%d transactions commited total %s\n courses not found %s' % (tr_count,total,course_count)
1134                transaction.commit()
1135                logger.info(em)
[1168]1136                regs = []
[1146]1137                total += tr_count
1138                tr_count = 0
1139        open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
1140                                            '\n'.join(imported))
1141        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
1142                                                '\n'.join(no_import))
1143        if no_course_list:
1144            open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write(
1145                                    '\n'.join(no_course_list))
[1393]1146        em = '%d transactions commited total %s\n courses not found %s' % (tr_count,total,course_count)
1147        logger.info(em)
[1146]1148        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1149    ###)
1150
[1065]1151    security.declareProtected(ModifyPortalContent,"updateStudyCourse")###(
1152    def updateStudyCourse(self):
1153        """update StudyCourse from CSV values"""
1154        import transaction
1155        import random
1156        from pdb import set_trace
1157        wftool = self.portal_workflow
1158        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
1159        csv_d = {'jamb_reg_no': "RegNumber",
1160                 'jamb_lastname': "Name",
1161                 'session': "Session",
1162                 'pume_tot_score': "PUME SCORE",
1163                 'jamb_score': "JambScore",
1164                 'jamb_sex': "Sex",
1165                 'jamb_state': "State",
1166##                 'jamb_first_cos': "AdminCourse",
1167                 'faculty': "AdminFaculty",
1168                 'course_code': "AdmitCoscode",
1169                 'stud_status':"AdmitStatus",
1170                 'department': "AdmitDept",
1171                 'jamb_lga': "LGA",
1172                 'app_email': "email",
1173                 'app_mobile': "PhoneNumbers",
1174                 }
1175        csv_fields = [f[1] for f in csv_d.items()]
1176        tr_count = 0
1177        total = 0
1178        #name = 'pume_results'
1179        name = 'StudyCourseChange'
1180        no_import = []
[1321]1181        s = ','.join(['"%s"' % fn for fn in csv_fields])
[1065]1182        no_import.append('"Error",%s' % s)
1183        format = '"%(Error)s",' + ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
1184        no_certificate = "no certificate %s" % format
1185        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
1186                    '\n'.join(no_import))
[1596]1187        logger = logging.getLogger('Students.StudentsFolder.updateStudyCourse')
[1065]1188        logger.info('Start loading from %s.csv' % name)
1189        l = self.portal_catalog({'meta_type': "Certificate"})
1190        try:
1191            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
1192        except:
1193            logger.error('Error reading %s.csv' % name)
1194            return
1195        for jamb in result:
1196            jamb['Error'] = "Processing "
1197            logger.info(format % jamb)
1198            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
1199            res = self.portal_catalog({'portal_type': "StudentApplication",
1200                                     'SearchableText': jamb_reg_no })
1201            if not res:
[1596]1202                em = 'Student with jamb_reg_no %s does not exists\n' % jamb_reg_no
[1065]1203                logger.info(em)
[1596]1204                jamb['Error'] = "Student does not exist"
[1065]1205                no_import.append(format % jamb)
1206                continue
1207            sid = res[0].getPath().split('/')[-2]
1208            cert_id = makeCertificateCode(jamb.get(csv_d['course_code']))
1209            res = self.portal_catalog(portal_type = "Certificate", id = cert_id)
1210            if not res:
1211                em = 'No Certificate with ID %s \n' % cert_id
1212                logger.info(em)
1213                jamb['Error'] = "No Certificate %s" % cert_id
1214                no_import.append( format % jamb)
1215                continue
1216            cert_brain = res[0]
1217            catalog_entry = {}
1218            student = getattr(self,sid)
1219            #
1220            # Study Course
1221            #
1222            study_course = student.study_course
1223            dsc = {}
1224            cert_pl = cert_brain.getPath().split('/')
1225            catalog_entry['id'] = sid
1226            catalog_entry['faculty'] = cert_pl[-4]
1227            catalog_entry['department'] = cert_pl[-3]
1228            catalog_entry['course'] = cert_id
1229            dsc['study_course'] = cert_id
1230            study_course.getContent().edit(mapping=dsc)
1231            self.students_catalog.modifyRecord(**catalog_entry)
1232            if tr_count > 10:
1233                if len(no_import) > 1:
1234                    open("%s/import/%s_not_imported.csv" % (i_home,name),"w+").write(
1235                             '\n'.join(no_import))
1236                    no_import = []
1237                em = '%d transactions commited\n' % tr_count
1238                transaction.commit()
1239                logger.info(em)
1240                total += tr_count
1241                tr_count = 0
1242            tr_count += 1
1243        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
1244    ###)
1245
[1597]1246    security.declareProtected(View,"fixOwnership") ###(
[511]1247    def fixOwnership(self):
1248        """fix Ownership"""
1249        for s in self.portal_catalog(meta_type = 'Student'):
1250            student = s.getObject()
1251            sid = s.getId
1252            import pdb;pdb.set_trace()
1253            student.application.manage_setLocalRoles(sid, ['Owner',])
1254            student.personal.manage_setLocalRoles(sid, ['Owner',])
[1597]1255    ###)
[603]1256
[1597]1257    security.declareProtected(View,"Title") ###(
[364]1258    def Title(self):
1259        """compose title"""
[382]1260        return "Student Section"
[1597]1261    ###)
[361]1262
[714]1263    def generateStudentId(self,letter): ###(
1264        import random
1265        r = random
1266        if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
1267            letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY')
1268        students = self.portal_catalog(meta_type = "StudentsFolder")[-1]
1269        sid = "%c%d" % (letter,r.randint(99999,1000000))
1270        while hasattr(students, sid):
1271            sid = "%c%d" % (letter,r.randint(99999,1000000))
[758]1272        return sid
[714]1273        #return "%c%d" % (r.choice('ABCDEFGHKLMNPQRSTUVWXY'),r.randint(99999,1000000))
1274    ###)
1275
[361]1276InitializeClass(StudentsFolder)
1277
[1597]1278def addStudentsFolder(container, id, REQUEST=None, **kw): ###(
[361]1279    """Add a Student."""
1280    ob = StudentsFolder(id, **kw)
1281    return CPSBase_adder(container, ob, REQUEST=REQUEST)
[1597]1282    ###)
1283
[361]1284###)
1285
[1597]1286
[57]1287class Student(CPSDocument): ###(
1288    """
[154]1289    WAeUP Student container for the various student data
[57]1290    """
1291    meta_type = 'Student'
1292    portal_type = meta_type
1293    security = ClassSecurityInfo()
[154]1294
[152]1295    security.declareProtected(View,"Title")
1296    def Title(self):
1297        """compose title"""
[153]1298        reg_nr = self.getId()[1:]
[362]1299        data = getattr(self,'personal',None)
[152]1300        if data:
1301            content = data.getContent()
[1143]1302            return "%s %s %s" % (content.firstname,content.middlename,content.lastname)
[472]1303        data = getattr(self,'application',None)
[464]1304        if data:
1305            content = data.getContent()
1306            return "%s" % (content.jamb_lastname)
[152]1307        return self.title
[154]1308
[511]1309    security.declarePrivate('makeStudentMember') ###(
1310    def makeStudentMember(self,sid,password='uNsEt'):
1311        """make the student a member"""
1312        membership = self.portal_membership
[603]1313        membership.addMember(sid,
[511]1314                             password ,
1315                             roles=('Member',
1316                                     'Student',
[522]1317                                     ),
[511]1318                             domains='',
[904]1319                             properties = {'memberareaCreationFlag': False,
1320                                           'homeless': True},)
[511]1321        member = membership.getMemberById(sid)
1322        self.portal_registration.afterAdd(member, sid, password, None)
1323        self.manage_setLocalRoles(sid, ['Owner',])
1324
1325###)
1326
[764]1327    security.declareProtected(View,'createSubObjects') ###(
1328    def createSubObjects(self):
1329        """make the student a member"""
1330        dp = {'Title': 'Personal Data'}
1331        app_doc = self.application.getContent()
1332        names = app_doc.jamb_lastname.split()
1333        if len(names) == 3:
1334            dp['firstname'] = names[0].capitalize()
1335            dp['middlename'] = names[1].capitalize()
1336            dp['lastname'] = names[2].capitalize()
1337        elif len(names) == 2:
1338            dp['firstname'] = names[0].capitalize()
1339            dp['lastname'] = names[1].capitalize()
1340        else:
1341            dp['lastname'] = app_doc.jamb_lastname
1342        dp['sex'] = app_doc.jamb_sex == 'F'
1343        dp['lga'] = "%s/%s" % (app_doc.jamb_state,app_doc.jamb_lga )
1344        proxy = self.aq_parent
1345        proxy.invokeFactory('StudentPersonal','personal')
1346        per = proxy.personal
1347        per_doc = per.getContent()
1348        per_doc.edit(mapping = dp)
[927]1349        per.manage_setLocalRoles(proxy.getId(), ['Owner',])
[764]1350        #self.portal_workflow.doActionFor(per,'open',dest_container=per)
[603]1351
[511]1352###)
1353
[57]1354InitializeClass(Student)
1355
1356def addStudent(container, id, REQUEST=None, **kw):
1357    """Add a Student."""
1358    ob = Student(id, **kw)
1359    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1360
1361###)
[91]1362
[639]1363class StudentAccommodation(CPSDocument): ###(
1364    """
1365    WAeUP Student container for the various student data
1366    """
1367    meta_type = 'StudentAccommodation'
1368    portal_type = meta_type
1369    security = ClassSecurityInfo()
1370
1371    security.declareProtected(View,"Title")
1372    def Title(self):
1373        """compose title"""
1374        content = self.getContent()
1375        #return "Accommodation Data for %s %s" % (content.firstname,content.lastname)
1376        return "Accommodation Data for Session %s" % content.session
1377
1378
1379InitializeClass(StudentAccommodation)
1380
1381def addStudentAccommodation(container, id, REQUEST=None, **kw):
1382    """Add a Students personal data."""
1383    ob = StudentAccommodation(id, **kw)
1384    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1385
1386###)
1387
[89]1388class StudentPersonal(CPSDocument): ###(
1389    """
[154]1390    WAeUP Student container for the various student data
[89]1391    """
1392    meta_type = 'StudentPersonal'
1393    portal_type = meta_type
1394    security = ClassSecurityInfo()
[152]1395
1396    security.declareProtected(View,"Title")
1397    def Title(self):
1398        """compose title"""
1399        content = self.getContent()
[364]1400        #return "Personal Data for %s %s" % (content.firstname,content.lastname)
1401        return "Personal Data"
[152]1402
[154]1403
[89]1404InitializeClass(StudentPersonal)
1405
1406def addStudentPersonal(container, id, REQUEST=None, **kw):
1407    """Add a Students personal data."""
1408    ob = StudentPersonal(id, **kw)
1409    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1410
1411###)
1412
[423]1413class StudentClearance(CPSDocument): ###(
1414    """
1415    WAeUP Student container for the various student data
1416    """
1417    meta_type = 'StudentClearance'
1418    portal_type = meta_type
1419    security = ClassSecurityInfo()
1420
1421    security.declareProtected(View,"Title")
1422    def Title(self):
1423        """compose title"""
1424        content = self.getContent()
[840]1425        #return "Clearance/Eligibility Record for %s %s" % (content.firstname,content.lastname)
1426        return "Clearance/Eligibility Record"
[423]1427
1428
1429InitializeClass(StudentClearance)
1430
1431def addStudentClearance(container, id, REQUEST=None, **kw):
1432    """Add a Students personal data."""
1433    ob = StudentClearance(id, **kw)
1434    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1435
1436###)
1437
[454]1438class StudentStudyLevel(CPSDocument): ###(
1439    """
1440    WAeUP Student container for the various student data
1441    """
1442    meta_type = 'StudentStudyLevel'
1443    portal_type = meta_type
1444    security = ClassSecurityInfo()
1445
1446    security.declareProtected(View,"Title")
1447    def Title(self):
1448        """compose title"""
1449        return "Level %s" % self.aq_parent.getId()
1450
[723]1451##    security.declarePublic("gpa")
1452##    def gpa(self):
1453##        """calculate the gpa"""
1454##        sum = 0
1455##        course_count = 0
1456##        for sc in self.objectValues():
1457##            result = sc.getContent()
1458##            if not result.grade:
1459##                continue
1460##            res = self.portal_catalog({'meta_type': 'Course',
1461##                                          'id': sc.aq_parent.id})
1462##            if len(res) != 1:
1463##                continue
1464##            course = res[0].getObject().getContent()
1465##            sum += course.credits * ['F','E','D','C','B','A'].index(result.grade)
1466##            course_count += 1
1467##        if course_count:
1468##            return sum/course_count
1469##        return 0.0
[472]1470
[454]1471InitializeClass(StudentStudyLevel)
1472
1473def addStudentStudyLevel(container, id, REQUEST=None, **kw):
1474    """Add a Students personal data."""
1475    ob = StudentStudyLevel(id, **kw)
1476    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1477
1478###)
1479
[362]1480class StudentStudyCourse(CPSDocument): ###(
1481    """
1482    WAeUP Student container for the various student data
1483    """
1484    meta_type = 'StudentStudyCourse'
1485    portal_type = meta_type
1486    security = ClassSecurityInfo()
1487
[364]1488    security.declareProtected(View,"Title")
1489    def Title(self):
1490        """compose title"""
1491        content = self.getContent()
[453]1492        return "Study Course"
[362]1493
1494
1495InitializeClass(StudentStudyCourse)
1496
1497def addStudentStudyCourse(container, id, REQUEST=None, **kw):
1498    """Add a Students personal data."""
1499    ob = StudentStudyCourse(id, **kw)
1500    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1501
1502###)
1503
[472]1504class StudentApplication(CPSDocument): ###(
[179]1505    """
1506    WAeUP Student container for the various student data
1507    """
[472]1508    meta_type = 'StudentApplication'
[179]1509    portal_type = meta_type
1510    security = ClassSecurityInfo()
1511
[181]1512    security.declareProtected(View,"Title")
1513    def Title(self):
1514        """compose title"""
[472]1515        return "Application Data"
[179]1516
[181]1517
[472]1518InitializeClass(StudentApplication)
[179]1519
[472]1520def addStudentApplication(container, id, REQUEST=None, **kw):
[179]1521    """Add a Students eligibility data."""
[472]1522    ob = StudentApplication(id, **kw)
[179]1523    return CPSBase_adder(container, ob, REQUEST=REQUEST)
[763]1524###)
[179]1525
[758]1526class StudentPume(CPSDocument): ###(
1527    """
1528    WAeUP Student container for the various student data
1529    """
1530    meta_type = 'StudentPume'
1531    portal_type = meta_type
1532    security = ClassSecurityInfo()
1533
1534    security.declareProtected(View,"Title")
1535    def Title(self):
1536        """compose title"""
1537        return "PUME Results"
1538
1539
1540InitializeClass(StudentPume)
1541
1542def addStudentPume(container, id, REQUEST=None, **kw):
1543    """Add a Students PUME data."""
1544    ob = StudentPume(id, **kw)
1545    return CPSBase_adder(container, ob, REQUEST=REQUEST)
[179]1546###)
[181]1547
[565]1548##class StudentSemester(CPSDocument): ###(
1549##    """
1550##    WAeUP StudentSemester containing the courses and students
1551##    """
1552##    meta_type = 'StudentSemester'
1553##    portal_type = meta_type
1554##    security = ClassSecurityInfo()
1555##
1556##InitializeClass(StudentSemester)
1557##
1558##def addStudentSemester(container, id, REQUEST=None, **kw):
1559##    """Add a StudentSemester."""
1560##    ob = StudentSemester(id, **kw)
1561##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1562##
1563#####)
[464]1564
[758]1565##class Semester(CPSDocument): ###(
1566##    """
1567##    WAeUP Semester containing the courses and students
1568##    """
1569##    meta_type = 'Semester'
1570##    portal_type = meta_type
1571##    security = ClassSecurityInfo()
1572##
1573##InitializeClass(Semester)
1574##
1575##def addSemester(container, id, REQUEST=None, **kw):
1576##    """Add a Semester."""
1577##    ob = Semester(id, **kw)
1578##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
1579##
1580#####)
[464]1581
1582class StudentCourseResult(CPSDocument): ###(
[89]1583    """
[464]1584    WAeUP StudentCourseResult
[89]1585    """
[464]1586    meta_type = 'StudentCourseResult'
[89]1587    portal_type = meta_type
1588    security = ClassSecurityInfo()
[472]1589
[454]1590    def getCourseEntry(self,cid):
[723]1591        res = self.portal_catalog({'meta_type': "Course",
[454]1592                                           'id': cid})
1593        if res:
1594            return res[-1]
1595        else:
1596            return None
[154]1597
[454]1598    security.declareProtected(View,"Title")
1599    def Title(self):
1600        """compose title"""
[723]1601        cid = self.aq_parent.getId()
[454]1602        ce = self.getCourseEntry(cid)
1603        if ce:
1604            return "%s" % ce.Title
1605        return "No course with id %s" % cid
[152]1606
[464]1607InitializeClass(StudentCourseResult)
[454]1608
[464]1609def addStudentCourseResult(container, id, REQUEST=None, **kw):
1610    """Add a StudentCourseResult."""
1611    ob = StudentCourseResult(id, **kw)
[89]1612    return CPSBase_adder(container, ob, REQUEST=REQUEST)
[139]1613###)
1614
[579]1615# Backward Compatibility StudyLevel
1616
1617from Products.WAeUP_SRP.Academics import StudyLevel
1618
1619from Products.WAeUP_SRP.Academics import addStudyLevel
1620
Note: See TracBrowser for help on using the repository browser.