-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
102 lines (84 loc) · 3.28 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
from flask import Flask, render_template, request, redirect, url_for, session, flash
from werkzeug.security import generate_password_hash, check_password_hash
from flask_sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
app.secret_key = 'supersecretkey'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///todo.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(150), unique=True, nullable=False)
password_hash = db.Column(db.String(150), nullable=False)
class Task(db.Model):
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String(200), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
with app.app_context():
db.create_all()
def login_required(f):
def decorated_function(*args, **kwargs):
if 'user_id' not in session:
return redirect(url_for('login'))
return f(*args, **kwargs)
decorated_function.__name__ = f.__name__
return decorated_function
@app.route('/')
@login_required
def index():
user_id = session['user_id']
user_tasks = Task.query.filter_by(user_id=user_id).all()
return render_template('index.html', tasks=user_tasks)
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
existing_user = User.query.filter_by(username=username).first()
if existing_user:
flash("Username already exists!")
return redirect(url_for('register'))
password_hash = generate_password_hash(password)
new_user = User(username=username, password_hash=password_hash)
db.session.add(new_user)
db.session.commit()
flash("Registration successful, please log in.")
return redirect(url_for('login'))
return render_template('register.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter_by(username=username).first()
if user and check_password_hash(user.password_hash, password):
session['user_id'] = user.id
return redirect(url_for('index'))
flash("Invalid username or password")
return render_template('login.html')
@app.route('/logout')
def logout():
session.pop('user_id', None)
flash("You have been logged out.")
return redirect(url_for('login'))
@app.route('/add', methods=['POST'])
@login_required
def add_task():
task_content = request.form.get('content')
if task_content:
user_id = session['user_id']
new_task = Task(content=task_content, user_id=user_id)
db.session.add(new_task)
db.session.commit()
return redirect(url_for('index'))
@app.route('/delete/<int:task_id>')
@login_required
def delete_task(task_id):
task = Task.query.get_or_404(task_id)
if task.user_id == session['user_id']:
db.session.delete(task)
db.session.commit()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)