source: WAeUP_SRP/branches/uli/Students.py @ 17421

Last change on this file since 17421 was 1653, checked in by uli, 18 years ago

Incorporated changeset 1593:1652 of trunk into uli-branch.

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