11
11
from marshmallow import fields as mf
12
12
from sqlalchemy .orm import DeclarativeBase
13
13
14
+ from flask_muck import FlaskMuck
14
15
from flask_muck .views import FlaskMuckApiView
15
16
16
17
# Create a Flask app
17
18
app = Flask (__name__ )
18
19
app .config ["SECRET_KEY" ] = "super-secret"
20
+ muck = FlaskMuck ()
21
+ muck .init_app (app )
19
22
20
23
21
24
# Init Flask-SQLAlchemy and set database to a local sqlite file.
@@ -50,8 +53,8 @@ class TodoSchema(ma.Schema):
50
53
text = mf .String (required = True )
51
54
52
55
53
- # Add a Flask blueprint for the base of the REST API and register it with the app .
54
- api_blueprint = Blueprint ("v1_api " , __name__ , url_prefix = "/api/v1/ " )
56
+ # Add a Flask blueprint to organize authentication views .
57
+ auth_blueprint = Blueprint ("auth " , __name__ , url_prefix = "/auth " )
55
58
56
59
57
60
# Init Flask-Login for user authentication and add login/logout endpoints.
@@ -64,7 +67,7 @@ def load_user(user_id):
64
67
return UserModel .query .get (user_id )
65
68
66
69
67
- @api_blueprint .route ("login" , methods = ["POST" ])
70
+ @auth_blueprint .route ("login" , methods = ["POST" ])
68
71
def login_view ():
69
72
"""Dummy login view that creates a User and authenticates them."""
70
73
user = UserModel ()
@@ -74,7 +77,7 @@ def login_view():
74
77
return {}, 200
75
78
76
79
77
- @api_blueprint .route ("logout" , methods = ["POST" ])
80
+ @auth_blueprint .route ("logout" , methods = ["POST" ])
78
81
def logout_view ():
79
82
logout_user ()
80
83
return {}, 200
@@ -102,13 +105,11 @@ class TodoApiView(BaseApiView):
102
105
searchable_columns = [TodoModel .text ]
103
106
104
107
105
- # Add all url rules to the blueprint.
106
- TodoApiView .add_rules_to_blueprint (api_blueprint )
107
-
108
- # Register api blueprint with the app.
109
- app .register_blueprint (api_blueprint )
108
+ # Register auth blueprint with the app.
109
+ app .register_blueprint (auth_blueprint )
110
110
111
111
if __name__ == "__main__" :
112
112
with app .app_context ():
113
113
db .create_all ()
114
+ muck .register_muck_views ([TodoApiView ])
114
115
app .run (debug = True )
0 commit comments