librelist archives

« back to archive

@route equivalent for class-based views

@route equivalent for class-based views

From:
Frederik Dohr
Date:
2012-01-31 @ 13:01
Hello,

After refactoring my route handlers / view functions to group them in
MethodView classes, I couldn't find a `@route` equivalent for such
classes.

Since I quite like using decorators for this, I wrote (a hacky) one
myself: http://paste.pocoo.org/show/543420/
It kinda seems like I might be reinventing the wheel here though - am I
perhaps missing anything? If not, would something like this make sense
to be added to the framework?


Thanks,

F.

Re: [flask] @route equivalent for class-based views

From:
Frederik Dohr
Date:
2012-02-02 @ 17:12
> It kinda seems like I might be reinventing the wheel here though

Unsurprisingly, turns out (after a discussion on IRC[1]) this is more
complex than I first thought. As I understand it (and I'm not sure I do)
it comes down to class parameters - here's the relevant excerpt:

<Eftarjin> mitsuhiko: what would you think of having
    @app.route('/foo')\nclass Foo(View): in Flask?
<mitsuhiko> Eftarjin: i was considering it but the exact semantics i
    could never decide on
<Eftarjin> app.add_url_rule would call view_func.as_view() if the method
    exists, and name in View.as_view would default to cls.__name__
<mitsuhiko> Eftarjin: and how do you define class parameters?
<mitsuhiko> if it's just for one case it makes for a weird api
<Eftarjin> mitsuhiko: oh right. missed that
<mitsuhiko> where you can use app.route for some views but have to switch
    to add_url_rule for the cases where class based views are interesting
<mitsuhiko> the moment someone comes up with a nice syntax for it i'll
    add it :)
<FND> can you give me an example of the complex case?
<mitsuhiko> FND: http://flask.pocoo.org/docs/views/#method-views-for-apis
<mitsuhiko> and http://flask.pocoo.org/docs/views/#basic-principle
<Eftarjin> mitsuhiko: interesting cases, you mean when the same class is
    used with multiple sets for arguments?
<mitsuhiko> Eftarjin: for instance, yes


-- F.


[1] http://dev.pocoo.org/irclogs/%23pocoo.2012-01-31.log

Re: [flask] @route equivalent for class-based views

From:
Sean Lynch
Date:
2012-02-03 @ 07:25
Not sure if this is directly related, but I would like something that
matched closely to ASP.NET MVC's controller/routing, where a generic route
is registered and used to find the matching controller.

[C#]
routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id =
UrlParameter.Optional } // Parameter defaults
            );

So a path like /user/view/3 would find the UserController, View method, and
pass 3 as the "id" parameter to the method (you can also pass via query
string)


Maybe if we have something like MethodView, but instead of using methods
based on HTTP method, you used the methods as views/routes

class UserView(RouteView):
    def list(self):
        pass

    def show(self, user_id):
        pass

Then you would register the class like:
url('/user', 'user', 'views.UserView.as_view("user")')

There would still need to be a way to specify the HTTP methods on a per
view/method basis (ex. only allow GET to "list", but GET,POST to "show")

While this isn't the best example it would be best to use MethodView for
CRUD based APIs.  I'm just spitballing, so there are probably holes in this
idea, but I'd love for some simpler routing in flask when using classes.

I typically use Google App Engine and thus register my views Lazily (based
off of http://flask.pocoo.org/docs/patterns/lazyloading/) instead of using
the @app.route decorator, but would still like the option when not using
GAE.

On Thu, Feb 2, 2012 at 12:12 PM, Frederik Dohr <fdg001@gmx.net> wrote:

> > It kinda seems like I might be reinventing the wheel here though
>
> Unsurprisingly, turns out (after a discussion on IRC[1]) this is more
> complex than I first thought. As I understand it (and I'm not sure I do)
> it comes down to class parameters - here's the relevant excerpt:
>
> <Eftarjin> mitsuhiko: what would you think of having
>    @app.route('/foo')\nclass Foo(View): in Flask?
> <mitsuhiko> Eftarjin: i was considering it but the exact semantics i
>    could never decide on
> <Eftarjin> app.add_url_rule would call view_func.as_view() if the method
>    exists, and name in View.as_view would default to cls.__name__
> <mitsuhiko> Eftarjin: and how do you define class parameters?
> <mitsuhiko> if it's just for one case it makes for a weird api
> <Eftarjin> mitsuhiko: oh right. missed that
> <mitsuhiko> where you can use app.route for some views but have to switch
>    to add_url_rule for the cases where class based views are interesting
> <mitsuhiko> the moment someone comes up with a nice syntax for it i'll
>    add it :)
> <FND> can you give me an example of the complex case?
> <mitsuhiko> FND: http://flask.pocoo.org/docs/views/#method-views-for-apis
> <mitsuhiko> and http://flask.pocoo.org/docs/views/#basic-principle
> <Eftarjin> mitsuhiko: interesting cases, you mean when the same class is
>    used with multiple sets for arguments?
> <mitsuhiko> Eftarjin: for instance, yes
>
>
> -- F.
>
>
> [1] http://dev.pocoo.org/irclogs/%23pocoo.2012-01-31.log
>