|
1 | 1 | from flask import Flask, render_template,request,url_for,redirect
|
2 |
| - |
3 |
| - |
| 2 | +from json import dumps |
| 3 | +from models import * |
| 4 | +from flask_moment import Moment |
| 5 | +import datetime |
4 | 6 | app=Flask(__name__)
|
5 |
| -users=[] |
| 7 | +moment=Moment(app) |
6 | 8 | @app.route("/",methods=["GET"])
|
7 |
| -def home(): |
| 9 | +def home(): |
| 10 | + posts=Post.select().order_by(Post.date_posted.desc()) |
| 11 | + users=User.select() |
| 12 | + |
8 | 13 | success=request.args.get('success')
|
9 |
| - return render_template("index.html",success=success,users=users,count=len(users)) |
10 |
| -@app.route("/<username>", methods=['POST','DELETE',"GET"]) |
11 |
| -def remove(username=None): |
12 |
| - users.remove(username) |
13 |
| - afterdelete="Record <strong><em>{}</em></strong> successfully deleted".format(username) |
| 14 | + return render_template("index.html",success=success,posts=posts,count=len(posts),users=len(users) ,datenow=datetime.datetime.now()) |
| 15 | +@app.route("/delete/<int:postid>", methods=['POST','DELETE',"GET"]) |
| 16 | +def remove(postid): |
| 17 | + post=Post.get(Post.id==postid) |
| 18 | + post.delete_instance() |
| 19 | + afterdelete="Post successfully deleted" |
14 | 20 | redirect(url_for("home"))
|
15 |
| - return render_template("index.html",users=users,after=afterdelete,count=len(users)) |
| 21 | + return render_template("index.html",posts=Post.select().order_by(Post.date_posted.desc()),after=afterdelete,count=len(Post.select().order_by(Post.date_posted.desc()))) |
16 | 22 | @app.route("/<username>/edit",methods=['POST','GET'])
|
17 |
| -def viewuser(username): |
| 23 | +def edituser(username): |
18 | 24 | error=""
|
19 | 25 | success="The record <strong><em>{}</strong></em> was successfully update".format(username)
|
| 26 | + if username not in users: |
| 27 | + return render_template("404.html") |
20 | 28 | if request.method=="POST":
|
21 | 29 | newusername=request.form['username'].strip()
|
22 | 30 | if(newusername==""):
|
23 | 31 | error="Please give us a username"
|
24 | 32 | else:
|
| 33 | + if username not in users: |
| 34 | + return render_template("404.html") |
25 | 35 | users[users.index(username)]=newusername
|
26 | 36 | return redirect(url_for('home',success=success))
|
27 | 37 | return render_template("view.html",user=username,error=error)
|
28 | 38 | @app.route("/user/new",methods=["POST","GET"])
|
29 | 39 | def newuser():
|
30 | 40 | error=None
|
31 | 41 | if request.method=="POST":
|
32 |
| - if(request.form['username'].strip()==""): |
33 |
| - error="Please give us a username" |
| 42 | + if(request.form['userid'].strip()=="" or request.form['title'].strip()=="" or request.form['content'].strip()==""): |
| 43 | + error="Please fill in all the fields " |
34 | 44 | else:
|
35 |
| - users.append(request.form['username']) |
| 45 | + Post.create(user_id=request.form['userid'],title=request.form['title'],content=request.form['content']) |
36 | 46 | return redirect(url_for('home',success="The record was successfully saved"))
|
37 | 47 | return render_template('new_user.html',error=error)
|
| 48 | +@app.route("/post/<int:postid>",methods=["GET"]) |
| 49 | +def view_post(postid): |
| 50 | + post=Post.get(Post.id==postid) |
| 51 | + user=User.get(User.id==post.user_id) |
| 52 | + print(post) |
| 53 | + return render_template("view_post.html",post=post,datenow=datetime.datetime.now(),user=user) |
| 54 | +@app.errorhandler (404) |
| 55 | +def not_found(error): |
| 56 | + return render_template('404.html'), 404 |
38 | 57 | if __name__ =="__main__":
|
39 | 58 | app.run(debug=True)
|
40 | 59 |
|
0 commit comments