Skip to content

Commit 769b6e1

Browse files
committed
ORm in posttags
1 parent f663581 commit 769b6e1

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

PostModel.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,20 @@ def deletePost(self, id):
7878

7979
from models.shared import db
8080
from models.Post import Post
81+
from models.Comment import Comment
8182

8283

83-
sql = 'DELETE FROM PostTag WHERE Post='+id
84-
db.engine.execute(sql)
85-
84+
post = db.session.query(Post).get(id)
85+
comments = Comment.query.filter_by(Post=id).all()
86+
for c in comments:
87+
db.session.delete(c)
88+
89+
post.tags = []
90+
91+
db.session.delete(post)
92+
db.session.commit()
8693

8794

88-
singlepost = Post.query.filter_by(Id=id).first()
89-
if singlepost:
90-
db.session.delete(singlepost)
91-
db.session.flush()
9295

9396

9497

@@ -106,18 +109,25 @@ def addTags(self, tags, post):
106109

107110

108111
from models.shared import db
112+
from models.Post import Post
113+
from models.Tag import Tag
109114

110-
sql = 'DELETE FROM PostTag WHERE Post='+post
111-
db.engine.execute(sql)
112-
db.session.commit()
115+
postTags = db.session.query(Post).get(post)
113116

114-
for tag in tags:
117+
if postTags.tags:
118+
postTags.tags = []
119+
db.session.commit()
115120

116121

117-
sql = 'INSERT INTO PostTag VALUES (%s, %s)'
118-
db.engine.execute(sql, (tag, post, ))
122+
for tag in tags:
123+
124+
t = db.session.query(Tag).get(tag)
125+
postTags.tags.append(t)
126+
db.session.add(t)
119127
db.session.commit()
120128

129+
130+
121131

122132

123133

0 commit comments

Comments
 (0)