-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (62 loc) · 1.91 KB
/
app.py
File metadata and controls
73 lines (62 loc) · 1.91 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
# Refactored: Use CRUD naming (read, create) in InfoModel
from flask import Flask, jsonify, request
from flask_cors import CORS
from flask_restful import Api, Resource
app = Flask(__name__)
CORS(app, supports_credentials=True, origins='*')
api = Api(app)
# --- Model class for InfoDb with CRUD naming ---
class InfoModel:
def __init__(self):
self.data = [
{
"FirstName": "John",
"LastName": "Mortensen",
"DOB": "October 21",
"Residence": "San Diego",
"Email": "jmortensen@powayusd.com",
"Owns_Cars": ["2015-Fusion", "2011-Ranger", "2003-Excursion", "1997-F350", "1969-Cadillac", "2015-Kuboto-3301"]
},
{
"FirstName": "Shane",
"LastName": "Lopez",
"DOB": "February 27",
"Residence": "San Diego",
"Email": "slopez@powayusd.com",
"Owns_Cars": ["2021-Insight"]
}
]
def read(self):
return self.data
def create(self, entry):
self.data.append(entry)
# Instantiate the model
info_model = InfoModel()
# --- API Resource ---
class DataAPI(Resource):
def get(self):
return jsonify(info_model.read())
def post(self):
# Add a new entry to InfoDb
entry = request.get_json()
if not entry:
return {"error": "No data provided"}, 400
info_model.create(entry)
return {"message": "Entry added successfully", "entry": entry}, 201
api.add_resource(DataAPI, '/api/data')
# Wee can use @app.route for HTML endpoints, this will be style for Admin UI
@app.route('/')
def say_hello():
html_content = """
<html>
<head>
<title>Hello</title>
</head>
<body>
<h2>Hello, World!</h2>
</body>
</html>
"""
return html_content
if __name__ == '__main__':
app.run(port=5001)