I fix the classes I gave as example:
# The validation model
WikiPageValidator(object):
todict(self):
return {
"title": validators.UnicodeString,
"content": validators.UnicodeString,
}
from formencode import validators
# Here is a controller method (or function)
@expose('json')
@validate(validators=WikiPageValidator.todict())
def post(self, title=None, content=None, *args):
errors = [{key, value} in pylons.c.form_errors.iteritems()]
values = pylons.c.form_values
return dict(a=a, b=b, errors=str(errors), values=str(values))
What would be wonderfull would be to define controllers with given Validator as
controller parameters, eg:
# The validation model
WikiPage(object):
title = validators.UnicodeString
content = validators.UnicodeString
from formencode import validators
# Here is a controller method (or function)
@expose('json')
@validate(validators=WikiPage)
def post(self, workload = WikiPage()):
...
...
return dict(workload = workload)
Do you think it's easy to implement? Maybe there is already something that
would act similarly to this ? Maybe a decorator that would map every attribute
to the same class member of the given model ?
----- Mail original -----
De: "damien accorsi" <[email protected]>
À: [email protected]
Envoyé: Lundi 31 Mars 2014 11:22:11
Objet: Re: [TurboGears] Web services with turbogears2
Thanks for your answer, Alessandro. Another question about the @validate
decorator. What I like with the WSME webservice layer is that it allows to
define structures to be used for validation. For example, I create a class
WikiPage with required properties, and when the client sends a request in json
format eg { "title": "blabla", "content": "<html>this is the wiki page body
</html>"}, WSME will try to match my model class with the json sctructure.
hm... actually, I looked at http://turbogears.org/2.1/docs/main/Validation.html
and I believe I can build classes like this:
WikiPageValidator(object):
todict(self):
return {
"title": validators.UnicodeString,
"content": validators.UnicodeString,
}
from formencode import validators
@expose('json')
@validate(validators=WikiPageValidator.todict())
def two_validators(self, a=None, b=None, *args):
errors = [{key, value} in pylons.c.form_errors.iteritems()]
values = pylons.c.form_values
return dict(a=a, b=b, errors=str(errors), values=str(values))
What do you think ? Is it a good way to use it ?
Damien
----- Mail original -----
De: "Alessandro Molina" <[email protected]>
À: "TurboGears ." <[email protected]>
Envoyé: Vendredi 28 Mars 2014 18:45:15
Objet: Re: [TurboGears] Web services with turbogears2
Actually TG works pretty well for REST API due to built-in support for JSON,
RestController and @validate
There is also a tutorial on rapid-prototyping REST API using the
EasyCrudRestController which shows how to setup an autogenerated rest api with
minimum effort:
http://turbogears.readthedocs.org/en/latest/cookbook/Crud/restapi.html
On Fri, Mar 28, 2014 at 5:22 PM, < [email protected] > wrote:
Hi all,
I'm about to start a new project which is a web application mostly based on a
server-side application implementing a REST/Json API and a database connection
(through SQLAlchemy). I was thinking of using WSME for the webservice layer
(formatting output + data validation) but it seems that it does not support
TG2.
My question is: is there a simple way to implement a REST/Json api with TG2
(with data validation) ? If not, what framework would you recommend to do so ?
I'm looking for a full-stack framework with features like user rights
management (like TG2 do).
Thanks in advance.
--
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected] .
To post to this group, send email to [email protected] .
Visit this group at http://groups.google.com/group/turbogears .
For more options, visit https://groups.google.com/d/optout .
--
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected] .
To post to this group, send email to [email protected] .
Visit this group at http://groups.google.com/group/turbogears .
For more options, visit https://groups.google.com/d/optout .
--
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.