-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
96 lines (67 loc) · 2.49 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_pyfile("config.py")
db = SQLAlchemy(app)
tags = db.Table(
"tags",
db.Column("tag_id", db.Integer, db.ForeignKey("tag.id"), primary_key=True),
db.Column("post_id", db.Integer, db.ForeignKey("post.id"), primary_key=True),
)
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
def __repr__(self):
return f"<Tag {self.name}>"
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), nullable=False)
body = db.Column(db.Text, nullable=False)
category_id = db.Column(db.Integer, db.ForeignKey("category.id"), nullable=False)
category = db.relationship("Category", backref=db.backref("posts", lazy=True))
tags = db.relationship(
"Tag", secondary=tags, lazy="subquery", backref=db.backref("posts", lazy=True)
)
def __repr__(self):
return f"<Post {self.title}>"
class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
def __repr__(self):
return f"<Category {self.name}>"
@app.route("/tags", methods=["GET", "POST"])
def get_tags():
if request.method == "POST":
name = request.form["name"]
tag = Tag(name=name)
db.session.add(tag)
db.session.commit()
return render_template("tags.html", tags=Tag.query.all())
@app.route("/posts", methods=["GET", "POST"])
def get_posts():
if request.method == "POST":
title = request.form["title"]
body = request.form["body"]
category_id = request.form["category_id"]
tag_id = request.form["tag_id"]
post = Post(title=title, body=body, category_id=category_id)
post.tags.append(Tag.query.get(tag_id))
db.session.add(post)
db.session.commit()
return render_template(
"posts.html",
posts=Post.query.all(),
tags=Tag.query.all(),
categories=Category.query.all(),
)
@app.route("/categories", methods=["GET", "POST"])
def get_categories():
if request.method == "POST":
name = request.form["name"]
category = Category(name=name)
db.session.add(category)
db.session.commit()
return render_template("categories.html", categories=Category.query.all())
if __name__ == "__main__":
db.create_all()
app.run(debug=True)