Skip to content

Instantly share code, notes, and snippets.

@pigeonflight
Last active March 5, 2019 12:19
Show Gist options
  • Save pigeonflight/bd593a0838bb4c4c679a7d4d022eb9d3 to your computer and use it in GitHub Desktop.
Save pigeonflight/bd593a0838bb4c4c679a7d4d022eb9d3 to your computer and use it in GitHub Desktop.
Plone: convert one content type with leadimage to another content type

In this example we have a Folder which has lead image behaviour applied to it. We want a special folderish type called category to replace the folders in a specific location of the site.

This script is invoked with the zope controller for your Zope/Plone site.

   bin/instance -OPlone run {scriptname}

or in your site is not at the root of your Zope application server then the full path to the site

  bin/instance -O'full/path/to/plone' run {scriptname}

This is also a simple example of setting image-like content when dealing with a lead image.

Read through this code if you want to know more about dealing with images and Plone https://gist.github.com/pigeonflight/2f2fb187f2681bed90cf0c1baffe09d4

################################
#
# run this script using
# bin/instance -OPlone run {scriptname}
# assumptions:
# we assume that you have a working zope server with an instance
# of plone located at the root named "Plone"
# we also assume that you have an image named "
#
##################################################
from plone import api
import transaction
from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManager import setSecurityPolicy
from Testing.makerequest import makerequest
from Products.CMFCore.tests.base.security import PermissiveSecurityPolicy, OmnipotentUser
def spoofRequest(app):
"""
Make REQUEST variable to be available on the Zope application server.
This allows acquisition to work properly
"""
_policy=PermissiveSecurityPolicy()
_oldpolicy=setSecurityPolicy(_policy)
newSecurityManager(None, OmnipotentUser().__of__(app.acl_users))
return makerequest(app)
# Enable Faux HTTP request object so we can do "browsery" things on the cmd
app = spoofRequest(app)
request = app.REQUEST
# -------------------------
# -------------------------
#site = app['Plone']
# print site.Title()
api.portal.get()
portal = api.portal.get()
discount_partners = portal.membership['discount-partners']
folders = api.content.find(
context=discount_partners,
depth=1,
portal_type='Folder')
for folder in folders:
folder = folder.getObject()
title,description,image = folder.title,folder.description,folder.image
api.content.delete(obj=folder)
print("---------> ",title)
obj = api.content.create(
type='category',
title=title,
description=description,
container=discount_partners
)
obj.image = image
transaction.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment