Re: [web2py] convert request.vars to set default value in a custom .factory() form
Will see, if I can make a simplified version, that long... I appreciate your help Richard On Thu, Nov 3, 2016 at 10:01 PM, Anthony wrote: > On Thursday, November 3, 2016 at 4:10:04 PM UTC-4, Richard wrote: >> >> I can show you privately... >> > > Or maybe create a simplified example that demonstrates the problem. > > -- > 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 web2py+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [web2py] convert request.vars to set default value in a custom .factory() form
On Thursday, November 3, 2016 at 4:10:04 PM UTC-4, Richard wrote: > > I can show you privately... > Or maybe create a simplified example that demonstrates the problem. -- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [web2py] convert request.vars to set default value in a custom .factory() form
I can show you privately... Richard On Thu, Nov 3, 2016 at 12:13 PM, Anthony wrote: > Can you show your code? It is not clear why you can't use the "keepvalues" > argument. Please explain the workflow in more detail. > > > On Tuesday, November 1, 2016 at 10:45:45 AM UTC-4, Richard wrote: >> >> Hello, >> >> I have an high customized SQLFORM.factory() form... I set default value >> for the update form for which I have no problem with values types as they >> get out of the database... But in case I want to emulate keepvalue feature >> with this form I need to pass value to request.vars which I need to set as >> default... The problem is that all value are string when I get them and I >> need to populate many fields and would greatly prefer not have to handle >> each fields manually one by one but instead do it dynamically so if I had >> field to the table I don't have to add it... So I am searching a way to >> convert appropriately value for each field type... I could use the approach >> proposed in the answer accepted hear : http://stackoverflow.com/que >> stions/7402573/use-type-information-to-cast-values-stored-as-strings >> >> I there surely such a thing in the web2py internal... I thought >> _filter_fields() would do it, but it only filter field that are member of a >> table, it doesn't act over the value of the field... I thought of using >> requires/validator, but I am not sure how to use them. >> >> Thanks >> >> Richard >> >> >> >> -- > 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 web2py+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [web2py] convert request.vars to set default value in a custom .factory() form
Can you show your code? It is not clear why you can't use the "keepvalues" argument. Please explain the workflow in more detail. On Tuesday, November 1, 2016 at 10:45:45 AM UTC-4, Richard wrote: > > Hello, > > I have an high customized SQLFORM.factory() form... I set default value > for the update form for which I have no problem with values types as they > get out of the database... But in case I want to emulate keepvalue feature > with this form I need to pass value to request.vars which I need to set as > default... The problem is that all value are string when I get them and I > need to populate many fields and would greatly prefer not have to handle > each fields manually one by one but instead do it dynamically so if I had > field to the table I don't have to add it... So I am searching a way to > convert appropriately value for each field type... I could use the approach > proposed in the answer accepted hear : > http://stackoverflow.com/questions/7402573/use-type-information-to-cast-values-stored-as-strings > > I there surely such a thing in the web2py internal... I thought > _filter_fields() would do it, but it only filter field that are member of a > table, it doesn't act over the value of the field... I thought of using > requires/validator, but I am not sure how to use them. > > Thanks > > Richard > > > > -- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [web2py] convert request.vars to set default value in a custom .factory() form
The above not handle bool correctly : def controller_func(): ... field_type_mapping = \ {'id': 'int', 'reference': 'int', 'boolean': 'bool', 'string': 'str', 'decimal': 'decimal', 'date': 'date', 'datetime': 'datetime'} for f in table_name_fields: if db.table_name[f].type.split('(')[0] == 'decimal': field_type = field_type_mapping['decimal'] elif db.table_name[f].type.split(' ')[0] == 'reference': field_type = field_type_mapping['reference'] else: field_type = field_type_mapping[db.table_name[f].type] if f in request.vars: db.table_name[f].default = request.vars[f] table_name_fields[f] = request.vars[f] # If there is value in request vars it has priority over derived value elif '_' + f in request.vars: if field_type not in ('date', 'datetime', 'decimal', 'bool'): db.table_name[f].default = convert(request.vars['_' + f], field_type) table_name_fields[f] = convert(request.vars['_' + f], field_type) elif field_type in ('date', 'datetime'): if f == 'date': db.table_name[f].default = datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d').date() table_name_fields[f] = datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d').date() elif f == 'datetime': db.table_name[f].default = datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d %H:%M:%S') table_name_fields[f] = datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d %H:%M:%S') elif field_type == 'decimal': db.table_name[f].default = Decimal(request.vars['_' + f]) table_name_fields[f] = Decimal(request.vars['_' + f]) elif field_type == 'bool': if request.vars['_' + f] in ['True']: request.vars['_' + f] = True elif request.vars['_' + f] in ['False']: request.vars['_' + f] = False else: request.vars['_' + f] = None db.table_name[f].default = request.vars['_' + f] table_name_fields[f] = request.vars['_' + f] ... On Thu, Nov 3, 2016 at 11:24 AM, Richard Vézina wrote: > I come up with this : > > def convert(value, type_): > import importlib > try: > # Check if it's a builtin type > module = importlib.import_module('__builtin__') > cls = getattr(module, type_) > except AttributeError: > # if not, separate module and class > module, type_ = type_.rsplit(".", 1) > module = importlib.import_module(module) > cls = getattr(module, type_) > return cls(value) > > def controller_func(): > ... > field_type_mapping = \ > {'id': 'int', > 'reference': 'int', > 'boolean': 'bool', > 'string': 'str', > 'decimal': 'decimal', > 'date': 'date', > 'datetime': 'datetime'} > > for f in table_name_fields: > if db.table_name[f].type.split('(')[0] == 'decimal': > field_type = field_type_mapping['decimal'] > elif db.table_name[f].type.split(' ')[0] == 'reference': > field_type = field_type_mapping['reference'] > else: > field_type = field_type_mapping[db.table_name[f].type] > if f in request.vars: > db.table_name[f].default = request.vars[f] > table_name_fields[f] = request.vars[f] > # If there is value in request vars it has priority over derived > value > elif '_' + f in request.vars: > if field_type not in ('date', 'datetime', 'decimal'): > db.table_name[f].default = convert(request.vars['_' + f], > field_type) > table_name_fields[f] = convert(request.vars['_' + f], > field_type) > elif field_type in ('date', 'datetime'): > if f == 'date': > db.table_name[f].default = > datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d').date() > table_name_fields[f] = > datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d').date() > elif f == 'datetime': > db.table_name[f].default = > datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d %H:%M:%S') > table_name_fields[f] = > datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d %H:%M:%S') > elif field_type == 'decimal': > db.table_name[f].default = Decimal(request.vars['_' + f]) > table_name_fields[f] = Decimal(request.vars['_' + f]) > ... > > > > But I am sure that there is something to parse request.vars and convert > string vars into their specific type in web2py i
Re: [web2py] convert request.vars to set default value in a custom .factory() form
I come up with this : def convert(value, type_): import importlib try: # Check if it's a builtin type module = importlib.import_module('__builtin__') cls = getattr(module, type_) except AttributeError: # if not, separate module and class module, type_ = type_.rsplit(".", 1) module = importlib.import_module(module) cls = getattr(module, type_) return cls(value) def controller_func(): ... field_type_mapping = \ {'id': 'int', 'reference': 'int', 'boolean': 'bool', 'string': 'str', 'decimal': 'decimal', 'date': 'date', 'datetime': 'datetime'} for f in table_name_fields: if db.table_name[f].type.split('(')[0] == 'decimal': field_type = field_type_mapping['decimal'] elif db.table_name[f].type.split(' ')[0] == 'reference': field_type = field_type_mapping['reference'] else: field_type = field_type_mapping[db.table_name[f].type] if f in request.vars: db.table_name[f].default = request.vars[f] table_name_fields[f] = request.vars[f] # If there is value in request vars it has priority over derived value elif '_' + f in request.vars: if field_type not in ('date', 'datetime', 'decimal'): db.table_name[f].default = convert(request.vars['_' + f], field_type) table_name_fields[f] = convert(request.vars['_' + f], field_type) elif field_type in ('date', 'datetime'): if f == 'date': db.table_name[f].default = datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d').date() table_name_fields[f] = datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d').date() elif f == 'datetime': db.table_name[f].default = datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d %H:%M:%S') table_name_fields[f] = datetime.datetime.strptime(request.vars['_' + f], '%Y-%m-%d %H:%M:%S') elif field_type == 'decimal': db.table_name[f].default = Decimal(request.vars['_' + f]) table_name_fields[f] = Decimal(request.vars['_' + f]) ... But I am sure that there is something to parse request.vars and convert string vars into their specific type in web2py internal... Still interested in another cleaner solution... Thanks Richard On Tue, Nov 1, 2016 at 10:45 AM, Richard wrote: > Hello, > > I have an high customized SQLFORM.factory() form... I set default value > for the update form for which I have no problem with values types as they > get out of the database... But in case I want to emulate keepvalue feature > with this form I need to pass value to request.vars which I need to set as > default... The problem is that all value are string when I get them and I > need to populate many fields and would greatly prefer not have to handle > each fields manually one by one but instead do it dynamically so if I had > field to the table I don't have to add it... So I am searching a way to > convert appropriately value for each field type... I could use the approach > proposed in the answer accepted hear : http://stackoverflow.com/ > questions/7402573/use-type-information-to-cast-values-stored-as-strings > > I there surely such a thing in the web2py internal... I thought > _filter_fields() would do it, but it only filter field that are member of a > table, it doesn't act over the value of the field... I thought of using > requires/validator, but I am not sure how to use them. > > Thanks > > Richard > > > > -- > 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 web2py+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[web2py] convert request.vars to set default value in a custom .factory() form
Hello, I have an high customized SQLFORM.factory() form... I set default value for the update form for which I have no problem with values types as they get out of the database... But in case I want to emulate keepvalue feature with this form I need to pass value to request.vars which I need to set as default... The problem is that all value are string when I get them and I need to populate many fields and would greatly prefer not have to handle each fields manually one by one but instead do it dynamically so if I had field to the table I don't have to add it... So I am searching a way to convert appropriately value for each field type... I could use the approach proposed in the answer accepted hear : http://stackoverflow.com/questions/7402573/use-type-information-to-cast-values-stored-as-strings I there surely such a thing in the web2py internal... I thought _filter_fields() would do it, but it only filter field that are member of a table, it doesn't act over the value of the field... I thought of using requires/validator, but I am not sure how to use them. Thanks Richard -- 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 web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.