I don't think a decorator is the best solution.
For instance, many times the HTML version needs a more data and
processing than others (because it must render a header, sidebar,
etc.)

Something like this would be more flexible IMHO:

def GET(self, arg1, arg2, ...):
    ... code to retrieve the data ...

    if json_requested():
         return json(data)
    elif txt_requested():
        return txt(data)

    ...  more processing ...
    ... normal html rendering ...



On 22 abr, 18:00, Martin <[email protected]> wrote:
> Hi all,
> I started using web.py for programming web services and, although I'm
> quite happy with the framework, I couldn't find an out-of-the-box
> solution for what I was looking for. More specifically, I wanted to
> select the appropriate representation for the service's resources in a
> RESTful way, i.e. using the HTTP Accept header.
> I didn't like this:
>
> render_html = web.template.render('templates/html')
> render_xml = web.template.render('templates/xml')
> render_json = lambda **args: json.dumps(args)
>
> class hello:
>     def GET(self, name):
>         if not name: name = 'world'
>         message = 'Hello, %s!'%name
>         accept_string = web.ctx.env['HTTP_ACCEPT']
>         mime = find_best_supported_mime(accept_string)
>         if mime=='application/html':
>             return render_html.hello(message)
>         elif mime=='application/xml':
>             return render_xml.hello(message)
>         elif mime=='application/json':
>             return render_json(message)
>         else:
>             raise web.internalerror('no representation for "%s"'%mime)
>
> So I made it like this:
>
>     @mimerender.represent(
>         html = render_html.hello,
>         xml  = render_xml.hello,
>         json = render_json)
>     def GET(self, name):
>         if not name: name = 'world'
>         message = 'Hello, %s!'%name
>         return {'message': message}
>
> I've made the library open-source and posted it 
> athttp://code.google.com/p/mimerender
> Hope someone finds it useful, and I'll gladly receive all comments and
> criticisms.
> Best,
>
> Martín
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to