auto-generated fields in zeam.form
Andrew Altepeter <[email protected]> Fri, 15 Jul 2011 07:50:19 -0500
| Newsgroups | gmane.comp.web.zope.silva.devel |
|---|---|
| Message-ID | <CAEssFdAPsHjszHks0TroMpQ+AccfaaCvd42H4qfw3MecQZa-PA@mail.gmail.com> |
--===============4210375733318245214==
Content-Type: multipart/alternative; boundary=20cf303f6d5c855c0d04a81b17d9
--20cf303f6d5c855c0d04a81b17d9
Content-Type: text/plain; charset=ISO-8859-1
Greetings,
(this is a summary of an issue sylvain helped me work though off-line. I'm
posting this to the list, as someone else may find it useful)
I've been working in Silva 2.3, developing a custom (internal) extension
which synchronizes data from our student information system into campaign
monitor.
This extension includes a management component in the SMI to configure
campaign monitor clients. We're using zeam.form for the SMI configuration
screens.
One of the screens has a form with fields generated on request, as the
fields are dependent on a selection on the previous SMI screen. (i.e. if
you're familiar with campaign monitor, then on the first screen you choose
the client, the second screen shows configurations for each of the client's
subscription lists).
So in this SMIForm's update method, we get the client's subscription lists
and add the appropriate fields for each subscription list to 'self.fields'.
Essentially, here is the pattern we settled on:
class myform(silvaforms.SMIForm):
#del configuration stuff for the form
def update(self):
#get the client, and list of subscriptions
lists = self.get_subscriptions(client_id)
class DummySchemaInterface(Interface):
"""Needed to get the data validation working. The assumption is
that the schema fields are _always_ part of a schema interface
(but here, they are not).
"""
for l in lists:
lid = l.ListID
lname = l.Name
excl_id = 'listexcl_'+lid
name_id = 'listname_'+lid
#__name__ is required when creating schema fields outside of
# an interface. See zeam/form/ztk/tests/fields.txt (search for
# __name__)
s1 = schema.Choice(
__name__=excl_id,
title=lname,
required=False,
source=exclusion_codes_source)
s2 = schema.TextLine(
__name__=name_id,
title=u'List Name',
required=True,
readonly=True,
default=lname)
#these schema fields are not technically part of an interface,
# but it they need to be for validation. Add dummy interface
s1.interface = DummySchemaInterface
s2.interface = DummySchemaInterface
nf = silvaforms.Fields(s1, s2)
self.fields += nf
This took me a while to figure out. The __name__ parameter to the schema
constructors is very important. This is apparently a "hidden" design
feature of zope.interface (or zope.schema) -- this parameter needs to be
defined when the object is created, or the form will not render. Sylvain
kindly explained this to me and also pointed out the doctests in
zeam/form/ztk/tests/fields.txt (search for __name__) -- they're very
helpful to read through if you're doing more comlicated things with zeam
fields.
Perhaps you've also noticed DummySchemaInterface. I needed to attach this
to each schema field for the form would not validate once submitted. In
another offline conversation, sylvain showed me that zeam SilvaForms (of
which SMIForm is one) have a validator which checks interface invariants.
This form shouldn't need an interface, and as such shouldn't need to check
interface invariants. So to disable that validator, in the form you set
dataValidators = []
That's about it with that. zeam is nice to work with, and now I know these
things it's even easier.
peace,
Andy
--20cf303f6d5c855c0d04a81b17d9
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Greetings,<div><br></div><div>(this is a summary of an issue sylvain helped=
me work though off-line. =A0I'm posting this to the list, as someone e=
lse may find it useful)<br><div><br></div><div>I've been working in Sil=
va 2.3, developing a custom (internal) extension which synchronizes data fr=
om our student information system into campaign monitor.</div>
<div><br></div><div>This extension includes a management component in the S=
MI to configure campaign monitor clients. =A0We're using zeam.form for =
the SMI configuration screens.</div><div><br></div><div>One of the screens =
has a form with fields generated on request, as the fields are dependent on=
a selection on the previous SMI screen. =A0(i.e. if you're familiar wi=
th campaign monitor, then on the first screen you choose the client, the se=
cond screen shows configurations for each of the client's subscription =
lists).</div>
<div><br></div><div>So in this SMIForm's update method, we get the clie=
nt's subscription lists and add the appropriate fields for each subscri=
ption list to 'self.fields'.</div><div><br></div><div>Essentially, =
here is the pattern we settled on:</div>
<div><br></div><div>class myform(silvaforms.SMIForm):</div><div>=A0=A0#del =
configuration stuff for the form</div><div>=A0=A0def update(self):<br>=A0=
=A0 =A0 #get the client, and list of subscriptions</div><div>=A0=A0 =A0list=
s =3D self.get_subscriptions(client_id)</div>
<div><div>=A0=A0 =A0class DummySchemaInterface(Interface):</div><div>=A0=A0=
=A0 =A0 =A0"""Needed to get the data validation working. =
=A0The assumption is</div><div>=A0=A0 =A0 =A0 =A0 =A0 that the schema field=
s are _always_ part of a schema interface</div>
<div>=A0=A0 =A0 =A0 =A0 =A0 (but here, they are not).</div><div>=A0=A0 =A0 =
=A0 =A0"""</div><div>=A0=A0=A0</div><div>=A0=A0 =A0for l in =
lists:</div><div>=A0=A0 =A0 =A0 =A0lid =3D l.ListID</div><div>=A0=A0 =A0 =
=A0 =A0lname =3D l.Name</div><div>=A0=A0 =A0 =A0 =A0excl_id =3D 'listex=
cl_'+lid</div>
<div>=A0=A0 =A0 =A0 =A0name_id =3D 'listname_'+lid</div><div>=A0=A0=
=A0 =A0 =A0#__name__ is required when creating schema fields outside of</d=
iv><div>=A0=A0 =A0 =A0 =A0# an interface. =A0See zeam/form/ztk/tests/fields=
.txt (search for</div><div>=A0=A0 =A0 =A0 =A0# __name__)</div>
<div>=A0=A0 =A0 =A0 =A0s1 =3D schema.Choice(</div><div>=A0=A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0__name__=3Dexcl_id,</div><div>=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0title=3Dlname,</div><div>=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0required=3DF=
alse,</div><div>=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0source=3Dexclusion_codes_=
source)</div><div>
=A0=A0 =A0 =A0 =A0s2 =3D =A0schema.TextLine(</div><div>=A0=A0 =A0 =A0 =A0 =
=A0 =A0 =A0 =A0__name__=3Dname_id,</div><div>=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0=
=A0title=3Du'List Name',</div><div>=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =
=A0required=3DTrue,</div><div>=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0readonly=3D=
True,</div><div>
=A0=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0default=3Dlname)</div><div>=A0=A0 =A0 =A0=
=A0#these schema fields are not technically part of an interface,=A0</div>=
<div>=A0=A0 =A0 =A0 =A0# but it they need to be for validation. =A0Add dumm=
y interface</div><div>=A0=A0 =A0 =A0 =A0s1.interface =3D DummySchemaInterfa=
ce</div>
<div>=A0=A0 =A0 =A0 =A0s2.interface =3D DummySchemaInterface</div><div>=A0=
=A0 =A0 =A0 =A0nf =3D silvaforms.Fields(s1, s2)</div><div>=A0=A0 =A0 =A0 =
=A0self.fields +=3D nf</div></div><div><br></div><div>This took me a while =
to figure out. =A0The __name__ parameter to the schema constructors is very=
important. =A0This is=A0apparently=A0a "hidden" design feature o=
f zope.interface (or zope.schema) -- this parameter needs to be defined whe=
n the object is created, or the form will not render. =A0Sylvain kindly exp=
lained this to me and also pointed out the doctests in zeam/form/ztk/tests/=
fields.txt (search for=A0__name__) =A0-- they're very helpful to read t=
hrough if you're doing more comlicated things with zeam fields.</div>
</div><div><br></div><div>Perhaps you've also noticed DummySchemaInterf=
ace. =A0I needed to attach this to each schema field for the form would not=
validate once submitted. =A0In another offline conversation, sylvain showe=
d me that zeam SilvaForms (of which SMIForm is one) have a validator which =
checks interface invariants. =A0 This form shouldn't need an interface,=
and as such shouldn't need to check interface invariants. =A0So to dis=
able that validator, in the form you set=A0</div>
<div>dataValidators =3D []</div><div><br></div><div>That's about it wit=
h that. =A0zeam is nice to work with, and now I know these things it's =
even easier.</div><div><br></div><div>peace,</div><div>Andy</div>
--20cf303f6d5c855c0d04a81b17d9--
--===============4210375733318245214==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
silva-dev mailing list
silva-dev-IAPFreCvJWM6s/[email protected]
https://lists.infrae.com/mailman/listinfo/silva-dev
--===============4210375733318245214==--