/
app.py
147 lines (100 loc) · 4.25 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
"""Flask app for Cupcakes"""
from flask import Flask, jsonify, request, render_template
from flask_debugtoolbar import DebugToolbarExtension
from models import db, connect_db, Cupcake
from forms import AddCupcakeForm
app = Flask(__name__)
app.config['SECRET_KEY'] = "secret"
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql:///cupcakes"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
connect_db(app)
db.create_all()
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
toolbar = DebugToolbarExtension(app)
########################################################
# Homepage Route
@app.route("/")
def show_homepage():
""" Show the homepage to show and add cupcakes """
form = AddCupcakeForm()
return render_template('base.html', form=form)
########################################################
# Cupcakes API Routes
@app.route("/api/cupcakes")
def get_all_cupcakes():
""" Get all the data about cupcakes
Returns JSON of list of serialized cupcakes like:
{cupcakes: [{id, flavor, size, rating, image}, ...]} """
cupcakes = Cupcake.query.all()
serialized_list = [cupcake.serialize() for cupcake in cupcakes]
return jsonify(cupcakes=serialized_list)
@app.route("/api/cupcakes/<int:cupcake_id>")
def get_a_cupcake(cupcake_id):
""" Return serialized version of cupcake with cupcake id
If not found, return 404 with error message
"""
cupcake = Cupcake.query.get(cupcake_id)
# Return error if cupcake is not found
if cupcake:
serialized = cupcake.serialize()
else:
message = "Cupcake does not exist"
return (jsonify(error=message), 404)
return jsonify(cupcake=serialized)
@app.route("/api/cupcakes", methods=["POST"])
def create_cupcake():
""" Create a new cupcake, return JSON with status code 201
Respond with JSON like: {cupcake: {id, flavor, size, rating, image}}.
"""
# Remove csrf because we don't have form to submit, we are using Axios to POST data
form = AddCupcakeForm(data=request.json, csrf_enabled=False)
if form.validate():
form.image.data = form.image.data or None
new_cupcake = Cupcake()
form.populate_obj(new_cupcake)
db.session.add(new_cupcake)
db.session.commit()
return (jsonify(cupcake=new_cupcake.serialize()), 201)
else:
return (jsonify(errors=[field.errors for field in form if field.widget.input_type != 'hidden']), 200)
@app.route("/api/cupcakes/<int:cupcake_id>", methods=["PATCH"])
def update_cupcake(cupcake_id):
""" Updates the cupcake from request data and returns it
Returns with JSON like: {cupcake: {id, flavor, size, rating, image}}.
"""
cupcake = Cupcake.query.get(cupcake_id)
if not cupcake:
message = "Cupcake does not exist"
return (jsonify(error=message), 404)
# If we want to update only parts of the data,
# use .get here or the current value if JSON body has no data
cupcake.flavor = request.json.get('flavor') or cupcake.flavor
cupcake.size = request.json.get('size') or cupcake.size
cupcake.rating = request.json.get('rating') or cupcake.rating
cupcake.image = request.json.get('image') or cupcake.image
db.session.commit()
return jsonify(cupcake=cupcake.serialize())
@app.route("/api/cupcakes/<int:cupcake_id>", methods=["DELETE"])
def delete_cupcake(cupcake_id):
""" Deletes the cupcake at cupcake_id and returns a success message
{message: "Deleted"}
If cupcake not found, raise a 404 error message
"""
cupcake = Cupcake.query.get(cupcake_id)
message = "Deleted" if cupcake else "Cupcake does not exist"
if not cupcake:
return (jsonify(message=message), 404)
db.session.delete(cupcake)
db.session.commit()
return jsonify(message=message)
@app.route("/api/cupcakes/search")
def search_cupcakes_by_term():
""" Gets all cupcakes from database filtered by search term
returning JSON of a list of serialized cupcakes like:
{cupcakes: [{id, flavor, size, rating, image}, ...]} """
search_term = request.args["search_term"]
filtered_cupcakes = Cupcake.query.filter(
Cupcake.flavor.ilike(f"%{search_term}%")
)
serialized = [cupcake.serialize() for cupcake in filtered_cupcakes]
return jsonify(cupcakes=serialized)