Thanks Anthony,

I changed it to:
from decimal import Decimal

and now it works.


Kenneth

That's an issue with your Python code. You should either move the import outside the function or only import specific objects from the Decimal module (which is probably a better idea -- "from module import *" is usually not recommended).
Anthony

On Saturday, July 23, 2011 6:57:54 PM UTC-4, Kenneth wrote:

    Thank you Massimo for your answers.

    I can�t get the onvalidation to work.

    My code looks like this:

    def check(form):
         if ((form.vars.f_internal != 0 and form.vars.f_customer !=
    None) or
    (form.vars.f_internal == 0 and form.vars.f_customer == None)):
             form.errors.f_customer = "a and b cannot be both empty or
    selected"

    @auth.requires_login()
    def create_receipt():

         from decimal import *
    ......

         if form_item.accepts(request.vars, session, onvalidation=lambda
    form_item: check(form_item), formname='receipt_item'):

    this results in an error ticket:

    Traceback (most recent call last):
       File "/web2py/gluon/restricted.py", line 191, in restricted
         ccode = compile2(code,layer)
       File "/web2py/gluon/restricted.py", line 178, in compile2
         return compile(code.rstrip().replace('\r\n','\n')+'\n',
    layer, 'exec')
    SyntaxError: import * is not allowed in function 'create_receipt'
    because it is contains a nested function with free variables
    (receipt.py, line 8)

    This line 8 is the "    from decimal import *" line shown in the
    example.


    Kenneth

    > As I guess Massimo wanted to post these answers to the list and not
    > only to me, I�ll forward his answers here.
    >
    > On Jul 23, 10:19 am, Kenneth Lundstr�m<[email protected]>
    > wrote:
    >
    >> I m creating a form with SQLFORM that includes 6 fields.
    >>
    >> One field is f_customer with requires IS_IN_DB(....)
    >>
    >> Now I'd like to add a new field not taken directly from
    database. Lets
    >> call it f_extra.
    >>
    >> f_customer is a drop-down and f_extra would also be a
    drop-down. Now I
    >> have a list of questions.
    >>
    >> 1) Is it possible to get the form.accepts to validate that either
    >> f_customer or f_extra is selected. Both can t be selected and
    both can t
    >> be empty? I remember seeing this discussed on the list not long
    ago, but
    >> could not find it.
    >>
    >> 2) If 1) is not possible then I guess I have to put dbio=False
    and do
    >> the validation "manually". How do I show the error message the
    same way
    >> web2py does?
    > yes
    >
    > def check(form):
    >     if (form.vars.a and form.vars.b) or (not form.vars.a and not
    > form.vars.b):
    >         form.errors.b = "a and b cannot be both empty or selected"
    > form.accepts(...,onvalidation=lambda form: check(form))
    >
    >
    >> 3) Instead of creating a custom layout for this form I d like
    to add the
    >> field in the controller with something like:
    >>
    form.element('table').insert(2,TR('label',INPUT(_name='name'))),
    but how
    >> do I add a SELECT with many OPTIONS? The f_extra field drop-down is
    >> created from two different tables auth_user and t_extra_fields.
    I guess
    >> I should use query auth_user and t_extra_fields and then create
    a list.
    >> That list is used to create the drop-down.
    > Is this a FORM or  SQLFORM. In the latter case you need a custom
    > widget. In the former case you can do
    >
    > SELECT(*[OPTION(value) for value in ....],**dict(value='default'))
    >
    >
    >> 4) When using the OPTION helper is it possible to have a
    different value
    >> of the option then what is shown?
    > OPTION('shown value',_value='hidden value')
    >
    >
    >
    >


Reply via email to