From 4729e0995e526e0ee723c163cf9ba672a0e4cf54 Mon Sep 17 00:00:00 2001 From: conroy-cheers Date: Tue, 5 Nov 2019 20:17:33 +1100 Subject: [PATCH 1/2] Add custom base URL, configurable with environment variable APP_BASE_URL --- app.py | 55 ++++++++++++++++++++---------------- functions.py | 3 ++ templates/confirm_price.html | 2 +- templates/index.html | 4 +-- templates/price.html | 8 +++--- 5 files changed, 41 insertions(+), 31 deletions(-) diff --git a/app.py b/app.py index 6affe6d..b2d55c4 100644 --- a/app.py +++ b/app.py @@ -20,6 +20,7 @@ from flask import Flask, render_template, request, redirect, url_for, session, flash +from flask.blueprints import Blueprint # Optional Security # Uncomment Basic Auth section to enable basic authentication so users will be prompted a username and password before seeing the website. @@ -56,8 +57,9 @@ print("Note: You have not set a device ID. A random one will be set when you login.") +app_blueprint = Blueprint('7eleven', __name__, template_folder='templates', url_prefix=functions.APP_BASE_URL, + static_url_path=functions.APP_BASE_URL or '' + '/static') -app = Flask(__name__) # Uncomment to enable basic authentication #app.config['BASIC_AUTH_FORCE'] = True @@ -65,7 +67,7 @@ #app.config['BASIC_AUTH_PASSWORD'] = 'lockit' #basic_auth = BasicAuth(app) -@app.route('/') +@app_blueprint.route('/') def index(): # If they have pressed the refresh link remove the error and success messages @@ -99,7 +101,7 @@ def index(): -@app.route('/login', methods=['POST', 'GET']) +@app_blueprint.route('/login', methods=['POST', 'GET']) def login(): # Clear the error and success message session.pop('ErrorMessage', None) @@ -144,7 +146,7 @@ def login(): # If there was an error logging in, redirect to the index page with the 7Eleven response if(returnContent['Message']): session['ErrorMessage'] = returnContent['Message'] - return redirect(url_for('index')) + return redirect(url_for('.index')) except: @@ -195,13 +197,13 @@ def login(): with open('./autolock.ini', 'w') as configfile: config.write(configfile) - return redirect(url_for('index')) + return redirect(url_for('.index')) else: # They didn't submit a POST request, so we will redirect to index - return redirect(url_for('index')) + return redirect(url_for('.index')) -@app.route('/logout') +@app_blueprint.route('/logout') def logout(): # The logout payload is an empty string but it is still needed @@ -223,10 +225,10 @@ def logout(): # Clear all of the previously set session variables and then redirect to the index page session.clear() - return redirect(url_for('index')) + return redirect(url_for('.index')) # The confirmation page for a manual lock in -@app.route('/confirm') +@app_blueprint.route('/confirm') def confirm(): # See if we have a temporary lock in price try: @@ -236,11 +238,11 @@ def confirm(): except: # We haven't started locking in yet, show an error and redirect to the index session['ErrorMessage'] = "You haven't started the lock in process yet. Please try again." - return redirect(url_for('index')) + return redirect(url_for('.index')) # Save the auto lock in settings -@app.route('/save_settings', methods=['POST']) +@app_blueprint.route('/save_settings', methods=['POST']) def save_settings(): # If we have sent the form if request.method == 'POST': @@ -259,18 +261,18 @@ def save_settings(): config.set('General', 'max_price', request.form['max_price']) else: session['ErrorMessage'] = "The price you tried to lock in was either too cheap or too expensive. It should be between 100 and 200cents" - return redirect(url_for('index')) + return redirect(url_for('.index')) # Save the config file with open('./autolock.ini', 'w') as configfile: config.write(configfile) session['SuccessMessage'] = "Settings saved succesfully." - return redirect(url_for('index')) + return redirect(url_for('.index')) -@app.route('/lockin', methods=['POST', 'GET']) +@app_blueprint.route('/lockin', methods=['POST', 'GET']) def lockin(): if request.method == 'POST': # Variable used to search for a manual price @@ -295,7 +297,7 @@ def lockin(): # They tried to do something different from the manual and automatic form, so throw up an error if(submissionMethod != "manual" and submissionMethod != "automatic"): session['ErrorMessage'] = "Invalid form submission. Either use the manual or automatic one on the main page." - return redirect(url_for('index')) + return redirect(url_for('.index')) # If they have manually chosen a postcode/suburb set the price overide to true if(submissionMethod == "manual"): @@ -316,7 +318,7 @@ def lockin(): if not custom_coords: # If it is, get the error message and return back to the index session['ErrorMessage'] = "Error: You entered a manual postcode where no 7-Eleven store is. Please set a Google Maps API key or enter a valid postcode." - return redirect(url_for('index')) + return redirect(url_for('.index')) # Initiate the Google Maps API gmaps = googlemaps.Client(key = API_KEY) # Get the longitude and latitude from the submitted postcode @@ -360,7 +362,7 @@ def lockin(): try: if returnContent['ErrorType'] == 0: session['ErrorMessage'] = "An error has occured. This is most likely due to a fuel lock already being in place." - return redirect(url_for('index')) + return redirect(url_for('.index')) except: pass @@ -379,10 +381,10 @@ def lockin(): if not(priceOveride): if not(float(LockinPrice) <= float(locationResult[1])): session['ErrorMessage'] = "The fuel price is too high compared to the cheapest available. The cheapest we found was at " + locationResult[0] + ". Try locking in there!" - return redirect(url_for('index')) + return redirect(url_for('.index')) if(priceOveride): - return redirect(url_for('confirm')) + return redirect(url_for('.confirm')) # Now we want to lock in the maximum litres we can. NumberOfLitres = int(float(session['cardBalance']) / session['LockinPrice'] * 100) @@ -412,7 +414,7 @@ def lockin(): if(returnContent['Message']): # If it is, get the error message and return back to the index session['ErrorMessage'] = returnContent['Message'] - return redirect(url_for('index')) + return redirect(url_for('.index')) # Otherwise we most likely locked in the price! if(returnContent['Status'] == "0"): # Update the fuel prices that are locked in @@ -424,7 +426,7 @@ def lockin(): # Pop our fueltype and lock in price variables session.pop('fuelType', None) session.pop('LockinPrice', None) - return redirect(url_for('index')) + return redirect(url_for('.index')) # For whatever reason it saved our lock in anyway and return to the index page except: @@ -437,11 +439,16 @@ def lockin(): # Pop our fueltype and lock in price variables session.pop('fuelType', None) session.pop('LockinPrice', None) - return redirect(url_for('index')) + return redirect(url_for('.index')) else: # They just tried to load the lock in page without sending any data session['ErrorMessage'] = "Unknown error occured. Please try again!" - return redirect(url_for('index')) + return redirect(url_for('.index')) + + +app = Flask(__name__, static_url_path=functions.APP_BASE_URL or '' + '/static') +app.register_blueprint(app_blueprint) + if __name__ == '__main__': # Try and open stores.json @@ -475,7 +482,7 @@ def lockin(): # Check if the autolock.ini file exists, if it doesn't create it. if not (os.path.exists("./autolock.ini")): autolocker.create_ini() - + # Open the config file and read the settings config = configparser.ConfigParser() config.read("./autolock.ini") diff --git a/functions.py b/functions.py index 114c7aa..c00645c 100644 --- a/functions.py +++ b/functions.py @@ -43,6 +43,9 @@ OS_VERSION = os.getenv('OS_VERSION', settings.OS_VERSION) APP_VERSION = os.getenv('APP_VERSION', settings.APP_VERSION) +# Base URL for app. e.g. APP_BASE_URL='/7eleven' will put the index page at /7eleven/index instead of /index +APP_BASE_URL = os.getenv('APP_BASE_URL', '').rstrip('/') or None + def getKey(): # Found in file au.com.seveneleven.y.h a = [103, 180, 267, 204, 390, 504, 497, 784, 1035, 520, 1155, 648, 988, 1456, 1785] diff --git a/templates/confirm_price.html b/templates/confirm_price.html index 24525ac..ab90221 100644 --- a/templates/confirm_price.html +++ b/templates/confirm_price.html @@ -1,7 +1,7 @@ {% extends "index.html" %} {% block content %}
-
+




diff --git a/templates/index.html b/templates/index.html index f36a852..4dae617 100644 --- a/templates/index.html +++ b/templates/index.html @@ -93,7 +93,7 @@

Cents per litre: {{ session['fuelLockCPL'] }}

Please enter your email and password below:

- +





@@ -121,7 +121,7 @@

Refresh

{% if session['accountID'] %} diff --git a/templates/price.html b/templates/price.html index e0e271b..069a48f 100644 --- a/templates/price.html +++ b/templates/price.html @@ -4,7 +4,7 @@
- +
These settings are for the automatic fuel lock function. Proceed with caution, you will not be warned when a fuel lock is found.


@@ -22,7 +22,7 @@
These settings are for the a {% else %}
- +