Methods returning database/table scheme

136 views
Skip to first unread message

Alan Etkin

unread,
Jan 20, 2013, 4:58:54 PM1/20/13
to web2py-d...@googlegroups.com
I wonder if other frameworks support this and if web2py should implement it (if not already supported):

form.as_dict() # a dict representation of the form fields, values and errors
table.as_dict() # returns table scheme with fields, types, defaults and validators
db.as_dict() # returns table.as_dict values and db connection information

This methods can be useful for data exchange with client software like angularjs but also for services in general.
There's PySimpleSOAP and the SOAP for all the above requirements, but this should provide a more simple (yet less powerful) interface.

I think it's an easy feature to add if values are retrieved statically (a defined subset of the given object structure), but there is at least one problem: AFAIK, there is no way of serving that information in a standard way (or maybe there is a conventional way for each db engine), so different apps probably need a diferent output scheme.

So apps should implement their own scheme retrieval tools or should web2py provide it (and how)?

Massimo DiPierro

unread,
Jan 20, 2013, 5:27:09 PM1/20/13
to web2py-d...@googlegroups.com
We can implement it. Not sure how useful but no objection.

--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
 
 

lpg

unread,
Feb 20, 2013, 12:28:33 PM2/20/13
to web2py-d...@googlegroups.com
Is anyone else actually using Angular in production with web2py? If so how have you handled server-side validators? 

>table.as_dict() # returns table scheme with fields, types, defaults and validators

We're using Angularjs, but are having to custom code a lot of form validation. So any API for this would be helpful. 

Just exposing errors by json would solve many issues (check if an object is IN DB or not).

Alan Etkin

unread,
Feb 21, 2013, 8:46:28 AM2/21/13
to web2py-d...@googlegroups.com
Is anyone else actually using Angular in production with web2py? If so how have you handled server-side validators? 

I sent an enhancement request for allowing:


def service_action():
    form
= ...
   
return form.as_dict(flat=True)

Then it would be simpler to handle form processing client-side

It also adds form.as_json form.as_yaml and form.as_xml

http://code.google.com/p/web2py/issues/detail?id=1350

Niphlod

unread,
Feb 21, 2013, 9:03:37 AM2/21/13
to web2py-d...@googlegroups.com
Sorry for being late .... patch helps, but I wonder why don't you exploit the facilities that web2py already provides....

if you're posting the form values back to web2py just using json(form.errors) in the case should suffice....
i.e.

def something():
      form
= SQLFORM(db.table)
     
if form.process().accepted:
          rtn
= {'status' : 'success'}
     
elif form.errors:
          rtn
= {'status': 'error', 'errors' : form.errors}
     
......
     
return json(rtn)

If you need instead to check if a record can be inserted or not, you can use validate_and_insert
i.e.

def theendpointoftheapi():
    fields
= request.vars
    rtn
= db.table.validate_and_insert(**fields)
   
if rtn.error:
       
return json({'status': 'error', 'errors' : rtn.error})
   
else:
       
return json({'status' : 'success', 'id' : rtn.id})


Alan Etkin

unread,
Feb 22, 2013, 6:45:48 AM2/22/13
to web2py-d...@googlegroups.com

if you're posting the form values back to web2py just using json(form.errors) in the case should suffice....

Of course, web2py api has the tools to return data back in a custom way, validate and do db i/o. The patch saves code for those not worried about what's in the output. Let the client code handle it. For example:

def myaction():
    form
= SQLFORM(db.data)
   
if form.process().accepted:
       
...
   
return form.as_json()

A client that doesn't need the html can simply read the form content and provide it's own user interface by using the database scheme serialization feature and then submitting the data with the form parameters. Custom implementations i.e. for adressing performance and desing issues not solved by a generic interface can always use the common api.

BTW: It would be useful also a javascript function at web2py.js to handle non html form submission as a wrapper for jQuery.post(). Something like:

formsubmit (action, vars, protocol, callback)

I think web2py_trap_form does something similar but it needs an html form

Niphlod

unread,
Feb 22, 2013, 8:02:28 AM2/22/13
to web2py-d...@googlegroups.com
ok, understood the the form.as_json() utility.

form submission should be handled finely with w2p_ajax_page('post', url, form.serialize(), '') ?
I'm using that a lot working on the scheduler monitor.

Honestly, the only thing I'm missing from web2py.js facilities is the complete lack of callbacks, but then again, once added them one needs the onsuccess, onerror, oncomplete, etc etc etc....everything becomes too custom.

Alan Etkin

unread,
Feb 22, 2013, 9:54:20 AM2/22/13
to web2py-d...@googlegroups.com

form submission should be handled finely with w2p_ajax_page('post', url, form.serialize(), '') ?

I think so, but not for json forms, AFAIK it only works with html elements.

Niphlod

unread,
Feb 22, 2013, 10:14:11 AM2/22/13
to web2py-d...@googlegroups.com
wait a bit.... what are json forms ?
if you mean forms that post raw json data, isn't there something that translate the key --> value pairs to a "more digestable" url-encoded string (like .serialize() is doing collecting values from form inputs) ?

Alan Etkin

unread,
Feb 22, 2013, 10:27:20 AM2/22/13
to web2py-d...@googlegroups.com
wait a bit.... what are json forms ?
if you mean forms that post raw json data, isn't there something that translate the key --> value pairs to a "more digestable" url-encoded string (like .serialize() is doing collecting values from form inputs) ?

By json forms I meant a web2py form object sent to client as json (the output of form.as_json()). It's not a straight mapping of form widgets, but it's .__dict__ representation plus some filtering of incompatible/unsafe data. It's intended as a way of retrieving form data client-side, setting values and (not directly provided by web2py api, but perhaps a useful enhancement) post the vars back to the server, maybe updating it automatically with the server processed form sent back. I don't actually know if web2py.js functions accept serialized forms for posting data. It can be done with jQuery anyway.
 

Alec Taylor

unread,
Feb 22, 2013, 10:39:25 AM2/22/13
to web2py-d...@googlegroups.com
Nice beginning; is the end plan to generate decoupled AngularJS
forms—complete with validation—from web2py SQLFORM?

/me for one would love that idea
> --
> -- mail from:GoogleGroups "web2py-developers" mailing list
> make speech: web2py-d...@googlegroups.com
> unsubscribe: web2py-develop...@googlegroups.com
> details : http://groups.google.com/group/web2py-developers
> the project: http://code.google.com/p/web2py/
> official : http://www.web2py.com/
> ---
> You received this message because you are subscribed to the Google Groups
> "web2py-developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to web2py-develop...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

lpg

unread,
Feb 23, 2013, 12:38:38 PM2/23/13
to web2py-d...@googlegroups.com
+1 for Alan's Patch
It's a way to future-proof web2py with all the JS MVC frameworks growing up.

Niphlod

unread,
Feb 23, 2013, 12:49:56 PM2/23/13
to web2py-d...@googlegroups.com
What's really needed at this point is someone that is working with some js framework to do some handwork and map new widgets to the fields and validators.

lpg

unread,
Feb 23, 2013, 7:02:33 PM2/23/13
to web2py-d...@googlegroups.com
We're just getting started with angular. If you give us a couple weeks I'm happy to contribute the components for data bindings to forms in angular. If someone else has already implemented angular "directives" we could share notes.

Alec Taylor

unread,
Feb 25, 2013, 10:31:41 AM2/25/13
to web2py-d...@googlegroups.com
On Sun, Feb 24, 2013 at 11:02 AM, lpg <lucas....@gmail.com> wrote:
> We're just getting started with angular. If you give us a couple weeks I'm happy to contribute the components for data bindings to forms in angular. If someone else has already implemented angular "directives" we could share notes.

Thanks Lucas; looking forward to your contribution :)

In the meantime, I will finish up my OAuth2 server implementation for web2py.
Reply all
Reply to author
Forward
0 new messages