|
| 1 | +from flask import Flask, render_template, request, redirect, url_for |
| 2 | +from flask_sqlalchemy import SQLAlchemy |
| 3 | +from flask_migrate import Migrate |
| 4 | +from flask_wtf.csrf import CSRFProtect |
| 5 | +from datetime import datetime |
| 6 | +import os |
| 7 | + |
| 8 | +app = Flask(__name__, static_folder='static') |
| 9 | +csrf = CSRFProtect(app) |
| 10 | + |
| 11 | +# WEBSITE_HOSTNAME exists only in production environment |
| 12 | +if not 'WEBSITE_HOSTNAME' in os.environ: |
| 13 | + # local development, where we'll use environment variables |
| 14 | + print("Loading config.development and environment variables from .env file.") |
| 15 | + app.config.from_object('azureproject.development') |
| 16 | +else: |
| 17 | + # production |
| 18 | + print("Loading config.production.") |
| 19 | + app.config.from_object('azureproject.production') |
| 20 | + |
| 21 | +app.config.update( |
| 22 | + SQLALCHEMY_DATABASE_URI=app.config.get('DATABASE_URI'), |
| 23 | + SQLALCHEMY_TRACK_MODIFICATIONS=False, |
| 24 | +) |
| 25 | + |
| 26 | +# Initialize the database connection |
| 27 | +db = SQLAlchemy(app) |
| 28 | + |
| 29 | +# Enable Flask-Migrate commands "flask db init/migrate/upgrade" to work |
| 30 | +migrate = Migrate(app, db) |
| 31 | + |
| 32 | +# Create databases, if databases exists doesn't issue create |
| 33 | +# For schema changes, run "flask db migrate" |
| 34 | +from models import Restaurant, Review |
| 35 | +db.create_all() |
| 36 | +db.session.commit() |
| 37 | + |
| 38 | +@app.route('/', methods=['GET']) |
| 39 | +def index(): |
| 40 | + from models import Restaurant |
| 41 | + print('Request for index page received') |
| 42 | + restaurants = Restaurant.query.all() |
| 43 | + return render_template('index.html', restaurants=restaurants) |
| 44 | + |
| 45 | +@app.route('/<int:id>', methods=['GET']) |
| 46 | +def details(id): |
| 47 | + from models import Restaurant, Review |
| 48 | + restaurant = Restaurant.query.where(Restaurant.id == id).first() |
| 49 | + reviews = Review.query.where(Review.restaurant==id) |
| 50 | + return render_template('details.html', restaurant=restaurant, reviews=reviews) |
| 51 | + |
| 52 | +@app.route('/create', methods=['GET']) |
| 53 | +def create_restaurant(): |
| 54 | + print('Request for add restaurant page received') |
| 55 | + return render_template('create_restaurant.html') |
| 56 | + |
| 57 | +@app.route('/add', methods=['POST']) |
| 58 | +@csrf.exempt |
| 59 | +def add_restaurant(): |
| 60 | + from models import Restaurant |
| 61 | + try: |
| 62 | + name = request.values.get('restaurant_name') |
| 63 | + street_address = request.values.get('street_address') |
| 64 | + description = request.values.get('description') |
| 65 | + except (KeyError): |
| 66 | + # Redisplay the question voting form. |
| 67 | + return render_template('add_restaurant.html', { |
| 68 | + 'error_message': "You must include a restaurant name, address, and description", |
| 69 | + }) |
| 70 | + else: |
| 71 | + restaurant = Restaurant() |
| 72 | + restaurant.name = name |
| 73 | + restaurant.street_address = street_address |
| 74 | + restaurant.description = description |
| 75 | + db.session.add(restaurant) |
| 76 | + db.session.commit() |
| 77 | + |
| 78 | + return redirect(url_for('details', id=restaurant.id)) |
| 79 | + |
| 80 | +@app.route('/review/<int:id>', methods=['POST']) |
| 81 | +@csrf.exempt |
| 82 | +def add_review(id): |
| 83 | + from models import Review |
| 84 | + try: |
| 85 | + user_name = request.values.get('user_name') |
| 86 | + rating = request.values.get('rating') |
| 87 | + review_text = request.values.get('review_text') |
| 88 | + except (KeyError): |
| 89 | + #Redisplay the question voting form. |
| 90 | + return render_template('add_review.html', { |
| 91 | + 'error_message': "Error adding review", |
| 92 | + }) |
| 93 | + else: |
| 94 | + review = Review() |
| 95 | + review.restaurant = id |
| 96 | + review.review_date = datetime.now() |
| 97 | + review.user_name = user_name |
| 98 | + review.rating = int(rating) |
| 99 | + review.review_text = review_text |
| 100 | + db.session.add(review) |
| 101 | + db.session.commit() |
| 102 | + |
| 103 | + return redirect(url_for('details', id=id)) |
| 104 | + |
| 105 | +@app.context_processor |
| 106 | +def utility_processor(): |
| 107 | + def star_rating(id): |
| 108 | + from models import Review |
| 109 | + reviews = Review.query.where(Review.restaurant==id) |
| 110 | + |
| 111 | + ratings = [] |
| 112 | + review_count = 0; |
| 113 | + for review in reviews: |
| 114 | + ratings += [review.rating] |
| 115 | + review_count += 1 |
| 116 | + |
| 117 | + avg_rating = sum(ratings)/len(ratings) if ratings else 0 |
| 118 | + stars_percent = round((avg_rating / 5.0) * 100) if review_count > 0 else 0 |
| 119 | + return {'avg_rating': avg_rating, 'review_count': review_count, 'stars_percent': stars_percent} |
| 120 | + |
| 121 | + return dict(star_rating=star_rating) |
| 122 | + |
| 123 | +if __name__ == '__main__': |
| 124 | + app.run() |
0 commit comments