157 lines
5.2 KiB
Python
157 lines
5.2 KiB
Python
#author jason
|
|
from flask import Flask, render_template, request, redirect, url_for, session, send_file
|
|
import sqlite3
|
|
import os
|
|
from captcha import generate_captcha
|
|
from datetime import timedelta
|
|
|
|
#init flask and database
|
|
app = Flask(__name__)
|
|
app.secret_key = 'your_secret_key'
|
|
DATABASE = 'app.db'
|
|
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=1)
|
|
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
|
|
|
|
def get_db_connection():
|
|
conn = sqlite3.connect(DATABASE)
|
|
conn.row_factory = sqlite3.Row
|
|
return conn
|
|
#index.html load the homepage(index.html) and load the information from database
|
|
@app.route('/')
|
|
def index():
|
|
conn = get_db_connection()
|
|
posts = conn.execute('SELECT * FROM posts ORDER BY created_at DESC').fetchall()
|
|
conn.close()
|
|
return render_template('index.html', posts=posts)
|
|
#signin.html load the page and get post from the form ,confirm users account and key
|
|
@app.route('/signin', methods=['GET', 'POST'])
|
|
def signin():
|
|
if request.method == 'POST':
|
|
account = request.form['account']
|
|
password = request.form['password']
|
|
captcha = request.form['captcha']
|
|
|
|
if captcha != session.get('captcha', ''):
|
|
return 'Invalid captcha'
|
|
|
|
conn = get_db_connection()
|
|
user = conn.execute('SELECT * FROM users WHERE username = ? AND password = ?', (account, password)).fetchone()
|
|
conn.close()
|
|
|
|
if user:
|
|
session['user'] = account
|
|
return redirect(url_for('dashboard'))
|
|
else:
|
|
return 'Invalid credentials'
|
|
|
|
captcha_image, captcha_text = generate_captcha()
|
|
session['captcha'] = captcha_text
|
|
return render_template('signin.html', captcha_image=captcha_image)
|
|
|
|
#signup.html sign up and add accout and keys information into database
|
|
@app.route('/signup', methods=['GET', 'POST'])
|
|
def signup():
|
|
if request.method == 'POST':
|
|
username = request.form['username']
|
|
password = request.form['password']
|
|
confirm_password = request.form['confirm-password']
|
|
age = request.form['age']
|
|
captcha = request.form['captcha']
|
|
|
|
if password != confirm_password:
|
|
return 'Passwords do not match'
|
|
|
|
if captcha != session.get('captcha', ''): #this part will be change ,give user a window ,rather than a new page
|
|
return 'Invalid captcha'
|
|
|
|
conn = get_db_connection()
|
|
existing_user = conn.execute('SELECT * FROM users WHERE username = ?', (username,)).fetchone()
|
|
if existing_user:
|
|
conn.close()
|
|
return 'Username already exists'
|
|
|
|
conn.execute('INSERT INTO users (username, password, age) VALUES (?, ?, ?)', (username, password, age))
|
|
conn.commit()
|
|
conn.close()
|
|
|
|
return redirect(url_for('signin'))
|
|
|
|
captcha_image, captcha_text = generate_captcha()
|
|
session['captcha'] = captcha_text
|
|
return render_template('signup.html', captcha_image=captcha_image)
|
|
|
|
|
|
@app.route('/captcha')
|
|
def captcha():
|
|
captcha_image, captcha_text = generate_captcha()
|
|
session['captcha'] = captcha_text
|
|
return send_file(captcha_image, mimetype='image/png')
|
|
|
|
|
|
@app.route('/dashboard')
|
|
def dashboard():
|
|
if 'user' not in session:
|
|
return redirect(url_for('signin'))
|
|
return render_template('dashboard.html')
|
|
|
|
@app.route('/publish', methods=['POST'])
|
|
def publish():
|
|
if 'user' not in session:
|
|
return redirect(url_for('signin'))
|
|
content = request.form['content']
|
|
username = session['user']
|
|
conn = get_db_connection()
|
|
conn.execute('INSERT INTO posts (username, content) VALUES (?, ?)', (username, content))
|
|
conn.commit()
|
|
conn.close()
|
|
return redirect(url_for('index'))
|
|
|
|
@app.route('/delete_post/<int:post_id>', methods=['POST'])
|
|
def delete_post(post_id):
|
|
if 'user' not in session:
|
|
return redirect(url_for('signin'))
|
|
username = session['user']
|
|
conn = get_db_connection()
|
|
post = conn.execute('SELECT * FROM posts WHERE id = ?', (post_id,)).fetchone()
|
|
|
|
if post and post['username'] == username:
|
|
conn.execute('DELETE FROM posts WHERE id = ?', (post_id,))
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
return redirect(url_for('user'))
|
|
|
|
@app.route('/user')
|
|
def user():
|
|
if 'user' not in session:
|
|
return redirect(url_for('signin'))
|
|
username = session['user']
|
|
conn = get_db_connection()
|
|
user_info = conn.execute('SELECT * FROM users WHERE username = ?', (username,)).fetchone()
|
|
user_posts = conn.execute('SELECT * FROM posts WHERE username = ? ORDER BY created_at DESC', (username,)).fetchall()
|
|
conn.close()
|
|
return render_template('user.html', user_info=user_info, user_posts=user_posts)
|
|
|
|
@app.route('/logout')
|
|
def logout():
|
|
session.pop('user', None)
|
|
return redirect(url_for('signin'))
|
|
|
|
@app.route('/aboutus')
|
|
def aboutus():
|
|
return render_template('aboutus.html')
|
|
@app.route('/commands')
|
|
def commands():
|
|
return render_template('commands.html')
|
|
@app.route('/python')
|
|
def python():
|
|
return render_template('python.html')
|
|
@app.route('/python2')
|
|
def python2():
|
|
return render_template('python2.html')
|
|
@app.route('/video')
|
|
def video():
|
|
return render_template('video.html')
|
|
if __name__ == '__main__':
|
|
app.run(debug=True)
|