source: WAeUP_SRP/trunk/Students.py @ 428

Last change on this file since 428 was 427, checked in by Henrik Bettermann, 18 years ago

types modified and FT student import changed

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