Skip to content

Commit d4d87ba

Browse files
authored
Merge pull request #21 from pamelafox/pamela-style
Style improvements to Flask sample
2 parents 346b123 + 9783e20 commit d4d87ba

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed

app.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
from datetime import datetime
21
import os
2+
from datetime import datetime
33

4-
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
5-
from flask_sqlalchemy import SQLAlchemy
4+
from flask import Flask, redirect, render_template, request, send_from_directory, url_for
65
from flask_migrate import Migrate
6+
from flask_sqlalchemy import SQLAlchemy
77
from flask_wtf.csrf import CSRFProtect
88

99

1010
app = Flask(__name__, static_folder='static')
1111
csrf = CSRFProtect(app)
1212

1313
# WEBSITE_HOSTNAME exists only in production environment
14-
if not 'WEBSITE_HOSTNAME' in os.environ:
15-
# local development, where we'll use environment variables
16-
print("Loading config.development and environment variables from .env file.")
17-
app.config.from_object('azureproject.development')
14+
if 'WEBSITE_HOSTNAME' not in os.environ:
15+
# local development, where we'll use environment variables
16+
print("Loading config.development and environment variables from .env file.")
17+
app.config.from_object('azureproject.development')
1818
else:
19-
# production
20-
print("Loading config.production.")
21-
app.config.from_object('azureproject.production')
19+
# production
20+
print("Loading config.production.")
21+
app.config.from_object('azureproject.production')
2222

2323
app.config.update(
2424
SQLALCHEMY_DATABASE_URI=app.config.get('DATABASE_URI'),
@@ -37,13 +37,13 @@
3737
@app.route('/', methods=['GET'])
3838
def index():
3939
print('Request for index page received')
40-
restaurants = Restaurant.query.all()
40+
restaurants = Restaurant.query.all()
4141
return render_template('index.html', restaurants=restaurants)
4242

4343
@app.route('/<int:id>', methods=['GET'])
4444
def details(id):
4545
restaurant = Restaurant.query.where(Restaurant.id == id).first()
46-
reviews = Review.query.where(Review.restaurant==id)
46+
reviews = Review.query.where(Review.restaurant == id)
4747
return render_template('details.html', restaurant=restaurant, reviews=reviews)
4848

4949
@app.route('/create', methods=['GET'])
@@ -94,21 +94,21 @@ def add_review(id):
9494
review.review_text = review_text
9595
db.session.add(review)
9696
db.session.commit()
97-
98-
return redirect(url_for('details', id=id))
97+
98+
return redirect(url_for('details', id=id))
9999

100100
@app.context_processor
101101
def utility_processor():
102102
def star_rating(id):
103-
reviews = Review.query.where(Review.restaurant==id)
103+
reviews = Review.query.where(Review.restaurant == id)
104104

105105
ratings = []
106-
review_count = 0;
106+
review_count = 0
107107
for review in reviews:
108108
ratings += [review.rating]
109109
review_count += 1
110110

111-
avg_rating = sum(ratings)/len(ratings) if ratings else 0
111+
avg_rating = sum(ratings) / len(ratings) if ratings else 0
112112
stars_percent = round((avg_rating / 5.0) * 100) if review_count > 0 else 0
113113
return {'avg_rating': avg_rating, 'review_count': review_count, 'stars_percent': stars_percent}
114114

@@ -120,4 +120,4 @@ def favicon():
120120
'favicon.ico', mimetype='image/vnd.microsoft.icon')
121121

122122
if __name__ == '__main__':
123-
app.run()
123+
app.run()

azureproject/development.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from pathlib import Path
21
import os
2+
from pathlib import Path
33

44
# Build paths inside the project like this: BASE_DIR / 'subdir'.
55
BASE_DIR = Path(__file__).resolve().parent.parent

azureproject/production.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
SECRET_KEY = os.getenv('SECRET_KEY', 'flask-insecure-7ppocbnx@w71dcuinn*t^_mzal(t@o01v3fee27g%rg18fc5d@')
55

66
ALLOWED_HOSTS = [os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []
7-
CSRF_TRUSTED_ORIGINS = ['https://'+ os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []
7+
CSRF_TRUSTED_ORIGINS = ['https://' + os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []
88

99
# Configure Postgres database; the full username for PostgreSQL flexible server is
1010
# username (not @sever-name).

models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
1+
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String
22
from sqlalchemy.orm import validates
33

44
from app import db
@@ -10,6 +10,7 @@ class Restaurant(db.Model):
1010
name = Column(String(50))
1111
street_address = Column(String(50))
1212
description = Column(String(250))
13+
1314
def __str__(self):
1415
return self.name
1516

@@ -28,4 +29,4 @@ def validate_rating(self, key, value):
2829
return value
2930

3031
def __str__(self):
31-
return self.restaurant.name + " (" + self.review_date.strftime("%x") +")"
32+
return f"{self.restaurant.name} ({self.review.date:%x})"

0 commit comments

Comments
 (0)