Skip to content

Commit faac546

Browse files
committed
Recompress to continue
1 parent 4714f38 commit faac546

File tree

6 files changed

+63
-15
lines changed

6 files changed

+63
-15
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1+
static/
12
templates/
2-
static/

app.py

+33-14
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,59 @@
11
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
46
app=Flask(__name__)
5-
users=[]
7+
moment=Moment(app)
68
@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+
813
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"
1420
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())))
1622
@app.route("/<username>/edit",methods=['POST','GET'])
17-
def viewuser(username):
23+
def edituser(username):
1824
error=""
1925
success="The record <strong><em>{}</strong></em> was successfully update".format(username)
26+
if username not in users:
27+
return render_template("404.html")
2028
if request.method=="POST":
2129
newusername=request.form['username'].strip()
2230
if(newusername==""):
2331
error="Please give us a username"
2432
else:
33+
if username not in users:
34+
return render_template("404.html")
2535
users[users.index(username)]=newusername
2636
return redirect(url_for('home',success=success))
2737
return render_template("view.html",user=username,error=error)
2838
@app.route("/user/new",methods=["POST","GET"])
2939
def newuser():
3040
error=None
3141
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 "
3444
else:
35-
users.append(request.form['username'])
45+
Post.create(user_id=request.form['userid'],title=request.form['title'],content=request.form['content'])
3646
return redirect(url_for('home',success="The record was successfully saved"))
3747
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
3857
if __name__ =="__main__":
3958
app.run(debug=True)
4059

db/coding.db

7 KB
Binary file not shown.

models.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from peewee import *
2+
from datetime import datetime
3+
from crypt import crypt
4+
db=SqliteDatabase("./db/coding.db")
5+
class User(Model):
6+
id=PrimaryKeyField()
7+
username=CharField()
8+
emailaddress=CharField(unique=True)
9+
password=CharField()
10+
created_on= DateTimeField(default=datetime.now)
11+
class Meta:
12+
database=db
13+
class Post(Model):
14+
id=PrimaryKeyField()
15+
user=ForeignKeyField(User)
16+
title=CharField()
17+
content=TextField()
18+
date_posted=DateTimeField(default=datetime.now)
19+
class Meta:
20+
database=db
21+
def initialize_db():
22+
db.connect()
23+
db.create_tables([User,Post],safe=True)
24+
def create_data():
25+
user=User.create(username="Silas Kenneth",emailaddress="silaskenn@gmail.com",password=crypt("Nyamwaro2012"))
26+
user1=User.create(username="James Makori",emailaddress="jamesmakori@gmail.com",password=crypt("Nyamwaro2012"))
27+
user2=User.create(username="John Kennedy",emailaddress="johnkennedy@gmail.com",password=crypt("Nyamwaro2012"))
28+
initialize_db()
29+

static.zip

-23.1 KB
Binary file not shown.

templates.zip

1.62 KB
Binary file not shown.

0 commit comments

Comments
 (0)