-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
152 lines (123 loc) · 4.45 KB
/
main.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
from flask import render_template, redirect, g, Flask, request, url_for, session
from sqlite3 import dbapi2 as sqlite3
from os import path
app = Flask(__name__)
app.config.from_pyfile('config.py')
def get_db():
db = getattr(g, '_database', None)
if db is None:
db_file = app.config["DATABASE"]
exists = path.isfile(db_file)
db = g._database = sqlite3.connect(app.config['DATABASE'])
if not exists:
qry = open('schema.sql', 'r').read()
c = db.cursor()
c.executescript(qry)
db.commit()
c.close()
# Make sure database has tables
return db
def cursor_to_dict_array(cur):
columns = [column[0] for column in cur.description]
return [dict(zip(columns, row)) for row in cur.fetchall()]
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
@app.route('/', methods=["GET"])
def base():
if not session.get('logged_in'):
return redirect("/login")
return redirect("/home")
@app.route('/login', methods=["GET", "POST"])
def login():
error = None
if request.method == "POST":
if request.form['username'] != app.config['USERNAME']:
error = "Invalid Username"
elif request.form['password'] != app.config['PASSWORD']:
error = "Invalid Username"
else:
session['logged_in'] = True
return redirect(url_for('home'))
return render_template('login.html', error=error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
return redirect("/login")
@app.route('/home')
def home():
return redirect("/home/active")
@app.route('/home/active')
def home_active():
if not session.get('logged_in'):
return redirect("/login")
# load completed projects
db = get_db()
cur = db.execute('select * from projects where is_complete=0')
entries = cursor_to_dict_array(cur)
# render home page with completed projects
return render_template('home.html', type="active", entries=entries)
@app.route('/home/completed')
def home_completed():
if not session.get('logged_in'):
return redirect("/login")
# load completed projects
db = get_db()
cur = db.execute('select * from projects where is_complete=1')
entries = cursor_to_dict_array(cur)
# render home page with completed projects
return render_template('home.html', type="completed", entries=entries)
@app.route("/projects", methods=["POST"])
def add_project():
if not session.get('logged_in'):
return redirect("/login")
db = get_db()
db.execute(
'insert into projects (contact_email,contact_name,contract_type,quote_dollars,is_complete) values (?,?,?, ?, ?)',
(
request.form.get('contact_email', type=str),
request.form.get('contact_name', type=str),
'hourly' if request.form.get(
'contract_type') == "hourly" else "milestone",
request.form.get('quote_dollars', type=int),
0 if not request.form.get('is_complete') else 1
)
)
db.commit()
db.close()
return redirect(url_for('home'))
@app.route("/home/projects/<int:project_id>", methods=["GET"])
def edit_project_page(project_id):
if not session.get('logged_in'):
return redirect("/login")
# Load particular project
db = get_db()
cur = db.execute("select * from projects where id=?", (int(project_id),))
entries = cursor_to_dict_array(cur)
if len(entries) == 0:
return redirect('/home')
# Return page
return render_template('edit_form.html', project=entries[0])
@app.route("/projects/edit/<int:project_id>", methods=["POST"])
def edit_project(project_id):
if not session.get('logged_in'):
return redirect("/login")
# Save the information into the database
db = get_db()
db.execute(
'''UPDATE projects SET contact_email=?, contact_name=?, contract_type=?, quote_dollars=?, is_complete=? where id=?''',
(
request.form.get('contact_email', type=str),
request.form.get('contact_name', type=str),
'hourly' if request.form.get(
'contract_type') == "hourly" else "milestone",
request.form.get('quote_dollars', type=int),
0 if not request.form.get('is_complete') else 1,
project_id
)
)
db.commit()
db.close()
return redirect(url_for('home'))