35. Dexterity Types III: Sponsors

Without sponsors, a conference would be hard to finance! Plus it is a good opportunity for Plone companies to advertise their services.

In this part we will:

  • Create a sponsor contenttype to manage sponsors

  • Store non-visible information about the sponsor in the sponsor-type

The topics we cover are:

  • Schema hint and directives

  • Field permissions

  • Vocabularies

35.1. The Python schema

First we create the schema for the new content type.

Add a new file content/sponsor.py.

 1# -*- coding: utf-8 -*-
 2from plone.app.textfield import RichText
 3from plone.autoform import directives
 4from plone.dexterity.content import Container
 5from plone.namedfile import field as namedfile
 6from plone.supermodel import model
 7from plone.supermodel.directives import fieldset
 8from ploneconf.site import _
 9from z3c.form.browser.radio import RadioFieldWidget
10from zope import schema
11from zope.schema.vocabulary import SimpleTerm
12from zope.schema.vocabulary import SimpleVocabulary
15LevelVocabulary = SimpleVocabulary(
16    [SimpleTerm(value=u'platinum', title=_(u'Platinum Sponsor')),
17     SimpleTerm(value=u'gold', title=_(u'Gold Sponsor')),
18     SimpleTerm(value=u'silver', title=_(u'Silver Sponsor')),
19     SimpleTerm(value=u'bronze', title=_(u'Bronze Sponsor'))]
20    )
23class ISponsor(model.Schema):
24    """Dexterity Schema for Sponsors
25    """
27    directives.widget(level=RadioFieldWidget)
28    level = schema.Choice(
29        title=_(u'Sponsoring Level'),
30        vocabulary=LevelVocabulary,
31        required=True
32    )
34    text = RichText(
35        title=_(u'Text'),
36        required=False
37    )
39    url = schema.URI(
40        title=_(u'Link'),
41        required=False
42    )
44    fieldset('Images', fields=['logo', 'advertisement'])
45    logo = namedfile.NamedBlobImage(
46        title=_(u'Logo'),
47        required=False,
48    )
50    advertisement = namedfile.NamedBlobImage(
51        title=_(u'Advertisement (Gold-sponsors and above)'),
52        required=False,
53    )
55    directives.read_permission(notes='cmf.ManagePortal')
56    directives.write_permission(notes='cmf.ManagePortal')
57    notes = RichText(
58        title=_(u'Secret Notes (only for site-admins)'),
59        required=False
60    )
63class Sponsor(Container):
64    """Sponsor instance class"""

Some things are notable here:

  • LevelVocabulary is used to create the options used in the field level. This way we could easily translate the displayed value.

  • fieldset('Images', fields=['logo', 'advertisement']) moves the two image fields to another tab.

  • directives.read_permission(...) sets the read and write permission for the field notes to users who can add new members. Usually this permission is only granted to Site Administrators and Managers. We use it to store information that should not be publicly visible. Please note that obj.notes is still accessible in templates and Python.

See also

See the chapter Dexterity: Reference for a reference of all field-types and directives you can use in dexterity.

35.2. The Factory Type Information, or FTI

Next, we create the factory type information (“FTI”) for the new type in profiles/default/types/sponsor.xml

 1<?xml version="1.0"?>
 2<object name="sponsor" meta_type="Dexterity FTI" i18n:domain="plone"
 3   xmlns:i18n="http://xml.zope.org/namespaces/i18n">
 4 <property name="title" i18n:translate="">Sponsor</property>
 5 <property name="description" i18n:translate=""></property>
 6 <property name="icon_expr">string:${portal_url}/document_icon.png</property>
 7 <property name="factory">sponsor</property>
 8 <property name="add_view_expr">string:${folder_url}/++add++sponsor</property>
 9 <property name="link_target"></property>
10 <property name="immediate_view">view</property>
11 <property name="global_allow">True</property>
12 <property name="filter_content_types">True</property>
13 <property name="allowed_content_types"/>
14 <property name="allow_discussion">False</property>
15 <property name="default_view">view</property>
16 <property name="view_methods">
17  <element value="view"/>
18 </property>
19 <property name="default_view_fallback">False</property>
20 <property name="add_permission">cmf.AddPortalContent</property>
21 <property name="schema">ploneconf.site.content.sponsor.ISponsor</property>
22 <property name="klass">ploneconf.site.content.sponsor.Sponsor</property>
23 <property name="behaviors">
24  <element value="plone.dublincore"/>
25  <element value="plone.namefromtitle"/>
26  <element value="plone.versioning"/>
27 </property>
28 <property name="model_source"></property>
29 <property name="model_file"></property>
30 <property name="schema_policy">dexterity</property>
31 <alias from="(Default)" to="(dynamic view)"/>
32 <alias from="edit" to="@@edit"/>
33 <alias from="sharing" to="@@sharing"/>
34 <alias from="view" to="(selected layout)"/>
35 <action title="View" action_id="view" category="object" condition_expr=""
36    description="" icon_expr="" link_target="" url_expr="string:${object_url}"
37    visible="True">
38  <permission value="View"/>
39 </action>
40 <action title="Edit" action_id="edit" category="object" condition_expr=""
41    description="" icon_expr="" link_target=""
42    url_expr="string:${object_url}/edit" visible="True">
43  <permission value="Modify portal content"/>
44 </action>

Then we register the FTI in profiles/default/types.xml

1<?xml version="1.0"?>
2<object name="portal_types" meta_type="Plone Types Tool">
3 <object name="talk" meta_type="Dexterity FTI"/>
4 <object name="sponsor" meta_type="Dexterity FTI"/>

After reinstalling our package we can create the new type.

35.2.1. Exercise 1

Sponsors are containers but they don’t need to be. Turn them into items by changing their class to plone.dexterity.content.Item.


Modify the instance class.

1from plone.dexterity.content import Item
4class Sponsor(Item):
5    """Sponsor instance class"""


Changing the base-class of existing content from Item to Container or the other way around is possible but requires. See https://github.com/plone/plone.app.contenttypes#changing-the-base-class-for-existing-objects for details.

35.3. Summary

  • You created a new content type to store information on sponsors

  • You learned how to protect individual fields from being edited with permissions

  • Next you will learn how to display the sponsors at the bottom of every page