source: WAeUP_SRP/trunk/Students.py @ 1864

Last change on this file since 1864 was 1848, checked in by joachim, 18 years ago

real catalog

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