Ignore:
Timestamp:
29 Dec 2006, 07:00:35 (18 years ago)
Author:
joachim
Message:

added import for registered and results,
the csv-files must contain a modified heading
results can be viewed with session_results_anon_view
modified Courses import to use courses catalog

File:
1 edited

Legend:

Unmodified
Added
Removed
  • WAeUP_SRP/trunk/Students.py

    r1143 r1146  
    809809    ###)
    810810
     811    security.declareProtected(ModifyPortalContent,"importRegisteredStudents")###(
     812    def importRegisteredStudents(self):
     813        """load Registered Studentdata from CSV values"""
     814        import transaction
     815        import random
     816        #from pdb import set_trace
     817        wftool = self.portal_workflow
     818        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
     819        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
     820        tr_count = 1
     821        total = 0
     822        #name = 'pume_results'
     823        name = 'Registered'
     824        table = self.registered_import
     825        no_import = []
     826        imported = []
     827        logger = logging.getLogger('Import.%s' % name)
     828        try:
     829            registered = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
     830        except:
     831            logger.error('Error reading %s.csv' % name)
     832            return
     833        l = self.portal_catalog({'meta_type': "Certificate"})
     834        certs = {}
     835        cert_docs = {}
     836        for f in l:
     837            certs[f.getId] = f.getObject().getContent()
     838        start = True
     839        res = table()
     840        regs = []
     841        if len(res) > 0:
     842            regs = [s.matric_no for s in res]
     843        for student in registered:
     844            if start:
     845                start = False
     846                logger.info('start loading from %s.csv' % name)
     847                s = ','.join(['"(%s)"' % fn for fn in student.keys()])
     848                imported.append(s)
     849                no_import.append('%s,"Error"' % s)
     850                format = ','.join(['"%%(%s)s"' % fn for fn in student.keys()])
     851                format_error = format + ',"%(Error)s"'
     852                no_certificate = "no certificate %s" % format
     853            if student.get('matric_no') == '':
     854                student['Error'] = "Empty matric_no" 
     855                no_import.append( format_error % student)
     856                continue
     857            if student.get('matric_no') in regs:
     858                student['Error'] = "Duplicate" 
     859                no_import.append( format_error % student)
     860                continue
     861            cert_id = makeCertificateCode(student.get('Coursemajorcode'))
     862            if cert_id not in certs.keys():
     863                student['Error'] = "No Certificate %s" % cert_id
     864                no_import.append( format_error % student)
     865                continue
     866            try:
     867                table.addRecord(**student)
     868            except ValueError:
     869                import pdb;pdb.set_trace()
     870                student['Error'] = "Duplicate" 
     871                no_import.append( format_error % student)
     872                continue
     873            regs.append(student.get('matric_no'))
     874            imported.append(format % student)
     875            tr_count += 1
     876            if tr_count > 1000:
     877                if len(no_import) > 0:
     878                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
     879                             '\n'.join(no_import))
     880                    no_import = []
     881                    imported = []
     882                open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
     883                                            '\n'.join(no_import))
     884                em = '%d transactions commited total %s\n' % (tr_count,total)
     885                transaction.commit()
     886                logger.info(em)
     887                total += tr_count
     888                tr_count = 0
     889        open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
     890                                            '\n'.join(no_import))
     891        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
     892                                                '\n'.join(no_import))
     893        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
     894    ###)
     895
     896    security.declareProtected(ModifyPortalContent,"importResults")###(
     897    def importResults(self):
     898        """load Registered Studentdata from CSV values"""
     899        import transaction
     900        import random
     901        #from pdb import set_trace
     902        wftool = self.portal_workflow
     903        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
     904        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
     905        tr_count = 1
     906        total = 0
     907        #name = 'pume_results'
     908        name = 'Results'
     909        table = self.results_import
     910        no_import = []
     911        imported = []
     912        logger = logging.getLogger('Import.%s' % name)
     913        try:
     914            results = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
     915        except:
     916            logger.error('Error reading %s.csv' % name)
     917            return
     918        l = self.portal_catalog({'meta_type': "Course"})
     919        courses = [f.getId for f in l]
     920        start = True
     921        res = table()
     922        regs = []
     923        if len(res) > 0:
     924            regs = [s.key for s in res]
     925        no_course = []
     926        no_course_list = []
     927        course_count = 0
     928        for result in results:
     929            if start:
     930                start = False
     931                logger.info('start loading from %s.csv' % name)
     932                s = ','.join(['"(%s)"' % fn for fn in result.keys()])
     933                imported.append(s)
     934                no_import.append('%s,"Error"' % s)
     935                format = ','.join(['"%%(%s)s"' % fn for fn in result.keys()])
     936                format_error = format + ',"%(Error)s"'
     937                no_certificate = "no certificate %s" % format
     938            course_id = result.get('CosCode')
     939            key = result.get('matric_no')+course_id
     940            if key == '':
     941                result['Error'] = "Empty matric_no" 
     942                no_import.append( format_error % result)
     943                continue
     944            if key in regs:
     945                result['Error'] = "Duplicate" 
     946                no_import.append( format_error % result)
     947                continue
     948            if course_id not in courses:
     949                if course_id not in no_course:
     950                    course_count +=1
     951                    no_course.append(course_id)
     952                    no_course_list.append('"%s"' % course_id)
     953                    #result['Error'] = "No Course"
     954                    #logger.info(format_error % result)
     955            result['key'] = key
     956            try:
     957                table.addRecord(**result)
     958            except ValueError:
     959                import pdb;pdb.set_trace()
     960                result['Error'] = "Duplicate" 
     961                no_import.append( format_error % result)
     962                continue
     963            regs.append(key)
     964            imported.append(format % result)
     965            tr_count += 1
     966            if tr_count > 1000:
     967                if len(no_import) > 0:
     968                    open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
     969                             '\n'.join(no_import))
     970                    no_import = []
     971                open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
     972                                            '\n'.join(imported))
     973                imported = []
     974                if no_course_list:
     975                    open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write(
     976                                            '\n'.join(no_course_list))
     977                    no_course_list = []
     978                em = '%d transactions commited total %s\n courses not found %s' % (tr_count,total,course_count)
     979                transaction.commit()
     980                logger.info(em)
     981                total += tr_count
     982                tr_count = 0
     983        open("%s/import/%simported%s.csv" % (i_home,name,current),"a").write(
     984                                            '\n'.join(imported))
     985        open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
     986                                                '\n'.join(no_import))
     987        if no_course_list:
     988            open("%s/import/%sno_courses%s.csv" % (i_home,name,current),"a").write(
     989                                    '\n'.join(no_course_list))
     990        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
     991    ###)
     992
    811993    security.declareProtected(ModifyPortalContent,"updateStudyCourse")###(
    812994    def updateStudyCourse(self):
     
    10851267            no_import = open("%s/import/%s_not_imported.csv" % (i_home,name),"w")
    10861268            no_import.write('"Matnumber","CosCode","Ansbook","CosStuatus","Session","Mat_Cos","Score","CarryLevel","Grade","Weight","Semster","Verdict","Level","id","GPA"\n')
    1087         logger = logging.getLogger('Ímport.%s' % name)
     1269        logger = logging.getLogger('import.%s' % name)
    10881270        logger.info('Start loading from %s.csv' % name)
    10891271        students_folder = self.portal_catalog({'meta_type': 'StudentsFolder'})[-1].getObject()
Note: See TracChangeset for help on using the changeset viewer.