On Apr 25, 3:05 am, Anand Chitipothu <[email protected]> wrote:
> How about doing it this way:
>
> class jsonrender:
> def __getattr__(self, name):
> return self
> def __call__(self, d):
> return simplejson.dumps(d)
>
> mimerender = web.mimerender(
> html=render.html,
> xml=render.xml,
> json=jsonrender()
> )
>
> class Hello:
> def GET(self, name):
> msg = "hello, " + (name or "world")
> return render.hello({"message": msg})
>
> Here multirender dispatches to appropriate render bases as HTTP_ACCEPT.
> I tried this approach in one of my project and it turned out that I
> don't need json for all pages. Probably supporting _accept parameter
> to render argument may help.
>
> For example, it probably makes sense to allow only html encoding for
> user registrations.
>
> class register:
> def GET(self):
> return render.register(_accept=["html"])
I like this approach in general, although I think it has 2 weak
points:
1. Sometimes, as Juan Pablo said in a previous message, some
representations need an extra bit of logic than others. With this
approach, I don't see where one could put that extra code in a tidy
way.
2. I think the _accept part looks better in a decorator than in the
return line. It's just a question of style, I know, but decorators
look as part of the method's signature. You can then, without having
to read the method's body, see easily that "class X, when handling an
http GET, can produce html, xml and json".
Anyway, I think it's a good idea to have a non-decorator
implementation, some people don't like them at all. It would be very
easy to write oneself the decorator on top of it.
>
> Another approach that I tried was this:
>
> app.add_processor(accept_processor)
>
> class hello:
> def GET(self, name):
> name = name or "world"
> return render.hello(name)
>
> def GET_json(self, name):
> return {"message": "Hello, " + (name or "world")}
>
> def GET_xml(self, name):
> name = name or "world"
> return render_xml.hello(name)
>
> Here I assumed that accept_processor knows when to convert data to
> json. But we can explicitly specify by adding multiple
> accept_processors, each handling only one mimetype.
>
> app.add_processor(accept_processor(prefix="xml", mimetype="text/xml"))
> app.add_processor(accept_processor(prefix="json",
> mimetype="application/json", process=lambda d: simplejson.dumps(d)))
I don't think this is necesary. GET_json could explicitly call
simplejson.dumps, the same way their counterparts do with the
templates.
>
> This delegates all text/xml requests to GET_xml method and
> application/json requests to GET_json method.
>
> any comments?
I like the second approach. Having a separate method for each content
type is very flexible and it's very straightforward to implement
special processing for each type.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---