Changeset 746


Ignore:
Timestamp:
25 Oct 2006, 06:14:35 (18 years ago)
Author:
joachim
Message:

fixed fulltime Studentimport

File:
1 edited

Legend:

Unmodified
Added
Removed
  • WAeUP_SRP/trunk/Students.py

    r742 r746  
    7070        pwlist.append('"student_id","firstname","middlename","lastname","matric_no","jamb_reg_no","access_code"')
    7171        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().makeStudentMember(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"')
    72248        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
    73249        try:
     
    230406    ###)
    231407
    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 
    408408    security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
    409409    def loadFullTimeStudentsResultsFromCSV(self):
Note: See TracChangeset for help on using the changeset viewer.