Changeset 1747 for WAeUP_SRP/trunk


Ignore:
Timestamp:
7 May 2007, 20:56:24 (18 years ago)
Author:
joachim
Message:

new function in WAeUPTool importData
usage from url:
.../uniben/waeup_tool/importData?filename=PreviousTest?&name=student

Location:
WAeUP_SRP/trunk
Files:
3 added
6 edited

Legend:

Unmodified
Added
Removed
  • WAeUP_SRP/trunk/WAeUPTool.py

    r1730 r1747  
    2828from OFS.SimpleItem import SimpleItem
    2929
     30from Products.CPSSchemas.DataStructure import DataStructure
     31from Products.CPSSchemas.DataModel import DataModel
     32from Products.CPSSchemas.StorageAdapter import MappingStorageAdapter
    3033from Products.CMFCore.ActionProviderBase import ActionProviderBase
    3134from Products.CMFCore.permissions import View
     
    3437from Products.CMFCore.utils import UniqueObject
    3538from Products.CMFCore.URLTool import URLTool
     39from Products.CMFCore.utils import getToolByName
    3640from Students import makeCertificateCode
    3741from Globals import package_home,INSTANCE_HOME
     
    276280        self.portal_workflow.doActionFor(student_obj,wfaction)
    277281        student_obj.manage_setLocalRoles(sid, ['Owner',])
    278 ##        matric_no = dict.get('matric_no')
    279 ##        email = dict.get('email')
    280 ##        #level = dict.get('level')
    281 ##        jamb_reg_no = dict.get('jamb_reg_no')
    282 ##        study_course = dict.get('study_course')
    283 ##        self.portal_workflow.doActionFor(student_obj,wfaction)
    284 ##        student_obj.manage_setLocalRoles(sid, ['Owner',])
    285 ##        student_obj.invokeFactory('StudentApplication','application')
    286 ##        application = student_obj.application
    287 ##        self.portal_workflow.doActionFor(application,'open',dest_container=application)
    288 ##        da = {'Title': 'Application Data'}
    289 ##        student_obj.invokeFactory('StudentPersonal','personal')
    290 ##        da['entry_mode'] = entry_mode
    291 ##        personal = student_obj.personal
    292 ##        self.portal_workflow.doActionFor(personal,'open',dest_container=personal)
    293 ##        dp = {'Title': 'Personal Data'}
    294 ##        student_obj.invokeFactory('StudentClearance','clearance')
    295 ##        clearance = student_obj.clearance
    296 ##        self.portal_workflow.doActionFor(clearance,'open',dest_container=clearance)
    297 ##        dc = {'Title': 'Clearance/Eligibility Record'}
    298 ##        dc['matric_no'] = matric_no
    299 ##        da['app_email'] = dp['email'] = email
    300 ##        da['jamb_reg_no'] = jamb_reg_no
    301 ##        dp['firstname'] = dict.get('firstname')
    302 ##        dp['middlename'] = dict.get('middlename')
    303 ##        dp['lastname'] = dict.get('lastname')
    304 ##        da['jamb_lastname'] = "%(firstname)s %(middlename)s %(lastname)s" % dict
    305 ##        sex = dict.get('sex')
    306 ##        if sex:
    307 ##            da['jamb_sex'] = 'F'
    308 ##        else:
    309 ##            da['jamb_sex'] = 'M'
    310 ##        dp['sex'] = sex
    311 ##        application.getContent().edit(mapping=da)
    312 ##        self.portal_workflow.doActionFor(application,'close',dest_container=application)
    313 ##        personal.getContent().edit(mapping=dp)
    314 ##        clearance.getContent().edit(mapping=dc)
    315 ##        self.portal_workflow.doActionFor(clearance,'close',dest_container=clearance)
    316 ####        catd = {}
    317 ####        catd['id'] = sid
    318 ####        catd['entry_mode']= entry_mode
    319 ####        catd['email'] = email
    320 ####        catd['jamb_reg_no'] = jamb_reg_no
    321 ####        catd['matric_no'] = matric_no
    322 ####        catd['name'] = "%(firstname)s %(middlename)s %(lastname)s" % dp
    323 ####        catd['sex'] = dp['sex']
    324 ####        catd['level'] = level
    325 ####        certificate_brain = self.getCertificateBrain(study_course)
    326 ####        if certificate_brain:
    327 ####            cpath = certificate_brain.getPath().split('/')
    328 ####            catd['faculty'] = cpath[-4]
    329 ####            catd['department'] = cpath[-3]
    330 ####            catd['course'] = study_course
    331 ####        self.students_catalog.addRecord(**catd)
    332 ##        #
    333 ##        # Study Course
    334 ##        #
    335 ##        student_obj.invokeFactory('StudentStudyCourse','study_course')
    336 ##        sc = student_obj.study_course
    337 ##        self.portal_workflow.doActionFor(sc,'open',dest_container=sc)
    338 ##        dsc = {}
    339 ##        dsc['study_course'] = study_course
    340 ##        #dsc['current_level'] = level
    341 ##        sc.getContent().edit(mapping=dsc)
    342 ##
    343282        return sid,password
    344283    ###)
     
    757696    ###)
    758697
     698    security.declareProtected(ModifyPortalContent,'create_student') ###(
     699    def create_student(self,dict):
     700        "create a students data"
     701        logger = logging.getLogger('WAeUPTool.create_student')
     702        students_folder = self.portal_url.getPortalObject().campus.students
     703        jamb_reg_no = dict.get('jamb_reg_no',None)
     704        if jamb_reg_no:
     705            res = self.students_catalog(jamb_reg_no = jamb_reg_no)
     706            if res:
     707                return '',"jamb_reg_no exists"
     708        matric_no = dict.get('matric_no',None)
     709        if matric_no:
     710            res = self.students_catalog(matric_no = matric_no)
     711            if res:
     712                return '',"matric_no exists"
     713        sid = self.waeup_tool.generateStudentId('?')
     714        students_folder.invokeFactory('Student', sid)
     715        student_obj = getattr(students_folder,sid)
     716        #password = self.generatePassword()
     717        #self.makeStudentMember(sid,password)
     718        f2t = self.student_field2types
     719        for pt in f2t.keys():
     720            student_obj.invokeFactory(pt,f2t[pt]['id'])
     721            sub_obj = getattr(student_obj,f2t[pt]['id'])
     722            sub_doc = sub_obj.getContent()
     723            self.portal_workflow.doActionFor(sub_obj,'open',dest_container=sub_obj)
     724            d = {'Title': f2t[pt]['title']}
     725            for field in f2t[pt]['fields']:
     726                d[field] = dict.get(field,'')
     727            if pt == 'StudentPersonal':
     728                d['sex'] = dict.get('sex') == 'F'
     729            sub_doc.edit(mapping = d)
     730            self.portal_workflow.doActionFor(sub_obj,'close',dest_container=sub_obj)
     731##        entry_session = dict.get('entry_session')
     732##        wfaction = 'return'
     733##        if entry_session == self.getSessionId():
     734##            wfaction = 'admit'
     735##        self.portal_workflow.doActionFor(student_obj,wfaction)
     736        student_obj.manage_setLocalRoles(sid, ['Owner',])
     737        return sid,''
     738    ###)
     739
    759740    security.declareProtected(ModifyPortalContent,"importData")###(
    760     def importData(self,name):
     741    def importData(self,filename,name):
    761742        """load data from CSV values"""
    762743        import transaction
    763744        import random
     745        logger = logging.getLogger('WAeUPTool.importData')
    764746        current = DateTime.DateTime().strftime("%d-%m-%y_%H_%M_%S")
    765747        students_folder = self.portal_url.getPortalObject().campus.students
     
    767749        tr_count = 1
    768750        total = 0
    769         from pdb import set_trace;set_trace()
    770751        iname = "import_%s" % name
    771         schema = getattr(self.portal_schemas,iname ,None)
     752        stool = getToolByName(self, 'portal_schemas')
     753        ltool = getToolByName(self, 'portal_layouts')
     754        schema = stool._getOb(iname)
    772755        if schema is None:
    773             logger.error('No such schema %s' % iname)
     756            logger.error('No such schema import_%s' % iname)
    774757            return
    775         layout = getattr(self.portal_layouts,iname,None)
     758        layout = ltool._getOb(iname)
    776759        if layout is None:
    777             logger.error('No such layout %s' % iname)
     760            logger.error('No such layout import_%s' % iname)
    778761            return
    779762        validators = {}
    780763        for widget in layout.keys():
    781764            validators[widget] = layout[widget].validate 
    782         no_import = []
     765        creator = getattr(self, 'create_%s' % name,None)
     766        if creator is None:
     767            logger.error('No creator function create_%s' % name)
     768            return
     769        not_imported = []
    783770        imported = []
    784         logger = logging.getLogger('WAeUPTool.importData')
    785771        try:
    786             items = csv.DictReader(open("%s/import/%s.csv" % (i_home,name),"rb"))
     772            items = csv.DictReader(open("%s/import/%s.csv" % (i_home,filename),"rb"))
    787773        except:
    788             logger.error('Error reading %s.csv' % name)
     774            logger.error('Error reading %s.csv' % filename)
    789775            return
    790776        for item in items:
     
    792778            if start:
    793779                start = False
     780                adapters = [MappingStorageAdapter(schema, item)]
     781                dm = DataModel(item, adapters,context=self)
    794782                logger.info('Start importing from %s.csv' % name)
    795783                import_keys = item.keys()
     
    802790                    logger.error('%s not in layout.' % diff_set)
    803791                    return
    804                 s = ','.join(['"%s"' % fn for fn in import_keys])
     792                s = '"id",'+','.join(['"%s"' % fn for fn in import_keys])
    805793                imported.append(s)
    806                 no_import.append('%s,"Error"' % s)
     794                not_imported.append('%s,"Error"' % s)
    807795                format = ','.join(['"%%(%s)s"' % fn for fn in import_keys])
    808796                format_error = format + ',"%(Error)s"'
     797            ds = DataStructure(data=item,datamodel=dm)
     798            error_string = ""
    809799            for k in import_keys:
    810                 if validators[k](item):
    811                     pass
     800                if not validators[k](ds):
     801                    error_string += " %s : %s" % (k,ds.getError(k))
     802            if not error_string:
     803                item['id'],error = creator(dm)
     804                if error:
     805                    error_string += error
     806            if error_string:
     807                item['Error'] = error_string
     808                not_imported.append(format_error % item)
     809            else:
     810                imported.append(format % dm)
    812811            if tr_count > 1000:
    813                 if len(no_import) > 0:
    814                     open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
    815                              '\n'.join(no_import) + '\n')
    816                     no_import = []
     812                if len(not_imported) > 0:
     813                    open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write(
     814                             '\n'.join(not_imported) + '\n')
     815                    not_imported = []
     816                if len(imported) > 0:
     817                    open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write(
     818                             '\n'.join(not_imported) + '\n')
     819                    imported = []
    817820                em = '%d transactions committed\n' % (tr_count)
    818821                transaction.commit()
     
    821824                total += tr_count
    822825                tr_count = 0
    823         open("%s/import/%s_not_imported%s.csv" % (i_home,name,current),"a").write(
    824                                                 '\n'.join(no_import))
     826        if len(imported) > 1:
     827            open("%s/import/%s_imported%s.csv" % (i_home,filename,current),"a").write(
     828                                                '\n'.join(imported))
     829        if len(not_imported) > 1:
     830            open("%s/import/%s_not_imported%s.csv" % (i_home,filename,current),"a").write(
     831                                                '\n'.join(not_imported))
    825832        return self.REQUEST.RESPONSE.redirect("%s" % self.REQUEST.get('URL1'))
    826833    ###)
  • WAeUP_SRP/trunk/Widgets.py

    r1718 r1747  
    1414from AccessControl import getSecurityManager
    1515from Products.WAeUP_SRP.Students import getStudentByRegNo
     16from Products.WAeUP_SRP.Academics import makeCertificateCode
    1617from re import compile
    1718import logging
     
    255256        #from Products.zdb import set_trace
    256257        #set_trace()
    257         valid = CPSStringWidget.validate(self, datastructure, **kw)
     258##        valid = CPSStringWidget.validate(self, datastructure, **kw)
     259##        if not valid:
     260##            return 0
     261        widget_id = self.getWidgetId()
     262        value = makeCertificateCode(datastructure[widget_id]).upper()
    258263        id_pat_str = r"\S"
    259264        inv_id_pat = compile(r"^%s$" % id_pat_str)
    260         if not valid:
    261             return 0
    262         else:
    263             widget_id = self.getWidgetId()
    264             value = datastructure[widget_id].upper()
    265             err = 0
    266             if len(value.split()) > 1:
    267                 err = 'Invalid Id, Id contains space(s).'
    268             elif not self.portal_catalog(portal_type='Certificate',id=value):
    269                 err = 'No such certificate'
    270             if err:
    271                 datastructure.setError(widget_id, err)
    272             else:
    273                 datamodel = datastructure.getDataModel()
    274                 datamodel[self.fields[0]] = value
    275             return not err
     265        err = 0
     266        if len(value.split()) > 1:
     267            err = 'Invalid Id, Id contains space(s).'
     268        elif not self.portal_catalog(portal_type='Certificate',id=value):
     269            err = 'No such certificate'
     270        if err:
     271            datastructure.setError(widget_id, err)
     272        else:
     273            datamodel = datastructure.getDataModel()
     274            datamodel[self.fields[0]] = value
     275        return not err
    276276
    277277InitializeClass(StudyCourseWidget)
     
    342342         )
    343343    reference = "student"
     344    matric_no_catalog = 'returning_import'
    344345    check_fields = ("Firstname", "Middlename","Lastname")
    345346    def validate(self, datastructure, **kw):
     
    347348        logger = logging.getLogger('Widgets.SecretWidget.validate')
    348349        valid = CPSStringWidget.validate(self, datastructure, **kw)
    349         #import pdb;pdb.set_trace()
    350350        widget_id = self.getWidgetId()
    351351        value = datastructure[widget_id].upper()
    352352        err = 0
    353353        record = datastructure.get(self.reference,None)
     354        #import pdb;pdb.set_trace()
    354355        if not valid or len(value) < 2:
    355356            err = 'Invalid String'
     
    363364                if len(cv.split()) > 1:
    364365                    for splited in cv.split():
    365                         cvs.append(splited)
     366                        cvs.append(splited.strip())
    366367                else:
    367368                    cvs.append(cv)
     369            for cv in cvs:
    368370                if cv  == value.upper():
    369371                    found = True
     
    391393    """ MatricNo Widget"""
    392394    meta_type = "MatricNo Widget"
     395
     396    _properties = CPSStringWidget._properties + (
     397        {'id': 'matric_no_catalog', 'type': 'string', 'mode': 'w',
     398         'label': 'Catalog to search for MatricNo'},
     399        { 'id': 'results_catalog', 'type': 'string', 'mode': 'w',
     400         'label': 'Results Catalog'},
     401         )
     402    matric_no_catalog = "" #the catalog to search for matric_no
     403    results_import = "results_import" #results catalog
     404
    393405    def validate(self, datastructure, **kw):
    394406        """Validate datastructure and update datamodel."""
     407        #import pdb;pdb.set_trace()
    395408        valid = CPSStringWidget.validate(self, datastructure, **kw)
    396409        logger = logging.getLogger('Widgets.MatricNoWidget.validate')
    397         #import pdb;pdb.set_trace()
    398         returning = self.returning_import
    399         results = self.results_import
     410        returning = getattr(self,self.matric_no_catalog)
     411        results = getattr(self,self.results_catalog,None)
    400412        err = 0
    401413        widget_id = self.getWidgetId()
     
    412424                    logger.info('matric_no %s not found' % value)
    413425                    err = 'No student with matric_no %s.' % (value)
    414                     continue
     426                    break
    415427                datastructure['student'] = res[0]
    416                 res = results(matric_no = value)
    417                 if len(res) < 1:
    418                     err = 'No results with matric_no %s.' % (value)
    419                     continue
    420                 datastructure['results'] = res
     428                if results is not None:
     429                    res = results(matric_no = value)
     430                    if len(res) < 1:
     431                        err = 'No results with matric_no %s.' % (value)
     432                        continue
     433                    datastructure['results'] = res
    421434                break
    422435        if err:
  • WAeUP_SRP/trunk/profiles/default/layouts.xml

    r1728 r1747  
    2929  <object name="student_course_result" meta_type="CPS Layout"/>
    3030  <object name="student_create" meta_type="CPS Layout"/>
     31  <object name="student_created_login" meta_type="CPS Layout"/>
    3132  <object name="student_personal" meta_type="CPS Layout"/>
    3233  <object name="student_personal_fe" meta_type="CPS Layout"/>
  • WAeUP_SRP/trunk/profiles/default/layouts/import_student.xml

    r1728 r1747  
    3232  <property name="display_width">15</property>
    3333 </widget>
    34  <widget name="address" meta_type="String Widget">
     34 <widget name="perm_address" meta_type="String Widget">
    3535  <property name="title">Address</property>
    3636  <property name="fields">
    37    <element value="address"/>
     37   <element value="perm_address"/>
    3838  </property>
    3939  <property name="label">Address</property>
     
    122122  <property name="label">Current Session</property>
    123123  <property name="label_edit">Current Session</property>
     124  <property name="description">The currently paid session</property>
     125  <property name="help">The last session the student has paid his schoolfee</property>
    124126  <property name="readonly_layout_modes"/>
    125127  <property name="hidden_layout_modes"/>
     
    134136  <property name="label">Current Level</property>
    135137  <property name="label_edit">Current Level</property>
     138  <property name="description">The level of the last paid session</property>
     139  <property name="help">The last level the student has paid his schoolfee</property>
    136140  <property name="readonly_layout_modes"/>
    137141  <property name="hidden_layout_modes"/>
     
    146150  <property name="label">Current Verdict</property>
    147151  <property name="label_edit">Current Verdict</property>
     152  <property name="description">The verdict of the current level</property>
     153  <property name="help">The verdict of the level the student has paid his schoolfee</property>
    148154  <property name="readonly_layout_modes"/>
    149155  <property name="hidden_layout_modes"/>
  • WAeUP_SRP/trunk/profiles/default/layouts/student_session_results_search.xml

    r1449 r1747  
    5151  <property name="hidden_layout_modes"/>
    5252  <property name="hidden_readonly_layout_modes"/>
     53  <property name="matric_no_catalog">returning_import</property>
     54  <property name="results_catalog">results_import</property>
    5355  <property name="display_width">10</property>
    5456  <property name="size_max">10</property>
     
    105107 <widget name="secret" meta_type="Secret Widget">
    106108  <property name="title">Secret Widget</property>
    107   <property name="fields">
    108    <element value="secret"/>
     109  <property name="check_fields">
     110   <element value="Firstname"/>
     111   <element value="Middlename"/>
     112   <element value="Lastname"/>
    109113  </property>
    110114  <property name="label">Firstname, Middlename or Lastname</property>
  • WAeUP_SRP/trunk/skins/waeup_default/sessions_voc.py

    r1736 r1747  
    66"""
    77if key is None:
    8     l = [('unknown','unknown')]
     8    l = [('xx','unknown')]
    99    for y in range(1998,2008):
    1010        l.append( ('%s' % str(y)[-2:],'%4d/%4d' % (y,y+1)))
     
    1212
    1313if len(key) != 2:
    14     return "invalid Session %s" % key
     14    return 'unknown'
    1515try:
    1616    y = int(key)
    1717except:
    18     return "invalid Session %s" % key
     18    return 'unknown'
    1919if y < 90:
    2020    cent = 2000
Note: See TracChangeset for help on using the changeset viewer.