librelist archives

« back to archive

Question about Human-readable forms + REST CRUD APIs.

Question about Human-readable forms + REST CRUD APIs.

From:
Matthew Wood
Date:
2012-07-29 @ 02:39
A quick question about basic CRUD for both humans (basic HTML) and computers
(REST)

I'm trying to pick a solid python framework for a project at work.  We have
a
lot of situations where we need to expose datbase tables for data CRUD.

I'm trying to pick between Flask and Web2py at this time, but I've got a
specific need that I'm not sure is solved by either framework.

Obviously for human consumption, I'll be using some sort of form-library
that
will read the table-schema auto-create the form for me.  (Built in with
Web2py,
WTForms + SQL-Alchemy for Flask.)  For the REST interfaces, I'll use a
similar
library (Built in with Web2py, flask-rest for flask).

What I'd REALLY like, is to have both the REST interfaces and html-forms
use as
much of a common code-path as possible.  It's really frustrating when things
like data-validation work differently between one's REST interfaces and
one's
html forms.

Thusfar, in my investigations with both frameworks, I've felt that both
mechanisms appear quite different, and/or I have to do a lot of manual
parsing
of XML in the Post/Put REST case.

HOWEVER...  I've really only spent a little time investigating either one.
It's extremely likely that I'm just completely missing the best way to solve
the issue I'm describing.

Can anyone point me to an example that works the way I'm describing?

Hopefully, I have asked my question well enough.  :-) I'm feeling horribly
inelequent right now.


--
Matthew Wood

Big Jiu Jitsu Noob
Brand new daughter who's so cute
Soon she'll submit me

Re: [flask] Question about Human-readable forms + REST CRUD APIs.

From:
Mark Steve Samson
Date:
2012-07-31 @ 02:54
You can use same routes for your forms and rest interface.
Just add some header checks (X-Requested-With, Referrer, etc) or use a 
flag (?form=1) to identify where the request is coming from and adjust 
input (request.form vs request.json), auth (session vs tokens) and output 
(flash, redirect vs return jsonify [or you could use same output and use 
ajax for the forms as well]).


-- 
Mark Steve Samson


On Sunday, July 29, 2012 at 10:39 AM, Matthew Wood wrote:

> A quick question about basic CRUD for both humans (basic HTML) and computers
> (REST)
> 
> I'm trying to pick a solid python framework for a project at work.  We have a
> lot of situations where we need to expose datbase tables for data CRUD.
> 
> I'm trying to pick between Flask and Web2py at this time, but I've got a
> specific need that I'm not sure is solved by either framework.
> 
> Obviously for human consumption, I'll be using some sort of form-library that 
> will read the table-schema auto-create the form for me.  (Built in with Web2py,
> WTForms + SQL-Alchemy for Flask.)  For the REST interfaces, I'll use a similar
> library (Built in with Web2py, flask-rest for flask).
> 
> What I'd REALLY like, is to have both the REST interfaces and html-forms use as
> much of a common code-path as possible.  It's really frustrating when things
> like data-validation work differently between one's REST interfaces and one's
> html forms.
> 
> Thusfar, in my investigations with both frameworks, I've felt that both
> mechanisms appear quite different, and/or I have to do a lot of manual parsing
> of XML in the Post/Put REST case.
> 
> HOWEVER...  I've really only spent a little time investigating either one.
> It's extremely likely that I'm just completely missing the best way to solve
> the issue I'm describing.
> 
> Can anyone point me to an example that works the way I'm describing?
> 
> Hopefully, I have asked my question well enough.  :-) I'm feeling horribly
> inelequent right now.
> 
> 
> 
> --
> Matthew Wood
> 
> Big Jiu Jitsu Noob
> Brand new daughter who's so cute
> Soon she'll submit me
> 

Re: [flask] Question about Human-readable forms + REST CRUD APIs.

From:
Live Flex
Date:
2012-07-31 @ 12:45
I wanted to use Flask for an API and encountered the same problem - there
is no reasonably documented method for performing validation on the models.
Various solutions include hooking into WTForm's validation and using
Colander (
http://docs.pylonsproject.org/projects/colander/en/latest/index.html), but
all of these seem half baked solutions

On Tue, Jul 31, 2012 at 3:54 AM, Mark Steve Samson <hello@marksteve.com>wrote:

>  You can use same routes for your forms and rest interface.
> Just add some header checks (X-Requested-With, Referrer, etc) or use a
> flag (?form=1) to identify where the request is coming from and adjust
> input (request.form vs request.json), auth (session vs tokens) and output
> (flash, redirect vs return jsonify [or you could use same output and use
> ajax for the forms as well]).
>
> --
> Mark Steve Samson
>
> On Sunday, July 29, 2012 at 10:39 AM, Matthew Wood wrote:
>
> A quick question about basic CRUD for both humans (basic HTML) and
> computers
> (REST)
>
> I'm trying to pick a solid python framework for a project at work.  We
> have a
> lot of situations where we need to expose datbase tables for data CRUD.
>
> I'm trying to pick between Flask and Web2py at this time, but I've got a
> specific need that I'm not sure is solved by either framework.
>
> Obviously for human consumption, I'll be using some sort of form-library
> that
> will read the table-schema auto-create the form for me.  (Built in with
> Web2py,
> WTForms + SQL-Alchemy for Flask.)  For the REST interfaces, I'll use a
> similar
> library (Built in with Web2py, flask-rest for flask).
>
> What I'd REALLY like, is to have both the REST interfaces and html-forms
> use as
> much of a common code-path as possible.  It's really frustrating when
> things
> like data-validation work differently between one's REST interfaces and
> one's
> html forms.
>
> Thusfar, in my investigations with both frameworks, I've felt that both
> mechanisms appear quite different, and/or I have to do a lot of manual
> parsing
> of XML in the Post/Put REST case.
>
> HOWEVER...  I've really only spent a little time investigating either one.
> It's extremely likely that I'm just completely missing the best way to
> solve
> the issue I'm describing.
>
> Can anyone point me to an example that works the way I'm describing?
>
> Hopefully, I have asked my question well enough.  :-) I'm feeling horribly
> inelequent right now.
>
>
> --
> Matthew Wood
>
> Big Jiu Jitsu Noob
> Brand new daughter who's so cute
> Soon she'll submit me
>
>
>

Re: [flask] Question about Human-readable forms + REST CRUD APIs.

From:
Jesse Panganiban
Date:
2012-07-31 @ 14:20
You could write your own model validation. It's not that hard.

class BaseClass(object):
     # an attribute you could use to keep track of your model's attributes
     # could be a dict containing the data-type, or simply a tuple.
     model_attrs = {}

     # Create a special classmethod that contains the validation logic 
for your models
     # that you would use to instantiate them.
     @classmethod
     def validated_instantiation(cls, **kwargs):
         # you can pass the data as keword arguments
         # ...do validation here
         # errors = []
         # ex. for key, value in kwargs.iteritems():
         # if key in cls.model_attrs:
         #     if not isinstance(value, cls.model_attrs[key]):
         #         errors.append({key: '<state the reason for error>'})
         # if not errors:
         #     return cls(**kwargs)
         # return {'errors': errors}

Base = declarative_base(cls=BaseClass)

Creation:

class Model(Base):
     id = ...
     name = ...
     model_attrs = {
         'id': int,
         'name': string,
     }

     def to_dict(self):
         # return data as dictionary

# Inside a request assuming you have the data.
m = Model.validated_instantiation(**request.json)
if isinstance(m, dict):  # this one's if the validation has an error
     return jsonify(m)
session.add(m)
session.commit()
return jsonify(m.to_dict())

I hope this helped.

On Tuesday, 31 July, 2012 08:45 PM, Live Flex wrote:
> I wanted to use Flask for an API and encountered the same problem - 
> there is no reasonably documented method for performing validation on 
> the models. Various solutions include hooking into WTForm's validation 
> and using Colander 
> (http://docs.pylonsproject.org/projects/colander/en/latest/index.html), but 
> all of these seem half baked solutions
>
> On Tue, Jul 31, 2012 at 3:54 AM, Mark Steve Samson 
> <hello@marksteve.com <mailto:hello@marksteve.com>> wrote:
>
>     You can use same routes for your forms and rest interface.
>     Just add some header checks (X-Requested-With, Referrer, etc) or
>     use a flag (?form=1) to identify where the request is coming from
>     and adjust input (request.form vs request.json), auth (session vs
>     tokens) and output (flash, redirect vs return jsonify [or you
>     could use same output and use ajax for the forms as well]).
>
>     -- 
>     Mark Steve Samson
>
>     On Sunday, July 29, 2012 at 10:39 AM, Matthew Wood wrote:
>
>>     A quick question about basic CRUD for both humans (basic HTML)
>>     and computers
>>     (REST)
>>
>>     I'm trying to pick a solid python framework for a project at
>>     work.  We have a
>>     lot of situations where we need to expose datbase tables for data
>>     CRUD.
>>
>>     I'm trying to pick between Flask and Web2py at this time, but
>>     I've got a
>>     specific need that I'm not sure is solved by either framework.
>>
>>     Obviously for human consumption, I'll be using some sort of
>>     form-library that
>>     will read the table-schema auto-create the form for me.  (Built
>>     in with Web2py,
>>     WTForms + SQL-Alchemy for Flask.)  For the REST interfaces, I'll
>>     use a similar
>>     library (Built in with Web2py, flask-rest for flask).
>>
>>     What I'd REALLY like, is to have both the REST interfaces and
>>     html-forms use as
>>     much of a common code-path as possible.  It's really frustrating
>>     when things
>>     like data-validation work differently between one's REST
>>     interfaces and one's
>>     html forms.
>>
>>     Thusfar, in my investigations with both frameworks, I've felt
>>     that both
>>     mechanisms appear quite different, and/or I have to do a lot of
>>     manual parsing
>>     of XML in the Post/Put REST case.
>>
>>     HOWEVER...  I've really only spent a little time investigating
>>     either one.
>>     It's extremely likely that I'm just completely missing the best
>>     way to solve
>>     the issue I'm describing.
>>
>>     Can anyone point me to an example that works the way I'm describing?
>>
>>     Hopefully, I have asked my question well enough.  :-) I'm feeling
>>     horribly
>>     inelequent right now.
>>
>>
>>     --
>>     Matthew Wood
>>
>>     Big Jiu Jitsu Noob
>>     Brand new daughter who's so cute
>>     Soon she'll submit me
>>
>
>