-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
196 lines (164 loc) · 6.46 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
from flask import Flask, render_template, request, redirect, url_for, flash, session
import sqlite3
import markdown2
from werkzeug.security import generate_password_hash, check_password_hash
app = Flask(__name__)
app.secret_key = 'asfkjhdasfg4ljk4ad1hflkjash2lkaj'
DATABASE = 'database.db'
@app.route('/edit_user/<int:id>', methods=['GET', 'POST'])
def edit_user(id):
if not session.get('is_admin'):
flash('Only admin users can access this page.')
return redirect(url_for('index'))
db = get_db()
user = db.execute('SELECT * FROM users WHERE id = ?', (id,)).fetchone()
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
is_admin = 1 if 'is_admin' in request.form else 0
db.execute('UPDATE users SET username = ?, email = ?, is_admin = ? WHERE id = ?', (username, email, is_admin, id))
db.commit()
flash('User updated successfully.')
return redirect(url_for('admin'))
return render_template('edit_user.html', user=user)
@app.route('/delete_user/<int:id>', methods=['POST'])
def delete_user(id):
if not session.get('is_admin'):
flash('Only admin users can access this page.')
return redirect(url_for('index'))
db = get_db()
db.execute('DELETE FROM users WHERE id = ?', (id,))
db.commit()
flash('User deleted successfully.')
return redirect(url_for('admin'))
@app.route('/delete/<int:id>', methods=['POST'])
def delete(id):
if not session.get('user_id'):
flash('Please log in to delete a post.')
return redirect(url_for('login'))
db = get_db()
post = db.execute('SELECT * FROM posts WHERE id = ?', (id,)).fetchone()
if post['user_id'] != session['user_id'] and not session.get('is_admin'):
flash('You can only delete your own posts.')
return redirect(url_for('index'))
db.execute('DELETE FROM posts WHERE id = ?', (id,))
db.commit()
flash('Post deleted successfully.')
return redirect(url_for('index'))
def get_db():
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row
return conn
def get_post(post_id):
db = get_db()
post = db.execute('SELECT * FROM posts WHERE id = ?', (post_id,)).fetchone()
return post
@app.route('/')
def index():
db = get_db()
cur = db.execute('''
SELECT posts.*, users.username
FROM posts
JOIN users ON posts.user_id = users.id
ORDER BY created DESC
''')
posts = cur.fetchall()
return render_template('index.html', posts=posts)
def render_markdown(content):
html_content = markdown2.markdown(content, extras=["fenced-code-blocks", "code-friendly", "tables"])
return html_content
@app.route('/post/<int:post_id>')
def post(post_id):
db = get_db()
cur = db.execute('''
SELECT posts.*, users.username
FROM posts
JOIN users ON posts.user_id = users.id
WHERE posts.id = ?
''', [post_id])
post = cur.fetchone()
if post:
post_content = render_markdown(post['content'])
return render_template('post.html', post=post, post_content=post_content)
else:
flash('Post not found')
return redirect(url_for('index'))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
db = get_db()
user = db.execute('SELECT * FROM users WHERE email = ?', (email,)).fetchone()
if user and check_password_hash(user['password'], password):
session['user_id'] = user['id']
session['is_admin'] = user['is_admin']
session['username'] = user['username']
if user['is_admin']:
return redirect(url_for('admin'))
return redirect(url_for('index'))
else:
flash('Invalid email or password')
return render_template('login.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
hashed_password = generate_password_hash(password)
db = get_db()
try:
db.execute('INSERT INTO users (username, email, password, is_admin) VALUES (?, ?, ?, 0)', (username, email, hashed_password))
db.commit()
return redirect(url_for('login'))
except sqlite3.IntegrityError:
flash('Username or email already exists')
return render_template('register.html')
@app.route('/logout')
def logout():
session.pop('user_id', None)
session.pop('is_admin', None)
session.pop('username', None)
return redirect(url_for('index'))
@app.route('/create', methods=['GET', 'POST'])
def create():
if not session.get('user_id'):
flash('Please log in to create a post.')
return redirect(url_for('login'))
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
user_id = session['user_id']
db = get_db()
db.execute('INSERT INTO posts (title, content, user_id) VALUES (?, ?, ?)', (title, content, user_id))
db.commit()
return redirect(url_for('index'))
return render_template('create.html')
@app.route('/edit/<int:id>', methods=['GET', 'POST'])
def edit(id):
if not session.get('user_id'):
flash('Please log in to edit a post.')
return redirect(url_for('login'))
db = get_db()
post = db.execute('SELECT * FROM posts WHERE id = ?', (id,)).fetchone()
if post['user_id'] != session['user_id'] and not session.get('is_admin'):
flash('You can only edit your own posts.')
return redirect(url_for('index'))
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
db.execute('UPDATE posts SET title = ?, content = ? WHERE id = ?', (title, content, id))
db.commit()
return redirect(url_for('index'))
return render_template('edit.html', post=post)
@app.route('/admin')
def admin():
if not session.get('is_admin'):
flash('Only admin users can access this page.')
return redirect(url_for('index'))
db = get_db()
users = db.execute('SELECT * FROM users').fetchall()
return render_template('admin.html', users=users)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)