-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
200 lines (149 loc) · 5.22 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from flask import Flask, request, url_for
from flask import render_template
from flask import redirect
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///ctm.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
class Projects(db.Model):
"""Projects schema"""
project_id = db.Column(db.Integer, primary_key=True)
project_name = db.Column(db.String(20))
active = db.Column(db.Boolean)
def __init__(self, project, active):
self.project_name = project
self.active = active
def __repr__(self):
return '<Project {}>'.format(self.project_name)
class Tasks(db.Model):
"""Tasks schema"""
task_id = db.Column(db.Integer, primary_key=True)
project_id = db.Column(db.Integer, db.ForeignKey('projects.project_id'))
task = db.Column(db.Text)
status = db.Column(db.Boolean, default=False)
def __init__(self, project_id, task, status=True):
self.project_id = project_id
self.task = task
self.status = status
def __repr__(self):
return '<Task {}>'.format(self.task)
# initialize the database
db.create_all()
@app.route('/')
def index():
"""Home page of the app
It loads the template page and passes on any current tasks and projects that exist.
Also passes along the currently active tab. If the active tab was removed, selects
the first project in the Projects database and sets that one as the active one.
"""
active = None
projects = Projects.query.all()
tasks = Tasks.query.all()
if len(projects) == 1:
projects[0].active = True
active = projects[0].project_id
db.session.commit()
if projects:
for project in projects:
if project.active:
active = project.project_id
if not active:
projects[0].active = True
active = projects[0].project_id
else:
projects = None
if projects:
return render_template('index.html', tasks=tasks, projects=projects, active=active)
else:
return render_template('index.html', tasks=tasks, active=active)
@app.route('/add', methods=['POST'])
def add_task():
"""Adds a new task
Redirects to home page if no task was entered. Sets project to default of Tasks if
none was entered. If the entered project does not exists, it is added to the database
and sets the active tab.
"""
found = False
project_id = None
task = request.form['task']
project = request.form['project']
if not task:
return redirect('/')
if not project:
project = 'Tasks'
projects = Projects.query.all()
for proj in projects:
if proj.project_name == project:
found = True
# add the project if not in database already
if not found:
add_project = Projects(project, True)
db.session.add(add_project)
db.session.commit()
projects = Projects.query.all()
# set the active tab
for proj in projects:
if proj.project_name == project:
project_id = proj.project_id
proj.active = True
else:
proj.active = False
status = bool(int(request.form['status']))
# add the new task
new_task = Tasks(project_id, task, status)
db.session.add(new_task)
db.session.commit()
return redirect('/')
@app.route('/close/<int:task_id>')
def close_task(task_id):
"""Changes the state of a task
If the task is open, it closes it. If it's close, it opens it.
Redirects to home page if the task does not exists.
"""
task = Tasks.query.get(task_id)
if not task:
return redirect('/')
if task.status:
task.status = False
else:
task.status = True
db.session.commit()
return redirect('/')
@app.route('/delete/<int:task_id>')
def delete_task(task_id):
"""Deletes task by its ID
If the task does not exist, redirects to home page.
"""
task = Tasks.query.get(task_id)
if not task:
return redirect('/')
db.session.delete(task)
db.session.commit()
return redirect('/')
@app.route('/clear/<delete_id>')
def clear_all(delete_id):
"""Dumps all tasks from the active tab and removes the project tab"""
Tasks.query.filter(Tasks.project_id == delete_id).delete()
Projects.query.filter(Projects.project_id == delete_id).delete()
db.session.commit()
return redirect('/')
@app.route('/remove/<lists_id>')
def remove_all(lists_id):
"""Dumps all tasks from the active tab"""
Tasks.query.filter(Tasks.project_id == lists_id).delete()
db.session.commit()
return redirect('/')
@app.route('/project/<tab>')
def tab_nav(tab):
"""Switches between active tabs"""
projects = Projects.query.all()
for project in projects:
if project.project_name == tab:
project.active = True
else:
project.active = False
db.session.commit()
return redirect('/')
if __name__ == '__main__':
app.run(debug=True)