Re: htmlfill.render question

Chris Lambacher <[email protected]> Thu, 12 Jul 2012 10:37:15 -0400
Newsgroups gmane.comp.python.formencode
Message-ID <CAAXXHgLJ9CV8nv11m68__ZzVkpRHM-KZcthtLs8TeAGrGb99xw@mail.gmail.com>
--===============3352086655687624228==
Content-Type: multipart/alternative; boundary=bcaec554de56639de004c4a2e69b

--bcaec554de56639de004c4a2e69b
Content-Type: text/plain; charset=UTF-8

Hi,

htmlfill does not need to take a whole page as input and running htmlfill
over a particular form is likely to give you the result you want (see
below). However, your second suggestion sounds like a good one, and I would
also consider a patch teaching htmlfill about more than one form.

The easiest way to do this will htmlfill as it is now will depend on how
you render your pages. I'll use Mako, as an example. You can create a def
for the form something like this:

<%def name="renderform()" buffer="True">
<input type="text" name="name" value="fill">
<select name="occupation"> <option value="">Default</option>
<option value="Crazy Cultist">Crazy cultist</option> </select>
<textarea cols="20" style="width: 100%" name="address">
An address</textarea>
<input type="radio" name="living" value="yes">
<input type="radio" name="living" value="no">
<input type="checkbox" name="nice_guy" checked="checked">
</%def>

and then place the form and htmlfill it like so:
<% defaults = dict(name='Bob Jones',
            occupation='Crazy Cultist',
            address='14 W. Canal\nNew Guinea',
            living='no',
            nice_guy=0)
%>
${htmlfill.render(renderform(), defaults)}

will result in:

<input type="text" name="name" value="Bob Jones">
<select name="occupation">
<option value="">Default</option>
<option value="Crazy Cultist" selected="selected">Crazy cultist</option>
</select>
<textarea cols="20" style="width: 100%" name="address">14 W. Canal
New Guinea</textarea>
<input type="radio" name="living" value="yes">
<input type="radio" name="living" value="no" checked="checked">
<input type="checkbox" name="nice_guy">

This limits the htmlfill to the one form in question.

You can probably pass the result of functools.partial from your controller
to have the defaults, errors, etc all pre-bound to the function ahead of
time. Something like:

formfillfn = functools.partial(htmlfill.render,
defaults=pre_computed_defaults, errors=pre_computed_errors)

then the call above can be changed to filter syntax:
${renderform() | formfillfn}


I don't use Jinja but it looks like you could do this for the
functools.partial method:

{% filter formfillfn %}
<input type="text" name="name" value="fill">
<select name="occupation"> <option value="">Default</option>
<option value="Crazy Cultist">Crazy cultist</option> </select>
<textarea cols="20" style="width: 100%" name="address">
An address</textarea>
<input type="radio" name="living" value="yes">
<input type="radio" name="living" value="no">
<input type="checkbox" name="nice_guy" checked="checked">
{% endfilter %}


-Chris

On Wed, Jul 11, 2012 at 6:48 PM, Jonathan Vanasco <[email protected]> wrote:

> hoping someone can give me a suggestion :
>
> I sometimes have two forms displayed on a single page.  They're validated
> separately and submitted to different locations.  ( this is under pylons
> and pyramid )
>
> example:
>         the left side of the screen shows Account Data
>         the right side of the screen has a "change password" and "change
> email" field
>
>
> If i use htmlfill.render on this page to reprint on an error the following
> behavior happens:
>
>         1) default settings
>                 - the validated form looks like i want it to
>                 -- there are errors displayed
>                 -- the 'invalid' fields are blank
>                 -- the 'valid' fields are filled
>                 - the non-validated forms are all blank
>
>         2) force_defaults=False
>                 - the non-validated forms have their default values
>                 - the validated form loses all the submitted values
>
> does anyone have a suggestion on how i can achieve this:
>
>                 - the non-validated forms have their default values
>                 - the validated form has the submitted values &
> errors/blanks
>
> the ideas i have so far are:
>         - push the defaults from the other form into the validated form's
> defaults
>         - add a kwarg to htmlfill.render that would only allow the form
> re-writing to affect fields explicitly listed in `defaults` and `errors`
>
> it'll probably take me as much time to code either of these solutions.  if
> the community would benefit from the second idea - i'd be happy to write a
> patch.
>
>
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> FormEncode-discuss mailing list
> FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/formencode-discuss
>



-- 
Christopher Lambacher
[email protected]

--bcaec554de56639de004c4a2e69b
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div>Hi,</div><div><br></div>htmlfill does not need to take a whole page as=
 input and running htmlfill over a particular form is likely to give you th=
e result you want (see below). However, your second suggestion sounds like =
a good one, and I would also consider a patch teaching htmlfill about more =
than one form.=C2=A0<div>
<br></div><div>The easiest way to do this will htmlfill as it is now will d=
epend on how you render your pages. I&#39;ll use Mako, as an example. You c=
an create a def for the form something like this:<div><br></div><div>&lt;%d=
ef name=3D&quot;renderform()&quot; buffer=3D&quot;True&quot;&gt;</div>
<div><div>&lt;input type=3D&quot;text&quot; name=3D&quot;name&quot; value=
=3D&quot;fill&quot;&gt;</div><div>&lt;select name=3D&quot;occupation&quot;&=
gt; &lt;option value=3D&quot;&quot;&gt;Default&lt;/option&gt;</div><div>&lt=
;option value=3D&quot;Crazy Cultist&quot;&gt;Crazy cultist&lt;/option&gt; &=
lt;/select&gt;</div>
<div>&lt;textarea cols=3D&quot;20&quot; style=3D&quot;width: 100%&quot; nam=
e=3D&quot;address&quot;&gt;</div><div>An address&lt;/textarea&gt;</div><div=
>&lt;input type=3D&quot;radio&quot; name=3D&quot;living&quot; value=3D&quot=
;yes&quot;&gt;</div>
<div>&lt;input type=3D&quot;radio&quot; name=3D&quot;living&quot; value=3D&=
quot;no&quot;&gt;</div><div>&lt;input type=3D&quot;checkbox&quot; name=3D&q=
uot;nice_guy&quot; checked=3D&quot;checked&quot;&gt;</div></div><div>&lt;/%=
def&gt;</div>
<div><br></div><div>and then place the form and htmlfill it like so:</div><=
div>&lt;% defaults =3D=C2=A0dict(name=3D&#39;Bob Jones&#39;,</div><div><div=
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 occupation=3D&#39;Crazy Cultist&=
#39;,</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 address=3D&#39;14=
 W. Canal\nNew Guinea&#39;,</div>
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 living=3D&#39;no&#39;,</div>=
<div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 nice_guy=3D0)</div></div><di=
v>%&gt;</div><div>${htmlfill.render(renderform(), defaults)}</div><div><br>=
</div><div>will result in:</div><div><br></div><div><div>&lt;input type=3D&=
quot;text&quot; name=3D&quot;name&quot; value=3D&quot;Bob Jones&quot;&gt;</=
div>
<div>&lt;select name=3D&quot;occupation&quot;&gt;</div><div>&lt;option valu=
e=3D&quot;&quot;&gt;Default&lt;/option&gt;</div><div>&lt;option value=3D&qu=
ot;Crazy Cultist&quot; selected=3D&quot;selected&quot;&gt;Crazy cultist&lt;=
/option&gt;</div>
<div>&lt;/select&gt;</div><div>&lt;textarea cols=3D&quot;20&quot; style=3D&=
quot;width: 100%&quot; name=3D&quot;address&quot;&gt;14 W. Canal</div><div>=
New Guinea&lt;/textarea&gt;</div><div>&lt;input type=3D&quot;radio&quot; na=
me=3D&quot;living&quot; value=3D&quot;yes&quot;&gt;</div>
<div>&lt;input type=3D&quot;radio&quot; name=3D&quot;living&quot; value=3D&=
quot;no&quot; checked=3D&quot;checked&quot;&gt;</div><div>&lt;input type=3D=
&quot;checkbox&quot; name=3D&quot;nice_guy&quot;&gt;</div><div><br></div><d=
iv>This limits the htmlfill to the one form in question.</div>
<div><br></div><div>You can probably pass the result of functools.partial f=
rom your controller to have the defaults, errors, etc all pre-bound to the =
function ahead of time. Something like:</div><div><br></div><div>formfillfn=
 =3D functools.partial(htmlfill.render, defaults=3Dpre_computed_defaults, e=
rrors=3Dpre_computed_errors)</div>
<div><br></div><div>then the call above can be changed to filter syntax:</d=
iv><div>${renderform() | formfillfn}</div><div><br></div><div><br></div><di=
v>I don&#39;t use Jinja but it looks like you could do this for the functoo=
ls.partial method:</div>
<div><br></div><div>{% filter formfillfn %}</div><div><div>&lt;input type=
=3D&quot;text&quot; name=3D&quot;name&quot; value=3D&quot;fill&quot;&gt;</d=
iv><div>&lt;select name=3D&quot;occupation&quot;&gt; &lt;option value=3D&qu=
ot;&quot;&gt;Default&lt;/option&gt;</div>
<div>&lt;option value=3D&quot;Crazy Cultist&quot;&gt;Crazy cultist&lt;/opti=
on&gt; &lt;/select&gt;</div><div>&lt;textarea cols=3D&quot;20&quot; style=
=3D&quot;width: 100%&quot; name=3D&quot;address&quot;&gt;</div><div>An addr=
ess&lt;/textarea&gt;</div>
<div>&lt;input type=3D&quot;radio&quot; name=3D&quot;living&quot; value=3D&=
quot;yes&quot;&gt;</div><div>&lt;input type=3D&quot;radio&quot; name=3D&quo=
t;living&quot; value=3D&quot;no&quot;&gt;</div><div>&lt;input type=3D&quot;=
checkbox&quot; name=3D&quot;nice_guy&quot; checked=3D&quot;checked&quot;&gt=
;</div>
</div><div>{% endfilter %}</div><div><br></div><div><br></div><div>-Chris<b=
r><br><div class=3D"gmail_quote">On Wed, Jul 11, 2012 at 6:48 PM, Jonathan =
Vanasco <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" target=
=3D"_blank">[email protected]</a>&gt;</span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">hoping someone can give me a suggestion :<br=
>
<br>
I sometimes have two forms displayed on a single page. =C2=A0They&#39;re va=
lidated separately and submitted to different locations. =C2=A0( this is un=
der pylons and pyramid )<br>
<br>
example:<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 the left side of the screen shows Account Data<=
br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 the right side of the screen has a &quot;change=
 password&quot; and &quot;change email&quot; field<br>
<br>
<br>
If i use htmlfill.render on this page to reprint on an error the following =
behavior happens:<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 1) default settings<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 - the validated for=
m looks like i want it to<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -- there are errors=
 displayed<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -- the &#39;invalid=
&#39; fields are blank<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -- the &#39;valid&#=
39; fields are filled<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 - the non-validated=
 forms are all blank<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 2) force_defaults=3DFalse<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 - the non-validated=
 forms have their default values<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 - the validated for=
m loses all the submitted values<br>
<br>
does anyone have a suggestion on how i can achieve this:<br>
<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 - the non-validated=
 forms have their default values<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 - the validated for=
m has the submitted values &amp; errors/blanks<br>
<br>
the ideas i have so far are:<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 - push the defaults from the other form into th=
e validated form&#39;s defaults<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 - add a kwarg to htmlfill.render that would onl=
y allow the form re-writing to affect fields explicitly listed in `defaults=
` and `errors`<br>
<br>
it&#39;ll probably take me as much time to code either of these solutions. =
=C2=A0if the community would benefit from the second idea - i&#39;d be happ=
y to write a patch.<br>
<br>
<br>
---------------------------------------------------------------------------=
---<br>
Live Security Virtual Conference<br>
Exclusive live event will cover all the ways today&#39;s security and<br>
threat landscape has changed and how IT managers can respond. Discussions<b=
r>
will include endpoint security, mobile security and the latest in malware<b=
r>
threats. <a href=3D"http://www.accelacomm.com/jaw/sfrnl04242012/114/5012226=
3/" target=3D"_blank">http://www.accelacomm.com/jaw/sfrnl04242012/114/50122=
263/</a><br>
_______________________________________________<br>
FormEncode-discuss mailing list<br>
<a href=3D"mailto:FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org">FormEncode-disc=
[email protected]</a><br>
<a href=3D"https://lists.sourceforge.net/lists/listinfo/formencode-discuss"=
 target=3D"_blank">https://lists.sourceforge.net/lists/listinfo/formencode-=
discuss</a><br>
</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>Christopher =
Lambacher<br><a href=3D"mailto:[email protected]">[email protected]=
et</a><br>
</div></div></div>

--bcaec554de56639de004c4a2e69b--


--===============3352086655687624228==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
--===============3352086655687624228==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
FormEncode-discuss mailing list
FormEncode-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/formencode-discuss

--===============3352086655687624228==--