Ignore:
Timestamp:
19 Mar 2007, 21:45:44 (18 years ago)
Author:
joachim
Message:

merged changes from trunk to joachim-event-branch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • WAeUP_SRP/branches/joachim-event-branch/Students.py

    r1529 r1596  
    2222i_home = Globals.INSTANCE_HOME
    2323MAX_TRANS = 1000
     24from urllib import urlencode
     25
    2426import DateTime
    2527# import PIL.Image
     
    3638
    3739###)
     40
     41def response_write(response,s):
     42    response.setHeader('Content-type','text/html; charset=ISO-8859-15')
     43    while s.find('<') > -1:
     44        s = s.replace('<','&lt;')
     45    while s.find('>') > -1:
     46        #from pdb import set_trace;set_trace()
     47        s = s.replace('>','&gt;')
     48    response.write("%s<br>\n" % s)
    3849
    3950def getInt(s): ###(
     
    8293    security = ClassSecurityInfo()
    8394
    84     security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsFromCSV")###(
    85     def loadFullTimeStudentsFromCSV(self):
    86         """load Fulltime Studentdata from CSV values"""
    87         import transaction
    88         import random
    89         tr_count = 0
    90         name = 'short_full_time'
    91         no_import = False
    92         if not no_import:
    93             no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
    94             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')
    95         logger = logging.getLogger('Import.%s' % name)
    96         logger.info('Start loading from %s.csv' % name)
    97         pwlist  = []
    98         pwlist.append('"student_id","firstname","middlename","lastname","matric_no","jamb_reg_no","access_code"')
    99         pwl_template = Template('"$student_id","$firstname","$middlename","$lastname","$matric_no","$jamb_reg_no","$access_code"')
    100         students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
    101         try:
    102             students = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
    103         except:
    104             logger.error('Error reading %s.csv' % name)
    105             return
    106         l = self.portal_catalog({'meta_type': "StudentClearance",})
    107         matrics = []
    108         for s in l:
    109             matrics.append(s.getObject().getContent().matric_no)
    110         print matrics
    111         l = self.portal_catalog({'meta_type': "Certificate"})
    112         certs = {}
    113         for c in l:
    114             ca,ac,fa,dep_id,co,certcode = c.relative_path.split('/')
    115             cid = "%(dep_id)s_%(certcode)s" % vars()
    116             certs[cid] = c.getObject()
    117         for student in students:
    118             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)
    119             sid = student.get('MatricNo')
    120             if sid == "":
    121                 em = 'Empty MatricNo\n'
    122                 logger.info(em)
    123                 no_import.write(em)
    124                 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)
    125                 continue
    126             certcode = makeCertificateCode(student.get('CourseMajor'))
    127             dep_id = student.get('Dept')
    128             fac_id = student.get('Faculty')
    129             cid = "%(dep_id)s_%(certcode)s" % vars()
    130             if cid not in certs.keys():
    131                 em = 'Certificate with ID %s %s not found\n' % (certcode, student.get('CourseMajor'))
    132                 logger.info(em)
    133                 no_import.write(em)
    134                 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)
    135                 continue
    136             certificate_doc = certs[cid].getContent()
    137             level = student.get('StudentLevel')
    138             try:
    139                 int(level)
    140             except:
    141                 em = 'Student with ID %(MatricNo)s StudentLevel is empty\n' % student
    142                 logger.info(em)
    143                 no_import.write(em)
    144                 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)
    145                 continue
    146             matric_no = student.get('MatricNo')
    147             if matric_no not in matrics:
    148                 matrics.append(matric_no)
    149                 sid = self.generateStudentId(student.get('Lastname')[0])
    150                 #self.log('Creating Faculty %(id)s = %(Title)s' % faculty)
    151                 students_folder.invokeFactory('Student', sid)
    152                 logger.info('%(tr_count)s: Creating Student with ID %(sid)s Matric_no %(matric_no)s ' % vars())
    153                 student_obj = getattr(self,sid)
    154                 access_code = "%d" % random.randint(1000000000,9999999999)
    155                 student_obj.getContent().makeStudentMember(sid,access_code,)
    156                 pwl_dict = {'student_id': sid,'access_code':access_code}
    157                 student_obj.invokeFactory('StudentApplication','application')
    158                 application = student_obj.application
    159                 da = {'Title': 'Application Data'}
    160                 student_obj.invokeFactory('StudentPersonal','personal')
    161                 da['jamb_reg_no'] = student.get('EntryRegNo')
    162                 personal = student_obj.personal
    163                 dp = {'Title': 'Personal Data'}
    164                 student_obj.invokeFactory('StudentClearance','clearance')
    165                 clearance = student_obj.clearance
    166                 dc = {'Title': 'Clearance/Eligibility Record'}
    167                 dc['matric_no'] = matric_no
    168                 state = student.get('State')
    169                 lga = student.get('LGA')
    170                 if state and lga:
    171                     lga =  state + ' / ' + lga
    172                 else:
    173                     lga = "None"
    174                 dc['lga'] = lga
    175                 dc['nationality'] = student.get('Nationality')
    176                 dc['email'] = student.get('Emailaddress')
    177                 dp['firstname'] = student.get('FirstName')
    178                 dp['middlename'] = student.get('MiddleName')
    179                 dp['lastname'] = student.get('Lastname')
    180                 dp['former_surname'] = student.get('FormerSurname')
    181                 dp['sex'] = student.get('Sex') == 'F'
    182                 dp['perm_address'] = student.get('PermanentAddress')
    183                 dp['perm_city'] = student.get('PermanentAddressCity')
    184                 dp['campus_address'] = student.get('CampusAddress')
    185                 dp['phone'] = student.get('PhoneNumber')
    186                 application.getContent().edit(mapping=da)
    187                 personal.getContent().edit(mapping=dp)
    188                 clearance.getContent().edit(mapping=dc)
    189                 #
    190                 # Study Course
    191                 #
    192                 student_obj.invokeFactory('StudentStudyCourse','study_course')
    193                 studycourse = student_obj.study_course
    194                 dsc = {}
    195                 from_certificate = ['title',
    196                                    'max_elect',
    197                                    'max_pass',
    198                                    'n_core',
    199                                    'nr_years',
    200                                    'probation_credits',
    201                                    'promotion_credits',
    202                                    'start_level',
    203                                   ]
    204                 for f in from_certificate:
    205                     dsc[f] = getattr(certificate_doc,f)
    206                 dsc['faculty'] = fac_id
    207                 dsc['department'] = dep_id
    208                 dsc['study_course'] = certcode
    209                 css = student.get('CurrentSession') or '2004-2005'
    210                 cs = int(css.split('-')[0]) - 2000
    211                 cl = int(student.get('StudentLevel') or '100')/100
    212                 dsc['entry_session'] = "200%s" % (cs - cl)
    213                 dsc['clr_ac_pin'] = access_code
    214                 studycourse.getContent().edit(mapping=dsc)
    215                 #
    216                 # Level
    217                 #
    218 ##                l = getattr(studycourse,level,None)
    219 ##                if 0 and l is None:
    220 ##                    #self.log('Creating Department %(DeptCode)s = %(Description)s' % dep)
    221 ##                    logger.info('Creating Level %(StudentLevel)s for %(fullname)s' % student)
    222 ##                    studycourse.invokeFactory('StudentStudyLevel', level)
    223 ##                    l = getattr(studycourse, level)
    224 ##                    certificate = certs[certcode]
    225 ##                    cert_level = getattr(certificate,level,None)
    226 ##                    if cert_level is None:
    227 ##                        logger.info('Level %(level)s not in %(certcode)s' % vars())
    228 ##                    l.getContent().edit(mapping={'Title': "Level %s" % level})
    229             else:
    230                 em = 'Student with ID %(MatricNo)s %(fullname)s already exists\n' % student
    231                 logger.info(em)
    232                 no_import.write(em)
    233                 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)
    234                 continue
    235             if tr_count > MAX_TRANS:
    236                 transaction.commit()
    237                 em = 'Transaction commited\n' % student
    238                 logger.info(em)
    239                 tr_count = 0
    240             tr_count += 1
    241             pwl_dict.update(dc)
    242             pwl_dict.update(da)
    243             pwl_dict.update(dp)
    244             wftool = self.portal_workflow
    245             pwlist.append(pwl_template.substitute(pwl_dict))
    246             wftool.doActionFor(student_obj,'clear_and_validate')
    247             student_obj.manage_setLocalRoles(sid, ['Owner',])
    248             wftool.doActionFor(application,'close')
    249             application.manage_setLocalRoles(sid, ['Owner',])
    250             wftool.doActionFor(clearance,'close')
    251             clearance.manage_setLocalRoles(sid, ['Owner',])
    252             wftool.doActionFor(personal,'close')
    253             personal.manage_setLocalRoles(sid, ['Owner',])
    254             wftool.doActionFor(studycourse,'close_for_edit')
    255             studycourse.manage_setLocalRoles(sid, ['Owner',])
    256         open("%s/import/pwlist-%s.csv" % (i_home,name),"w+").write('\n'.join(pwlist))
    257         return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
    258     ###)
    259 
    260     security.declareProtected(ModifyPortalContent,"loadPumeResultsFromCSV")###(
    261     def loadPumeResultsFromCSV(self):
    262         """load Fulltime Studentdata from CSV values into pumeresults catalog"""
    263         import transaction
    264         import random
    265 ##        csv_d = {'jamb_reg_no': "RegNumber", ###(
    266 ##                 'status': "Admission Status",
    267 ##                 'name': "Name",
    268 ##                 'score': "Score",
    269 ##                 'sex': "Sex",
    270 ##                 'faculty': "Faculty",
    271 ##                 'department': "Dept",
    272 ##                 'course': "Course",
    273 ##                 'course_code_org': "Course Code",
    274 ##                 }
    275 ###)
    276         csv_d = {'jamb_reg_no': "JAMBRegno",
    277                  'name': "Name",
    278                  'score': "Score",
    279                  'sex': "Sex",
    280                  'course': "Course",
    281                  'faculty': "Faculty",
    282                  'department': "Dept",
    283                  'course_code_org': "Course Code",
    284                  'status': "Admission Status",
    285                  'result_type': None,
    286                  }
    287         csv_fields = [f[1] for f in csv_d.items() if f[1]]
    288         tr_count = 0
    289         total = 0
    290         #name = 'pup_new'
    291         name = 'pup_update'
    292         update = name.endswith('update')
    293         no_import = []
    294         ok_import = []
    295         ok_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_d.keys()]))
    296         no_import.append('%s' % ','.join(['"%s"' % fn for fn in csv_fields]))
    297         current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
    298         ok_import_name = "%s/import/%s_imported_%s.csv" % (i_home,name,current)
    299         #open(ok_import_name,"w").write('\n'.join(no_import))
    300         no_import_name = "%s/import/%s_not_imported_%s.csv" % (i_home,name,current)
    301         #open(no_import_name,"w").write('\n'.join(no_import))
    302         logger = logging.getLogger('Import.%s' % name)
    303         starttime = DateTime.now()
    304         logger.info('Start loading from %s.csv' % name)
    305         try:
    306             result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
    307         except:
    308             logger.error('Error reading %s.csv' % name)
    309             return
    310         pume = self.portal_pumeresults
    311         format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
    312         import_format = ','.join(['"%%(%s)s"' % fn for fn in csv_d.keys()])
    313         eduplicate = '%s,"duplicate"' % format
    314         einvalidjamb = '%s,"invalid JambRegNo"' % format
    315         added = 'added ,%s' % format
    316         #from pdb import set_trace;set_trace()
    317         for jamb in result:
    318             dict = {}
    319             for f,fn in csv_d.items():
    320                 dict[f] = jamb.get(csv_d[f])
    321             dict['result_type'] = 'DE'
    322             jnr = jamb.get(csv_d['jamb_reg_no'])
    323             if not checkJambNo(jnr):
    324                 logger.info(einvalidjamb % jamb)
    325                 dd = {}
    326                 for f,fn in csv_d.items():
    327                     dd[fn] = getattr(data,f)
    328                     no_import.append(eduplicate % dd)
    329                     no_import.append(eduplicate % jamb)
    330                 continue
    331             res = pume(jamb_reg_no=jnr)
    332             if len(res) > 0:
    333                 if update:
    334                     try:
    335                         pume.modifyRecord(**dict)
    336                     except ValueError:
    337                         logger.info(eduplicate % jamb)
    338                         continue
    339                     except KeyError:
    340                         pume.addRecord(**dict)
    341                         logger.info(added % jamb)
    342                         continue
    343                 else:
    344                     data = res[0]
    345                     if data.name != jamb.get(csv_d['name']):
    346                         #set_trace()
    347                         logger.info(eduplicate % jamb)
    348                         #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
    349                         #logger.info(em)
    350                         dd = {}
    351                         for f,fn in csv_d.items():
    352                             dd[fn] = getattr(data,f)
    353                         no_import.append(eduplicate % dd)
    354                         no_import.append(eduplicate % jamb)
    355                     continue
    356             try:
    357                 pume.addRecord(**dict)
    358                 ok_import.append(import_format % dict)
    359             except ValueError:
    360                 logger.info(eduplicate % jamb)
    361                 #em = 'Student with REG-NO %(jamb_reg_no)s already exists\n' % dict
    362                 #logger.info(em)
    363                 no_import.append(eduplicate % jamb)
    364         logger.info('End loading from %s.csv' % name)
    365         if len(no_import) > 1:
    366             open(no_import_name,"w+").write('\n'.join(no_import))
    367         open(ok_import_name,"w+").write('\n'.join(ok_import))
    368         return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
    369     ###)
     95
    37096
    37197    security.declareProtected(ModifyPortalContent,"createDEStudents")###(
     
    405131        no_certificate = "no certificate %s" % format
    406132        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write('\n'.join(no_import))
    407         logger = logging.getLogger('Import.%s' % name)
    408         logger.info('start loading from %s.csv' % name)
     133        logger = logging.getLogger('Students.StudentsFolder.createDEStudents')
     134        logger.info('Start loading from %s.csv' % name)
    409135        l = self.portal_catalog({'meta_type': "Certificate"})
    410136        certs = {}
     
    418144            return
    419145        for jamb in result:
    420             jamb['Error'] = "Processing "
     146            jamb['Error'] = "Processing"
    421147            logger.info(format % jamb)
    422148            jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
     
    624350        no_certificate = "no certificate %s" % format
    625351        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write('\n'.join(no_import))
    626         logger = logging.getLogger('Import.%s' % name)
    627         logger.info('start loading from %s.csv' % name)
     352        logger = logging.getLogger('Students.StudentsFolder.createNewStudents')
     353        logger.info('Start loading from %s.csv' % name)
    628354        l = self.portal_catalog({'meta_type': "Certificate"})
    629355        certs = {}
     
    821547        wftool = self.portal_workflow
    822548        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
    823         students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
     549        #students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
     550        students_folder = self.portal_url().getPortalObject().campus.students
    824551        tr_count = 1
    825552        total = 0
     
    829556        no_import = []
    830557        imported = []
    831         logger = logging.getLogger('Import.%s' % name)
     558        logger = logging.getLogger('Students.StudentsFolder.importReturningStudents')
    832559        try:
    833560            returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
     
    849576            if start:
    850577                start = False
    851                 logger.info('start loading from %s.csv' % name)
     578                logger.info('Start loading from %s.csv' % name)
    852579                s = ','.join(['"%s"' % fn for fn in student.keys()])
    853580                imported.append(s)
     
    922649        for k,v in verdicts_voc.items():
    923650            rverdicts[v.upper()] = k
    924         logger = logging.getLogger('fixVerdicts')
     651        rverdicts['STUDENT ON PROBATION'] = 'C'
     652        logger = logging.getLogger('Students.StudentsFolder.fixVerdicts')
    925653        try:
    926654            verdicts = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
     
    933661            if start:
    934662                start = False
    935                 logger.info('start loading from %s.csv' % name)
     663                logger.info('Start loading from %s.csv' % name)
    936664                s = ','.join(['"%s"' % fn for fn in verdict.keys()])
    937665                no_import.append('%s,"Error"' % s)
     
    957685                        no_import.append( format_error % verdict)
    958686                        continue
    959                 try:
    960                     st_cat.modifyRecord(id = student_id,
     687                st_cat.modifyRecord(id = student_id,
    961688                                    verdict=verdict_code)
    962                 except KeyError:
    963                     student['Error'] = "Not found in students_catalog"
    964                     no_import.append( format_error % student)
    965                     continue
     689
    966690                dsc = {}
    967691                dsc['current_verdict'] = verdict_code
    968692                study_course.getContent().edit(mapping=dsc)
     693            else:
     694                verdict['Error'] = "Not found in students_catalog"
     695                no_import.append( format_error % verdict)
     696                continue
    969697            tr_count += 1
    970698            if tr_count > 1000:
     
    987715    ###)
    988716
     717    security.declareProtected(ModifyPortalContent,"fixAllNames")###(
     718    def fixAllNames(self):
     719        "fix all students names"
     720        import transaction
     721        response = self.REQUEST.RESPONSE
     722        logger = logging.getLogger('fixAllNames')
     723        logger.info('Start')
     724        students = self.portal_catalog(portal_type='Student')
     725        count = 0
     726        total = 0
     727        for student in students:
     728            scat_res = self.students_catalog(id = student.getId)
     729            if not scat_res:
     730                self.students_catalog.addRecord(id = student.getId)
     731                scat_res = self.students_catalog(id = student.getId)
     732            student_entry = scat_res[0]
     733            old_new = self.fixName(student,student_entry)
     734            count += 1
     735            response_write(response,"%d) %s: %s" % (count + total,student_entry.id,old_new))
     736            if count > 2000:
     737                transaction.commit()
     738                logger.info("%d transactions commited" % count)
     739                total += count
     740                count = 0
     741    ###)
     742
     743    security.declareProtected(ModifyPortalContent,"fixName")###(
     744    def fixName(self,student_brain, student_entry):
     745        "fix the name of a student"
     746        fix = "first"
     747        if student_entry.get('name_fixed',None) == fix:
     748            return "Name already fixed"
     749        student_id = student_entry.id
     750        new_student = student_entry.jamb_reg_no.startswith('6')
     751        student_obj = student_brain.getObject()
     752        personal = getattr(student_obj,'personal',None)
     753        invalid = ''
     754        if personal is None:
     755            return '"%s", "Returning: %s","%s"' % (invalid,student_entry.name,"not logged in")
     756        per_doc = personal.getContent()
     757        old_first = per_doc.firstname
     758        old_middle = per_doc.middlename
     759        old_last = per_doc.lastname
     760        new_first = ''
     761        new_middle = ''
     762        new_last = ''
     763        if new_student:
     764            if not old_first and not old_middle and old_last:
     765                new_names = [n.capitalize() for n in old_last.split()]
     766                if len(new_names) > 3:
     767                    old_first = new_names[0]
     768                    old_last = new_names[1]
     769                    old_middle = ' '.join(new_names[2:])
     770                elif len(new_names) == 3:
     771                    old_first = new_names[0]
     772                    old_last = new_names[1]
     773                    old_middle = new_names[2]
     774                elif len(new_names) == 2:
     775                    old_first = new_names[0]
     776                    old_last = new_names[1]
     777                    old_middle = ''
     778                elif len(new_names) == 1:
     779                    old_last = new_names[0]
     780                    old_first = ''
     781                    old_middle = ''
     782            if old_last:
     783                new_first = old_last
     784            if old_middle:
     785                new_middle = old_middle
     786            if old_first:
     787                new_last = old_first
     788                   
     789            if old_first.find('<') != -1 or\
     790               old_first.find('>') != -1 or\
     791               old_middle.find('<') != -1 or\
     792               old_middle.find('>') != -1 or\
     793               old_last.find('<') != -1 or\
     794               old_last.find('>') != -1:
     795                   invalid = "invalid characters"
     796        else:
     797            if old_first and old_middle and old_last:
     798                new_first = old_first.capitalize()
     799                new_middle = old_middle.capitalize()
     800                new_last = old_last.capitalize()
     801            elif old_first and not old_middle and old_last:
     802                new_names = [n.capitalize() for n in old_first.split()]
     803                if len(new_names) > 1:
     804                    new_first = new_names[0]
     805                    new_middle = ' '.join(new_names[1:])
     806                else:
     807                    new_first = old_first.capitalize()
     808                    new_middle = ''
     809                new_last = old_last.capitalize()
     810            elif not old_first and not old_middle and old_last:
     811                new_first = ''
     812                new_middle = ''
     813                new_last = old_last.capitalize()
     814            else:
     815                new_first = ''
     816                new_middle = ''
     817                new_last = ''
     818        name = "%(new_last)s %(new_first)s %(new_middle)s" % vars()
     819        if new_student:
     820            text = "New"
     821        else:
     822            text = "Returning"
     823        old_new = '"%s", "%s: %s","%s"' % (invalid,text,
     824                                           student_entry.name,
     825                                           name)
     826        if not invalid:
     827            self.students_catalog.modifyRecord(id = student_id,
     828                                      name_fixed = fix,
     829                                      name = name)
     830            per_doc.edit(mapping = {'firstname' : new_first,
     831                                'middlename' : new_middle,
     832                                'lastname' : new_last,
     833                                })
     834        return old_new
     835    ###)
     836
    989837    security.declareProtected(ModifyPortalContent,"fixAllEntryModeForReturning")###(
    990838    def fixAllEntryModeForReturning(self):
     
    1017865        st_cat = self.students_catalog
    1018866        no_import = []
    1019         logger = logging.getLogger('getEntryMode.%s' % name)
     867        logger = logging.getLogger('Students.StudentsFolder.fixEntryModeForReturning')
    1020868        try:
    1021869            returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
     
    1027875            if start:
    1028876                start = False
    1029                 logger.info('start loading from %s.csv' % name)
     877                logger.info('Start loading from %s.csv' % name)
    1030878                s = ','.join(['"%s"' % fn for fn in student.keys()])
    1031879                no_import.append('%s,"Error"' % s)
     
    1120968        no_import = []
    1121969        imported = []
    1122         logger = logging.getLogger('Import.%s' % name)
     970        logger = logging.getLogger('Students.StudentsFolder.updateReturningStudents')
    1123971        try:
    1124972            returning = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
     
    1139987            if start:
    1140988                start = False
    1141                 logger.info('start loading from %s.csv' % name)
     989                logger.info('Start loading from %s.csv' % name)
    1142990                s = ','.join(['"%s"' % fn for fn in student.keys()])
    1143991                imported.append(s)
     
    12141062        no_import = []
    12151063        imported = []
    1216         logger = logging.getLogger('Import.%s' % name)
     1064        logger = logging.getLogger('Students.StudentsFolder.importResults')
    12171065        try:
    12181066            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
     
    12331081            if start:
    12341082                start = False
    1235                 logger.info('start loading from %s.csv' % name)
     1083                logger.info('Start loading from %s.csv' % name)
    12361084                s = ','.join(['"%s"' % fn for fn in result.keys()])
    12371085                imported.append(s)
     
    13391187        open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
    13401188                    '\n'.join(no_import))
    1341         logger = logging.getLogger('Import.%s' % name)
     1189        logger = logging.getLogger('Students.StudentsFolder.updateStudyCourse')
    13421190        logger.info('Start loading from %s.csv' % name)
    13431191        l = self.portal_catalog({'meta_type': "Certificate"})
     
    13541202                                     'SearchableText': jamb_reg_no })
    13551203            if not res:
    1356                 em = 'Student with RegNo %s does not exists\n' % jamb_reg_no
     1204                em = 'Student with jamb_reg_no %s does not exists\n' % jamb_reg_no
    13571205                logger.info(em)
    1358                 jamb['Error'] = "Student not exists"
     1206                jamb['Error'] = "Student does not exist"
    13591207                no_import.append(format % jamb)
    13601208                continue
     
    13981246    ###)
    13991247
    1400     security.declareProtected(ModifyPortalContent,"OLDloadPumeResultsFromCSV")###(
    1401     def OLDloadPumeResultsFromCSV(self):
    1402         """load Fulltime Studentdata from CSV values"""
    1403         import transaction
    1404         import random
    1405         wftool = self.portal_workflow
    1406         students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
    1407         csv_d = {'jamb_reg_no': "JAMBRegno",
    1408                  'jamb_lastname': "Name",
    1409                  'pume_options': "Options",
    1410                  'session': "Session",
    1411                  'days': "Days",
    1412                  'response': "Responce",
    1413                  'wrong': "Wrong",
    1414                  'pume_eng_score': "EngScore",
    1415                  'pume_gen_score': "GenScore",
    1416                  'pume_tot_score': "Score",
    1417                  'batch': "Batch",
    1418                  'serial': "SerialNo",
    1419                  'jamb_score': "JambScore",
    1420                  'omitted':"Omitted",
    1421                  'search_key': "SearchKey",
    1422                  'jamb_sex': "Sex",
    1423                  'fac1': "Fac1",
    1424                  'fac2': "Fac2",
    1425                  'jamb_first_cos': "CourseofStudy",
    1426                  'stud_status':"StudStatus",
    1427                  'registered': "Registered",
    1428                  'jamb_state': "State",
    1429                  'eng_fail': "EngFail",
    1430                  'gen_fail': "GenFail",
    1431                  'un_ans_eng': "UnAnsEng",
    1432                  'un_ans_eng': "UnAnsGen",
    1433                  'total_ans': "TotalUnAns",
    1434                  'dept': "Dept",
    1435                  'jamb_second_cos': "Course2",
    1436                  'jamb_third_cos': "course3",
    1437                  }
    1438         csv_fields = [f[1] for f in csv_d.items()]
    1439         tr_count = 0
    1440         name = 'pume_results'
    1441         no_import = []
    1442         s = ','.join(['"%s"' % fn for fn in csv_fields])
    1443         no_import.append('%s\n' % s)
    1444         logger = logging.getLogger('Import.%s' % name)
    1445         logger.info('Start loading from %s.csv' % name)
    1446         try:
    1447             result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
    1448         except:
    1449             logger.error('Error reading %s.csv' % name)
    1450             return
    1451         for jamb in result:
    1452             format = ','.join(['"%%(%s)s"' % fn for fn in csv_fields])
    1453             processing = "processing %s" % format
    1454             logger.info(processing % jamb)
    1455             jamb_reg_no = jamb.get(csv_d['jamb_reg_no'])
    1456             #import pdb;pdb.set_trace()
    1457             res = self.portal_catalog({'portal_type': "StudentApplication",
    1458                                      'jamb_reg_no': jamb_reg_no })
    1459             if res:
    1460                 em = 'Student with REG-NO %s already exists\n' % jamb_reg_no
    1461                 logger.info(em)
    1462                 no_import.append(em)
    1463                 no_import.append(format % jamb)
    1464                 continue
    1465             cert_id = jamb.get(csv_d['jamb_first_cos']).upper()
    1466             res = self.portal_catalog({'portal_type': "Certificate",
    1467                                      'id': cert_id })
    1468             if len(res) < 1:
    1469                 em = 'No Certificate with ID %s \n' % cert_id
    1470                 logger.info(em)
    1471                 no_import.append(em)
    1472                 no_import.append(format % jamb)
    1473                 continue
    1474             cert = res[0].getObject()
    1475             cert_path = res[0].getPath()
    1476             cert_doc = cert.getContent()
    1477             jamb_name = jamb.get(csv_d['jamb_lastname'])
    1478             jamb_name.replace('>','')
    1479             names = jamb_name.split()
    1480             letter = names[-1][0].upper()
    1481             sid = self.generateStudentId(letter)
    1482             not_created = True
    1483             while not_created:
    1484                 try:
    1485                     students_folder.invokeFactory('Student', sid)
    1486                     not_created = False
    1487                 except BadRequest:
    1488                     sid = self.generateStudentId(letter)
    1489             logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
    1490             student = getattr(self,sid)
    1491             student.manage_setLocalRoles(sid, ['Owner',])
    1492             student.invokeFactory('StudentClearance','clearance')
    1493             #wftool.doActionFor(student.clearance,'open')
    1494             dp = {'Title': 'Clearance/Eligibility Record'}
    1495             student.clearance.manage_setLocalRoles(sid, ['Owner',])
    1496             student.invokeFactory('StudentPume','pume')
    1497             dp = {'Title': 'Pume Data'}
    1498             student.invokeFactory('StudentApplication','application')
    1499             da = {'Title': 'Application Data'}
    1500             da["jamb_lastname"] = jamb_name
    1501             da_fields = ('jamb_reg_no',
    1502                          'jamb_sex',
    1503                          'jamb_state',
    1504                          'jamb_score',
    1505                          'jamb_first_cos',
    1506                          'jamb_sex',
    1507                          'jamb_state',
    1508                          'jamb_first_cos',
    1509                          'jamb_second_cos',
    1510                          )
    1511             for f in da_fields:
    1512                 da[f] = jamb.get(csv_d[f])
    1513             app = student.application
    1514             app.getContent().edit(mapping=da)
    1515             app.manage_setLocalRoles(sid, ['Owner',])
    1516             #wftool.doActionFor(app,'close')
    1517             dp_fields = (
    1518                          'pume_eng_score',
    1519                          'pume_gen_score',
    1520                          'pume_tot_score',
    1521                          )
    1522             for f in dp_fields:
    1523                 dp[f] = float(jamb.get(csv_d[f]))
    1524             pume = student.pume
    1525             pume.getContent().edit(mapping=dp)
    1526             #wftool.doActionFor(pume,'close')
    1527             pume.manage_setLocalRoles(sid, ['Owner',])
    1528             #
    1529             # Study Course
    1530             #
    1531             student.invokeFactory('StudentStudyCourse','study_course')
    1532             study_course = student.study_course
    1533             dsc = {}
    1534             from_certificate = ['title',
    1535                                'max_elect',
    1536                                'max_pass',
    1537                                'n_core',
    1538                                'nr_years',
    1539                                'probation_credits',
    1540                                'promotion_credits',
    1541                                'start_level',
    1542                               ]
    1543             for f in from_certificate:
    1544                 dsc[f] = getattr(cert_doc,f)
    1545             cpl = cert_path.split('/')
    1546             dsc['faculty'] = cpl[-4]
    1547             dsc['department'] = cpl[-3]
    1548             dsc['study_course'] = cert_id
    1549             dsc['entry_session'] = jamb.get(csv_d['session'])
    1550             study_course.getContent().edit(mapping=dsc)
    1551             student.getContent().createSubObjects()
    1552             if dp['pume_tot_score']>49:
    1553                 wftool.doActionFor(student,'pume_pass')
    1554                 wftool.doActionFor(student,'admit')
    1555             else:
    1556                 wftool.doActionFor(student,'pume_fail')
    1557                 wftool.doActionFor(student,'reject_admission')
    1558             if len(no_import) > 1:
    1559                 open("%s/import/%s_not_imported.csv" % (i_home,name),"w").write(
    1560                      '\n'.join(no_import))
    1561         return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
    1562     ###)
    1563 
    1564     security.declareProtected(ModifyPortalContent,"loadFullTimeStudentsResultsFromCSV") ###(
    1565     def loadFullTimeStudentsResultsFromCSV(self):
    1566         """load Fulltime Studentdata from CSV values"""
    1567         #return
    1568         level_wf_actions = {}
    1569         level_wf_actions["SUCCESSFUL STUDENT"] = "pass_A"
    1570         level_wf_actions["STUDENT WITH CARRYOVER COURSES"] = "pass_B"
    1571         level_wf_actions["STUDENT FOR PROBATION"] = "probate_C"
    1572         level_wf_actions["STUDENT ON PROBATION/TRANSFER"] = "reject_D"
    1573         import transaction
    1574         wftool = self.portal_workflow
    1575         tr_count = 0
    1576         name = 'short_full_time_results_2004_2005'
    1577         no_import = False
    1578         if not no_import:
    1579             no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
    1580             no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
    1581         logger = logging.getLogger('import.%s' % name)
    1582         logger.info('Start loading from %s.csv' % name)
    1583         students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
    1584         try:
    1585             results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
    1586         except:
    1587             logger.error('Error reading %s.csv' % name)
    1588             return
    1589         l = self.portal_catalog({'meta_type': "Course"})
    1590         courses = {}
    1591         for c in l:
    1592             courses[c.id] = c.getObject()
    1593         level_changed = False
    1594         student_changed = False
    1595         sid = ''
    1596         #import pdb;pdb.set_trace()
    1597         for result in results:
    1598             temp_sid = result.get('Matnumber')
    1599             if temp_sid != sid:
    1600                 student_changed = True
    1601                 res = self.portal_catalog({'meta_type': "StudentClearance",
    1602                                          'SearchableText': temp_sid })
    1603                 if not res:
    1604                     em = 'Student with ID %(Matnumber)s not found\n' % result
    1605                     logger.info(em)
    1606                     no_import.write(em)
    1607                     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)
    1608                     continue
    1609                 elif len(res) > 1:
    1610                     em = 'More than one Student with ID %(Matnumber)s found\n' % result
    1611                     logger.info(em)
    1612                     no_import.write(em)
    1613                     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)
    1614                     continue
    1615                 sid = temp_sid
    1616                 sf = res[0].getObject().aq_parent
    1617                 sc = getattr(sf,'study_course')
    1618                 level = ''
    1619             else:
    1620                 student_changed = False
    1621             course = result.get('CosCode')
    1622             if course not in courses.keys():
    1623                 em = 'Course with ID %(CosCode)s not found\n' % result
    1624                 logger.info(em)
    1625                 no_import.write(em)
    1626                 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)
    1627                 continue
    1628             course_doc = courses[course].getContent()
    1629             temp_level = result.get('Level')
    1630             student_id = sf.getId()
    1631             result['StudentId'] = student_id
    1632             if temp_level != level:
    1633                 try:
    1634                     int(temp_level)
    1635                 except:
    1636                     em = 'Result with ID %(Matnumber)s Course %(CosCode)s Level is empty\n' % result
    1637                     logger.info(em)
    1638                     no_import.write(em)
    1639                     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)
    1640                     continue
    1641                 level_changed = True
    1642                 if 'dlev' in vars().keys():
    1643                     wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
    1644                 level = temp_level
    1645                 l = getattr(sc,level,None)
    1646                 if l is None:
    1647                     logger.info('Creating Level %(Level)s for %(StudentId)s %(Matnumber)s' % result)
    1648                     sc.invokeFactory('StudentStudyLevel', level)
    1649                     l = getattr(sc, level)
    1650                     l.manage_setLocalRoles(student_id, ['Owner',])
    1651             else:
    1652                 level_changed = False
    1653             cr = getattr(l,course,None)
    1654             if cr is None:
    1655                 logger.info('Creating Course %(CosCode)s for %(StudentId)s %(Matnumber)s in Level %(Level)s' % result)
    1656                 l.invokeFactory('StudentCourseResult',course)
    1657             cr = getattr(l,course)
    1658             dcr = {}
    1659             from_course = ['title',
    1660                            'credits',
    1661                            'passmark',
    1662                            ]
    1663             for f in from_course:
    1664                 dcr[f] = getattr(course_doc,f)
    1665             dlev = {}
    1666             dcr['ansbook'] = result.get('Ansbook')
    1667             dcr['semester'] = getInt(result.get('Semster'))
    1668             dcr['status'] = result.get('CosStuatus')
    1669             dcr['score'] = getInt(result.get('Score'))
    1670             dlev['session'] = result.get('Session')
    1671             dcr['carry_level'] = result.get('CarryLevel')
    1672             dcr['grade'] = result.get('Grade')
    1673             dcr['weight'] = result.get('Weight')
    1674             dlev['verdict'] = result.get('Verdict')
    1675             dcr['import_id'] = result.get('id')
    1676             gpa = result.get('GPA').replace(',','.')
    1677             dlev['imported_gpa'] = getFloat(gpa)
    1678             cr.getContent().edit(mapping = dcr)
    1679             cr.manage_setLocalRoles(student_id, ['Owner',])
    1680             l.getContent().edit(mapping = dlev)
    1681             if tr_count > MAX_TRANS:
    1682                 transaction.commit()
    1683                 tr_count = 0
    1684             tr_count += 1
    1685             wftool.doActionFor(cr,'close')
    1686         wftool.doActionFor(l,level_wf_actions[dlev['verdict']])
    1687         return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
    1688 
    1689 ###)
    1690 
    1691     security.declareProtected(ModifyPortalContent,"loadJAMBFromCSV")###(
    1692     def loadJAMBFromCSV(self):
    1693         """load JAMB data from CSV values"""
    1694         #return
    1695         students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
    1696         import transaction
    1697         tr_count = 0
    1698         name = 'SampleJAMBDataII'
    1699         wftool = self.portal_workflow
    1700         no_import = False
    1701         if not no_import:
    1702             no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
    1703             no_import.write('REG-NO,NAME,SEX,STATE,LGA,ENG-SCORE,SUBJ1,SUBJ1-SCORE,SUBJ2,SUBJ2-SCORE,SUBJ3,SUBJ3-SCORE,AGGREGATE,UNIV1,FACULTY1,COURSE1,UNIV2,FACULTY2,COURSE2')
    1704         logger = logging.getLogger('Import.%s' % name)
    1705         logger.info('Start loading from %s.csv' % name)
    1706         try:
    1707             result = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
    1708         except:
    1709             logger.error('Error reading %s.csv' % name)
    1710             return
    1711         for jamb in result:
    1712             logger.info('processing %(REG-NO)s,%(NAME)s,%(SEX)s,%(STATE)s,%(LGA)s,%(ENG-SCORE)s,%(SUBJ1)s,%(SUBJ1-SCORE)s,%(SUBJ2)s,%(SUBJ2-SCORE)s,%(SUBJ3)s,%(SUBJ3-SCORE)s,%(AGGREGATE)s,%(UNIV1)s,%(FACULTY1)s,%(COURSE1)s,%(UNIV2)s,%(FACULTY2)s,%(COURSE2)s\n' % jamb)
    1713             jamb_reg_no = jamb.get('REG-NO')
    1714             res = self.portal_catalog({'meta_type': "StudentApplication",
    1715                                      'jamb_reg_no': jamb_reg_no })
    1716             if res:
    1717                 em = 'Student with REG-NO %(REG-NO)s already exists\n' % jamb
    1718                 logger.info(em)
    1719                 no_import.write(em)
    1720                 no_import.write('%(REG-NO)s,%(NAME)s,%(SEX)s,%(STATE)s,%(LGA)s,%(ENG-SCORE)s,%(SUBJ1)s,%(SUBJ1-SCORE)s,%(SUBJ2)s,%(SUBJ2-SCORE)s,%(SUBJ3)s,%(SUBJ3-SCORE)s,%(AGGREGATE)s,%(UNIV1)s,%(FACULTY1)s,%(COURSE1)s,%(UNIV2)s,%(FACULTY2)s,%(COURSE2)s\n' % jamb)
    1721                 continue
    1722             jamb_name = jamb.get("NAME")
    1723             jamb_name.replace('>','')
    1724             names = jamb_name.split()
    1725             letter = names[-1][0].upper()
    1726             sid = self.generateStudentId(letter)
    1727             not_created = True
    1728             while not_created:
    1729                 try:
    1730                     students_folder.invokeFactory('Student', sid)
    1731                     not_created = False
    1732                 except BadRequest:
    1733                     sid = self.generateStudentId(letter)
    1734             logger.info('%(tr_count)s: Creating Student with ID %(sid)s REG-NO %(jamb_reg_no)s ' % vars())
    1735             student = getattr(self,sid)
    1736             student.manage_setLocalRoles(sid, ['Owner',])
    1737             student.invokeFactory('StudentApplication','application')
    1738             da = {'Title': 'Application Data'}
    1739             da["jamb_reg_no"] = jamb.get("REG-NO")
    1740             da["jamb_lastname"] = jamb_name
    1741             da["jamb_sex"] = jamb.get("SEX")
    1742             da["jamb_state"] = jamb.get("STATE")
    1743             da["jamb_lga"] = jamb.get("LGA")
    1744             da["jamb_score"] = jamb.get("AGGREGATE")
    1745             da["jamb_first_cos"] = jamb.get("COURSE1")
    1746             da["jamb_second_cos"] = jamb.get("COURSE2")
    1747             da["jamb_first_uni"] = jamb.get("UNIV1")
    1748             da["jamb_second_uni"] = jamb.get("UNIV2")
    1749             app = student.application
    1750             app_doc = app.getContent()
    1751             app_doc.edit(mapping=da)
    1752             #wftool.doActionFor(app,'open',dest_container=app)
    1753             app.manage_setLocalRoles(sid, ['Owner',])
    1754             student.getContent().createSubObjects()
    1755         return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
    1756     ###)
    1757 
    1758  
     1248
     1249
    17591250    security.declareProtected(View,"fixOwnership")
    17601251    def fixOwnership(self):
Note: See TracChangeset for help on using the changeset viewer.