Re: [Zope3-Users] Re: Compound Form Elements
On Oct 21, 2005, at 12:16 PM, James Allwyn wrote: Following Christian's tweaks to the browser.py file (creating a CustomSequenceWidget), I've got this working well in isolation - I can add a ContactData object on its own no problem. However, I've hit a brick wall about how to use the ContactData item as a form element within another schema (say, IPerson). I've tried to access it with variations upon: [...] contact_data = Tuple( title = u'Contact Details', value_type = Object( schema=IContactDatum, title=u'Contact Datum')) To me, this or List() are the only ones that make any sense... Here's why: One of the things you're doing with this interface is implicitly telling z3 how you want this field to be displayed (because, by default, it makes that decision based on the field type. With a List(), etc., you get the sub-schema displayed and then the option to add * or removed checked -- an auto-generated multi-widget. It it's not a standard field type, z3's not going to know what do do with it, and I think you'd have to use the widget sub-config in your zcml to tell it explicitly what widget to use when building add/edit forms. and invoking the browser class for the add view in the configure.zcml for the User: class=.contactdata.browser.ContactDataAddView In effect, this method doesn't use ContactData at all, it replicates its functionality out of a Tuple of ContactDatum objects in the IPerson itself. But what I want to do is be able to put a ContactData element into IPerson and have it 'just work', in the same way I can for, say, TextLine, or Bool. Hmm, we'll need some more details here... what do you mean by just work? In what context? Add/edit? Final rendering? If the later, I believe you will need to add your view class that processes the list so that ZPT can handle it with repeat (at least, that's what I would do...). d ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
[Zope3-Users] Re: Compound Form Elements
On 04/10/05, James Allwyn [EMAIL PROTECTED] wrote: Hello, I wish my users to be able to register various contact details. For each, I would record the type (e.g. email, landline tel, mobile tel, fax...), the value (e.g. [EMAIL PROTECTED]) and a True/False value on whether it is to be visible on the site. Thinking further about this, it seems to me that the natural data structure would be dicts within a list. For example, if were doing it in plain python it might go something like this: contacts = [] contacts [] contact1 = {show:True, type:mobile tel, value: 07123 456 789} contact2 = {show:False, type:email, value: [EMAIL PROTECTED]} contact1['type'] 'mobile tel' contact2['type'] 'email' contacts.append(contact1) contacts.append(contact2) contacts [{'type': 'mobile tel', 'value': '07123 456 789', 'show': True}, {'type': 'email ', 'value': '[EMAIL PROTECTED]', 'show': False}] len(contacts) 2 I've tried to implement this zopeishly, and the stumbling block I've hit seems to be specifying the key and value_type pairs in the dict. So, for example: from zope.interface import Interface from zope.schema import List, Dict, Bool, TextLine class IUser(Interface): ... contacts = List( ... title=uContact Details, ... value_type=Dict( ... title=udictionary, ... value_type=Bool(title=ushow) ... value_type=TextLine(title=uvalue) ... value_type=TextLine(title=utype) ... ) ... ) This barfs when it hits the second value_type in the dict. Look at the zope.schema sources it seems I can only specify one value_type and one key_type. What I want to do is specify three pairs of keys and value_types - note, specifying the actual keys, not just their types. Is this possible? Or am I barking up the wrong tree!? Any hints gratefully received. Thanks, James This seems to me to add up to an amalgamation of one each of Choice, TextLine and Bool Schema elements. Should I build up a custom Schema item that combines these three things, or would anyone recommend an alternative route? It would be nice if I could combine this with a bit of checking for validity of the value (obviously based on the type selected). From a visual perspective, Schema based forms seem to stack each schema element up on a new line. To make better use of screen 'real estate' (and to logically group) I think it would be better if my amalgamation appeared on one line. Does this have any baring on how I should approach the issue? Alternatively, I'd be open to any suggestions on completely different ways of handling this - the 'custom schema element' way does have the problem that if I wanted to allow up to, say, six contact details to be provided I would have to put six sets of three control elements on screen, which would be wasteful for users who only want to specify one contact detail. Thanks, James ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users
Re: [Zope3-Users] Re: Compound Form Elements
On Oct 6, 2005, at 2:29 AM, James Allwyn wrote: On 04/10/05, James Allwyn [EMAIL PROTECTED] wrote: Hello, I wish my users to be able to register various contact details. For each, I would record the type (e.g. email, landline tel, mobile tel, fax...), the value (e.g. [EMAIL PROTECTED]) and a True/False value on whether it is to be visible on the site. I've tried to implement this zopeishly, and the stumbling block I've hit seems to be specifying the key and value_type pairs in the dict. So, for example: [snip] This barfs when it hits the second value_type in the dict. Look at the zope.schema sources it seems I can only specify one value_type and one key_type. What I want to do is specify three pairs of keys and value_types - note, specifying the actual keys, not just their types. Is this possible? Or am I barking up the wrong tree!? Any hints gratefully received. Hey James, What you probably want to explore is the Object field. This lets you define a sub-schema, as it were, and use that in your main schema. An example (untested)... maybe in yourproject/types/interfaces.py: from zope.interface import Interface from zope.schema import List, Text, TextLine, Int, Choice from zope.schema import Object from zope.i18nmessageid import MessageIDFactory _ = MessageIDFactory('yourproj') # sub-schema class IContactData(Interface): show = Bool(title = _(uShow Data?)) type = TextLine(title = _(uData Type)) value = TextLine(title = _(uData Value)) # main schema class IUser(Interface) name_first = TextLine( title = _(uFirst Name) ) name_last = TextLine( title = _(uLast Name) ) contact_info = List( title = _(uContact Info), value_type = Object( schema=IContactData, ), ) Then (maybe in user.py, ): ... class ContactData(object): implements(IPageSection) # and whatever else you might want to # put in here class User(PortalContent): implements(IUser) # and whatever else you might want to # put in here ... Then you're going to have to do some extra work with a custom widget (a good place would be ./browser/userview.py): from zope.app.form.browser import ObjectWidget from zope.app.form.browser.editview import EditView from zope.app.form import CustomWidgetFactory from yourproject.types import ContactData from yourproject.types.interfaces import IUser parts_w = CustomWidgetFactory(ObjectWidget, ContactData) class ContactDataEditView(EditView): View for editing a page section. __used_for__ = IUser parts_widget = parts_w In your browser/configure.zcml, your going to need something like this: browser:editform schema=yourproject.types.interfaces.IUser class=.userview.ContactDataEditView label=Edit User name=edit.html menu=zmi_views title=Edit permission=zope.ManageContent / I may be forgetting something here... but this should certainly give you a head start. There's a bunch of very good information on this in these files: zope/app/form/browser/widgets.txt zope/app/form/browser/objectwidget.txt Hmmm, looking through these again, these are really good examples. Pay more attention to them than to what I wrote above ;-) By the way, (and irrespective of the structure of the data) this data you are adding seems to me a good candidate for annotations. You might want to explore that... Happy trails! d ___ Zope3-users mailing list Zope3-users@zope.org http://mail.zope.org/mailman/listinfo/zope3-users