Why does Plone hate me?

EDIT:I meant to make up a temporary ID… I need to look up how Plone does that now..

Maybe I’m confused, maybe I’m expecting too much, but is this the best way to generate content in a browser view (and do the nice normalized/duplicate-n ids like stock plone does)?

Warning: contrived mostly-working near-psudeo code ahead


import transaction
import datetime
from zope.app.container.interfaces import INameChooser

... imagine typical BrowserView code, maybe a z3c.form form, etc ...

@property
    def portal(self):
        return getToolByName(self.context, 'portal_url').getPortalObject() 

@button.buttonAndHandler(u'Submit Request')
def process(self):
    folder = getattr(self.portal, "my-folder", None)
    if folder:
		now = datetime.now()
		
        title = "My Great Page, with Weird & Strange characters in the Title."
        
        # "old"? way, sans the duplicate-n bit
        tempid = self.context.plone_utils.normalizeString("New page created at %s" % now.strftime('%s, %f'))
            
        folder.invokeFactory('Document', 
                             tempid,
                             title=title
                            )
        
        transaction.savepoint(1)
        
        newpage = folder[tempid]
        
        myid = chooser.chooseName(title, newpage)
        
        folder.manage_renameObject(newpage.getId(), myid)
        
This entry was posted in Uncategorized. Bookmark the permalink.

6 Responses to Why does Plone hate me?

  1. Alex Clark says:

    Yo! I have no idea what you are asking (and I tend to skim code in blog entries unless it is demonstrative of something that might help me ;-)), but if you don’t get an answer you should try the product-developers list.

  2. David says:

    Here’s the method I’ve used in the past, for generating new content from forms:

    (hopefully formatting will be retained)

    def createObject(self, parent, objtype, title, templateid, **kw):
    objid = self.plone_utils.normalizeString(title)
    objid = self.context.generateUniqueId(objid)
    objid = parent.invokeFactory(objtype, id=objid, title=title )
    context = getattr(parent, objid)
    template = context.restrictedTraverse(‘@@’ + str(templateid))
    context.edit(title=title,
    Text=template(context),
    text=template(context),
    **kw)
    context._renameAfterCreation()
    if templateid in [‘syllabus_view’]:
    context.setPresentation(True)
    context.reindexObject()
    return context

  3. yuri says:

    atreal.massloader/trunk/atreal/massloader/adapter.py

    +from zope.app.container.interfaces import INameChooser
    +
    + chooser = INameChooser(container)
    + # chooseName is expected to choose a name without error,
    + # so no try and except should be needed
    + return chooser.chooseName(txt, container)

    • jjmojojjmojo says:

      That’s what I tried initially, but it wouldn’t return a proper increment if there was another object with the same name in the container. From looking at the code for INameChooser (the implementation registered for a Plone stock Folder, anyway), it’s expecting an object within the container, not the container itself as the second argument to chooseName… Maybe that’s a bug, or deals with the rename-after-add script, I don’t know.

      here’s the code (not sure what version I’m using off the top of my head, but trunk looks pretty accurate), http://svn.plone.org/svn/plone/plone.app.content/trunk/plone/app/content/namechooser.py

      I don’t know if that’s a mistake in the way plone.app.content implements the interface, or if I need to use a multiAdapter to get it in the right frame of mind.

Leave a reply to yuri Cancel reply