Create Content from Debug Shell

Create Content from Debug Shell

Maybe you knew this, but I just found out and I had to write it… otherwise I’ll forget it.

First start a plone instance in debug mode:

  1. $ bin/instance debug
  2. Starting debugger (the name “app” is bound to the top-level Zope object)

Now you have your python shell to play!
First of all you need a user, so let’s try the admin user:

  1. from AccessControl.SecurityManagement import newSecurityManager
  2. user = app.acl_users.authenticate(“admin”, “admin”, None)
  3. newSecurityManager(None, user)

As you probably guessed the first “admin” is the username, and the second one the password.
OK, now we have enough permissions to create our content let’s move on, imagine you have a
plone site called “development” and a folder called “test” in it.

  1. container = app.development.test
  2. id = container.invokeFactory(type_name=“News Item”, id=‘new-news’)
  3. obj = container[id]
  4. obj.setTitle(‘This is my dumb test’)
  5. import transaction
  6. transaction.commit()
  7. app._p_jar.sync()

Now you can go into your plone site, and you’ll have the new news, if you can’t see them try going to:

http://localhost:8080/development/test/new-news

Remember changing the url according to your own.

Hope this helps someone

About the Author

I'm a developer, mostly interested in Computer Science. Favorite languages: C, ObjC and Python. Hobbies: Music. Favorite Games: Assassin's Creed II, MGS Series