-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_do_app.py
280 lines (235 loc) · 9.23 KB
/
to_do_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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import db_functions
import json
from flask import Flask, render_template, flash, request, Response
from datetime import datetime
import datetime
app = Flask(__name__)
SUCCESS='Success'
FAILURE = 'Failure'
sort_status = 0
#Display webpage with all to-do tasks
@app.route('/app')
def home_page():
data = db_functions.get_all_tasks()
updated_data =[]
for task in data:
updated_data.append(get_tasks_with_days_left(task))
return render_template('to_do.html', data = updated_data)
#Trigger sorting by due date and display table
@app.route('/toggleSorting')
def home_page_sorted():
global sort_status
if sort_status == 0:
data = db_functions.get_all_tasks_sorted(sort_status)
sort_status = 1
else:
data = db_functions.get_all_tasks_sorted(sort_status)
sort_status = 0
updated_data =[]
for task in data:
updated_data.append(get_tasks_with_days_left(task))
return render_template('to_do.html', data = updated_data)
#For API call from CLI
@app.route('/api/v1/all_tasks')
def get_all_items():
# Get items from the db
db_data = db_functions.get_all_tasks()
# Return response
response = Response(json.dumps(db_data), mimetype='application/json')
return response
#Wrapper when API call is made from Webpage
@app.route('/add_task_from_html', methods=['POST'])
def add_task_from_html():
# Get details from the HTML form
req_data = request.form
task = req_data.get("task")
duedate = req_data.get("duedate")
details = req_data.get("details")
result = add_task(task, details, duedate)
if "error" in result:
return result
data = db_functions.get_all_tasks()
updated_data =[]
for task in data:
updated_data.append(get_tasks_with_days_left(task))
return render_template('to_do.html', data = updated_data)
#Function for API call to add a new task to DB using CLI. add_task_from_html() utilize this when call is made from webpage
@app.route('/api/v1/add_task', methods=['POST'])
def add_task(*args):
try:
if len(args) == 0:
req_data = request.get_json()
task = req_data['task']
details = req_data['details']
duedate = req_data['duedate']
else:
task = args[0]
details = args[1]
duedate = args[2]
try:
datetime.datetime.strptime(duedate, '%Y-%m-%d')
except ValueError:
try:
duedate = datetime.datetime.strptime(duedate, '%d/%m/%Y').strftime('%Y-%m-%d')
except:
error = "error: incorrect date format, must be DD/MM/YYYY"
return error
# Add item to the list
db_data = db_functions.add_a_task(task, details, duedate)
# Return error if item not added
if db_data['Result'] == 'Failure':
if "UNIQUE constraint failed" in db_data['Remark']:
db_data['Remark'] = "Task exists in the list"
response = "error: task could not be added, reason: " + db_data['Remark']
return response
# Return response
response = "success: task added to the list"
return response
except Exception as e:
error = {"Error": "There is some error with API call, please check its format"}
if not datetime.datetime.strptime(duedate, '%d/%m/%Y'):
error = {"Error": "Incorrect date format, must be DD/MM/YYYY"}
return error
#Wrapper when API call is made from Webpage
@app.route('/edit_task_from_html', methods=['POST'])
def edit_task_from_html():
# Get details from the HTML form
req_data = request.form
task = req_data.get("task")
duedate = req_data.get("duedate")
details = req_data.get("details")
status = req_data.get("status")
result = edit_task(task, details, duedate, status)
if "error" in result:
return result
data = db_functions.get_all_tasks()
updated_data =[]
for task in data:
updated_data.append(get_tasks_with_days_left(task))
return render_template('to_do.html', data = updated_data)
#Function for API call to edit a task in DB using CLI. edit_task_from_html() utilize this when call is made from webpage
@app.route('/api/v1/edit_task', methods=['PUT'])
def edit_task(*args):
try:
if len(args) == 0:
req_data = request.get_json()
task = req_data['task']
details = req_data['details']
duedate = req_data['duedate']
status = req_data['status']
else:
task = args[0]
details = args[1]
duedate = args[2]
status = args[3]
try:
datetime.datetime.strptime(duedate, '%Y-%m-%d')
except ValueError:
try:
duedate = datetime.datetime.strptime(duedate, '%d/%m/%Y').strftime('%Y-%m-%d')
except:
error = "error: incorrect date format, must be DD/MM/YYYY"
return error
#edit item
db_data = db_functions.update_task(task, details, duedate, status)
# Return error if item not edited
if db_data['Result'] == 'Failure':
response = "error: task could not be edited, reason: " + db_data['Remark']
return response
# Return response
response = "success: task successfully edited"
return response
except Exception as e:
error = "Error There is some error with API call, please check its format"
return error
#Wrapper when API call is made from Webpage
@app.route('/complete_task_from_html', methods=['POST'])
def complete_task_from_html():
# Get task in JSON from frontend AJAX call
task = request.json['task']
result = complete_task(task)
if "error" in result:
return result
data = db_functions.get_all_tasks()
updated_data =[]
for task in data:
updated_data.append(get_tasks_with_days_left(task))
return render_template('to_do.html', data = updated_data)
#Function for API call to complete a task in DB using CLI. complete_task_from_html() utilize this when call is made from webpage
@app.route('/api/v1/complete_task', methods=['PUT'])
def complete_task(*args):
try:
status = "Completed"
if len(args) == 0:
req_data = request.get_json()
task = req_data['task']
else:
task = args[0]
#Mark task as completed
db_data = db_functions.update_task_status(task, status)
# Return error if status is not completed
if db_data['Result'] == 'Failure':
response = "error: task could marked completed, reason: " + db_data['Remark']
return response
# Return response
response = "success: task completed successfully"
return response
except Exception as e:
error = {"Error": "There is some error with API call, please check its format"}
response = Response(json.dumps(error), mimetype='application/json')
return response
#Wrapper when API call is made from Webpage
@app.route('/delete_task_from_html', methods=['POST'])
def delete_task_from_html():
# Get details from the HTML form
req_data = request.form
task = req_data.get("task")
result = delete_task(task)
if "error" in result:
return result
data = db_functions.get_all_tasks()
updated_data =[]
for task in data:
updated_data.append(get_tasks_with_days_left(task))
return render_template('to_do.html', data = updated_data)
#Function for API call to delete a task from DB using CLI. delete_task_from_html() utilize this when call is made from webpage
@app.route('/api/v1/delete_task', methods=['DELETE'])
def delete_task(*args):
try:
if len(args) == 0:
req_data = request.get_json()
task = req_data['task']
else:
task = args[0]
# Remove task from the list
db_data = db_functions.delete_item(task)
# Return error if item not deleted
if db_data['Result'] == 'Failure':
response = "error: task could not be deleted, reason: " + db_data['Remark']
return response
# Return response
response = "success: task deleted from list"
return response
except Exception as e:
error = {"Error": "There is some error with API call, please check its format"}
response = Response(json.dumps(error), mimetype='application/json')
return response
#Function to calculate number of days left for task and add date in dd/mm/yyyy format in task tuple
def get_tasks_with_days_left(data):
try:
data_list = list(data)
if data_list[3] == "Completed":
data_list.append("-")
else:
current_date = datetime.date.today()
task_duedate = datetime.datetime(int(data_list[2].split("-")[0]),int(data_list[2].split("-")[1]),int(data_list[2].split("-")[2])).date()
data_list.append(str((task_duedate-current_date).days))
data_list.append(data_list[2].split("-")[2]+"/"+data_list[2].split("-")[1]+"/"+data_list[2].split("-")[0])
data = tuple(data_list)
return data
except Exception as e:
error = {"Error": "Issue while updating date on task"}
response = Response(json.dumps(error), mimetype='application/json')
return response
if __name__ == "__main__":
app.run()