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@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/webware-discuss



#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'])

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

_______________________________________________
Webware-discuss mailing list
Webware-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to