Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

plone

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.

like image 408
Patrick Downey Avatar asked Dec 26 '22 05:12

Patrick Downey


1 Answers

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

like image 144
keul Avatar answered Jan 31 '23 08:01

keul