Plone- How can I create a control panel for a record in registry that is a dictionary type?

浪尽此生 提交于 2019-12-12 11:51:24

问题


I am trying to create a control panel add-on on my Plone site for editing a registry record that is a dictionary type.

My purpose is to store "types of suppliers" as a dictionary in registry.

My registry.xml in profiles/default:

<registry>
    <record interface="gpcl.assets.suppliertypes.ISupplierTypes" field="supplier_types">
        <value>
            <element key="1">Distributor</element>
            <element key="2">Manufacturer</element>
            <element key="3">Service Provider</element>
        </value>
    </record>
</registry>

My interface and form:

class ISupplierTypes(form.Schema):
    """ Define settings data structure
    """

    supplier_types = schema.Dict(title=u"Types of Suppliers",
                                 key_type=schema.Int(title=u"supplier_type_id"),
                                 value_type=schema.TextLine(title=u"supplier_type_name", 
                                                            required=False),
                                 required=False,
                                )

class SupplierTypesEditForm(RegistryEditForm):
    """
    Define form logic
    """
    schema = ISupplierTypes
    label = u"Types of Suppliers"

    description = u"Please enter types of suppliers"


class SupplierTypesView(grok.View):
    """
    View class
    """

    grok.name("supplier-types")
    grok.context(ISiteRoot)

    def render(self):
        view_factor = layout.wrap_form(SupplierTypesEditForm, ControlPanelFormWrapper)
        view = view_factor(self.context, self.request)
        return view()

I add it into the controlpanels.xml in my profiles/default and in the portal_quickinstaller, I install the product, and the control panel does show up in add-ons and displays the fields showing the default values. Unfortunately, when I try to add, edit, or delete, an error message is displayed stating "Wrong contained type." I assume that I am wrong on my approach to creating the control panel.

What is the correct way of creating a control panel for a record that is a dict type?

Ironically, in the render method of the view class, I tried to see if I could print the record (found how to do so here: https://pypi.python.org/pypi/plone.app.registry#using-the-records-proxy) and I was able to, showing up as a dict object. I was also able to add programmatically a new "element" to the record as well.

As far as me wanting to use a dict type, I do plan on taking advantage of the key values, that is why I want to use dictionary type.

I apologize if I am using improper terminology.

Thank you in advance.

Edit:

I am using Plone 4.3.2.

Edit:

Sorry, I was wrong. I found the traceback.

2014-08-20 13:13:07 ERROR Zope.SiteErrorLog 1408554787.930.279058908095
http://localhost:8080/GPCLAssetTracker/@@supplier-types/@@z3cform_validate_field

Traceback (innermost last):
    Module ZPublisher.Publish, line 138, in publish
    Module ZPublisher.mapply, line 72, in mapply
    Module ZPublisher.Publish, line 53, in missing_name
    Module ZPublisher.HTTPResponse, line 741, in badRequestError
    BadRequest:   <h2>Site Error</h2>
    <p>An error was encountered while publishing this resource.
    </p>
    <p><strong>Invalid request</strong></p>

    The parameter, <em>fname</em>, was omitted from the request.<p>Make sure to specify all        required parameters, and try the request again.</p>
<hr noshade="noshade"/>

<p>Troubleshooting Suggestions</p>

<ul>
<li>The URL may be incorrect.</li>
<li>The parameters passed to this resource may be incorrect.</li>
<li>A resource that this resource relies on may be
    encountering an error.</li>
</ul>

<p>For more detailed information about the error, please
refer to the error log.
</p>

<p>If the error persists please contact the site maintainer.
Thank you for your patience.
</p>

I also forgot to mention, the OS I am using is Xubuntu.


回答1:


You can choose to not use schema.Dict, but a different approach I used sometimes. Keep in mind that this will be dangerous: you must provide an uninstall step for your add-on or your registry will remain broken if you remove the add-on later.

It's described in those two articles:

  • http://blog.redturtle.it/plone.app.registry-how-to-use-it-and-love-it
  • http://blog.redturtle.it/plone-registry-strikes-back

In two word: use a schema.Tuple with schema.Object inside. For keeping the dict behavior that make keys unique, you must provide your own validator.

Another good approach (neved used) was the one described in the article "Complex Plone registry settings revisited", but it seems disappeared from the Web (...very sad...).




回答2:


Another approach, which I found really clever and simple is what collective.cron is using: JSON.

Instead of having to create a registry with an Object type and having to care about uninstall hassle and so on, just create a text line field and dump/load JSON on it.

See the interface registration, or for the lazy ones:

class IRegistryCrontab(Interface):
    """Plone.app.registry backend for the crontab persistence"""
    activated = schema.Bool(
        title=_('Global crontab activation switch'),
        default=True
    )
    crontab = schema.List(
        title=_(u'Crons'),
        value_type=schema.Text(title=_(u'JSON repr of a cron')),
    )

Simple, clever and working!



来源:https://stackoverflow.com/questions/25410178/plone-how-can-i-create-a-control-panel-for-a-record-in-registry-that-is-a-dicti

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!