-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathapp.py
More file actions
149 lines (112 loc) · 3.64 KB
/
app.py
File metadata and controls
149 lines (112 loc) · 3.64 KB
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
import flask
import re
import os
import string
import random
import json
import requests
from datetime import datetime
from random import randint
from todos_store import Store
app = flask.Flask(__name__, static_url_path='/static')
# unsafeRandId generates a random string composed from english upper case letters and digits
# it's called unsafe because it doesn't use a crypto random generator
url = "https://service.eu.apiconnect.ibmcloud.com/gws/apigateway/api/da713fba861ff19ef7cc15e87072dfd6ce556d30c2b0caac7f307ef844741e9a/281a066c-00fc-40a6-b272-0139a590ce7b/rookout"
def unsafeRandId(len):
return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(len))
def cleanStr(str):
return re.sub(r'[>|<|;|`|&|/|\\]', '', str)
@app.errorhandler(404)
def page_not_found(e):
return '404 Page Not Found'
@app.errorhandler(500)
def internal_server_error(e):
return '500 Internal Server Error'
@app.route("/error")
def render_bad_template():
try:
invalid_oper = 42 / 0
except Exception as e:
print('Operation failed to complete')
animal_list = ['dog', 'cat', 'turtle', 'fish', 'bird', 'cow', 'sealion']
time = datetime.now()
number = 0.01 * randint(10, 200) + 0.1
return flask.render_template('doesnotexist.html', animal_list=animal_list, time=time, number=number)
# redirect from base url to index.html
@app.route("/")
def index():
return flask.redirect('/static/index.html')
@app.route('/todos/<todoId>', methods=['DELETE'])
def del_todo(todoId):
todos = Store.getInstance().todos
newTodos = [t for t in todos if t['id'] != todoId]
todos = newTodos
return ('', 204)
@app.route('/todos/clear_completed', methods=['DELETE'])
def clear_completed():
todos = Store.getInstance().todos
todo = [t for t in todos if not t['completed']]
return ('', 204)
@app.route('/todos', methods=['UPDATE'])
def update_todo():
todos = Store.getInstance().todos
req = flask.request
todo = req.get_json()
for t in todos:
if t['id'] == todo['id']:
t['title'] = todo['title']
t['completed'] = todo['completed']
break
return ('', 204)
# add a new todo action
@app.route('/todos', methods=['POST'])
def add_todo():
todos = Store.getInstance().todos
fr = flask.request
req = fr.get_json()
todoStr = cleanStr(req['title'])
if not todoStr:
return ('', 400)
todo = {
"title": cleanStr(req['title']),
"id": unsafeRandId(10),
"completed": False
}
todos.append(todo)
return ('', 204)
@app.route('/todos/generate', methods=['POST'])
def generate_todo():
headers = {
'Content-Type': "application/json",
'Cache-Control': "no-cache",
}
response = requests.request("GET", url, headers=headers)
todos = Store.getInstance().todos
fr = flask.request
req = fr.get_json()
json_data = json.loads(response.text)
todoStr = json_data['todo']
todo = {
"title": todoStr,
"id": unsafeRandId(10),
"completed": False
}
todos.append(todo)
return ('', 204)
@app.route('/todos', methods=['GET'])
def get_todos():
todos = Store.getInstance().todos
return json.dumps(todos)
@app.route('/todo/dup/<todoId>', methods=['POST'])
def duplicate_todo(todoId):
todos = Store.getInstance().todos
for todo in todos:
if todoId == todo['id']:
dup = {'title': todo['completed'],
'id': unsafeRandId(10),
'completed': todo['title']}
todos.append(dup)
break
return ('', 204)
if __name__ == "__main__":
app.run(host='0.0.0.0')