Use ZODB with transaction

Good morning, Plone community!

I am in the process of creating a migration script using transaction and ZODB. When running the script, there are important pieces of data that get skipped. How would I go about fixing this problem in my code?

The code which I am using for the migration is below:


class Cvmigration(BrowserView):
       # init function here
    def __call__(self):
        alsoProvides(self.request, IDisableCSRFProtection)
        portal = api.portal.get()
        portal_catalog = api.portal.get_tool('portal_catalog')
        current_path = '/'.join(self.context.getPhysicalPath())
        print('Starting Migration')
        colleges = portal_catalog(portal_type='college')
        for college in colleges:
            print('Creating directory')
            college_id = 'directory'
            collegeLocation = current_path + '/' + college_id
            obj = college.getObject()
            try:
                transaction.begin()
                collegeObject = api.content.create(
                    type='directory',
                    id=college_id,
                    schoolsVocab=obj.schoolsVocab,
                    departmentVocab=obj.departmentVocab,
                    programVocab=obj.programVocab,
                    degreeVocab=obj.degreeVocab,
                    teachingTitleVocab=obj.teachingTitleVocab,
                    title=obj.title,
                    safe_id=True,
                    container=portal
                )
                collegeObject.manage_setLocalRoles('rnunez', ['Editor'])
                transaction.commit()
                print('Directory created ' + str(collegeObject.absolute_url()))
                print(collegeLocation)
            except IndexError as e:
                print(e)
                print('Failed to create directory ' + str(obj.absolute_url()))

        faculties = portal_catalog(portal_type="Faculty")
        for faculty in faculties:
            object = faculty.getObject()
            facultyLocation = '/'.join(object.getPhysicalPath())
            print(str(facultyLocation) + ' - migrating')
            try:
                image = NamedBlobImage(data=object.image.data,
                                       contentType=object.image.content_type,
                                       filename=unicode(object.image.filename))
            except AttributeError:
                image = None
            try:
                transaction.begin()
                facultyObject = api.content.create(
                    container=collegeObject,
                    type='faculty2',
                    id=object.id.lower(),
                    subject=object.subject,
                    userid=object.userid,
                    employeeType=object.employeeType,
                    first_name=object.first_name,
                    last_name=object.last_name,
                    teachingTitle=object.teachingTitle,
                    otherTitle=object.otherTitle,
                    email=object.email,
                    website=object.website,
                    officePhone=object.officePhone,
                    officeLocation=object.officeLocation,
                    officeHours=object.officeHours,
                    image=image,
                    dept=object.dept,
                    currentStatus=object.currentStatus,
                    orgStatus=object.orgStatus,
                    school=object.school,
                    prog=object.prog,
                    education=object.education,
                    managers=str(object.managers).replace('()-', '').replace('and', '-').replace(',', '').replace('&',
                                                                                                                  '-').lower(),
                    ssn=object.ssn,
                    citizen=object.citizen,
                    tenure=object.tenure,
                    Senior=object.Senior,
                    street=object.street,
                    apt=object.apt,
                    city=object.city,
                    state=object.state,
                    zipCode=object.zipCode,
                    homePhone=object.homePhone,
                    allowDiscussion=object.allowDiscussion,
                    location=object.location,
                    language=object.language,
                    contributors=object.contributors,
                    creators=object.creators,
                    rights=object.rights,
                    # description=object.description,
                    effectiveDate=object.effectiveDate,
                    expirationDate=object.expirationDate,
                    areaExpertise=object.areaExpertise,
                    briefBio=str(object.briefBio),
                    experienceFT=object.experienceFT,
                    experiencePT=object.experiencePT,
                    experienceNA=object.experienceNA,
                    employmentRecord=object.employmentRecord,
                    books=object.books,
                    articles=object.articles,
                    refereedProceedings=object.refereedProceedings,
                    nonRefereedProceedings=object.nonRefereedProceedings,
                    chapters=object.chapters,
                    monographs=object.monographs,
                    reviews=object.reviews,
                    presented=object.presented,
                    patents=object.patents,
                    pubEnhance=object.pubEnhance,
                    professional=object.professional,
                    grants=object.grants,
                    serviceDepartment=object.serviceDepartment,
                    serviceSchool=object.serviceSchool,
                    serviceCollege=object.serviceCollege,
                    serviceGraduateCenter=object.serviceGraduateCenter,
                    serviceUniversity=object.serviceUniversity,
                    officeHeld=object.officeHeld,
                    activities=object.activities,
                    coursesTaught=object.coursesTaught,
                    developed=object.developed,
                    hidecv=object.hidecv,
                    scholarEnhance=str(object.scholarEnhance),
                    teachEnhance=str(object.teachEnhance),
                    safe_id=True,
                )
                facultyObject.manage_setLocalRoles('rnunez', ['Editor'])
                state = api.content.get_state(obj=object)
                api.content.transition(obj=facultyObject, to_state=state)
                transaction.commit()
                print('Faculty created ' + str(facultyObject.absolute_url()))
            except IndexError as e:
                print(e)
                print('Failed to create Faculty ' + str(object.absolute_url()))

            # other submigrations in Faculty folder go here...
           
# staff goes here
# success message goes here

I thank you kindly.

Sincerely,

rbrown12

portal_catalog usually restricts search results to active content viewable by the current user. Therefore, it may not return all relevant content objects.

Either log in as a Zope "Manager" or use the catalog's "unrestricted search method".

1 Like

Relevant docs: Command-line interaction and scripting — Plone Documentation v5.2

1 Like

Like others, I think this is simply an issue of visibility and permission, re: catalog results, which leaves you with some adjustment choices:

  1. Use portal_catalog.unrestrictedSearchResults() instead of portal_catalog.call or portal_catalog.searchResults().
  2. In conjunction with the above, also use the catalog brain's _unrestrictedGetObject() method (faculty._unrestricteGetObject()) instead of getObject().
  3. Consider making your query run in the context of a user who can see those objects using api.env.adopt_user().
  4. Throw print and/or import pdb; pdb.set_trace() statements somewhere before each of your loops to step through this to see what is (and how many are) included in the results of your query.

Plone Foundation Code of Conduct