Skip to content

Commit c554761

Browse files
committed
✨ (Many-to-many) Add start and end folders in lectures
1 parent da90042 commit c554761

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1220
-1
lines changed

docs/docs/07_sqlalchemy_many_to_many/02_one_to_many_review/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ title: One-to-many relationships review
33
description: A super-quick look at creating the Tag model and setting up the one-to-many relationship with Stores.
44
---
55

6+
- [x] Set metadata above
7+
- [x] Start writing!
8+
- [x] Create `start` folder
9+
- [x] Create `end` folder
10+
- [ ] Create per-file diff between `end` and `start` (use "Compare Folders")
11+
612
# One-to-many relationship between Tag and Store
713

814
Since we've already learned how to set up one-to-many relationships with SQLAlchemy when we looked at Items and Stores, let's go quickly in this section.
@@ -139,5 +145,51 @@ class Tag(MethodView):
139145
def get(self, tag_id):
140146
tag = TagModel.query.get_or_404(tag_id)
141147
return tag
148+
```
149+
150+
## Register the Tag blueprint in `app.py`
151+
152+
Finally, we need to remember to import the blueprint and register it!
153+
154+
```python title="app.py"
155+
from flask import Flask
156+
from flask_smorest import Api
157+
158+
import models
159+
160+
from db import db
161+
from resources.item import blp as ItemBlueprint
162+
from resources.store import blp as StoreBlueprint
163+
# highlight-start
164+
from resources.tag import blp as TagBlueprint
165+
# highlight-end
166+
167+
168+
def create_app(db_url=None):
169+
app = Flask(__name__)
170+
app.config["API_TITLE"] = "Stores REST API"
171+
app.config["API_VERSION"] = "v1"
172+
app.config["OPENAPI_VERSION"] = "3.0.3"
173+
app.config["OPENAPI_URL_PREFIX"] = "/"
174+
app.config["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui"
175+
app.config[
176+
"OPENAPI_SWAGGER_UI_URL"
177+
] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/"
178+
app.config["SQLALCHEMY_DATABASE_URI"] = db_url or "sqlite:///data.db"
179+
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
180+
app.config["PROPAGATE_EXCEPTIONS"] = True
181+
db.init_app(app)
182+
api = Api(app)
183+
184+
@app.before_first_request
185+
def create_tables():
186+
db.create_all()
187+
188+
api.register_blueprint(ItemBlueprint)
189+
api.register_blueprint(StoreBlueprint)
190+
# highlight-start
191+
api.register_blueprint(TagBlueprint)
192+
# highlight-end
142193

194+
return app
143195
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FLASK_APP=app
2+
FLASK_ENV=development
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3.10
2+
EXPOSE 5000
3+
WORKDIR /app
4+
COPY ./requirements.txt requirements.txt
5+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
6+
COPY . .
7+
CMD ["flask", "run", "--host", "0.0.0.0"]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from flask import Flask
2+
from flask_smorest import Api
3+
4+
import models
5+
6+
from db import db
7+
from resources.item import blp as ItemBlueprint
8+
from resources.store import blp as StoreBlueprint
9+
from resources.tag import blp as TagBlueprint
10+
11+
12+
def create_app(db_url=None):
13+
app = Flask(__name__)
14+
app.config["API_TITLE"] = "Stores REST API"
15+
app.config["API_VERSION"] = "v1"
16+
app.config["OPENAPI_VERSION"] = "3.0.3"
17+
app.config["OPENAPI_URL_PREFIX"] = "/"
18+
app.config["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui"
19+
app.config[
20+
"OPENAPI_SWAGGER_UI_URL"
21+
] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/"
22+
app.config["SQLALCHEMY_DATABASE_URI"] = db_url or "sqlite:///data.db"
23+
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
24+
app.config["PROPAGATE_EXCEPTIONS"] = True
25+
db.init_app(app)
26+
api = Api(app)
27+
28+
@app.before_first_request
29+
def create_tables():
30+
db.create_all()
31+
32+
api.register_blueprint(ItemBlueprint)
33+
api.register_blueprint(StoreBlueprint)
34+
api.register_blueprint(TagBlueprint)
35+
36+
return app
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from flask_sqlalchemy import SQLAlchemy
2+
3+
db = SQLAlchemy()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from models.item import ItemModel
2+
from models.tag import TagModel
3+
from models.store import StoreModel
4+
from models.item_tags import ItemsTags
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from db import db
2+
3+
4+
class ItemModel(db.Model):
5+
__tablename__ = "items"
6+
7+
id = db.Column(db.Integer, primary_key=True)
8+
name = db.Column(db.String(80), unique=True, nullable=False)
9+
price = db.Column(db.Float(precision=2), unique=False, nullable=False)
10+
11+
store_id = db.Column(
12+
db.Integer, db.ForeignKey("stores.id"), unique=False, nullable=False
13+
)
14+
store = db.relationship("StoreModel", back_populates="items")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from db import db
2+
3+
4+
class StoreModel(db.Model):
5+
__tablename__ = "stores"
6+
7+
id = db.Column(db.Integer, primary_key=True)
8+
name = db.Column(db.String(80), unique=True, nullable=False)
9+
10+
tags = db.relationship("TagModel", back_populates="store", lazy="dynamic")
11+
items = db.relationship("ItemModel", back_populates="store", lazy="dynamic")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from db import db
2+
3+
4+
class TagModel(db.Model):
5+
__tablename__ = "tags"
6+
7+
id = db.Column(db.Integer, primary_key=True)
8+
name = db.Column(db.String(80), unique=True, nullable=False)
9+
store_id = db.Column(db.String(), db.ForeignKey("stores.id"), nullable=False)
10+
11+
store = db.relationship("StoreModel", back_populates="tags")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Flask-JWT-Extended
2+
Flask-Smorest
3+
Flask-SQLAlchemy
4+
passlib
5+
marshmallow
6+
python-dotenv
7+
gunicorn

0 commit comments

Comments
 (0)