source: WAeUP_SRP/trunk/Students.py @ 452

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

fixed course result import,
and semester default_view for students

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