#-*- mode: python; mode: fold -*- # $Id: Students.py 393 2006-08-18 09:56:34Z joachim $ from Globals import InitializeClass from AccessControl import ClassSecurityInfo from AccessControl.SecurityManagement import newSecurityManager from Products.CMFCore.utils import UniqueObject, getToolByName from Products.CMFCore.permissions import View from Products.CMFCore.permissions import ModifyPortalContent from Products.CPSCore.CPSBase import CPSBase_adder, CPSBaseFolder #from Products.CPSCore.CPSBase import CPSBaseDocument as BaseDocument from Products.CPSDocument.CPSDocument import CPSDocument from Products.CPSCore.CPSBase import CPSBaseBTreeFolder as BaseBTreeFolder from Products.CPSCore.CPSMembershipTool import CPSUnrestrictedUser from Products.WAeUP_SRP.Academics import makeCertificateCode import logging import csv,re import Globals p_home = Globals.package_home(globals()) i_home = Globals.INSTANCE_HOME class StudentsFolder(CPSDocument): ###( """ WAeUP container for the various WAeUP containers data """ meta_type = 'StudentsFolder' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsFromCSV")###( def loadFullTimeStudentsFromCSV(self): """load Fulltime Studentdata from CSV values""" #return name = 'short_full_time' no_import = False logger = logging.getLogger('%s_import' % name) logger.info('Start loading from %s.csv' % name) students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject() try: students = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb")) except: logger.error('Error reading %s.csv' % name) return l = self.portal_catalog({'meta_type': "Student"}) studs = {} for s in l: studs[s.id] = s.getObject() l = self.portal_catalog({'meta_type': "Certificate"}) certs = {} for c in l: certs[c.id] = c.getObject() for student in students: 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) sid = student.get('MatricNo') certcode = makeCertificateCode(student.get('CourseMajor')) s = studs.get(sid,None) if certcode not in certs.keys(): em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor')) if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") 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') logger.info(em) no_import.write(em) 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) continue if s is None: #self.log('Creating Faculty %(id)s = %(Title)s' % faculty) logger.info('Creating Student with ID %(MatricNo)s %(fullname)s' % student) students_folder.invokeFactory('Student', sid) s = getattr(self,sid) s.invokeFactory('StudentPersonal','personal') sp = s.personal d = {'Title': 'Personal Data'} d['matricel_no'] = student.get('MatricNo') d['entry_reg_no'] = student.get('EntryRegNo') d['fullname'] = student.get('fullname') d['firstname'] = student.get('FirstName') d['middlename'] = student.get('MiddleName') d['lastname'] = student.get('Lastname') d['former_surname'] = student.get('FormerSurname') d['sex'] = student.get('Sex') == 'F' d['nationality'] = student.get('Nationality') d['state'] = student.get('State') d['lga'] = student.get('LGA') d['street'] = student.get('PermanentAddress') d['city'] = student.get('PermanentAddressCity') d['campus_address'] = student.get('CampusAddress') d['phone'] = student.get('PhoneNumber') d['email'] = student.get('Emailaddress') sp.getContent().edit(mapping=d) # # Study Course # s.invokeFactory('StudentStudyCourse','study_course') sc = s.study_course d = {} d['matricel_no'] = student.get('MatricNo') d['entry_reg_no'] = student.get('EntryRegNo') d['faculty'] = student.get('Faculty') d['department'] = student.get('Dept') d['course_major'] = certcode css = student.get('CurrentSession') or '2004-2005' cs = int(css.split('-')[0]) - 2000 cl = int(student.get('StudentLevel'))/100 d['entry_session'] = "200%s" % (cs - cl) sc.getContent().edit(mapping=d) # # Level # level = student.get('StudentLevel') l = getattr(sc,level,None) if l is None: #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep) logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student) sc.invokeFactory('StudyLevel', level) l = getattr(sc, level) certificate = certs[certcode] cert_level = getattr(certificate,level,None) if cert_level is None: logger.info('Level %(level)s not in %(certcode)s' % vars()) l.getContent().edit(mapping={'Title': "Level %s" % level}) l.invokeFactory('Semester','first') l.invokeFactory('Semester','second') first_s = getattr(l,'first') first_s.getContent().edit(mapping={'Title': 'First Semester'}) second_s = getattr(l,'second') second_s.getContent().edit(mapping={'Title': 'Second Semester'}) else: em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student if not no_import: no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w") 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') logger.info(em) no_import.write(em) 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) return self.students.academics_contents() ###) security.declareProtected(View,"Title") def Title(self): """compose title""" return "Student Section" InitializeClass(StudentsFolder) def addStudentsFolder(container, id, REQUEST=None, **kw): """Add a Student.""" ob = StudentsFolder(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class Student(CPSDocument): ###( """ WAeUP Student container for the various student data """ meta_type = 'Student' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") def Title(self): """compose title""" reg_nr = self.getId()[1:] data = getattr(self,'personal',None) if data is None: data = getattr(self,'jamb',None) if data: content = data.getContent() return "%s %s" % (content.firstname,content.lastname) return self.title def Description(self): """compose description""" data = getattr(self,'PERSONAL',None) if data is None: return "none" if data: content = data.getContent() return "%s" % (content.description) return self.description security.declareProtected(View,"setScratchCardData") def setScratchCardData(self,ident,ds): """set this data """ dict = {'%s_sc_pin' % ident : ds.get('sc_pin'), '%s_sc_id' % ident : ds.get('sc_id'), '%s_sc_value' % ident : ds.get('sc_value'), '%s_date' % ident : ds.get('sc_date'), } old_user = self.portal_membership.getAuthenticatedMember() if self.portal_membership.isAnonymousUser(): tmp_user = CPSUnrestrictedUser('s%(jamb_id)s' % ds, '', ['StudentManager'], '') tmp_user = tmp_user.__of__(self.acl_users) newSecurityManager(None, tmp_user) #print str(dict) self.edit(mapping=dict) newSecurityManager(None, old_user) security.declareProtected(View,"memberIsOwner") def memberIsOwner(self): """is the current user the owner""" member = self.portal_membership.getAuthenticatedMember() #print member, self.getId(),self.aq_parent.getId() if self.aq_parent.getId() == str(member): return True return False security.declareProtected(View,"accommodationIsBooked") def accommodationIsBooked(self): """is the accommodation booked""" if self.accommodation_sc_pin != '': return True return False security.declareProtected(View,"accommodationIsPayed") def accommodationIsPayed(self): """is the accommodation payed""" if self.hostel_fee_sc_pin != '': return True return False security.declareProtected(View,"isRegisteredForCurrentLevel") def isRegisteredForCurrentLevel(self): """is the student registered for the current level""" for l in self.aq_parent.objectValues(): if l.portal_type == 'StudyLevel': return True return False InitializeClass(Student) def addStudent(container, id, REQUEST=None, **kw): """Add a Student.""" ob = Student(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class StudentPersonal(CPSDocument): ###( """ WAeUP Student container for the various student data """ meta_type = 'StudentPersonal' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") def Title(self): """compose title""" content = self.getContent() #return "Personal Data for %s %s" % (content.firstname,content.lastname) return "Personal Data" InitializeClass(StudentPersonal) def addStudentPersonal(container, id, REQUEST=None, **kw): """Add a Students personal data.""" ob = StudentPersonal(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class StudentStudyCourse(CPSDocument): ###( """ WAeUP Student container for the various student data """ meta_type = 'StudentStudyCourse' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") def Title(self): """compose title""" content = self.getContent() return "Course Major" InitializeClass(StudentStudyCourse) def addStudentStudyCourse(container, id, REQUEST=None, **kw): """Add a Students personal data.""" ob = StudentStudyCourse(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class StudentEligibility(CPSDocument): ###( """ WAeUP Student container for the various student data """ meta_type = 'StudentEligibility' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") def Title(self): """compose title""" return "Eligibility Data" InitializeClass(StudentEligibility) def addStudentEligibility(container, id, REQUEST=None, **kw): """Add a Students eligibility data.""" ob = StudentEligibility(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class StudentDocuments(CPSDocument): ###( """ WAeUP Student container for the various student data """ meta_type = 'StudentDocuments' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") def Title(self): """compose title""" content = self.getContent() return "Scanned Documents" InitializeClass(StudentDocuments) def addStudentDocuments(container, id, REQUEST=None, **kw): """Add a Students documents""" ob = StudentDocuments(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class Jamb(CPSDocument): ###( """ WAeUP Jamb containing the courses and students """ meta_type = 'Jamb' portal_type = meta_type security = ClassSecurityInfo() security.declareProtected(View,"Title") def Title(self): """compose title""" content = self.getContent() return "JAMB Data for %s %s" % (content.firstname,content.lastname) security.declareProtected(View,"setOwnership") def setOwnership(self,member_id): """set ownership""" pm = getattr(self,'portal_membership') member = pm.getMemberById(member_id) self.changeOwnership(member) InitializeClass(Jamb) def addJamb(container, id, REQUEST=None, **kw): """Add a Jamb.""" ob = Jamb(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class StudyLevel(CPSDocument): ###( """ WAeUP StudyLevel containing the courses and students """ meta_type = 'StudyLevel' portal_type = meta_type security = ClassSecurityInfo() InitializeClass(StudyLevel) def addStudyLevel(container, id, REQUEST=None, **kw): """Add a StudyLevel.""" ob = StudyLevel(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###) class Semester(CPSDocument): ###( """ WAeUP Semester containing the courses and students """ meta_type = 'Semester' portal_type = meta_type security = ClassSecurityInfo() InitializeClass(Semester) def addSemester(container, id, REQUEST=None, **kw): """Add a Semester.""" ob = Semester(id, **kw) return CPSBase_adder(container, ob, REQUEST=REQUEST) ###)