Skip to content

Commit 3902b3e

Browse files
committed
Remove dependency on external API.
1 parent a03a96d commit 3902b3e

File tree

17 files changed

+214
-68
lines changed

17 files changed

+214
-68
lines changed

.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
FLASK_APP=wsgi.py
2-
FLASK_DEBUG=False
1+
FLASK_APP=main.py
2+
FLASK_ENV=production
33
SECRET_KEY=randomstringofcharacters
44
LESS_BIN=/usr/local/bin/lessc
55
ASSETS_DEBUG=False

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Hackers and Slackers
3+
Copyright (c) 2023 Hackers and Slackers
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Get set up locally in two steps:
2323

2424
Replace the values in **.env.example** with your values and rename this file to **.env**:
2525

26-
* `FLASK_APP`: Entry point of your application; should be `wsgi.py`.
26+
* `FLASK_APP`: Entry point of your application; should be `main.py`.
2727
* `FLASK_ENV`: The environment in which to run your application; either `development` or `production`.
2828
* `SECRET_KEY`: Randomly generated string of characters used to encrypt your app's data.
2929
* `LESS_BIN` *(optional for static assets)*: Path to your local LESS installation via `which lessc`.

config.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
from dotenv import load_dotenv
55

6-
basedir = path.abspath(path.dirname(__file__))
7-
load_dotenv(path.join(basedir, ".env"))
6+
BASE_DIR = path.abspath(path.dirname(__file__))
7+
load_dotenv(path.join(BASE_DIR, ".env"))
88

99

1010
class Config:
1111
"""Configuration from environment variables."""
1212

1313
SECRET_KEY = environ.get("SECRET_KEY")
14-
FLASK_ENV = environ.get("FLASK_DEBUG")
15-
FLASK_APP = "wsgi.py"
14+
FLASK_ENV = environ.get("FLASK_ENV")
15+
FLASK_APP = "main.py"
1616

1717
# Flask-Assets
1818
LESS_BIN = environ.get("LESS_BIN")
@@ -27,5 +27,5 @@ class Config:
2727
# Datadog
2828
DD_SERVICE = environ.get("DD_SERVICE")
2929

30-
# API
31-
BEST_BUY_API_KEY = environ.get("BEST_BUY_API_KEY")
30+
# Hardcoded data
31+
PRODUCT_DATA_FILEPATH = f"{BASE_DIR}/data/products.json"

data/products.json

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[
2+
{
3+
"customerReviewAverage": 5.00,
4+
"customerReviewCount": 293,
5+
"name": "Red Robin - $25 Gift Card",
6+
"sku": 4259000,
7+
"image": "https://pisces.bbystatic.com/prescaled/500/500/image2/BestBuy_US/images/products/4259/4259000_sd.jpg",
8+
"manufacturer": "Red Robin",
9+
"longDescription": "Come in and enjoy an outrageously delicious burger with Bottomless Steak Fries. Pair it with a cold beer or signature Freckled Lemonade - it's a duo that's sure to make you smile.",
10+
"salePrice": 25.00
11+
},
12+
{
13+
"customerReviewAverage": 5.00,
14+
"customerReviewCount": 53,
15+
"name": "Bowers & Wilkins - 700 Series 3-way Floorstanding Speaker w/5\" midrange, dual 5\" bass (each) - White",
16+
"sku": 6023602,
17+
"image": "https://pisces.bbystatic.com/prescaled/500/500/image2/BestBuy_US/images/products/6023/6023602_sd.jpg",
18+
"manufacturer": "Bowers & Wilkins",
19+
"longDescription": "Generate commanding sound with this Bowers & Wilkins loudspeaker. Its Carbon Dome tweeter plays accurate, clear highs, and the two Aerofoil bass drivers deliver stiffness and rigidity while producing dynamic bass. This vented Bowers & Wilkins loudspeaker has a 5-inch midrange driver to round out its full sound, and its slim construction makes it suitable for small or large spaces.",
20+
"salePrice": 1487.99
21+
},
22+
{
23+
"customerReviewAverage": 5.00,
24+
"customerReviewCount": 53,
25+
"name": "Bowers & Wilkins - 700 Series 3-way Floorstanding Speaker w/5\" midrange, dual 5\" bass (each) - Gloss Black",
26+
"sku": 6027601,
27+
"image": "https://pisces.bbystatic.com/prescaled/500/500/image2/BestBuy_US/images/products/6027/6027601_sd.jpg",
28+
"manufacturer": "Bowers & Wilkins",
29+
"longDescription": "Enjoy accurate, realistic sound with this black Bowers & Wilkins floor speaker. Its two bass drivers deliver thumping low frequencies, and the Carbon Dome tweeter and midrange driver offer commanding, studio-quality audio. This Bowers & Wilkins floor speaker integrates into your living space and entertainment system for seamless sound production with a frequency range of 48Hz - 28kHz.",
30+
"salePrice": 1487.99
31+
},
32+
{
33+
"customerReviewAverage": 5.00,
34+
"customerReviewCount": 55,
35+
"name": "Canon - RF50mm F1.2 L USM Standard Prime Lens for EOS R-Series Cameras - Black",
36+
"sku": 6298180,
37+
"image": "https://pisces.bbystatic.com/prescaled/500/500/image2/BestBuy_US/images/products/6298/6298180_sd.jpg",
38+
"manufacturer": "Canon",
39+
"longDescription": "Capture high-quality images and sharp details with this 50mm Canon lens. The 1.31-foot minimum focusing distance and 0.19x magnification let you photograph from a range of distances, and its UD lens reduces distortion. This Canon lens has an added coating to minimize lens flare and ghosting in various types of light.",
40+
"salePrice": 2199.99
41+
},
42+
{
43+
"customerReviewAverage": 5.00,
44+
"customerReviewCount": 83,
45+
"name": "Nikkor Z 24-70mm f/2.8 S Optical Zoom Lens for Nikon Z - Black",
46+
"sku": 6334316,
47+
"image": "https://pisces.bbystatic.com/prescaled/500/500/image2/BestBuy_US/images/products/6334/6334316_sd.jpg",
48+
"manufacturer": "Nikon",
49+
"longDescription": "Capture high-quality photographs whether shooting at close, medium or long range with this Nikon NIKKOR Z 24-70mm lens. The dust-resistant and drip-resistant design helps keep this lens in good condition, and the auto-focusing feature is quick and quiet. This Nikon NIKKOR Z 24-70mm lens features a Z system that produces images with enhanced sharpness and illumination.",
50+
"salePrice": 2099.99
51+
},
52+
{
53+
"customerReviewAverage": 5.00,
54+
"customerReviewCount": 64,
55+
"name": "Apple Watch Ultra (GPS + Cellular) 49mm Titanium Case with White Ocean Band - Titanium (Verizon)",
56+
"sku": 6340050,
57+
"image": "https://pisces.bbystatic.com/prescaled/500/500/image2/BestBuy_US/images/products/6340/6340050_sd.jpg",
58+
"manufacturer": "Apple",
59+
"longDescription": "The most rugged and capable Apple Watch ever, designed for exploration, adventure, and endurance. With a 49mm aerospace-grade titanium case, extra-long battery life,¹ specialized apps that work with the advanced sensors, and a new customizable Action button. See Dimension section below for band sizing information.",
60+
"salePrice": 799.99
61+
},
62+
{
63+
"customerReviewAverage": 5.00,
64+
"customerReviewCount": 64,
65+
"name": "Apple Watch Ultra (GPS + Cellular) 49mm Titanium Case with Yellow Ocean Band - Titanium (Verizon)",
66+
"sku": 6340051,
67+
"image": "https://pisces.bbystatic.com/prescaled/500/500/image2/BestBuy_US/images/products/6340/6340051_sd.jpg",
68+
"manufacturer": "Apple",
69+
"longDescription": "The most rugged and capable Apple Watch ever, designed for exploration, adventure, and endurance. With a 49mm aerospace-grade titanium case, extra-long battery life,¹ specialized apps that work with the advanced sensors, and a new customizable Action button. See Dimension section below for band sizing information.",
70+
"salePrice": 799.99
71+
},
72+
{
73+
"customerReviewAverage": 5.00,
74+
"customerReviewCount": 64,
75+
"name": "Apple Watch Ultra (GPS + Cellular) 49mm Titanium Case with Midnight Ocean Band - Titanium (Verizon)",
76+
"sku": 6340057,
77+
"image": "https://pisces.bbystatic.com/prescaled/500/500/image2/BestBuy_US/images/products/6340/6340057_sd.jpg",
78+
"manufacturer": "Apple",
79+
"longDescription": "The most rugged and capable Apple Watch ever, designed for exploration, adventure, and endurance. With a 49mm aerospace-grade titanium case, extra-long battery life,¹ specialized apps that work with the advanced sensors, and a new customizable Action button. See Dimension section below for band sizing information.",
80+
"salePrice": 799.99
81+
},
82+
{
83+
"customerReviewAverage": 5.00,
84+
"customerReviewCount": 59,
85+
"name": "NETGEAR - 8-Port 10/100/1000 Gigabit Ethernet PoE/PoE+ Unmanaged Switch",
86+
"sku": 6356333,
87+
"image": "https://pisces.bbystatic.com/prescaled/500/500/image2/BestBuy_US/images/products/6356/6356333_sd.jpg",
88+
"manufacturer": "NETGEAR",
89+
"longDescription": "Introducing the NETGEAR GS108LP 8-port Gigabit Ethernet unmanaged switch with 60W PoE budget. The flexible PoE+ integrated technology allows you to increase or decrease the PoE budget at any time to provide to your devices the power they need with interchangeable external power supply and an intuitive power selector. The compact and fanless design makes this switch an ideal solution to connect or power any device in any business environment.",
90+
"salePrice": 98.99
91+
}
92+
]

flask_blueprint_tutorial/__init__.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
from flask import Flask
33
from flask_assets import Environment
44

5-
from config import Config
65

7-
8-
def init_app():
6+
def flask_app():
97
"""Create Flask application."""
108
app = Flask(__name__, instance_relative_config=False)
119
app.config.from_object("config.Config")
@@ -20,8 +18,8 @@ def init_app():
2018
from .profile import profile
2119

2220
# Register Blueprints
23-
app.register_blueprint(profile.profile_bp)
24-
app.register_blueprint(home.home_bp)
21+
app.register_blueprint(profile.profile_blueprint)
22+
app.register_blueprint(home.home_blueprint)
2523
app.register_blueprint(products.product_bp)
2624

2725
# Compile static assets

flask_blueprint_tutorial/api.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
"""Source app with worthless data."""
2-
import requests
1+
"""Read placeholder data for demo purposes."""
2+
import json
33

4+
from flask import Flask
45

5-
def fetch_products(app):
6-
"""Grab product listings from BestBuy."""
7-
endpoint = "https://api.bestbuy.com/v1/products(customerReviewAverage>=4&customerReviewCount>100&longDescription=*)"
8-
params = {
9-
"show": "customerReviewAverage,customerReviewCount,name,sku,image,description,manufacturer,longDescription,salePrice,sku",
10-
"apiKey": app.config["BEST_BUY_API_KEY"],
11-
"format": "json",
12-
"pageSize": 6,
13-
"totalPages": 1,
14-
"sort": "customerReviewAverage.dsc",
15-
}
16-
headers = {"Accept": "application/json", "Content-Type": "application/json"}
17-
req = requests.get(endpoint, params=params, headers=headers)
18-
products = req.json()["products"]
19-
return products
6+
7+
def fetch_products(app: Flask) -> dict:
8+
"""
9+
Grab hardcoded product listings.
10+
11+
:param Flask app: Flask application object.
12+
13+
:returns: dict
14+
"""
15+
product_data_filepath = app.config["PRODUCT_DATA_FILEPATH"]
16+
with open(product_data_filepath, encoding="utf-8") as file:
17+
products_data = json.load(file)
18+
return products_data

flask_blueprint_tutorial/assets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ def compile_static_assets(assets):
1414
extra={"rel": "stylesheet/less"},
1515
)
1616
home_style_bundle = Bundle(
17-
"home_bp/less/home.less",
17+
"home_blueprint/less/home.less",
1818
filters="less,cssmin",
1919
output="dist/css/home.css",
2020
extra={"rel": "stylesheet/less"},
2121
)
2222
profile_style_bundle = Bundle(
23-
"profile_bp/less/profile.less",
23+
"profile_blueprint/less/profile.less",
2424
filters="less,cssmin",
2525
output="dist/css/profile.css",
2626
extra={"rel": "stylesheet/less"},
2727
)
2828
product_style_bundle = Bundle(
29-
"products_bp/less/products.less",
29+
"products_blueprint/less/products.less",
3030
filters="less,cssmin",
3131
output="dist/css/products.css",
3232
extra={"rel": "stylesheet/less"},

flask_blueprint_tutorial/home/home.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
from flask_blueprint_tutorial.api import fetch_products
77

88
# Blueprint Configuration
9-
home_bp = Blueprint(
10-
"home_bp", __name__, template_folder="templates", static_folder="static"
11-
)
9+
home_blueprint = Blueprint("home_blueprint", __name__, template_folder="templates", static_folder="static")
1210

1311

14-
@home_bp.route("/", methods=["GET"])
12+
@home_blueprint.route("/", methods=["GET"])
1513
def home():
16-
"""Homepage."""
14+
"""Render application Homepage."""
1715
products = fetch_products(app)
1816
return render_template(
1917
"index.jinja2",
@@ -24,9 +22,9 @@ def home():
2422
)
2523

2624

27-
@home_bp.route("/about", methods=["GET"])
25+
@home_blueprint.route("/about", methods=["GET"])
2826
def about():
29-
"""About page."""
27+
"""Render static `about` page."""
3028
return render_template(
3129
"index.jinja2",
3230
title="About",
@@ -35,9 +33,9 @@ def about():
3533
)
3634

3735

38-
@home_bp.route("/contact", methods=["GET"])
36+
@home_blueprint.route("/contact", methods=["GET"])
3937
def contact():
40-
"""Contact page."""
38+
"""Render page."""
4139
return render_template(
4240
"index.jinja2",
4341
title="Contact",

flask_blueprint_tutorial/home/templates/index.jinja2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<h1>{{ title }}</h1>
1414
<h2>{{ subtitle }}</h2>
1515
<div class="resource-links">
16-
<p class="resource-link">Github repo found <a href="https://github.com/hackersandslackers/flask-blueprint-tutorial">here.</a></p>
17-
<p class="resource-link">Tutorial found <a href="https://hackersandslackers.com/flask-blueprints/">here.</a></p>
16+
<p class="resource-link">Github repo <a href="https://github.com/hackersandslackers/flask-blueprint-tutorial">here.</a></p>
17+
<p class="resource-link">Tutorial <a href="https://hackersandslackers.com/flask-blueprints/">here.</a></p>
1818
</div>
1919

2020
<div class="products">

0 commit comments

Comments
 (0)