2017-09-03 102 views
0

下面的Flask-Classful(它是现在被废弃的Flask-Classy的维护叉)的工作,但我想知道是否有逻辑I的“本地”版本在Flask-Classful的最后两条路线中,没有混合烧瓶路线和Flask-Classful内置特殊方法,如get()post()Flask-Classy/Flask-Classful中的子路由

来自Flask-Classy在https://pythonhosted.org/Flask-Classy/上的大量文档,我不幸在下面的代码中找不到最后两条路线的例子,例如: GET /news/123/comments/GET /news/123/comments/321。有一些例子可能类似于GET /news/comments/123,但不适用于从路由URI(将用于数据库查询)中选择两个变量的情况。

from flask_classful import FlaskView, route 


class NewsView(FlaskView): 
    def index(self): 
     return "This is GET /news\n" 

    def get(self, news_id): 
     return "This is GET /news/{}\n".format(news_id) 

    def post(self): 
     return "This is POST /news\n" 

    def put(self, news_id): 
     return "This is PUT /news/{}\n".format(news_id) 

    def patch(self, news_id): 
     return "This is PATCH /news/{}\n".format(news_id) 

    def delete(self, news_id): 
     return "This is DELETE /news/{}\n".format(news_id) 

    @route("/<int:news_id>/comments/<int:comment_id>", methods=["GET"]) 
    def news_comment(self, news_id, comment_id): 
     return "This is GET /news/{}/comments/{}\n".format(news_id, comment_id) 

    @route("/<int:news_id>/comments/", methods=["GET"]) 
    def news_comments(self, news_id): 
     return "This is GET /news/{}/comments/\n".format(news_id) 

的路由进行登记:

def register_views(app): 
    api_path = "/api/1.0" 

    from apps.news.views import NewsView 
    NewsView.register(app, route_base="{}/news/".format(api_path)) 

看来,虽然工作。也许这只是劈头发,但我认为这样的内容可以在Flask-Classful中内置支持?

这里的一些测试的要求,它的工作原理:

$ curl -X GET https://localhost:443/api/1.0/news/ --insecure -L 
This is GET /news 
$ curl -X GET https://localhost:443/api/1.0/news/123 --insecure -L 
This is GET /news/123 
$ curl -X POST https://localhost:443/api/1.0/news/ --insecure -L 
This is POST /news 
$ curl -X PUT https://localhost:443/api/1.0/news/123 --insecure -L 
This is PUT /news/123 
$ curl -X PATCH https://localhost:443/api/1.0/news/123 --insecure -L 
This is PATCH /news/123 
$ curl -X DELETE https://localhost:443/api/1.0/news/123 --insecure -L 
This is DELETE /news/123 
$ curl -X GET https://localhost:443/api/1.0/news/1/comments/ --insecure -L 
This is GET /news/1/comments/ 
$ curl -X GET https://localhost:443/api/1.0/news/1/comments/2 --insecure -L 
This is GET /news/1/comments/2 
+0

所以你要使用'news_comment'你的路线,而不必使用一个装饰?这是你的问题吗? –

+0

@TarunLalwani是的。基本上我正在寻找一个Flask-Classy的方法来在同一个视图中为多个子路径(它们都共享“/ api/1.0/news /”路径)使用get()。 –

回答

1

默认情况下,当你使用一些名字

class NewsView(FlaskView): 
    def comments(self, news_id, comment_id): 
     pass 

以上默认情况下将其注册为/comments/<news_id>/<comment_id>,自你的基地创建方法路线是news/。评论方法的最终路线将变为news/comments/<news_id>/<comment_id>

但是谁又阻止我们改变呢?

from flask import Flask, request, jsonify 
import json 

from flask_classy import FlaskView 

app = Flask(__name__) 


class MyFlaskView(FlaskView): 
    @classmethod 
    def build_rule(cls, rule, method=None): 
     path = super(MyFlaskView, cls).build_rule(rule, method) 
     parts = path.split("/") 
     if len(parts) >= 3 and parts[-1].startswith("<") and parts[-2].startswith("<"): 
      parts[-3], parts[-2] = parts[-2], parts[-3] 
      return "/".join(parts) 
     return path 

class NewsView(MyFlaskView): 

    def comments(self, news_id, comment_id): 
     return "This is GET /news\n" 

    def get(self, news_id): 
     return "This is GET /news2\n" 

if __name__ == "__main__": 
    NewsView.register(app, route_base="news/") 
    app.run(debug=True) 

这种重写方法将注册为/news/<news_id>/comments/<comment_id>