-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
111 lines (77 loc) · 2.72 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
from http import client
from flask import Flask, render_template, request, redirect, url_for
import json
app = Flask(__name__)
@app.route("/")
def clients():
f = open("data/clients.json", "r")
clients_str = f.read()
all_clients = json.loads(clients_str)
return render_template("clients.html", clients=all_clients)
@app.route("/clients/add", methods=['GET', 'POST'])
def create():
f = open("data/clients.json", "r")
clients_str = f.read()
f.close()
all_clients = json.loads(clients_str)
next_id = 1
if len(all_clients) > 0:
next_id = all_clients[-1]["id"] + 1
if request.method == 'POST':
client_name = request.form["client_name"]
if client_name == "":
return "Name is required"
all_clients.append(
{
"id": next_id,
"name": client_name
}
)
updated_clients = json.dumps(all_clients)
f = open("data/clients.json", "w")
f.write(updated_clients)
f.close()
return redirect("/")
@app.route("/clients/<id>")
def client(id):
f = open("data/clients.json", "r")
clients_str = f.read()
f.close()
all_clients = json.loads(clients_str)
client_by_id = list(filter(lambda x: (x["id"] == int(id)), all_clients))
client_by_id = client_by_id[0]
return render_template("client.html", client=client_by_id)
@app.route("/clients/edit/<id>", methods=['GET', 'POST'])
def client_edit(id):
f = open("data/clients.json", "r")
clients_str = f.read()
f.close()
all_clients = json.loads(clients_str)
client_by_id = list(filter(lambda x: (x["id"] == int(id)), all_clients))
client_by_id = client_by_id[0]
client_index = all_clients.index(client_by_id)
if request.method == 'POST':
client_name = request.form["client_name"]
all_clients[client_index] = {"id": client_by_id["id"], "name": client_name }
updated_clients = json.dumps(all_clients)
f = open("data/clients.json", "w")
f.write(updated_clients)
f.close()
return redirect('/')
return render_template("edit.html", client_id=id, client=client_by_id)
@app.route("/clients/delete")
def client_delete():
f = open("data/clients.json", "r")
clients_str = f.read()
f.close()
all_clients = json.loads(clients_str)
client_id = request.args.get("id")
client_by_id = list(filter(lambda x: (x["id"] == int(client_id)), all_clients))
client_by_id = client_by_id[0]
client_index = all_clients.index(client_by_id)
del all_clients[client_index]
updated_clients = json.dumps(all_clients)
f = open("data/clients.json", "w")
f.write(updated_clients)
f.close()
return redirect("/")