Skip to content
This repository was archived by the owner on Jan 4, 2021. It is now read-only.

Custom base URL support, configurable by environment variable #40

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -56,16 +57,17 @@
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
#app.config['BASIC_AUTH_USERNAME'] = 'petrol'
#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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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':
Expand All @@ -254,8 +256,8 @@ def save_settings():
if (float(request.form['max_price']) > 80 and float(request.form['max_price']) < 200):
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 80 cents and 2 dollars"
return redirect(url_for('index'))
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'))

else:
config.set('General', 'auto_lock_enabled', "False")
Expand All @@ -267,12 +269,12 @@ def save_settings():
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
Expand All @@ -297,7 +299,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"):
Expand All @@ -318,7 +320,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
Expand Down Expand Up @@ -362,7 +364,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

Expand All @@ -381,10 +383,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(150)
Expand Down Expand Up @@ -414,7 +416,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
Expand All @@ -426,7 +428,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:
Expand All @@ -439,11 +441,17 @@ 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)
print(app.url_map)


if __name__ == '__main__':
# Try and open stores.json
Expand Down
3 changes: 3 additions & 0 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
APP_VERSION = os.getenv('APP_VERSION', settings.APP_VERSION)
USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:71.0) Gecko/20100101 Firefox/71.0"

# 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]
Expand Down
2 changes: 1 addition & 1 deletion templates/confirm_price.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends "index.html" %}
{% block content %}
<div class="confirm_price" align="center">
<form class="form-inline mt-2 mt-md-0" action="/lockin" method="POST">
<form class="form-inline mt-2 mt-md-0" action="{{ url_for('.lockin') }}" method="POST">
<div class="form-group">
<label for="fueltype">The price per litre will be {{ session['LockinPrice'] }}c.</label></br></br>
<button class="btn btn-default" name="submit" value="confirm_price" type="submit" onclick="loader.style.display = 'block'">Confirm the price!</button><br><br>
Expand Down
10 changes: 5 additions & 5 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<!-- //for-mobile-apps -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link href="../static/css/style.css" rel="stylesheet" type="text/css" media="all" />
<link href="static/css/style.css" rel="stylesheet" type="text/css" media="all" />
<link href='//fonts.googleapis.com/css?family=Open+Sans:400,300,300italic,400italic,600,600italic,700,700italic,800,800italic' rel='stylesheet' type='text/css'>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Expand Down Expand Up @@ -103,7 +103,7 @@ <h1>7Eleven Fuel Locker</h1>
<div class="pic-sec">
{% if session['accountID'] %}
<div class="pic">
<img src="../static/images/7-eleven_logo.svg" alt=""/>
<img src="static/images/7-eleven_logo.svg" alt=""/>
</div>
<div class="pic_info">
<h2>Welcome, {{ session['firstName'] }}.</h2>
Expand Down Expand Up @@ -134,11 +134,11 @@ <h3>You purchased up to <strong class='bold_balance'>150</strong> litres at <str
<div class="clear"></div>
{% else %}
<div class="pic">
<img src="../static/images/7-eleven_logo.svg" alt=""/>
<img src="static/images/7-eleven_logo.svg" alt=""/>
</div>
<div class="pic_info">
<h2>Please enter your email and password below:<br><br></h2>
<form class="form-inline mt-2 mt-md-0" action="/login" method="POST">
<form class="form-inline mt-2 mt-md-0" action="{{ url_for('.login') }}" method="POST">
<input class="form-control mr-sm-2" type="text" placeholder="Email" name="email"><br><br>
<input class="form-control mr-sm-2" type="password" placeholder="Password" name="password"><br><br>
<input class="form-control mr-sm-2" type="text" placeholder="Device ID (can be blank)" value="{{ device_id }}" maxlength="16" name="device_id"><br><br>
Expand Down Expand Up @@ -166,7 +166,7 @@ <h3><a href="?action=refresh">Refresh</a></h3>
</div>
{% if session['accountID'] %}
<div class="text-center">
<a href="/logout" class="text-danger">
<a href="{{ url_for('.logout') }}" class="text-danger">
<span>LOGOUT</span> <span class="glyphicon glyphicon-log-out" aria-hidden="true"></span>
</a>
</div>
Expand Down
8 changes: 4 additions & 4 deletions templates/price.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="automanual" align="center"><a id="aset" href="#">Settings</a> | <a id="acredits" href="#">Credits</a><hr></div>

<div id="settings">
<form class="form-inline mt-2 mt-md-0" action="/save_settings" method="POST">
<form class="form-inline mt-2 mt-md-0" action="{{ url_for('.save_settings') }}" method="POST">
<h6 style="font-style: italic; margin-bottom: 2ch;">These settings are for the automatic fuel lock function. Proceed with caution, you will not be warned when a fuel lock is found.</h6>
<div class="form-group">
<label for="fueltype">Select a fuel type you wish to auto lock in and the maximum price</label><br><br>
Expand All @@ -31,7 +31,7 @@ <h6 style="font-style: italic; margin-bottom: 2ch;">These settings are for the a
{% else %}
<div class="automanual" align="center"><a id="aauto" href="#">Automatic</a> | <a id="aman" href="#">Manual</a> | <a id="aset" href="#">Settings</a> | <a id="acredits" href="#">Credits</a><hr></div>
<div id="automatic">
<form class="form-inline mt-2 mt-md-0" action="/lockin" method="POST">
<form class="form-inline mt-2 mt-md-0" action="{{ url_for('.lockin') }}" method="POST">
<div class="form-group">
<label for="fueltype">Which fuel do you want?</label>
<select id="fueltype" name="fueltype" class="custom-select">
Expand All @@ -54,7 +54,7 @@ <h5 id="53"> Special Diesel cheapest price found was <strong class='bold_balance
<h5 id="54"> Autogas cheapest price found was <strong class='bold_balance'>{{ session['price5'] }}</strong> at <strong class='bold_balance'>{{ session['postcode5'] }}</strong></h5>
</div>
<div id="manual">
<form class="form-inline mt-2 mt-md-0" action="/lockin" method="POST">
<form class="form-inline mt-2 mt-md-0" action="{{ url_for('.lockin') }}" method="POST">
<h6 style="font-style: italic; margin-bottom: 2ch;">Note: Searching manually lets you choose a custom postcode for where you want to lock in fuel from. You will land on a confirmation page where you can decide if you want to continue locking in that price or not.</h6>
<div class="form-group">
<label for="fueltype">Which fuel do you want?</label>
Expand All @@ -73,7 +73,7 @@ <h6 style="font-style: italic; margin-bottom: 2ch;">Note: Searching manually let
</form>
</div>
<div id="settings">
<form class="form-inline mt-2 mt-md-0" action="/save_settings" method="POST">
<form class="form-inline mt-2 mt-md-0" action="{{ url_for('.save_settings') }}" method="POST">
<h6 style="font-style: italic; margin-bottom: 2ch;">These settings are for the automatic fuel lock function. Proceed with caution, you will not be warned when a fuel lock is found.</h6>
<div class="form-group">
<label for="fueltype">What is the maximum you want to pay for Premium 98 in cents? </label><br><br>
Expand Down