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

The Python schema#

First we create the schema for the new content type.

Add a new file content/sponsor.py.

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

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>
45</object>

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"/>
5</object>

After reinstalling our package we can create the new type.

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.

Solution

Modify the instance class.

1from plone.dexterity.content import Item
2
3@implementer(ISponsor)
4class Sponsor(Item):
5    """Sponsor instance class"""

Note

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

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