/
tipsy.py
124 lines (106 loc) · 3.93 KB
/
tipsy.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
"""
tipsy.py -- A flask-based todo list
"""
#Added session, url_for, escape for username login and g for global variables
from flask import Flask, render_template, request, redirect, url_for, escape, session, g
# importa model file
import model
#initializes program to be a Flask application
app = Flask(__name__)
# Flask function before_request that will be called before every view is executed.
@app.before_request
def set_up_db():
# use g to help manage multiple users accessing the database
g.db = model.connect_db()
#global teardown request that cleans up after each view
#passes e to cover errors and make sure they are ignored and db closed
@app.teardown_request
def disconnect_db(e):
g.db.close()
#Add ability to login as a particular user
#create login form (in login.html)
#def function called login
@app.route("/authenticate", methods=["POST"])
def authenticate():
email = request.form['email']
password = request.form['password']
# capture the userid information from model-database
user_id = model.authenticate(g.db, email, password)
session['user_id'] = user_id
# after getting the session variable back, you have to point it to a page
return redirect("/")
@app.route("/")
def index():
user_id = session.get("user_id", None)
#if user_id:
# db_user = model.get_user(g.db, user_id)
return render_template("index.html", user_name=user_id['first_name'])
return 'You are not logged in'
# previous attempt at setting up login
# if 'username' in session:
# return render_template("index.html", user_name=escape(session['username']))
# return 'You are not logged in'
# take in user input to add tasks
@app.route("/new_task")
def new_tasks():
#db = model.connect_db()
# assign to variable the function from model file with the noted attributes
#insert_tasks_db = model.new_task(db, title, userid)
return render_template("new_task.html")
# runs function to save to the db
@app.route("/save_task", methods=["POST"])
def save_task():
task_title = request.form['add_title']
# db = model.connect_db() - took out when added global db call
# Assume that all tasks are attached to user 1.
# Need to automate
task_id = model.new_task(g.db, task_title, 1)
return redirect("/tasks")
# view which is also a function that lists multiple tasks
@app.route("/tasks")
def list_tasks():
# db = model.connect_db() - took out when added global db call
#following carries the session id through and requires it to lists the tasks
user_id = session.get("user_id", None)
tasks_from_db = model.get_tasks(g.db, user_id)
return render_template("list_tasks.html", tasks=tasks_from_db)
# view a single task
@app.route("/task/<int:id>", methods=["GET"])
def view_task(id):
# db = model.connect_db() - took out when added global db call
task_from_db = model.get_task(g.db, id)
return render_template("view_task.html", task=task_from_db)
#complete task
@app.route("/task/<int:id>", methods=["POST"])
def complete_task(id):
# db=model.connect_db() - took out when added global db call
model.complete_task(g.db, id)
return redirect("/tasks")
@app.route("/login")
def login():
return render_template("login.html")
#Block below shows our initial attempt at login
#@app.route('/login', methods=['GET', 'POST'])
#def login():
# db = model.connect_db()
# if request.method == 'POST':
# session['username'] = request.form['username']
# session['password'] = request.form['password']
# if model.authenticate(g.db, session['username'], session['password']):
# return redirect(url_for('index'))
# return '''
# <form action="" method="post">
# <p><input type=text name=username>
# <p><input type= text name=password>
# <p><input type=submit value=Login>
# </form>
# '''
# @app.route('/logout')
# def logout():
# #remove the username from the session if it's there
# session.pop('user_id', None)
# return redirect(url_for('index'))
# #set the secret key. keep this really secret:
app.secret_key = 'A0Zr98j/3yX R~XHH! jmN]LWX/ , ?RT'
if __name__ == "__main__":
app.run(debug=True)