source: WAeUP_SRP/trunk/Students.py @ 742

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

added pay-hostel-maintainance fee

  • Property svn:keywords set to Id
File size: 43.0 KB
Line 
1#-*- mode: python; mode: fold -*-
2# $Id: Students.py 742 2006-10-24 16:32:10Z 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
18import logging
19import csv,re
20import Globals
21p_home = Globals.package_home(globals())
22i_home = Globals.INSTANCE_HOME
23MAX_TRANS = 1000
24
25def getInt(s):
26    try:
27        return int(s)
28    except:
29        return 0
30
31def getFloat(s):
32    try:
33        return float(s)
34    except:
35        return 0.0
36
37def getStudentByRegNo(self,reg_no): ###(
38    """search student by JAMB Reg No and return StudentFolder"""
39    search = ZCatalog.searchResults(self.portal_catalog,{'meta_type': 'StudentApplication',
40                                  'SearchableText': reg_no,
41                                  })
42    if len(search) < 1:
43        return None
44    return search[0].getObject().aq_parent
45
46###)
47
48class StudentsFolder(CPSDocument): ###(
49    """
50    WAeUP container for the various WAeUP containers data
51    """
52    meta_type = 'StudentsFolder'
53    portal_type = meta_type
54    security = ClassSecurityInfo()
55
56    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsFromCSV")###(
57    def loadFullTimeStudentsFromCSV(self):
58        """load Fulltime Studentdata from CSV values"""
59        import transaction
60        import random
61        tr_count = 0
62        name = 'short_full_time'
63        no_import = False
64        if not no_import:
65            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
66            no_import.write('"MatricNo","EntryRegNo","CurrentSession","StudentLevel","fullname","FirstName","MiddleName","Lastname","FormerSurname","Sex","Nationality","State","LGA","PermanentAddress","PermanentAddressCity","CampusAddress","PhoneNumber","Emailaddress","Mode","CourseMajor","Faculty","Dept"\n')
67        logger = logging.getLogger('%s_import' % name)
68        logger.info('Start loading from %s.csv' % name)
69        pwlist  = []
70        pwlist.append('"student_id","firstname","middlename","lastname","matric_no","jamb_reg_no","access_code"')
71        pwl_template = Template('"$student_id","$firstname","$middlename","$lastname","$matric_no","$jamb_reg_no","$access_code"')
72        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
73        try:
74            students = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
75        except:
76            logger.error('Error reading %s.csv' % name)
77            return
78        l = self.portal_catalog({'meta_type': "StudentClearance",})
79        matrics = []
80        for s in l:
81            matrics.append(s.getObject().getContent().matric_no)
82        print matrics
83        l = self.portal_catalog({'meta_type': "Certificate"})
84        certs = {}
85        for c in l:
86            ca,ac,fa,dep_id,co,certcode = c.relative_path.split('/')
87            cid = "%(dep_id)s_%(certcode)s" % vars()
88            certs[cid] = c.getObject()
89        for student in students:
90            logger.info('processing "%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
91            sid = student.get('MatricNo')
92            if sid == "":
93                em = 'Empty MatricNo\n'
94                logger.info(em)
95                no_import.write(em)
96                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
97                continue
98            certcode = makeCertificateCode(student.get('CourseMajor'))
99            dep_id = student.get('Dept')
100            fac_id = student.get('Faculty')
101            cid = "%(dep_id)s_%(certcode)s" % vars()
102            if cid not in certs.keys():
103                em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
104                logger.info(em)
105                no_import.write(em)
106                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
107                continue
108            certificate_doc = certs[cid].getContent()
109            level = student.get('StudentLevel')
110            try:
111                int(level)
112            except:
113                em = 'Student with ID %(MatricNo)s StudentLevel is empty\n' % student
114                logger.info(em)
115                no_import.write(em)
116                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
117                continue
118            matric_no = student.get('MatricNo')
119            if matric_no not in matrics:
120                matrics.append(matric_no)
121                sid = self.generateStudentId(student.get('Lastname')[0])
122                #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
123                students_folder.invokeFactory('Student', sid)
124                logger.info('%(tr_count)s: Creating Student with ID %(sid)s Matric_no %(matric_no)s ' % vars())
125                student_obj = getattr(self,sid)
126                access_code = "%d" % random.randint(1000000000,9999999999)
127                student_obj.getContent().addMember(sid,access_code ,)
128                pwl_dict = {'student_id': sid,'access_code':access_code}
129                student_obj.invokeFactory('StudentApplication','application')
130                application = student_obj.application
131                da = {'Title': 'Application Data'}
132                student_obj.invokeFactory('StudentPersonal','personal')
133                da['jamb_reg_no'] = student.get('EntryRegNo')
134                personal = student_obj.personal
135                dp = {'Title': 'Personal Data'}
136                student_obj.invokeFactory('StudentClearance','clearance')
137                clearance = student_obj.clearance
138                dc = {'Title': 'Clearance Data'}
139                dc['matric_no'] = matric_no
140                state = student.get('State')
141                lga = student.get('LGA')
142                if state and lga:
143                    lga =  state + ' / ' + lga
144                else:
145                    lga = "None"
146                dc['lga'] = lga
147                dc['nationality'] = student.get('Nationality')
148                dc['email'] = student.get('Emailaddress')
149                dp['firstname'] = student.get('FirstName')
150                dp['middlename'] = student.get('MiddleName')
151                dp['lastname'] = student.get('Lastname')
152                dp['former_surname'] = student.get('FormerSurname')
153                dp['sex'] = student.get('Sex') == 'F'
154                dp['perm_address'] = student.get('PermanentAddress')
155                dp['perm_city'] = student.get('PermanentAddressCity')
156                dp['campus_address'] = student.get('CampusAddress')
157                dp['phone'] = student.get('PhoneNumber')
158                application.getContent().edit(mapping=da)
159                personal.getContent().edit(mapping=dp)
160                clearance.getContent().edit(mapping=dc)
161                #
162                # Study Course
163                #
164                student_obj.invokeFactory('StudentStudyCourse','study_course')
165                studycourse = student_obj.study_course
166                dsc = {}
167                from_certificate = ['title',
168                                   'max_elect',
169                                   'max_pass',
170                                   'n_core',
171                                   'nr_years',
172                                   'probation_credits',
173                                   'promotion_credits',
174                                   'start_level',
175                                  ]
176                for f in from_certificate:
177                    dsc[f] = getattr(certificate_doc,f)
178                dsc['faculty'] = fac_id
179                dsc['department'] = dep_id
180                dsc['study_course'] = certcode
181                css = student.get('CurrentSession') or '2004-2005'
182                cs = int(css.split('-')[0]) - 2000
183                cl = int(student.get('StudentLevel') or '100')/100
184                dsc['entry_session'] = "200%s" % (cs - cl)
185                dsc['clr_ac_pin'] = access_code
186                studycourse.getContent().edit(mapping=dsc)
187                #
188                # Level
189                #
190##                l = getattr(studycourse,level,None)
191##                if 0 and l is None:
192##                    #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
193##                    logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student)
194##                    studycourse.invokeFactory('StudentStudyLevel', level)
195##                    l = getattr(studycourse, level)
196##                    certificate = certs[certcode]
197##                    cert_level = getattr(certificate,level,None)
198##                    if cert_level is None:
199##                        logger.info('Level %(level)s not in %(certcode)s' % vars())
200##                    l.getContent().edit(mapping={'Title': "Level %s" % level})
201            else:
202                em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
203                logger.info(em)
204                no_import.write(em)
205                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
206                continue
207            if tr_count > MAX_TRANS:
208                transaction.commit()
209                em = 'Transaction commited\n' % student
210                logger.info(em)
211                tr_count = 0
212            tr_count += 1
213            pwl_dict.update(dc)
214            pwl_dict.update(da)
215            pwl_dict.update(dp)
216            wftool = self.portal_workflow
217            pwlist.append(pwl_template.substitute(pwl_dict))
218            wftool.doActionFor(student_obj,'clear_and_validate')
219            student_obj.manage_setLocalRoles(sid, ['Owner',])
220            wftool.doActionFor(application,'close')
221            application.manage_setLocalRoles(sid, ['Owner',])
222            wftool.doActionFor(clearance,'close')
223            clearance.manage_setLocalRoles(sid, ['Owner',])
224            wftool.doActionFor(personal,'close')
225            personal.manage_setLocalRoles(sid, ['Owner',])
226            wftool.doActionFor(studycourse,'close_for_edit')
227            studycourse.manage_setLocalRoles(sid, ['Owner',])
228        open("%s/import/pwlist-%s.csv" % (i_home,name),"w+").write('\n'.join(pwlist))
229        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
230    ###)
231
232    security.declareProtected(ModifyPortalContent,"loadPumeResultsFromCSV")###(
233    def loadPumeResultsFromCSV(self):
234        """load Fulltime Studentdata from CSV values"""
235        import transaction
236        import random
237        tr_count = 0
238        name = 'pume_results'
239        no_import = False
240        if not no_import:
241            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
242            no_import.write('"MatricNo","EntryRegNo","CurrentSession","StudentLevel","fullname","FirstName","MiddleName","Lastname","FormerSurname","Sex","Nationality","State","LGA","PermanentAddress","PermanentAddressCity","CampusAddress","PhoneNumber","Emailaddress","Mode","CourseMajor","Faculty","Dept"\n')
243        logger = logging.getLogger('%s_import' % name)
244        logger.info('Start loading from %s.csv' % name)
245        pwlist  = []
246        pwlist.append('"student_id","firstname","middlename","lastname","jamb_reg_no","access_code"')
247        pwl_template = Template('"$student_id","$firstname","$middlename","$lastname","$jamb_reg_no","$access_code"')
248        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
249        try:
250            students = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
251        except:
252            logger.error('Error reading %s.csv' % name)
253            return
254        l = self.portal_catalog({'meta_type': "StudentClearance",})
255        matrics = []
256        for s in l:
257            matrics.append(s.getObject().getContent().matric_no)
258        print matrics
259        l = self.portal_catalog({'meta_type': "Certificate"})
260        certs = {}
261        for c in l:
262            ca,ac,fa,dep_id,co,certcode = c.relative_path.split('/')
263            cid = "%(dep_id)s_%(certcode)s" % vars()
264            certs[cid] = c.getObject()
265        for student in students:
266            logger.info('processing "%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
267            sid = student.get('MatricNo')
268            if sid == "":
269                em = 'Empty MatricNo\n'
270                logger.info(em)
271                no_import.write(em)
272                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
273                continue
274            certcode = makeCertificateCode(student.get('CourseMajor'))
275            dep_id = student.get('Dept')
276            fac_id = student.get('Faculty')
277            cid = "%(dep_id)s_%(certcode)s" % vars()
278            if cid not in certs.keys():
279                em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
280                logger.info(em)
281                no_import.write(em)
282                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
283                continue
284            certificate_doc = certs[cid].getContent()
285            level = student.get('StudentLevel')
286            try:
287                int(level)
288            except:
289                em = 'Student with ID %(MatricNo)s StudentLevel is empty\n' % student
290                logger.info(em)
291                no_import.write(em)
292                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
293                continue
294            matric_no = student.get('MatricNo')
295            if matric_no not in matrics:
296                matrics.append(matric_no)
297                sid = self.generateStudentId(student.get('Lastname')[0])
298                #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
299                students_folder.invokeFactory('Student', sid)
300                logger.info('%(tr_count)s: Creating Student with ID %(sid)s Matric_no %(matric_no)s ' % vars())
301                student_obj = getattr(self,sid)
302                access_code = "%d" % random.randint(1000000000,9999999999)
303                student_obj.getContent().addMember(sid,access_code ,)
304                pwl_dict = {'student_id': sid,'access_code':access_code}
305                student_obj.invokeFactory('StudentApplication','application')
306                application = student_obj.application
307                da = {'Title': 'Application Data'}
308                student_obj.invokeFactory('StudentPersonal','personal')
309                da['jamb_reg_no'] = student.get('EntryRegNo')
310                personal = student_obj.personal
311                dp = {'Title': 'Personal Data'}
312                student_obj.invokeFactory('StudentClearance','clearance')
313                clearance = student_obj.clearance
314                dc = {'Title': 'Clearance Data'}
315                dc['matric_no'] = matric_no
316                state = student.get('State')
317                lga = student.get('LGA')
318                if state and lga:
319                    lga =  state + ' / ' + lga
320                else:
321                    lga = "None"
322                dc['lga'] = lga
323                dc['nationality'] = student.get('Nationality')
324                dc['email'] = student.get('Emailaddress')
325                dp['firstname'] = student.get('FirstName')
326                dp['middlename'] = student.get('MiddleName')
327                dp['lastname'] = student.get('Lastname')
328                dp['former_surname'] = student.get('FormerSurname')
329                dp['sex'] = student.get('Sex') == 'F'
330                dp['perm_address'] = student.get('PermanentAddress')
331                dp['perm_city'] = student.get('PermanentAddressCity')
332                dp['campus_address'] = student.get('CampusAddress')
333                dp['phone'] = student.get('PhoneNumber')
334                application.getContent().edit(mapping=da)
335                personal.getContent().edit(mapping=dp)
336                clearance.getContent().edit(mapping=dc)
337                #
338                # Study Course
339                #
340                student_obj.invokeFactory('StudentStudyCourse','study_course')
341                studycourse = student_obj.study_course
342                dsc = {}
343                from_certificate = ['title',
344                                   'max_elect',
345                                   'max_pass',
346                                   'n_core',
347                                   'nr_years',
348                                   'probation_credits',
349                                   'promotion_credits',
350                                   'start_level',
351                                  ]
352                for f in from_certificate:
353                    dsc[f] = getattr(certificate_doc,f)
354                dsc['faculty'] = fac_id
355                dsc['department'] = dep_id
356                dsc['study_course'] = certcode
357                css = student.get('CurrentSession') or '2004-2005'
358                cs = int(css.split('-')[0]) - 2000
359                cl = int(student.get('StudentLevel') or '100')/100
360                dsc['entry_session'] = "200%s" % (cs - cl)
361                dsc['clr_ac_pin'] = access_code
362                studycourse.getContent().edit(mapping=dsc)
363                #
364                # Level
365                #
366##                l = getattr(studycourse,level,None)
367##                if 0 and l is None:
368##                    #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
369##                    logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student)
370##                    studycourse.invokeFactory('StudentStudyLevel', level)
371##                    l = getattr(studycourse, level)
372##                    certificate = certs[certcode]
373##                    cert_level = getattr(certificate,level,None)
374##                    if cert_level is None:
375##                        logger.info('Level %(level)s not in %(certcode)s' % vars())
376##                    l.getContent().edit(mapping={'Title': "Level %s" % level})
377            else:
378                em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
379                logger.info(em)
380                no_import.write(em)
381                no_import.write('"%(MatricNo)s","%(EntryRegNo)s","%(CurrentSession)s","%(StudentLevel)s","%(fullname)s","%(FirstName)s","%(MiddleName)s","%(Lastname)s","%(FormerSurname)s","%(Sex)s","%(Nationality)s","%(State)s","%(LGA)s","%(PermanentAddress)s","%(PermanentAddressCity)s","%(CampusAddress)s","%(PhoneNumber)s","%(Emailaddress)s","%(Mode)s","%(CourseMajor)s","%(Faculty)s","%(Dept)s"\n' % student)
382                continue
383            if tr_count > MAX_TRANS:
384                transaction.commit()
385                em = 'Transaction commited\n' % student
386                logger.info(em)
387                tr_count = 0
388            tr_count += 1
389            pwl_dict.update(dc)
390            pwl_dict.update(da)
391            pwl_dict.update(dp)
392            wftool = self.portal_workflow
393            pwlist.append(pwl_template.substitute(pwl_dict))
394            wftool.doActionFor(student_obj,'clear_and_validate')
395            student_obj.manage_setLocalRoles(sid, ['Owner',])
396            wftool.doActionFor(application,'close')
397            application.manage_setLocalRoles(sid, ['Owner',])
398            wftool.doActionFor(clearance,'close')
399            clearance.manage_setLocalRoles(sid, ['Owner',])
400            wftool.doActionFor(personal,'close')
401            personal.manage_setLocalRoles(sid, ['Owner',])
402            wftool.doActionFor(studycourse,'close_for_edit')
403            studycourse.manage_setLocalRoles(sid, ['Owner',])
404        open("%s/import/pwlist-%s.csv" % (i_home,name),"w+").write('\n'.join(pwlist))
405        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
406    ###)
407
408    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
409    def loadFullTimeStudentsResultsFromCSV(self):
410        """load Fulltime Studentdata from CSV values"""
411        #return
412        level_wf_actions = {}
413        level_wf_actions["SUCCESSFUL STUDENT"] = "pass_A"
414        level_wf_actions["STUDENT WITH CARRYOVER COURSES"] = "pass_B"
415        level_wf_actions["STUDENT FOR PROBATION"] = "probate_C"
416        level_wf_actions["STUDENT ON PROBATION/TRANSFER"] = "reject_D"
417        import transaction
418        wftool = self.portal_workflow
419        tr_count = 0
420        name = 'short_full_time_results_2004_2005'
421        no_import = False
422        if not no_import:
423            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
424            no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
425        logger = logging.getLogger('%s_import' % name)
426        logger.info('Start loading from %s.csv' % name)
427        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
428        try:
429            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
430        except:
431            logger.error('Error reading %s.csv' % name)
432            return
433        l = self.portal_catalog({'meta_type': "Course"})
434        courses = {}
435        for c in l:
436            courses[c.id] = c.getObject()
437        level_changed = False
438        student_changed = False
439        sid = ''
440        #import pdb;pdb.set_trace()
441        for result in results:
442            temp_sid = result.get('Matnumber')
443            if temp_sid != sid:
444                student_changed = True
445                res = self.portal_catalog({'meta_type': "StudentClearance",
446                                         'SearchableText': temp_sid })
447                if not res:
448                    em = 'Student with ID %(Matnumber)s not found\n' % result
449                    logger.info(em)
450                    no_import.write(em)
451                    no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
452                    continue
453                elif len(res) > 1:
454                    em = 'More than one Student with ID %(Matnumber)s found\n' % result
455                    logger.info(em)
456                    no_import.write(em)
457                    no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
458                    continue
459                sid = temp_sid
460                sf = res[0].getObject().aq_parent
461                sc = getattr(sf,'study_course')
462                level = ''
463            else:
464                student_changed = False
465            course = result.get('CosCode')
466            if course not in courses.keys():
467                em = 'Course with ID %(CosCode)s not found\n' % result
468                logger.info(em)
469                no_import.write(em)
470                no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
471                continue
472            course_doc = courses[course].getContent()
473            temp_level = result.get('Level')
474            student_id = sf.getId()
475            result['StudentId'] = student_id
476            if temp_level != level:
477                try:
478                    int(temp_level)
479                except:
480                    em = 'Result with ID %(Matnumber)s Course %(CosCode)s Level is empty\n' % result
481                    logger.info(em)
482                    no_import.write(em)
483                    no_import.write('"%(Matnumber)s","%(CosCode)s","%(Ansbook)s","%(CosStuatus)s","%(Session)s","%(Mat_Cos)s","%(Score)s","%(CarryLevel)s","%(Grade)s","%(Weight)s","%(Semster)s","%(Verdict)s","%(Level)s","%(id)s","%(GPA)s"\n' % result)
484                    continue
485                level_changed = True
486                if 'dlev' in vars().keys():
487                    wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
488                level = temp_level
489                l = getattr(sc,level,None)
490                if l is None:
491                    logger.info('Creating Level %(Level)s for %(StudentId)s %(Matnumber)s' % result)
492                    sc.invokeFactory('StudentStudyLevel', level)
493                    l = getattr(sc, level)
494                    l.manage_setLocalRoles(student_id, ['Owner',])
495            else:
496                level_changed = False
497            cr = getattr(l,course,None)
498            if cr is None:
499                logger.info('Creating Course %(CosCode)s for %(StudentId)s %(Matnumber)s in Level %(Level)s' % result)
500                l.invokeFactory('StudentCourseResult',course)
501            cr = getattr(l,course)
502            dcr = {}
503            from_course = ['title',
504                           'credits',
505                           'passmark',
506                           ]
507            for f in from_course:
508                dcr[f] = getattr(course_doc,f)
509            dlev = {}
510            dcr['ansbook'] = result.get('Ansbook')
511            dcr['semester'] = getInt(result.get('Semster'))
512            dcr['status'] = result.get('CosStuatus')
513            dcr['score'] = getInt(result.get('Score'))
514            dlev['session'] = result.get('Session')
515            dcr['carry_level'] = result.get('CarryLevel')
516            dcr['grade'] = result.get('Grade')
517            dcr['weight'] = result.get('Weight')
518            dlev['verdict'] = result.get('Verdict')
519            dcr['import_id'] = result.get('id')
520            gpa = result.get('GPA').replace(',','.')
521            dlev['imported_gpa'] = getFloat(gpa)
522            cr.getContent().edit(mapping = dcr)
523            cr.manage_setLocalRoles(student_id, ['Owner',])
524            l.getContent().edit(mapping = dlev)
525            if tr_count > MAX_TRANS:
526                transaction.commit()
527                tr_count = 0
528            tr_count += 1
529            wftool.doActionFor(cr,'close')
530        wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
531        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
532
533###)
534
535    security.declareProtected(ModifyPortalContent,"loadJAMBFromCSV")###(
536    def loadJAMBFromCSV(self):
537        """load JAMB data from CSV values"""
538        #return
539        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
540        import transaction
541        tr_count = 0
542        name = 'SampleJAMBDataII'
543        wftool = self.portal_workflow
544        no_import = False
545        if not no_import:
546            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
547            no_import.write('REG-NO,NAME,SEX,STATE,LGA,ENG-SCORE,SUBJ1,SUBJ1-SCORE,SUBJ2,SUBJ2-SCORE,SUBJ3,SUBJ3-SCORE,AGGREGATE,UNIV1,FACULTY1,COURSE1,UNIV2,FACULTY2,COURSE2')
548        logger = logging.getLogger('%s_import' % name)
549        logger.info('Start loading from %s.csv' % name)
550        try:
551            result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
552        except:
553            logger.error('Error reading %s.csv' % name)
554            return
555        for jamb in result:
556            logger.info('processing %(REG-NO)s,%(NAME)s,%(SEX)s,%(STATE)s,%(LGA)s,%(ENG-SCORE)s,%(SUBJ1)s,%(SUBJ1-SCORE)s,%(SUBJ2)s,%(SUBJ2-SCORE)s,%(SUBJ3)s,%(SUBJ3-SCORE)s,%(AGGREGATE)s,%(UNIV1)s,%(FACULTY1)s,%(COURSE1)s,%(UNIV2)s,%(FACULTY2)s,%(COURSE2)s\n' % jamb)
557            jamb_reg_no = jamb.get('REG-NO')
558            res = self.portal_catalog({'meta_type': "StudentApplication",
559                                     'jamb_reg_no': jamb_reg_no })
560            if res:
561                em = 'Student with REG-NO %(REG-NO)s already exists\n' % jamb
562                logger.info(em)
563                no_import.write(em)
564                no_import.write('%(REG-NO)s,%(NAME)s,%(SEX)s,%(STATE)s,%(LGA)s,%(ENG-SCORE)s,%(SUBJ1)s,%(SUBJ1-SCORE)s,%(SUBJ2)s,%(SUBJ2-SCORE)s,%(SUBJ3)s,%(SUBJ3-SCORE)s,%(AGGREGATE)s,%(UNIV1)s,%(FACULTY1)s,%(COURSE1)s,%(UNIV2)s,%(FACULTY2)s,%(COURSE2)s\n' % jamb)
565                continue
566            jamb_name = jamb.get("NAME")
567            jamb_name.replace('>','')
568            names = jamb_name.split()
569            letter = names[-1][0].upper()
570            sid = self.generateStudentId(letter)
571            not_created = True
572            while not_created:
573                try:
574                    students_folder.invokeFactory('Student', sid)
575                    not_created = False
576                except BadRequest:
577                    sid = self.generateStudentId(letter)
578            logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
579            student = getattr(self,sid)
580            student.manage_setLocalRoles(sid, ['Owner',])
581            student.invokeFactory('StudentApplication','application')
582            da = {'Title': 'Application Data'}
583            da["jamb_reg_no"] = jamb.get("REG-NO")
584            da["jamb_lastname"] = jamb_name
585            da["jamb_sex"] = jamb.get("SEX")
586            da["jamb_state"] = jamb.get("STATE")
587            da["jamb_lga"] = jamb.get("LGA")
588            da["jamb_score"] = jamb.get("AGGREGATE")
589            da["jamb_first_cos"] = jamb.get("COURSE1")
590            da["jamb_second_cos"] = jamb.get("COURSE2")
591            da["jamb_first_uni"] = jamb.get("UNIV1")
592            da["jamb_second_uni"] = jamb.get("UNIV2")
593            app = student.application
594            app_doc = app.getContent()
595            app_doc.edit(mapping=da)
596            #wftool.doActionFor(app,'open',dest_container=app)
597            app.manage_setLocalRoles(sid, ['Owner',])
598            student.getContent().createSubObjects()
599        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
600    ###)
601
602
603    security.declareProtected(View,"fixOwnership")
604    def fixOwnership(self):
605        """fix Ownership"""
606        for s in self.portal_catalog(meta_type = 'Student'):
607            student = s.getObject()
608            sid = s.getId
609            import pdb;pdb.set_trace()
610            student.application.manage_setLocalRoles(sid, ['Owner',])
611            student.personal.manage_setLocalRoles(sid, ['Owner',])
612
613    security.declareProtected(View,"Title")
614    def Title(self):
615        """compose title"""
616        return "Student Section"
617
618    def generateStudentId(self,letter): ###(
619        import random
620        r = random
621        if letter not in ('ABCDEFGIHKLMNOPQRSTUVWXY'):
622            letter= r.choice('ABCDEFGHKLMNPQRSTUVWXY')
623        students = self.portal_catalog(meta_type = "StudentsFolder")[-1]
624        sid = "%c%d" % (letter,r.randint(99999,1000000))
625        while hasattr(students, sid):
626            sid = "%c%d" % (letter,r.randint(99999,1000000))
627        return sid
628        #return "%c%d" % (r.choice('ABCDEFGHKLMNPQRSTUVWXY'),r.randint(99999,1000000))
629    ###)
630
631InitializeClass(StudentsFolder)
632
633def addStudentsFolder(container, id, REQUEST=None, **kw):
634    """Add a Student."""
635    ob = StudentsFolder(id, **kw)
636    return CPSBase_adder(container, ob, REQUEST=REQUEST)
637###)
638
639class Student(CPSDocument): ###(
640    """
641    WAeUP Student container for the various student data
642    """
643    meta_type = 'Student'
644    portal_type = meta_type
645    security = ClassSecurityInfo()
646
647    security.declareProtected(View,"Title")
648    def Title(self):
649        """compose title"""
650        reg_nr = self.getId()[1:]
651        data = getattr(self,'personal',None)
652        if data:
653            content = data.getContent()
654            return "%s %s" % (content.firstname,content.lastname)
655        data = getattr(self,'application',None)
656        if data:
657            content = data.getContent()
658            return "%s" % (content.jamb_lastname)
659        return self.title
660
661    security.declarePrivate('makeStudentMember') ###(
662    def makeStudentMember(self,sid,password='uNsEt'):
663        """make the student a member"""
664        membership = self.portal_membership
665        membership.addMember(sid,
666                             password ,
667                             roles=('Member',
668                                     'Student',
669                                     ),
670                             domains='',
671                             properties = {'memberareaCreationFlag': False,},)
672        member = membership.getMemberById(sid)
673        self.portal_registration.afterAdd(member, sid, password, None)
674        self.manage_setLocalRoles(sid, ['Owner',])
675
676###)
677
678    security.declareProtected(View,'createSubObjects') ###(
679    def createSubObjects(self):
680        """make the student a member"""
681        dp = {'Title': 'Personal Data'}
682        app_doc = self.application.getContent()
683        names = app_doc.jamb_lastname.split()
684        if len(names) == 3:
685            dp['firstname'] = names[0].capitalize()
686            dp['middlename'] = names[1].capitalize()
687            dp['lastname'] = names[2].capitalize()
688        elif len(names) == 2:
689            dp['firstname'] = names[0].capitalize()
690            dp['lastname'] = names[1].capitalize()
691        else:
692            dp['lastname'] = app_doc.jamb_lastname
693        dp['sex'] = app_doc.jamb_sex == 'F'
694        dp['lga'] = "%s/%s" % (app_doc.jamb_state,app_doc.jamb_lga )
695        proxy = self.aq_parent
696        proxy.invokeFactory('StudentPersonal','personal')
697        per = proxy.personal
698        per_doc = per.getContent()
699        per_doc.edit(mapping = dp)
700        per.manage_setLocalRoles(self.getId(), ['Owner',])
701        #self.portal_workflow.doActionFor(per,'open',dest_container=per)
702
703###)
704
705InitializeClass(Student)
706
707def addStudent(container, id, REQUEST=None, **kw):
708    """Add a Student."""
709    ob = Student(id, **kw)
710    return CPSBase_adder(container, ob, REQUEST=REQUEST)
711
712###)
713
714class StudentAccommodation(CPSDocument): ###(
715    """
716    WAeUP Student container for the various student data
717    """
718    meta_type = 'StudentAccommodation'
719    portal_type = meta_type
720    security = ClassSecurityInfo()
721
722    security.declareProtected(View,"Title")
723    def Title(self):
724        """compose title"""
725        content = self.getContent()
726        #return "Accommodation Data for %s %s" % (content.firstname,content.lastname)
727        return "Accommodation Data for Session %s" % content.session
728
729
730InitializeClass(StudentAccommodation)
731
732def addStudentAccommodation(container, id, REQUEST=None, **kw):
733    """Add a Students personal data."""
734    ob = StudentAccommodation(id, **kw)
735    return CPSBase_adder(container, ob, REQUEST=REQUEST)
736
737###)
738
739class StudentPersonal(CPSDocument): ###(
740    """
741    WAeUP Student container for the various student data
742    """
743    meta_type = 'StudentPersonal'
744    portal_type = meta_type
745    security = ClassSecurityInfo()
746
747    security.declareProtected(View,"Title")
748    def Title(self):
749        """compose title"""
750        content = self.getContent()
751        #return "Personal Data for %s %s" % (content.firstname,content.lastname)
752        return "Personal Data"
753
754
755InitializeClass(StudentPersonal)
756
757def addStudentPersonal(container, id, REQUEST=None, **kw):
758    """Add a Students personal data."""
759    ob = StudentPersonal(id, **kw)
760    return CPSBase_adder(container, ob, REQUEST=REQUEST)
761
762###)
763
764class StudentClearance(CPSDocument): ###(
765    """
766    WAeUP Student container for the various student data
767    """
768    meta_type = 'StudentClearance'
769    portal_type = meta_type
770    security = ClassSecurityInfo()
771
772    security.declareProtected(View,"Title")
773    def Title(self):
774        """compose title"""
775        content = self.getContent()
776        #return "Clearance Data for %s %s" % (content.firstname,content.lastname)
777        return "Clearance Data"
778
779
780InitializeClass(StudentClearance)
781
782def addStudentClearance(container, id, REQUEST=None, **kw):
783    """Add a Students personal data."""
784    ob = StudentClearance(id, **kw)
785    return CPSBase_adder(container, ob, REQUEST=REQUEST)
786
787###)
788
789class StudentStudyLevel(CPSDocument): ###(
790    """
791    WAeUP Student container for the various student data
792    """
793    meta_type = 'StudentStudyLevel'
794    portal_type = meta_type
795    security = ClassSecurityInfo()
796
797    security.declareProtected(View,"Title")
798    def Title(self):
799        """compose title"""
800        return "Level %s" % self.aq_parent.getId()
801
802##    security.declarePublic("gpa")
803##    def gpa(self):
804##        """calculate the gpa"""
805##        sum = 0
806##        course_count = 0
807##        for sc in self.objectValues():
808##            result = sc.getContent()
809##            if not result.grade:
810##                continue
811##            res = self.portal_catalog({'meta_type': 'Course',
812##                                          'id': sc.aq_parent.id})
813##            if len(res) != 1:
814##                continue
815##            course = res[0].getObject().getContent()
816##            sum += course.credits * ['F','E','D','C','B','A'].index(result.grade)
817##            course_count += 1
818##        if course_count:
819##            return sum/course_count
820##        return 0.0
821
822InitializeClass(StudentStudyLevel)
823
824def addStudentStudyLevel(container, id, REQUEST=None, **kw):
825    """Add a Students personal data."""
826    ob = StudentStudyLevel(id, **kw)
827    return CPSBase_adder(container, ob, REQUEST=REQUEST)
828
829###)
830
831class StudentStudyCourse(CPSDocument): ###(
832    """
833    WAeUP Student container for the various student data
834    """
835    meta_type = 'StudentStudyCourse'
836    portal_type = meta_type
837    security = ClassSecurityInfo()
838
839    security.declareProtected(View,"Title")
840    def Title(self):
841        """compose title"""
842        content = self.getContent()
843        return "Study Course"
844
845
846InitializeClass(StudentStudyCourse)
847
848def addStudentStudyCourse(container, id, REQUEST=None, **kw):
849    """Add a Students personal data."""
850    ob = StudentStudyCourse(id, **kw)
851    return CPSBase_adder(container, ob, REQUEST=REQUEST)
852
853###)
854
855class StudentApplication(CPSDocument): ###(
856    """
857    WAeUP Student container for the various student data
858    """
859    meta_type = 'StudentApplication'
860    portal_type = meta_type
861    security = ClassSecurityInfo()
862
863    security.declareProtected(View,"Title")
864    def Title(self):
865        """compose title"""
866        return "Application Data"
867
868
869InitializeClass(StudentApplication)
870
871def addStudentApplication(container, id, REQUEST=None, **kw):
872    """Add a Students eligibility data."""
873    ob = StudentApplication(id, **kw)
874    return CPSBase_adder(container, ob, REQUEST=REQUEST)
875
876###)
877
878##class StudentSemester(CPSDocument): ###(
879##    """
880##    WAeUP StudentSemester containing the courses and students
881##    """
882##    meta_type = 'StudentSemester'
883##    portal_type = meta_type
884##    security = ClassSecurityInfo()
885##
886##InitializeClass(StudentSemester)
887##
888##def addStudentSemester(container, id, REQUEST=None, **kw):
889##    """Add a StudentSemester."""
890##    ob = StudentSemester(id, **kw)
891##    return CPSBase_adder(container, ob, REQUEST=REQUEST)
892##
893#####)
894
895class Semester(CPSDocument): ###(
896    """
897    WAeUP Semester containing the courses and students
898    """
899    meta_type = 'Semester'
900    portal_type = meta_type
901    security = ClassSecurityInfo()
902
903InitializeClass(Semester)
904
905def addSemester(container, id, REQUEST=None, **kw):
906    """Add a Semester."""
907    ob = Semester(id, **kw)
908    return CPSBase_adder(container, ob, REQUEST=REQUEST)
909
910###)
911
912class StudentCourseResult(CPSDocument): ###(
913    """
914    WAeUP StudentCourseResult
915    """
916    meta_type = 'StudentCourseResult'
917    portal_type = meta_type
918    security = ClassSecurityInfo()
919
920    def getCourseEntry(self,cid):
921        res = self.portal_catalog({'meta_type': "Course",
922                                           'id': cid})
923        if res:
924            return res[-1]
925        else:
926            return None
927
928    security.declareProtected(View,"Title")
929    def Title(self):
930        """compose title"""
931        cid = self.aq_parent.getId()
932        ce = self.getCourseEntry(cid)
933        if ce:
934            return "%s" % ce.Title
935        return "No course with id %s" % cid
936
937InitializeClass(StudentCourseResult)
938
939def addStudentCourseResult(container, id, REQUEST=None, **kw):
940    """Add a StudentCourseResult."""
941    ob = StudentCourseResult(id, **kw)
942    return CPSBase_adder(container, ob, REQUEST=REQUEST)
943###)
944
945# Backward Compatibility StudyLevel
946
947from Products.WAeUP_SRP.Academics import StudyLevel
948
949from Products.WAeUP_SRP.Academics import addStudyLevel
950
Note: See TracBrowser for help on using the repository browser.