source: WAeUP_SRP/trunk/Students.py @ 403

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

import of full_time_students_results

  • Property svn:keywords set to Id
File size: 21.8 KB
RevLine 
[57]1#-*- mode: python; mode: fold -*-
[200]2# $Id: Students.py 398 2006-08-21 14:29:27Z joachim $
[45]3from Globals import InitializeClass
4from AccessControl import ClassSecurityInfo
[164]5from AccessControl.SecurityManagement import newSecurityManager
[45]6
[47]7from Products.CMFCore.utils import UniqueObject, getToolByName
[45]8from Products.CMFCore.permissions import View
9from Products.CMFCore.permissions import ModifyPortalContent
[154]10from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder
11#from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument
12from Products.CPSDocument.CPSDocument import CPSDocument
13from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder
[164]14from Products.CPSCore.CPSMembershipTool import CPSUnrestrictedUser
[361]15from Products.WAeUP_SRP.Academics import makeCertificateCode
[362]16import logging
17import csv,re
18import Globals
19p_home = Globals.package_home(globals())
20i_home = Globals.INSTANCE_HOME
[398]21MAX_TRANS = 1000
[154]22
[361]23class StudentsFolder(CPSDocument): ###(
24    """
25    WAeUP container for the various WAeUP containers data
26    """
[362]27    meta_type = 'StudentsFolder'
[361]28    portal_type = meta_type
29    security = ClassSecurityInfo()
[154]30
[361]31    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsFromCSV")###(
32    def loadFullTimeStudentsFromCSV(self):
33        """load Fulltime Studentdata from CSV values"""
34        #return
[398]35        import transaction
36        tr_count = 0
[361]37        name = 'short_full_time'
38        no_import = False
[395]39        if not no_import:
40            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
41            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')
[361]42        logger = logging.getLogger('%s_import' % name)
43        logger.info('Start loading from %s.csv' % name)
44        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
45        try:
46            students = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
47        except:
48            logger.error('Error reading %s.csv' % name)
49            return
50        l = self.portal_catalog({'meta_type': "Student"})
51        studs = {}
52        for s in l:
53            studs[s.id] = s.getObject()
54        l = self.portal_catalog({'meta_type': "Certificate"})
55        certs = {}
[395]56        students_added = []
[361]57        for c in l:
[362]58            certs[c.id] = c.getObject()
[361]59        for student in students:
[393]60            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)
[361]61            sid = student.get('MatricNo')
[396]62            if sid == "":
63                em = 'Empty MatricNo\n'
64                logger.info(em)
65                no_import.write(em)
66                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)
67                continue
[361]68            certcode = makeCertificateCode(student.get('CourseMajor'))
[398]69##            if sid in students_added:
70##                em = 'Student with ID %(MatricNo)s %(fullname)s already exists in this import\n' % student
71##                logger.info(em)
72##                no_import.write(em)
73##                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)
74##                continue
[361]75            s = studs.get(sid,None)
76            if certcode not in certs.keys():
[393]77                em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
[361]78                logger.info(em)
[393]79                no_import.write(em)
80                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)
[361]81                continue
[395]82            level = student.get('StudentLevel')
83            try:
84                int(level)
85            except:
86                em = 'Student with ID %(MatricNo)s StudentLevel is empty\n' % student
87                logger.info(em)
88                no_import.write(em)
89                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)
90                continue
[361]91            if s is None:
92                #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
[362]93                logger.info('Creating Student with ID %(MatricNo)s %(fullname)s' % student)
[361]94                students_folder.invokeFactory('Student', sid)
95                s = getattr(self,sid)
[395]96                students_added.append(sid)
[398]97                studs[sid] = s
[361]98                s.invokeFactory('StudentPersonal','personal')
[362]99                sp = s.personal
[361]100                d = {'Title': 'Personal Data'}
[362]101                d['matricel_no'] = student.get('MatricNo')
102                d['entry_reg_no'] = student.get('EntryRegNo')
[361]103                d['fullname'] = student.get('fullname')
104                d['firstname'] = student.get('FirstName')
105                d['middlename'] = student.get('MiddleName')
[362]106                d['lastname'] = student.get('Lastname')
[361]107                d['former_surname'] = student.get('FormerSurname')
[362]108                d['sex'] = student.get('Sex') == 'F'
[361]109                d['nationality'] = student.get('Nationality')
[382]110                d['state'] = student.get('State')
111                d['lga'] = student.get('LGA')
[361]112                d['street'] = student.get('PermanentAddress')
113                d['city'] = student.get('PermanentAddressCity')
114                d['campus_address'] = student.get('CampusAddress')
115                d['phone'] = student.get('PhoneNumber')
[362]116                d['email'] = student.get('Emailaddress')
117                sp.getContent().edit(mapping=d)
118                #
119                # Study Course
120                #
121                s.invokeFactory('StudentStudyCourse','study_course')
122                sc = s.study_course
123                d = {}
124                d['matricel_no'] = student.get('MatricNo')
125                d['entry_reg_no'] = student.get('EntryRegNo')
126                d['faculty'] = student.get('Faculty')
127                d['department'] = student.get('Dept')
128                d['course_major'] = certcode
129                css = student.get('CurrentSession') or '2004-2005'
130                cs = int(css.split('-')[0]) - 2000
131                cl = int(student.get('StudentLevel'))/100
132                d['entry_session'] = "200%s" % (cs - cl)
133                sc.getContent().edit(mapping=d)
[364]134                #
135                # Level
136                #
137                l = getattr(sc,level,None)
138                if l is None:
139                    #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
140                    logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student)
141                    sc.invokeFactory('StudyLevel', level)
142                    l = getattr(sc, level)
143                    certificate = certs[certcode]
144                    cert_level = getattr(certificate,level,None)
145                    if cert_level is None:
146                        logger.info('Level %(level)s not in %(certcode)s' % vars())
147                    l.getContent().edit(mapping={'Title': "Level %s" % level})
148                    l.invokeFactory('Semester','first')
149                    l.invokeFactory('Semester','second')
150                    first_s = getattr(l,'first')
151                    first_s.getContent().edit(mapping={'Title': 'First Semester'})
152                    second_s = getattr(l,'second')
153                    second_s.getContent().edit(mapping={'Title': 'Second Semester'})
[361]154            else:
[393]155                em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
[361]156                logger.info(em)
[393]157                no_import.write(em)
158                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)
[398]159            if tr_count > MAX_TRANS:
160                #transaction.commit()
161                #em = '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Transaction commited\n' % student
162                logger.info(em)
163                tr_count = 0
164            tr_count += 1
[393]165        return self.students.academics_contents()
[361]166    ###)
[382]167
[398]168    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
[396]169    def loadFullTimeStudentsResultsFromCSV(self):
170        """load Fulltime Studentdata from CSV values"""
171        #return
[398]172        import transaction
173        tr_count = 0
174        name = 'short_full_time_results_2004_2005'
[396]175        no_import = False
176        if not no_import:
177            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
178            no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
179        logger = logging.getLogger('%s_import' % name)
180        logger.info('Start loading from %s.csv' % name)
181        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
182        try:
183            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
184        except:
185            logger.error('Error reading %s.csv' % name)
186            return
187        l = self.portal_catalog({'meta_type': "Student"})
188        students = {}
189        for s in l:
190            students[s.id] = s.getObject()
[398]191        l = self.portal_catalog({'meta_type': "Course"})
192        courses = {}
193        for c in l:
194            courses[c.id] = c.getObject()
[396]195        for result in results:
196            sid = result.get('Matnumber')
197            if sid not in students.keys():               
[398]198                em = 'Student with ID %(Matnumber)s not found\n' % result
[396]199                logger.info(em)
200                no_import.write(em)
[398]201                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)
[396]202                continue
[398]203            course = result.get('CosCode')
204            if course not in courses.keys():
205                em = 'Course with ID %(CosCode)s not found\n' % result
[396]206                logger.info(em)
207                no_import.write(em)
[398]208                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)
[396]209                continue
[398]210            level = result.get('Level')
[396]211            try:
212                int(level)
213            except:
[398]214                em = 'Result for result with ID %(Matnumber)s Course %(CosCode)s Level is empty\n' % result
[396]215                logger.info(em)
216                no_import.write(em)
[398]217                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)
[396]218                continue
219            sf = students.get(sid)
220            sc = getattr(sf,'study_course')
221            l = getattr(sc,level,None)
222            if l is None:
223                #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
[398]224                logger.info('Creating Level %(Level)s for %(Matnumber)s' % result)
[396]225                sc.invokeFactory('StudyLevel', level)
226                l = getattr(sc, level)
227                l.getContent().edit(mapping={'Title': "Level %s" % level})
228                l.invokeFactory('Semester','first')
229                l.invokeFactory('Semester','second')
230                first_s = getattr(l,'first')
231                first_s.getContent().edit(mapping={'Title': 'First Semester'})
232                second_s = getattr(l,'second')
233                second_s.getContent().edit(mapping={'Title': 'Second Semester'})
[398]234            snr = result.get('Semester')
[396]235            semester = getattr(l,'first')
[398]236            if snr == "2":
[396]237                semester = getattr(l,'second')
[398]238            logger.info('Creating CourseTicket %(CourseTicket)s in Level %(Level)s for %(Matnumber)s' % result)
239            semester.invokeFactory('CourseTicket',course)
240            ct = getattr(semester,course)
241            d = {}
242            d['ansbook'] = result.get('Ansbook')
243            d['status'] = result.get('CosStuatus')
244            d['score'] = result.get('Score')
245            d['carry_level'] = result.get('CarryLevel')
246            d['grade'] = result.get('Grade')
247            d['weight'] = result.get('Weight')
248            d['verdict'] = result.get('Verdict')
249            d['import_id'] = result.get('id')
250            gpa = result.get('GPA').replace(',','.')
251            d['gpa'] = float(gpa)
252            ct.getContent().edit(mapping = d)
253            if tr_count > MAX_TRANS:
254                transaction.commit()
255                tr_count = 0
256            tr_count += 1
257        return self.students.academics_contents()
[396]258           
[398]259###)
[396]260
[398]261           
[364]262    security.declareProtected(View,"Title")
263    def Title(self):
264        """compose title"""
[382]265        return "Student Section"
[361]266
267InitializeClass(StudentsFolder)
268
269def addStudentsFolder(container, id, REQUEST=None, **kw):
270    """Add a Student."""
271    ob = StudentsFolder(id, **kw)
272    return CPSBase_adder(container, ob, REQUEST=REQUEST)
273###)
274
[57]275class Student(CPSDocument): ###(
276    """
[154]277    WAeUP Student container for the various student data
[57]278    """
279    meta_type = 'Student'
280    portal_type = meta_type
281    security = ClassSecurityInfo()
[154]282
[152]283    security.declareProtected(View,"Title")
284    def Title(self):
285        """compose title"""
[153]286        reg_nr = self.getId()[1:]
[362]287        data = getattr(self,'personal',None)
[152]288        if data is None:
[362]289            data = getattr(self,'jamb',None)
[152]290        if data:
291            content = data.getContent()
292            return "%s %s" % (content.firstname,content.lastname)
293        return self.title
[154]294
295    def Description(self):
296        """compose description"""
[170]297        data = getattr(self,'PERSONAL',None)
298        if data is None:
[191]299            return "none"
[154]300        if data:
[191]301            content = data.getContent()
302            return "%s" % (content.description)
[154]303        return self.description
304
[164]305    security.declareProtected(View,"setScratchCardData")
306    def setScratchCardData(self,ident,ds):
307        """set this data """
308        dict = {'%s_sc_pin' % ident : ds.get('sc_pin'),
309                '%s_sc_id' % ident : ds.get('sc_id'),
310                '%s_sc_value' % ident : ds.get('sc_value'),
[166]311                '%s_date' % ident : ds.get('sc_date'),
[164]312                }
[170]313
[173]314        old_user = self.portal_membership.getAuthenticatedMember()
[166]315        if self.portal_membership.isAnonymousUser():
316            tmp_user = CPSUnrestrictedUser('s%(jamb_id)s' % ds, '',
[164]317                                       ['StudentManager'], '')
[166]318            tmp_user = tmp_user.__of__(self.acl_users)
319            newSecurityManager(None, tmp_user)
320        #print str(dict)
[164]321        self.edit(mapping=dict)
[173]322        newSecurityManager(None, old_user)
[164]323
324    security.declareProtected(View,"memberIsOwner")
325    def memberIsOwner(self):
326        """is the current user the owner"""
327        member = self.portal_membership.getAuthenticatedMember()
328        #print member, self.getId(),self.aq_parent.getId()
329        if self.aq_parent.getId() == str(member):
330            return True
331        return False
332
[166]333    security.declareProtected(View,"accommodationIsBooked")
334    def accommodationIsBooked(self):
335        """is the accommodation booked"""
336        if self.accommodation_sc_pin != '':
337            return True
338        return False
339
340    security.declareProtected(View,"accommodationIsPayed")
341    def accommodationIsPayed(self):
342        """is the accommodation payed"""
[168]343        if self.hostel_fee_sc_pin != '':
[166]344            return True
345        return False
346
347    security.declareProtected(View,"isRegisteredForCurrentLevel")
348    def isRegisteredForCurrentLevel(self):
349        """is the student registered for the current level"""
350        for l in  self.aq_parent.objectValues():
351            if l.portal_type == 'StudyLevel':
352                return True
353        return False
354
[57]355InitializeClass(Student)
356
357def addStudent(container, id, REQUEST=None, **kw):
358    """Add a Student."""
359    ob = Student(id, **kw)
360    return CPSBase_adder(container, ob, REQUEST=REQUEST)
361
362###)
[91]363
[89]364class StudentPersonal(CPSDocument): ###(
365    """
[154]366    WAeUP Student container for the various student data
[89]367    """
368    meta_type = 'StudentPersonal'
369    portal_type = meta_type
370    security = ClassSecurityInfo()
[152]371
372    security.declareProtected(View,"Title")
373    def Title(self):
374        """compose title"""
375        content = self.getContent()
[364]376        #return "Personal Data for %s %s" % (content.firstname,content.lastname)
377        return "Personal Data"
[152]378
[154]379
[89]380InitializeClass(StudentPersonal)
381
382def addStudentPersonal(container, id, REQUEST=None, **kw):
383    """Add a Students personal data."""
384    ob = StudentPersonal(id, **kw)
385    return CPSBase_adder(container, ob, REQUEST=REQUEST)
386
387###)
388
[362]389class StudentStudyCourse(CPSDocument): ###(
390    """
391    WAeUP Student container for the various student data
392    """
393    meta_type = 'StudentStudyCourse'
394    portal_type = meta_type
395    security = ClassSecurityInfo()
396
[364]397    security.declareProtected(View,"Title")
398    def Title(self):
399        """compose title"""
400        content = self.getContent()
[382]401        return "Course Major"
[362]402
403
404InitializeClass(StudentStudyCourse)
405
406def addStudentStudyCourse(container, id, REQUEST=None, **kw):
407    """Add a Students personal data."""
408    ob = StudentStudyCourse(id, **kw)
409    return CPSBase_adder(container, ob, REQUEST=REQUEST)
410
411###)
412
[179]413class StudentEligibility(CPSDocument): ###(
414    """
415    WAeUP Student container for the various student data
416    """
417    meta_type = 'StudentEligibility'
418    portal_type = meta_type
419    security = ClassSecurityInfo()
420
[181]421    security.declareProtected(View,"Title")
422    def Title(self):
423        """compose title"""
424        return "Eligibility Data"
[179]425
[181]426
[179]427InitializeClass(StudentEligibility)
428
429def addStudentEligibility(container, id, REQUEST=None, **kw):
430    """Add a Students eligibility data."""
431    ob = StudentEligibility(id, **kw)
432    return CPSBase_adder(container, ob, REQUEST=REQUEST)
433
434###)
[181]435
[166]436class StudentDocuments(CPSDocument): ###(
437    """
438    WAeUP Student container for the various student data
439    """
440    meta_type = 'StudentDocuments'
441    portal_type = meta_type
442    security = ClassSecurityInfo()
443
444    security.declareProtected(View,"Title")
445    def Title(self):
446        """compose title"""
447        content = self.getContent()
448        return "Scanned Documents"
449
450
451InitializeClass(StudentDocuments)
452
453def addStudentDocuments(container, id, REQUEST=None, **kw):
454    """Add a Students documents"""
455    ob = StudentDocuments(id, **kw)
456    return CPSBase_adder(container, ob, REQUEST=REQUEST)
457
458###)
459
[89]460class Jamb(CPSDocument): ###(
461    """
462    WAeUP Jamb containing the courses and students
463    """
464    meta_type = 'Jamb'
465    portal_type = meta_type
466    security = ClassSecurityInfo()
[154]467
[152]468    security.declareProtected(View,"Title")
469    def Title(self):
470        """compose title"""
471        content = self.getContent()
472        return "JAMB Data for %s %s" % (content.firstname,content.lastname)
473
474    security.declareProtected(View,"setOwnership")
475    def setOwnership(self,member_id):
476        """set ownership"""
477        pm = getattr(self,'portal_membership')
478        member = pm.getMemberById(member_id)
479        self.changeOwnership(member)
480
[89]481InitializeClass(Jamb)
482
483def addJamb(container, id, REQUEST=None, **kw):
484    """Add a Jamb."""
485    ob = Jamb(id, **kw)
486    return CPSBase_adder(container, ob, REQUEST=REQUEST)
487
[139]488###)
489
490class StudyLevel(CPSDocument): ###(
491    """
492    WAeUP StudyLevel containing the courses and students
493    """
494    meta_type = 'StudyLevel'
495    portal_type = meta_type
496    security = ClassSecurityInfo()
[154]497
[139]498InitializeClass(StudyLevel)
499
500def addStudyLevel(container, id, REQUEST=None, **kw):
501    """Add a StudyLevel."""
502    ob = StudyLevel(id, **kw)
503    return CPSBase_adder(container, ob, REQUEST=REQUEST)
504
505###)
506
507class Semester(CPSDocument): ###(
508    """
509    WAeUP Semester containing the courses and students
510    """
511    meta_type = 'Semester'
512    portal_type = meta_type
513    security = ClassSecurityInfo()
[154]514
[139]515InitializeClass(Semester)
516
517def addSemester(container, id, REQUEST=None, **kw):
518    """Add a Semester."""
519    ob = Semester(id, **kw)
520    return CPSBase_adder(container, ob, REQUEST=REQUEST)
[200]521
[139]522###)
523
Note: See TracBrowser for help on using the repository browser.