What is the right way to create content programmatically

54 views
Skip to first unread message

Toni Haka-Risku

unread,
Apr 25, 2013, 11:24:43 AM4/25/13
to ko...@googlegroups.com
Hi, coming from the Plone-world I would like to know how to create content progammatically in Kotti?

Here is my alternative solution and I'm not sure if it's the right way to do it:

 
view = CommentAddForm(context, request)

        comment = view.add(
            title=u'A title',
            description=u'A description',
            tags=[],
            example_text=message
        )

        from kotti.views.form import get_appstruct
        appstruct = get_appstruct(comment, CommentSchema())

        view.save_success(appstruct)

Toni Haka-Risku

unread,
Apr 25, 2013, 11:26:47 AM4/25/13
to ko...@googlegroups.com
The CommentAddForm is just a view inherited from kotti.views.edit.AddFormView

Daniel Nouri

unread,
Apr 25, 2013, 11:49:23 AM4/25/13
to Kotti mailing list, toju...@gmail.com
"Working with content objects" in the docs has examples of adding
content using Python interface.
http://kotti.readthedocs.org/en/latest/developing/developer-manual.html#working-with-content-objects

Here's an example:

>>> from kotti.resources import get_root
>>> from kotti.resources import Document
>>> root = get_root()
>>> root['first'] = Document(title=u'First page')
>>> root['second'] = Document(title=u'Second page')
>>> root['third'] = Document(title=u'Third page')

Document can be Comment or whatever content type you have. You can
see that the constructor takes keyword arguments for use as
attributes. So if you have a dict with the attributes, maybe that's
'comment' in your example, then you can do:

>>> cm = Comment(**comment_info)

Assign your new object to a parent to have it persisted in the content tree:

>>> mydoc['mycomment'] = cm
> --
> You received this message because you are subscribed to the Google Groups
> "Kotti" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to kotti+un...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
http://danielnouri.org

Toni Haka-Risku

unread,
Apr 25, 2013, 3:05:02 PM4/25/13
to ko...@googlegroups.com, toju...@gmail.com
OK I understand, but is my solution above "wrong" because the content object is created using get_appstruct()? That way I can create the object through form and  don't have to define ID manually. Thanks for your answer btw.

Daniel Nouri

unread,
Apr 26, 2013, 3:46:35 AM4/26/13
to Kotti mailing list, toju...@gmail.com
You can use 'kotti.util.title_to_name' to set the ID the same way the
add form sets it.

>>> from kotti.util import title_to_name
>>> comment = Comment(...)
>>> name = title_to_name(comment.title, blacklist=context.keys())
>>> context[name] = comment

Toni Haka-Risku

unread,
Apr 29, 2013, 7:32:37 AM4/29/13
to ko...@googlegroups.com, toju...@gmail.com
Thank you! That's all I need.
Reply all
Reply to author
Forward
0 new messages