Restish URL dispatching reusability

5 views
Skip to first unread message

Erik Allik

unread,
Aug 26, 2009, 5:35:33 AM8/26/09
to ish.io
Hi everyone,

First of all, I'd like to say that I think the URL dispatching/
rendering mechanism in Restish is the best I've seen yet. My question
is though: can it be decoupled from Restish in a way that would make
it reusable in other Python web/WSGI frameworks such as Django or
Werkzeug?

In a Django project I currently have a catch all URLconf entry ^(.*)/$
that does the custom dispatching and rendering based on what was
matched that I'd like to replace with (similar to) what Restish has.
And I currently cannot afford to migrate the whole project to Restish.

Any ideas?

Regards,
Erik Allik

Yoan Blanc

unread,
Aug 26, 2009, 10:04:07 AM8/26/09
to is...@googlegroups.com
Maybe Matt has a better point of view than me on this, but here are my 2 cents.

Restish says that if you want to show something, make it a resource
and the URL dispatching works that way.
Django gives let you doing a matching between URLs and views
(functions) called MTV

It's two approaches that won't play nice together without big changes
in the philosophy itself behind the framework.

Django has a powerful delegating mecanisms where you can nest them.
Which has been criticated and counter-criticated.
http://uswaretech.com/blog/2009/08/a-response-to-dropping-django/

What do you want to keep in Django ? Or how would you like something
called djangoish (which doesn't exist) to look like.

Cheers,

--
Yoan

2009/8/26 Erik Allik <eal...@gmail.com>:

Erik Allik

unread,
Aug 26, 2009, 10:47:46 AM8/26/09
to is...@googlegroups.com
As I've said, I've set up a catch all URL in Django that maps to a
view that behaves like a front controller. In that view, I could just
take the whole URL (effectively bypassing Django URLconf) and pass
that to Restish and just return the reponse.

urlpatterns = patterns('',
....,
url(r'^(?P<path>.*)/', front_controller_view),
)

def front_controller_view(request, path):
return delegate_to_restish(request, path):


Erik

Yoan Blanc

unread,
Aug 26, 2009, 12:13:51 PM8/26/09
to is...@googlegroups.com
So you want to put a WSGI application (what restish is) inside Django
which doesn't make sense at all but anyways. It would be better to
have a WSGI dispatcher that will send the URL x on a Restish app and
the others to Django for example.

What do you want to keep using from Django?

Matt Goodall

unread,
Aug 26, 2009, 2:13:21 PM8/26/09
to is...@googlegroups.com
2009/8/26 Yoan Blanc <yoan....@gmail.com>:
>
> Maybe Matt has a better point of view than me on this, but here are my 2 cents.

Probably not actually ;-).

Firstly, thanks for the kind words Erik. It's always nice to hear what
people think, good or bad (although good is best ;-)).

Werkzeug is more of a library that a web framework may use, so I would
expect Restish to use bits of Werkzeug rather than the other way
around.

I'm sure it's possible to wire Django and Restish togther somehow but,
I must admit, I'm finding it hard to imagine how the two styles of URL
dispatch, the dispatch endpoints (functions in a module vs methods of
a class) and mapping between the different style request objects would
fit together in a non-ugly way.

>
> Restish says that if you want to show something, make it a resource
> and the URL dispatching works that way.
> Django gives let you doing a matching between URLs and views
> (functions) called MTV
>
> It's two approaches that won't play nice together without big changes
> in the philosophy itself behind the framework.

Yeah, the big, centralised (kind of) URL mapping to functions vs
traversing resource instances (that are in full control, all the way
down) are quite a different style of application construction.

Both have pros and cons but traversal is more flexible, IMHO.

Erik Allik

unread,
Aug 26, 2009, 2:41:01 PM8/26/09
to is...@googlegroups.com
Yoan and Matt, thanks for your responses!

I can't migrate away from Django at the moment because of its ORM,
auth, templating and parts that have been written in a Django specific
way. The reason I'm looking at Restish is because our front end URL
mapping is something that Restish would fit for perfectly because all
URLs in our application map to objects in the database, not views, and
the hierarchy of objects is generic, i.e. something that Django can't
handle (which is why I've set up a (.*) catch-all view in Django that
does the actual URL parsing and dispatching - this is the part that
Restish would handle elegantly).

I assume it's possible to access the WSGI environment from a Django
view and just delegate to Restish using that. I just posted here to
get feedback on how this could be done elegantly (I'm not saying using
Restish inside a Django app is elegant, but I can't change that right
now).

Maybe I should phrase it this way: is it possible to decouple Restish
from WebOb and other parts that would block me from using it with
Django? Can I just use the part of Restish that treats all URLs as
resources while still returning Django HttpResponse objects from those
Restish resource handlers?

Erik

Erik Allik

unread,
Aug 26, 2009, 6:09:08 PM8/26/09
to ish.io
I think I found the solution even though it differs from what I
initially was looking for, i.e. being able to use Django HttpResponse
for returning responses inside Restish Resource's instead of webob,
but that's not a problem really (webob is probably better than Django
HttpResponse anyway :P).

Anyway, I found this:
http://code.djangoproject.com/ticket/8927#comment:8
which is basically about making it possible to call other WSGI apps
inside Django,

And the code I ended up using is:
http://github.com/rfk/django/blob/f96eb6068552e228f705beda9c8181db2079a797/django/wsgi/wsgi_to_django.py
Which just takes a WSGI app objet and wraps it as a regular Django
view that you can put into your Django URLconf. There are hacks
involved, but it works (and afaik also handles file uploads).

The code I have now is trivial:


from wsgi_to_django import django_view_from_wsgi_app
from restish.app import RestishApp
from restish import http, resource

class Root(resource.Resource):
@resource.child(resource.any)
def html(self, request, segments):
return http.ok([('Content-Type', 'text/html')],
"<p>Hello from resttest!</p>")

restish = django_view_from_wsgi_app(RestishApp(Root()))


I still have one question though: Restish doesn't automatically add a
trailing slash to URLs, so a @resource.GET method on a Resource does
not catch /foo/bar/ but just /foo/bar instead, which means I have to
declare a @resource.child('') or @resource.child(resource.any) in
order to catch /foo/bar/. Is there a way to change this behaviour in
Restish, i.e. to have Restish at least treat /bar/ equal to /bar, or
even better, redirect /bar to /bar/? (Although the latter is not
important for me as Django already does this for me.)


Thanks to everyone for their time,
Erik Allik


On 26 Aug, 21:41, Erik Allik <eal...@gmail.com> wrote:
> Yoan and Matt, thanks for your responses!
>
> I can't migrate away from Django at the moment because of its ORM,  
> auth, templating and parts that have been written in a Django specific  
> way. The reason I'm looking at Restish is because our front end URL  
> mapping is something that Restish would fit for perfectly because all  
> URLs in our application map to objects in the database, not views, and  
> the hierarchy of objects is generic, i.e. something that Django can't  
> handle (which is why I've set up a (.*) catch-all view in Django that  
> does the actual URL parsing and dispatching - this is the part that  
> Restish would handle elegantly).
>
> I assume it's possible to access the WSGI environment from a Django  
> view and just delegate to Restish using that. I just posted here to  
> get feedback on how this could be done elegantly (I'm not saying using  
> Restish inside a Django app is elegant, but I can't change that right  
> now).
>
> Maybe I should phrase it this way: is it possible to decouple Restish  
> from WebOb and other parts that would block me from using it with  
> Django? Can I just use the part of Restish that treats all URLs as  
> resources while still returning Django HttpResponse objects from those  
> Restish resource handlers?
>
> Erik
>
> On 26.08.2009, at 21:13, Matt Goodall wrote:
>
>
>
>
>
> > 2009/8/26 Yoan Blanc <yoan.bl...@gmail.com>:

Yoan Blanc

unread,
Aug 27, 2009, 3:06:10 AM8/27/09
to is...@googlegroups.com
the "foo/" to "foo" or vice-versa is the last thread we had here:

http://groups.google.co.uk/group/ishio/browse_thread/thread/f432f55b8b0ede18

--
Yoan

2009/8/27 Erik Allik <eal...@gmail.com>:

Matt Goodall

unread,
Aug 27, 2009, 8:21:10 AM8/27/09
to is...@googlegroups.com
2009/8/26 Erik Allik <eal...@gmail.com>:
>
> I think I found the solution even though it differs from what I
> initially was looking for, i.e. being able to use Django HttpResponse
> for returning responses inside Restish Resource's instead of webob,
> but that's not a problem really (webob is probably better than Django
> HttpResponse anyway :P).
>
> Anyway, I found this:
> http://code.djangoproject.com/ticket/8927#comment:8
> which is basically about making it possible to call other WSGI apps
> inside Django,
>
> And the code I ended up using is:
> http://github.com/rfk/django/blob/f96eb6068552e228f705beda9c8181db2079a797/django/wsgi/wsgi_to_django.py
> Which just takes a WSGI app objet and wraps it as a regular Django
> view that you can put into your Django URLconf. There are hacks
> involved, but it works (and afaik also handles file uploads).
>
> The code I have now is trivial:
>
>
> from wsgi_to_django import django_view_from_wsgi_app
> from restish.app import RestishApp
> from restish import http, resource
>
> class Root(resource.Resource):
>    @resource.child(resource.any)
>    def html(self, request, segments):
>        return http.ok([('Content-Type', 'text/html')],
>            "<p>Hello from resttest!</p>")
>
> restish = django_view_from_wsgi_app(RestishApp(Root()))

Ok, that's quite cool. Thanks for posting the code snippet back here.

Matt Goodall

unread,
Aug 27, 2009, 8:22:33 AM8/27/09
to is...@googlegroups.com
2009/8/27 Yoan Blanc <yoan....@gmail.com>:
>
> the "foo/" to "foo" or vice-versa is the last thread we had here:
>
> http://groups.google.co.uk/group/ishio/browse_thread/thread/f432f55b8b0ede18

Gah, that's 2 people wanting this now. I might have to add it as a
contrib utility soon ;-).

- Matt

Yoan Blanc

unread,
Aug 27, 2009, 10:09:09 AM8/27/09
to is...@googlegroups.com
I wouldn't got that way arguing that in Restish you have REST and that
maybe you should stick to that philosophy too.

/ -> the root
/books/ -> a directory of books
/books/Ants -> a book
/categories/ -> the categories
/categories/Sci-Fi -> a category
/categories/Sci-Fi/books/ -> a directory of Sci-Fi books
/categories/Sci-Fi/books/Ants -> the same book that above (see
rev/rel=canonical)

Just like any UN*X file system.

Cheers,

--
Yoan

2009/8/27 Matt Goodall <matt.g...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages