Re: Example howto use FormEncode with Webware-1.1b1 (2)

Georg Balmer <[email protected]> Tue, 11 May 2010 08:53:32 +0200
Newsgroups gmane.comp.python.webware
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------020206060309040309070905
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Thank you for your reply

My test setup is:

. fedora 11
. python 2.6
. formencode in  site-packages/formencode
    (had to add the i18n directory) from [2]
. webware-1.1.b1
. using utf-8

I got the example from a direct download from formencode.org:
.  [2] svn co http://svn.colorstudy.com/FormEncode/trunk FormEncode
     (examples directory: WebwareExamples/index.py)

. you may switch language for formencode in awake

. some lines in "save" are experimental.
   I made some changes following your suggestions.

   The german version doesn't break HTTPResponse anymore,
   but I still don't understand the mechanics of error catching and so on.


My aim is to have a basic example I can use for my next steps.

Regards
Georg
--------------------------------------------------------------------------------

Am 10.05.2010 23:10, schrieb Christoph Zwerschke:
> Am 10.05.2010 12:57 schrieb Georg Balmer:
>> The orginal WebwareExample by Ian Bicking is working after eliminating HTMLForm
>> but some german error messages produce a UnicodeEncodeError in HTTPResponse.
>
> Sorry, I cannot find this example you're referring to, where is it?
> Generally, you must output ordinary (encoded) strings in Webware, not
> unicode. It's best to always use the same encoding (e.g. latin-1 or
> utf-8) and work with encoded strings everywhere inside Webware.
>
> -- Christoph
>
> ------------------------------------------------------------------------------
>
> _______________________________________________
> Webware-discuss mailing list
> Webware-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/webware-discuss
>



--------------020206060309040309070905
Content-Type: text/x-python;
 name="index.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="index.py"

#from formencode.htmlform import HTMLForm
import formencode
from formencode import htmlfill
from formencode import validators, compound, schema
from WebKit.Page import Page


page_style = """
<html>
 <head>
  <title>Tell me about yourself</title>
  <style type="text/css">
    .error {background-color: #ffdddd}
    .error-message {border: 2px solid #f00}
  </style>
 </head>
 <body>

 <h1>Tell me about yourself</h1>
 <p><i>A FormEncode example</i></p>

 %s

 </body></html>"""

form_template = '''
<form action="" method="POST">
<input type="hidden" name="_action_" value="save">

Your name:<br>
<form:error name="name">
<input type="text" name="name">
<br>

Your age:<br>
<form:error name="age">
<input type="text" name="age">
<br>

Your favorite color:<br>
<form:error name="color">
<input type="checkbox" value="red"> Red<br>
<input type="checkbox" value="blue"> Blue<br>
<input type="checkbox" value="black"> Black<br>
<input type="checkbox" value="green"> Green<br>
<br>

<input name="_action_save" type="submit" value="submit">
</form>
'''

class FormSchema(schema.Schema):
    name = validators.String(not_empty=True)
    age = validators.Int(min=13, max=99)
    color = compound.All(
        validators.Set(),
        validators.OneOf(['red', 'blue', 'black', 'green']),
    )
    filter_extra_fields = True
    allow_extra_fields = True


class index(Page):

    def awake(self, trans):
        print ">>> DEBUG SimpleFlow: awake"
        Page.awake(self, trans)
        print formencode.api.get_localedir()
        formencode.api.set_stdtranslation(
            domain="FormEncode",
            languages=["de"],
        )

        fields = self.request().fields()
        try:
            self.rendered_form
        except:
            print ">>> DEBUG SimpleFlow: awake: rendered_form = NONE"
            defaults = {}
            errors = {}
            self.rendered_form = None

    def actions(self):
        print ">>> DEBUG SimpleFlow: actions"
        print Page.actions(self)
        fields = self.request().fields()
        for field in fields:
            print "%s: %s" % (field, fields[field])
        return ['save']

    def save(self):
        print ">>> DEBUG SimpleFlow: save"
        ok = None
        fields = self.request().fields()
        try:
            ok = FormSchema.to_python(fields)
            print ">>> DEBUG SimpleFlow: save: OK"
        except validators.Invalid, e:
            print ">>> DEBUG SimpleFlow: save: e:", type(e)
            if isinstance(e, validators.Invalid):
                print ">>> Invalid instance"
            errors = e.error_dict
            eKeys = errors.keys()
            errDict = {}
            for k in eKeys:
                print "%s: %s" % (k, type(errors[k]))
                if isinstance(errors[k], validators.Invalid):
                    print ">>> Invalid instance"
                    msg = errors[k].msg
                    errMsg =  msg.encode('utf-8', 'replace')
                    errDict[k] = errMsg
            print ">>> DEBUG SimpleFlow: save: e.error_dict:", errors
            defaults = fields # Hinweis
            self.rendered_form = htmlfill.render(form_template, defaults, errDict)

        self.write(page_style % self.rendered_form)
        self.defaults = {}
        self.rendered_form = None
            
    def writeContent(self):
        print ">>> DEBUG SimpleFlow: writeContent"
        if self.rendered_form is None:
            print ">>> DEBUG SimpleFlow: writeContent: rendered_form is NONE"
            defaults = {}
            errors = {}
            self.rendered_form = htmlfill.render(form_template, defaults, errors)
        self.write(page_style % self.rendered_form)
        
    def getDefaults(self):
        return dict(
            age='enter your age',
            color=['blue'])


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

------------------------------------------------------------------------------


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

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

--------------020206060309040309070905--