2017-09-03 17 views
0

以下はFlask-Classfulで動作しますが(現在廃止されているFlask-Classyの維持フォークです)、論理Iの「ネイティブ」バージョンがあるかどうかは疑問でしたFlask-Classfulの最後の2つのルートは、FlaskルートとFlask-Classful組み込み特殊メソッド(例えば、get()post()など)を混ぜる必要はありませんか?Flask-Classy/Flask-Classfulのサブルート

Flask-Classyはhttps://pythonhosted.org/Flask-Classy/にあります残念ながら、私は残念ながら以下のコードの最後の2つのルートの例を見つけることはできませんでした。 GET /news/123/comments/およびGET /news/123/comments/321GET /news/comments/123のようなケースがいくつかありましたが、ルートURI(DBクエリに使用される)から2つの変数を選択した場合はそうではありません。

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のサポートが組み込まれていると思いますか?あなたには、いくつかの名前

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

/comments/<news_id>/<comment_id>として、あなたのベース以来、それを登録しますデフォルトでは、上記を使用してメソッドを作成する場合、デフォルトで

$ 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'を使用したい/news/<news_id>/comments/<comment_id>として、あなたのルートを登録しますか?それはあなたの質問ですか? –

+0

@ TarunLalwaniはい。基本的に私は複数のサブパス(それらはすべて ''/api/1.0/news/''のパスを共有しています)を '' get() ''を同じビューで使うFlask-Classyの方法を探しています。 –

答えて

1

:ここ

は、それが動作しますいくつかのテスト要求、です経路は 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) 

このオーバーライドされたメソッドは、デコレータを使用せずに

関連する問題