3
3
from models import *
4
4
from flask_moment import Moment
5
5
import datetime
6
+ import jsonify
7
+ from requests import Response as res
6
8
app = Flask (__name__ )
7
9
moment = Moment (app )
8
10
@app .route ("/" ,methods = ["GET" ])
@@ -20,7 +22,7 @@ def remove(postid):
20
22
redirect (url_for ("home" ))
21
23
return render_template ("index.html" ,posts = Post .select ().order_by (Post .date_posted .desc ()),datenow = datetime .datetime .now () ,after = afterdelete ,count = len (Post .select ().order_by (Post .date_posted .desc ())))
22
24
@app .route ("/<postid>/edit" ,methods = ['POST' ,'GET' ])
23
- def edituser (postid ):
25
+ def editpost (postid ):
24
26
error = ""
25
27
success = "The record was successfully updated"
26
28
post = Post .get (Post .id == postid )
@@ -37,21 +39,25 @@ def edituser(postid):
37
39
return redirect (url_for ('home' ,success = success ))
38
40
return render_template ("view.html" ,post = post ,error = error )
39
41
@app .route ("/user/new" ,methods = ["POST" ,"GET" ])
40
- def newuser ():
42
+ def new_post ():
41
43
error = None
42
44
if request .method == "POST" :
43
45
if (request .form ['userid' ].strip ()== "" or request .form ['title' ].strip ()== "" or request .form ['content' ].strip ()== "" ):
44
46
error = "Please fill in all the fields "
45
47
else :
46
48
Post .create (user_id = request .form ['userid' ],title = request .form ['title' ],content = request .form ['content' ])
47
49
return redirect (url_for ('home' ,success = "The record was successfully saved" ))
48
- return render_template ('new_user .html' ,error = error )
50
+ return render_template ('new_post .html' ,error = error )
49
51
@app .route ("/post/<int:postid>" ,methods = ["GET" ])
50
52
def view_post (postid ):
51
53
post = Post .get (Post .id == postid )
52
54
user = User .get (User .id == post .user_id )
53
55
print (post )
54
56
return render_template ("view_post.html" ,post = post ,datenow = datetime .datetime .now (),user = user )
57
+ @app .route ("/api/posts" ,methods = ['GET' ])
58
+ def api_posts ():
59
+ posts = Post .select ().order_by (Post .date_posted .desc ())
60
+ return res .json (posts .title )
55
61
@app .errorhandler (404 )
56
62
def not_found (error ):
57
63
return render_template ('404.html' ), 404
0 commit comments