Hi There,

Today I needed to use a SQLCustomType to convert a boolean to a string (in 
view mode) and the other way around when storing it.

I used this customtype:

bool_to_text = SQLCustomType(
    type='string',
    native='boolean',
    encoder=lambda value: SQLFormFilters.store_text_as_boolean(value),
    decoder=lambda value: SQLFormFilters.view_boolean_as_text(value)
)

And this functions in the class

    @staticmethod
    def store_text_as_boolean(value):
        if value == 'true':
            return 'T'
        else:
            return 'F'

    @staticmethod
    def view_boolean_as_text(value):
        if value == 'T':
            return 'true'
        else:
            return 'false'

I needed to use this check:
value == 'T'

because it looks like the DAL is always giving back a string in this 
specific situation.

When I use:
value == True

Web2py crashes with this error: 

AttributeError: 'bool' object has no attribute 'replace'
<http://127.0.0.1:8000/admin/errors/portal#>

Traceback (most recent call last):
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/restricted.py", line 220, in 
restricted
    exec ccode in environment
  File 
"/home/eric/Projects/Web2Py/BricksSis/applications/portal/controllers/medication_templates.py",
 line 147, in <module>
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/globals.py", line 385, in 
<lambda>
    self._caller = lambda f: f()
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/tools.py", line 3287, in f
    return action(*a, **b)
  File 
"/home/eric/Projects/Web2Py/BricksSis/applications/portal/controllers/medication_templates.py",
 line 71, in edit
    if form.process().accepted:
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/html.py", line 2282, in 
process
    self.validate(**kwargs)
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/html.py", line 2219, in 
validate
    if self.accepts(**kwargs):
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/sqlhtml.py", line 1590, in 
accepts
    self.id_field_name]).update(**fields)
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/dal.py", line 10549, in 
update
    ret = db._adapter.update("%s" % table._tablename,self.query,fields)
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/dal.py", line 1612, in update
    sql = self._update(tablename, query, fields)
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/dal.py", line 1607, in 
_update
    for (field, value) in fields])
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/dal.py", line 1552, in expand
    return str(self.represent(expression,field_type))
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/dal.py", line 1978, in 
represent
    return self.adapt(value)
  File "/home/eric/Projects/Web2Py/BricksSis/gluon/dal.py", line 775, in adapt
    return "'%s'" % obj.replace("'", "''")
AttributeError: 'bool' object has no attribute 'replace'


Oh... I'm using SQLForms in the controller, the view contains a custom 
form, I'm printing the field like this:

{{=form.custom.widget.is_specialite}}


-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to