Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- # https://pypi.org/project/flask-googlemaps/
- # Import the modules that we will want to use
- from cs50 import SQL
- from flask import Flask, flash, jsonify, redirect, render_template, request, session
- from flask_session import Session
- from flask_googlemaps import GoogleMaps
- from tempfile import mkdtemp
- from werkzeug.exceptions import default_exceptions, HTTPException, InternalServerError
- from werkzeug.security import check_password_hash, generate_password_hash
- import config
- from helpers import login_required
- # Configure application
- app = Flask(__name__)
- # Ensure that user sessions when they are logged in are not perm
- app.config["SESSION_PERMANENT"] = False
- # Ensure the location that we want to store the data for user sessions is going to be in the file system of the webserver we'll be running this application from (CS50 IDE)
- app.config["SESSION_TYPE"] = "filesystem"
- # We would like to enable sessions for this particular flask web app
- Session(app)
- # Ensure templates are auto-reloaded when sent and recieved
- app.config["TEMPLATES_AUTO_RELOAD"] = True
- # set the maps api key as config
- app.config['GOOGLEMAPS_KEY'] = "8JZ7i18MjFuM35dJHq70n3Hx4"
- # Initialize the extension
- GoogleMaps(app)
- # Ensure responses aren't cached
- @app.after_request
- def after_request(response):
- response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
- response.headers["Expires"] = 0
- response.headers["Pragma"] = "no-cache"
- return response
- # Configure CS50 Library to use SQLite database
- db = SQL("sqlite:///parks.db")
- # app route to map out 1 park
- @app.route("/")
- @login_required
- def index():
- """Homepage with Park locator"""
- index_park_info = send_to_index()
- return render_template("index.html", index_park_info=index_park_info)
- # app route to search all parks
- @app.route("/allparks", methods=["GET", "POST"])
- @login_required
- def allparks():
- """Page with all parks"""
- if request.method == "GET":
- return render_template("allparks.html")
- # app route to see your saved parks
- @app.route("/myparks", methods=["GET", "POST"])
- @login_required
- def myparks():
- """Page with all parks"""
- if request.method == "GET":
- return render_template("myparks.html")
- # app route to see all reviews of parks
- @app.route("/reviews", methods=["GET", "POST"])
- @login_required
- def reviews():
- """Page with all parks"""
- if request.method == "GET":
- return render_template("reviews.html")
- # app route to register an account
- @app.route("/register", methods=["GET", "POST"])
- def register():
- """Register user"""
- if request.method == "GET":
- return render_template("register.html")
- else:
- errors = ["Must provide username", "Must provide password", "Password and confirmation must match", "Username is taken"]
- if not request.form.get("username"):
- return render_template("register.html", errors=errors[0])
- elif not request.form.get("password"):
- return render_template("register.html", errors=errors[1])
- elif request.form.get("password") != request.form.get("confirmation"):
- return render_template("register.html", errors=errors[2])
- rows = db.execute("SELECT username FROM users WHERE username = :username;",
- username=request.form.get("username"))
- if len(rows) != 0:
- return render_template("register.html", errors=errors[3])
- password_var = request.form.get("password")
- hash_pw = generate_password_hash(password_var)
- user_name = request.form.get("username")
- db.execute("INSERT INTO users(username,hash) VALUES (?,?);",
- user_name, hash_pw)
- success_login = ["Registered!"]
- # select the username from our db as the current session and store it as the current users logged in session, it's the first index in the list of dicts returned
- session["user_id"] = db.execute("SELECT id FROM users WHERE username = :username;",
- username=user_name) [0]["id"]
- index_park_info = send_to_index()
- return render_template("index.html", success_login=success_login[0], index_park_info=index_park_info)
- @app.route("/logout")
- def logout():
- """Log user out"""
- # Forget any user_id
- session.clear()
- # Redirect user to login form
- return redirect("/")
- @app.route("/login", methods=["GET", "POST"])
- def login():
- """Log user in"""
- # Forget any user_id
- session.clear()
- # User reached route via POST (as by submitting a form via POST)
- if request.method == "POST":
- # list of all potential errors that can be rendered on the buy html
- errors = ["Must provide a username", "Must provide password", "Invalid username and/or password"]
- # Ensure username was submitted
- if not request.form.get("username"):
- return render_template("login.html", errors=errors[0])
- # Ensure password was submitted
- elif not request.form.get("password"):
- return render_template("login.html", errors=errors[1])
- # Query database for username
- rows = db.execute("SELECT * FROM users WHERE username = :username",
- username=request.form.get("username"))
- # Ensure username exists and password is correct
- if len(rows) != 1 or not check_password_hash(rows[0]["hash"], request.form.get("password")):
- return render_template("login.html", errors=errors[2])
- # Remember which user has logged in
- session["user_id"] = rows[0]["id"]
- # Redirect user to home page
- return redirect("/")
- # User reached route via GET (as by clicking a link or via redirect)
- else:
- return render_template("login.html")
- def send_to_index():
- # Query db, joining both the all_skateparks & skateparks_location tables, to grab the park info + location of a park to send to my index.html
- index_park_info = db.execute("SELECT name, formatted_address, phone, website, location_lat, location_long FROM (SELECT * FROM all_skateparks JOIN skatepark_location ON all_skateparks.place_id = skatepark_location.place_id);")
- return index_park_info
Add Comment
Please, Sign In to add comment