24. Content types III: Sponsors – Mastering Plone 6 development

24. Content types III: Sponsors#

Backend chapter

Get the code! (More info)

git checkout dexterity_upgrade_steps

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

Topics covered:

  • Schema hint and directives

  • Field permissions

  • Vocabularies

24.1. 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 zope import schema
 8from zope.interface import implementer
 9
10
11class ISponsor(model.Schema):
12    """Dexterity Schema for Sponsors
13    """
14
15    level = schema.Choice(
16        title='Sponsoring Level',
17        vocabulary='ploneconf.sponsor_levels',
18        required=True
19    )
20
21    text = RichText(
22        title='Text',
23        required=False
24    )
25
26    url = schema.URI(
27        title='Link',
28        required=False
29    )
30
31    fieldset('Images', fields=['logo', 'advertisement'])
32    logo = namedfile.NamedBlobImage(
33        title='Logo',
34        required=False,
35    )
36
37    advertisement = namedfile.NamedBlobImage(
38        title='Advertisement (Gold-sponsors and above)',
39        required=False,
40    )
41
42    directives.read_permission(notes='cmf.ManagePortal')
43    directives.write_permission(notes='cmf.ManagePortal')
44    notes = RichText(
45        title='Secret Notes (only for site-admins)',
46        required=False
47    )
48
49
50@implementer(ISponsor)
51class Sponsor(Container):
52    """Sponsor instance class"""

Some things are notable here:

  • 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 plone6docs:backend-fields-reference-label for a reference of all field-types and directives you can use in dexterity.

24.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>
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.

In production you will want to manage and document such changes in code with upgrade steps. See the next chapter.

24.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