-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
160 lines (119 loc) · 4.32 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
from flask import Flask, Response
from webargs import fields
from webargs.flaskparser import use_args
from application.services import generate_humans
from application.services.create_table import create_table
from application.services.db_connection import DBConnection
from application.services.get_items_as_iterator import get_items_as_iterator
app = Flask(__name__)
@app.route("/users/create")
@use_args({"name": fields.Str(required=True), "age": fields.Int(required=True)}, location="query")
def users__create(args):
with DBConnection() as connection:
with connection:
connection.execute(
"INSERT INTO users (name, age) VALUES (:name, :age);",
{"name": args["name"], "age": args["age"]},
)
return "Ok"
@app.route("/users/read-all")
def users__read_all():
with DBConnection() as connection:
users = connection.execute("SELECT * FROM users;").fetchall()
return "<br>".join([f'{user["pk"]}: {user["name"]} - {user["age"]}' for user in users])
@app.route("/users/read/<int:pk>")
def users__read(pk: int):
with DBConnection() as connection:
user = connection.execute(
"SELECT * " "FROM users " "WHERE (pk=:pk);",
{
"pk": pk,
},
).fetchone()
return f'{user["pk"]}: {user["name"]} - {user["age"]}'
@app.route("/users/update/<int:pk>")
@use_args({"age": fields.Int(), "name": fields.Str()}, location="query")
def users__update(
args,
pk: int,
):
with DBConnection() as connection:
with connection:
name = args.get("name")
age = args.get("age")
if name is None and age is None:
return Response(
"Need to provide at least one argument",
status=400,
)
args_for_request = []
if name is not None:
args_for_request.append("name=:name")
if age is not None:
args_for_request.append("age=:age")
args_2 = ", ".join(args_for_request)
connection.execute(
"UPDATE users " f"SET {args_2} " "WHERE pk=:pk;",
{
"pk": pk,
"age": age,
"name": name,
},
)
return "Ok"
@app.route("/users/delete/<int:pk>")
def users__delete(pk):
with DBConnection() as connection:
with connection:
connection.execute(
"DELETE " "FROM users " "WHERE (pk=:pk);",
{
"pk": pk,
},
)
return "Ok"
@app.route("/")
def hello_world():
return "Hello World!"
@app.route("/generate-humans")
@use_args({"amount": fields.Int(missing=3)}, location="query")
def generate_humans_view(args):
humans_amount = args["amount"]
list_items_formatted = "".join(
f"<li>{human.name} - {human.age}</li>" for human in generate_humans(amount=humans_amount)
)
return f"<ol>{list_items_formatted}</ol>"
@app.route("/generate-humans-2")
@app.route("/generate-humans-2/<int:amount>")
def generate_humans_view_2(amount: int = 7):
humans_amount = amount
list_items_formatted = "".join(
f"<li>{human.name} - {human.age}</li>" for human in generate_humans(amount=humans_amount)
)
return f"<ol>{list_items_formatted}</ol>"
@app.route("/example")
def example():
# [parse_input]-[BEGIN]
# Nothing for now
# [parse_input]-[END]
# [run_main_handler]-[BEGIN]
items = get_items_as_iterator(amount=15)
# [run_main_handler]-[END]
# [format_output]-[BEGIN]
list_items_formatted = "".join(f"<li>{item}</li>" for item in items)
# return f'<ul>{list_items_formatted}</ul>'
return f"<ol>{list_items_formatted}</ol>"
# [format_output]-[BEGIN]
# return ''.join(f'<p>{random.randint(0, 100)}</p>' for _ in range(20))
@app.route("/greetings/<name>/<int:age>")
def greetings_by_path(name: str, age: int):
return f"Hi, {name}. You are {age} old."
@app.route("/greetings-2")
@use_args({"name": fields.Str(required=True), "age": fields.Int(required=True)}, location="query")
def greetings_by_query(args):
name: str = args["name"]
age: int = args["age"]
return f"Hi, {name}. You are {age} old."
create_table()
if __name__ == "__main__":
app.run()