Open source cms for mobile and desktop websites

Element plugin example: Advanced object configuration

advanced python configuration example for a page element including new object type, object functionality, data model, form definition, custom views and template

Scaffold download:

The module has to be in the current python path to be added to your website.

website.modules.append("nive_tmpl_element_advanced")

__init__.py

The modules __init__.py configures the plugin and sets it up as python module. Also it contains a customized object and view function.

The customized object

A simple python class subclassing nive.components.objects.base.PageElementBase.

from nive_cms.baseobjects import PageElementBase

class MyArticle(PageElementBase):
    """
    Custom functions
    """

    def related(self, max=5):
        """
        Lookup articles with same topic anywhere on the website.
        """
        dataroot = self.root()
        r = dataroot.SearchType("article_adv",
                                parameter={"topic":self.data.topic},
                                fields=["id", "title"],
                                operators={"topic":"="},
                                max=max)
        return r["items"]

Additional view function

This function is mapped to a view and directly mapped to a url.

from nive.utils.utils import ConvertHTMLToText

def astext(context, request):
    return context.title + "\r\n\r\n" + ConvertHTMLToText(context.textblock)

The configuration

This part configures the element.

from nive.definitions import ObjectConf

configuration = ObjectConf(
    id="article_adv",
    name="Article",
    dbparam="article_adv",
    context=MyArticle,
    template="nive_tmpl_element_advanced:article.pt",
    selectTag=20,
    description=""
)

category = [
    {"id": "politics", "name": "Politics"},
    {"id": "music", "name": "Music"},
    {"id": "tech", "name": "Technical"},
]

configuration.data = [
    FieldConf(id="textblock", datatype="htext", size=10000, default="",
        name="Text", fulltext=1),
    FieldConf(id="image",     datatype="file",  size=0,     default="",
        name="Image file"),
    FieldConf(id="link",      datatype="url",   size=255,   default="",
        name="Link"),
    FieldConf(id="topic",     datatype="list",  size=10,    default="music",
        name="Topic"),
]

configuration.forms = {
    "create": {"fields": ["topic", "title", "textblock", "image"]},
    "edit":   {"fields": ["title", "textblock", "image"]}
}

configuration.views = [
    ViewConf(name="astext", context=MyArticle, view=astext)
]

article.pt

The page template article.pt is used to render the contents on the web page.

<div class="pageelement peArticle" id="nive-element${context.GetID()}"
     tal:define="link view.ResolveLink(context.data.link)">

<div class="teaser">
<img tal:condition="context.files.get('image')"
          tal:attributes="src view.FileUrl('image', context);
                          title context.GetTitle()" />
<div class="itext">
<h3 tal:content="structure context.GetTitle()"/>
<div class="subline">${context.subline()}
    <a href="${view.FolderUrl()}astext">(as text)</a></div>

<p tal:condition="context.data.textblock"
   tal:replace="structure context.data.textblock"></p>
<a tal:condition="link" tal:attributes="href link">&gt; more</a>

<ul class="related">
<li tal:repeat="article context.related()">${article/title}</li>
</ul>
</div>
</div>

</div>

test.py

The test file shows how to call the configuration test method.

class TestContactConf(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_conf(self):
        r=module.configuration.test()
        if not r:
            return
        print FormatConfTestFailure(r)
        self.assert_(False, "Configuration Error")
© 2013 Nive GmbH