-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
111 lines (91 loc) · 3.52 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
import json
from flask import Flask, render_template, request, make_response, redirect, jsonify
from flask_cors import CORS
from ToDoList.main import retrieve_tasks, add_tasks, remove_tasks, complete_tasks
from ToDoList.UserCheck import check_user
from ToDoList.UserRegistration import add_user
from ToDoList.UserLogin import check_credentials
app = Flask(__name__)
CORS(app, resources={r"/todo/*": {"origins": "*"}})
@app.route('/')
def root():
if 'logged_in' in request.cookies and request.cookies.get('logged_in') == "yes":
return redirect("/todo", code=302)
else:
resp = make_response(render_template('index.html'))
resp.set_cookie('logged_in', 'no', max_age=28480)
return resp
@app.route('/login', methods=['POST'])
def login():
username = request.form.get('username')
password = request.form.get('password')
if check_credentials(username, password):
print(f"Received login request. Username: {username}")
resp = redirect("/todo", code=302)
resp.set_cookie('username', username, max_age=28480)
resp.set_cookie('logged_in', 'yes', max_age=28480)
return resp
else:
print(f"Received incorrect login request. Username: {username}")
error_message = "Invalid username or password"
return render_template('index.html', error_message=error_message)
@app.route('/register', methods=['POST'])
def signup():
username = request.form.get('username')
password = request.form.get('password')
if not check_user(username):
add_user(username, password)
resp = redirect("/todo", code=302)
resp.set_cookie('username', username, max_age=28480)
resp.set_cookie('logged_in', 'yes', max_age=28480)
return resp
else:
error_message = "Username already exists"
return render_template('index.html', error_message=error_message)
@app.route('/todo', methods=['POST', 'GET'])
def todo():
username = request.cookies.get('username')
data = {
"username": username
}
tasks = retrieve_tasks(username)
print(tasks)
return render_template("todo.html", data=data, task=tasks)
@app.route('/todo/add/<task>', methods=['POST'])
def add_task(task):
try:
username = request.cookies.get('username')
data = request.get_json()
add_tasks(username, data['task'])
return jsonify({'status': 'success'})
except Exception as e:
print(f"Error processing POST request: {str(e)}")
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/todo/remove/<task>', methods=['POST'])
def remove_task(task):
try:
username = request.cookies.get('username')
data = task
remove_tasks(username, data)
return jsonify({'status': 'success'})
except Exception as e:
print(f"Error processing POST request: {str(e)}")
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/todo/complete/<task>', methods=['POST'])
def complete_task(task):
try:
username = request.cookies.get('username')
data = task
complete_tasks(username, data)
return jsonify({'status': 'success'})
except Exception as e:
print(f"Error processing POST request: {str(e)}")
return jsonify({'status': 'error', 'message': str(e)}), 500
@app.route('/logout')
def logout():
resp = make_response(redirect('/', code=302))
resp.delete_cookie('username')
resp.delete_cookie('logged_in')
return resp
if __name__ == '__main__':
app.run(host='0.0.0.0')