[Zope-dev] External Method creation of ZClass instances

Jason Spisak webmaster@mtear.com
Thu, 15 Jul 1999 18:22:29 -0800


Zope Gang,

I am was using this external method it import the properties of a ZClass
instance and create it from a flat text file.  It worked fine under Zope
1.10, but now I am
trying like a bear to get it to jive with Zope 2, and failing miserably.
I'm just offering it up
in case anybody sees anything obvious that I missed.  I can create these
objects fine in the management interface using the same methods, and I
am using the same import files as I did before.


import string

def import_candidate(self,file,REQUEST,RESPONSE,URL2):
    """
    External Method to import Candidate data and build Candidate objects.
    
    file is a tab delimited file object (obtained from a file upload form).
    
    The file schema is:

    ID,FIRST_NAME,LAST_NAME,COMPANY,ADDRESS,CITY,STATE,ZIP,WORK_AREA_CODE,HOME_AREA_CODE,
    WORK_PHONE,HOME_PHONE,WORK_EXT,TITLE,RECRUITER,SKILLS,SKILLS,SKILLS,SALARY,SKILLS,
    SKILLS,SKILLS,SKILLS,CONTRACTOR,EMAIL,WAGNER,CREATE

    """
    added=0
    failed=[]
    while 1:
        line=file.readline()
        if line=="":
            break
        fields=string.split(line,'\t')
        id=fields[0]
        first_name=fields[1]
        last_name=fields[2]
        company=fields[3]
        address=fields[4]
        city=fields[5]
        state=fields[6]
        zip=fields[7]
        work_area_code=fields[8]
        work_phone=fields[10]
        work_ext=fields[12]
        home_area_code=fields[9]
        home_phone=fields[11]
        title=fields[13]
        recruiter=fields[14]
        skills="%s %s %s %s %s %s %s" % (fields[15], fields[16],
fields[17], fields[19], fields[20], fields[21], fields[22])
        salary=fields[18]
        contractor=fields[23]
        email=fields[24]
        wagner=fields[25]
        create=fields[26]
        if id=='ID':
            continue # bogus first line
        #try:
        REQUEST.set('id',id)
        REQUEST.set('first_name',first_name)
        REQUEST.set('last_name',last_name)
        REQUEST.set('current_company',company)
        REQUEST.set('address',address)
        REQUEST.set('city',city)
        REQUEST.set('state',state)
        REQUEST.set('zip',zip)
        REQUEST.set('work_area_code',work_area_code)
        REQUEST.set('work_phone',work_phone)
        REQUEST.set('work_extension',work_ext)
        REQUEST.set('home_area_code',home_area_code)
        REQUEST.set('home_phone',home_phone)
        REQUEST.set('title',title)
        REQUEST.set('recruiter',recruiter)
        if salary:
                REQUEST.set('salary',salary)
        else:
                REQUEST.set('salary',0)
        REQUEST.set('contractor',contractor)
        REQUEST.set('email_address',email)
        REQUEST.set('skills',skills)
        REQUEST.set('wagner',wagner)
        REQUEST.set('fax_phone','')
        REQUEST.set('fax_area_code','')
        REQUEST.set('fax_extension','')
        REQUEST.set('pager_phone','')
        REQUEST.set('pager_area_code','')
        REQUEST.set('pager_extension','')
        REQUEST.set('cell_area_code','')
        REQUEST.set('cell_phone','')
        REQUEST.set('resume_in',1)
        if create:
                REQUEST.set('create_date',create)
        else:
                REQUEST.set('create_date','1999/05/17')
        REQUEST.set('resume','/home/jason/resumes/' + id + '.html')
        method=self.manage_addProduct['Candidate'].CandidateClass_add
        method(method,REQUEST=REQUEST,RESPONSE=RESPONSE,
                    DestinationURL=lambda url=URL2: url)
        added=added+1
        #except:
        #    failed.append(title) 
    return """<html>
            <p>Added %d candidates.</p>
            <p>Failed to add: <br>
            %s
            </p>
            </html>""" % (added, string.join(failed,"<br>"))

The Candidate meta_type is created by a Factory for a ZClass.
The CandidateClass_add is the constructor, and it looks like this:


<!--#with "CandidateClass.createInObjectManager(REQUEST['id'], REQUEST)"-->

<!--#call "propertysheets.properties.manage_changeProperties(
    title=REQUEST['title'],
    first_name=REQUEST['first_name'],
    last_name=REQUEST['last_name'],
    recruiter=REQUEST['recruiter'],
    email_address=REQUEST['email_address'],
    address=REQUEST['address'],
    city=REQUEST['city'],
    state=REQUEST['state'],
    zip=REQUEST['zip'],
    work_phone=REQUEST['work_phone'],
    work_area_code=REQUEST['work_area_code'],
    work_extension=REQUEST['work_extension'],
    home_phone=REQUEST['home_phone'],
    home_area_code=REQUEST['home_area_code'],
    fax_phone=REQUEST['fax_phone'],
    fax_area_code=REQUEST['fax_area_code'],
    pager_phone=REQUEST['pager_phone'],
    pager_area_code=REQUEST['pager_area_code'],
    pager_extension=REQUEST['pager_extension'],
    cell_phone=REQUEST['cell_phone'],
    cell_area_code=REQUEST['cell_area_code'],
    current_company=REQUEST['current_company'],
    contractor=REQUEST['contractor'],
    skills=REQUEST['skills'],
    wagner=REQUEST['wagner'],
    resume_in=REQUEST['resume_in'],
    salary=REQUEST['salary'],
    create_date=REQUEST['create_date']
)"-->

<!--#var index_object-->

<!--#/with-->



The index_object method a add it to the Catalog.  I'll still be trying
to figure it out long after the sun goes own and comes up again.  Many
thanks to all in advance.

All my best,

Jason Spisak
webmaster@mtear.com