-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
208 lines (158 loc) · 5.43 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from flask import Flask , render_template , flash ,request , redirect , url_for , g ,session
import sqlite3
app = Flask(__name__)
app.config['SECRET_KEY'] = 'dev'
app.config['DATABASE_PATH'] = 'myfpb.db'
# db setup
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(
app.config['DATABASE_PATH'],
detect_types = sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
db = g.pop('db',None)
if 'db' is not None:
db.close()
def init_db():
db = get_db()
with app.open_resource('schema.sql') as f:
db.executescript(f.read().decode('utf8'))
# routing setup
@app.route('/')
def home():
if session.get('logged-in'):
session.clear()
return redirect(url_for('home'))
else:
session.clear()
db = get_db()
posts = db.execute(
'SELECT myfpbl.username, myfpbp.title, myfpbp.post'
' FROM myfpbp JOIN myfpbl ON myfpbl.id=myfpbp.author_id'
' ORDER BY myfpbl.username ASC'
).fetchall()
return render_template('home.html',title='Home',posts=posts)
@app.route('/login', methods=('GET','POST'))
def login():
error = None
if request.method == 'POST':
db = get_db()
username = request.form['username']
password = request.form['password']
if not username and not password:
error = "Please enter the username and password"
elif not username or not password:
error = "Enter the valid credentials"
else:
get_user = db.execute(
'SELECT * FROM myfpbl WHERE username = ?',(username,)
).fetchone()
db.commit()
if get_user != None :
session['username'] = username
session['user-id'] = get_user['id']
session['logged-in'] = True
session['sign-in'] = False
return redirect(url_for('users'))
else:
error = "User Not found"
if error != None:
flash(error)
return render_template('login.html',title='Login')
@app.route('/sign-in',methods=('GET','POST'))
def sign():
error = None
if request.method == 'POST':
db = get_db()
username = request.form['username']
password = request.form['password']
if not username and not password:
error = "Enter the valid credentials"
elif not username or not password:
error = "Please enter the relative fields"
else:
post_exits = db.execute(
'SELECT * FROM myfpbl WHERE username = ?',(username,)
).fetchone()
if post_exits:
error = "Username Already exists"
else:
db.execute('INSERT into myfpbl (username,password) VALUES(?,?)',
(username,password)
)
db.commit()
session['sign-in'] = True
return redirect(url_for('login'))
if error != None:
flash(error)
return render_template('signin.html',title="Sign-In")
@app.route('/users-posts')
def users():
if session.get('logged-in'):
db = get_db()
userId = session['user-id']
posts = db.execute(
'SELECT myfpbl.username, myfpbp.id, myfpbp.title, myfpbp.post '
' FROM myfpbp JOIN myfpbl ON myfpbl.id = myfpbp.author_id'
' WHERE myfpbl.id = ?',(userId,)
).fetchall()
db.commit()
return render_template('users.html',title='User',posts=posts,username=session['username'])
else:
return redirect(url_for('home'))
@app.route('/add-post', methods=('GET','POST'))
def post():
if session.get('logged-in'):
if request.method == 'POST':
post_title = request.form['title']
post_msg = request.form['post-content']
username = session['username']
db = get_db()
userId = session['user-id']
db.execute(
'INSERT INTO myfpbp (title, post, author_id) VALUES(?,?,?)',
(post_title,post_msg,userId)
)
db.commit()
return redirect(url_for('users'))
return render_template('post.html',title='Post')
else:
return redirect(url_for('home'))
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('home'))
# delete post
@app.route('/delete/<id>')
def delete(id):
if session.get('logged-in'):
db = get_db()
db.execute(
'DELETE FROM myfpbp WHERE id = ?',(id,)
)
db.commit()
return redirect(url_for('users'))
else:
return redirect(url_for('home'))
#edit post
@app.route('/edit/<id>',methods=('GET','POST'))
def edit(id):
db = get_db()
if session.get('logged-in'):
if request.method == 'POST':
title = request.form['title']
post = request.form['post-content']
db.execute(
'UPDATE myfpbp SET title = ? ,post = ? WHERE id = ? ',(title,post,id,)
)
db.commit()
return redirect(url_for('users'))
posts = db.execute(
'SELECT * FROM myfpbp WHERE id = ?',(id,)
).fetchone()
return render_template('edit.html',title='Edit',posts=posts)
else:
return redirect(url_for('home'))