19
19
def log_request ():
20
20
log_file .write (f"{ request .method } { request .path } { dict (request .form ) if request .form else '' } \n " )
21
21
22
-
23
22
# Set user_id on request if user is logged in, or else set it to None.
24
23
@app .before_request
25
24
def check_authentication ():
@@ -48,15 +47,28 @@ def get_comments_page(quote_id):
48
47
@app .route ("/quotes" , methods = ["POST" ])
49
48
def post_quote ():
50
49
with db :
51
- db .execute (f"""insert into quotes(text,attribution) values("{ request .form ['text' ]} ","{ request .form ['attribution' ]} ")""" )
50
+ db .execute ("""
51
+ INSERT INTO quotes (text, attribution)
52
+ VALUES (:text, :attribution)
53
+ """ , {"text" : request .form ['text' ], "attribution" : request .form ['attribution' ]}
54
+ )
55
+
52
56
return redirect ("/#bottom" )
53
57
54
58
55
59
# Post a new comment
56
60
@app .route ("/quotes/<int:quote_id>/comments" , methods = ["POST" ])
57
61
def post_comment (quote_id ):
58
62
with db :
59
- db .execute (f"""insert into comments(text,quote_id,user_id) values("{ request .form ['text' ]} ",{ quote_id } ,{ request .user_id } )""" )
63
+ db .execute ("""
64
+ INSERT INTO comments (text, quote_id, user_id)
65
+ VALUES (:text, :quote_id, :user_id)
66
+ """ , {
67
+ "text" : request .form ['text' ],
68
+ "quote_id" : quote_id ,
69
+ "user_id" : request .user_id
70
+ }
71
+ )
60
72
return redirect (f"/quotes/{ quote_id } #bottom" )
61
73
62
74
@@ -65,16 +77,30 @@ def post_comment(quote_id):
65
77
def signin ():
66
78
username = request .form ["username" ].lower ()
67
79
password = request .form ["password" ]
80
+
81
+ user = db .execute ("""
82
+ SELECT id, password
83
+ FROM users
84
+ WHERE name = :username
85
+ """ , {"username" : username }
86
+ ).fetchone ()
68
87
69
- user = db .execute (f"select id, password from users where name='{ username } '" ).fetchone ()
70
88
if user : # user exists
71
89
if password != user ['password' ]:
72
90
# wrong! redirect to main page with an error message
73
91
return redirect ('/?error=' + urllib .parse .quote ("Invalid password!" ))
74
92
user_id = user ['id' ]
75
93
else : # new sign up
76
94
with db :
77
- cursor = db .execute (f"insert into users(name,password) values('{ username } ', '{ password } ')" )
95
+ cursor = db .execute ("""
96
+ INSERT INTO users (name, password)
97
+ VALUES (:username, :password)
98
+ """ , {
99
+ "username" : username ,
100
+ "password" : password
101
+ }
102
+ )
103
+
78
104
user_id = cursor .lastrowid
79
105
80
106
response = make_response (redirect ('/' ))
0 commit comments