cool guy things
- updated app var name to be a cool guy - moved file path outside of client side cookie, oops - created logic stop back to back loading of templates
This commit is contained in:
parent
fd6994cd22
commit
9687c3e597
37
app.py
37
app.py
|
@ -2,46 +2,51 @@ import os
|
||||||
import random
|
import random
|
||||||
from flask import Flask, send_from_directory, session, render_template
|
from flask import Flask, send_from_directory, session, render_template
|
||||||
|
|
||||||
app = Flask(__name__)
|
Fyourday = Flask(__name__)
|
||||||
app.config['TRAP_HTTP_EXCEPTIONS']=True
|
Fyourday.config['TRAP_HTTP_EXCEPTIONS']=True
|
||||||
app.secret_key = '420-69-LOL' # For using client side session cookies
|
Fyourday.secret_key = '420-69-LOL' # For using client side session cookies
|
||||||
|
# Script must run from root dir containing all websites dirs
|
||||||
|
# OR change the ROOT_DIR path :)
|
||||||
|
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
# Site choosing logic
|
# Site choosing logic
|
||||||
def current_website_dir():
|
def current_website_dir():
|
||||||
# Script must run from root dir containing all websites dirs
|
|
||||||
# OR change the ROOT_DIR path :)
|
|
||||||
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
||||||
WEBSITE_DIRS = [name for name in os.listdir(ROOT_DIR)
|
WEBSITE_DIRS = [name for name in os.listdir(ROOT_DIR)
|
||||||
if not name.startswith('.')
|
if not name.startswith('.')
|
||||||
and os.path.isdir(os.path.join(ROOT_DIR, name))]
|
and os.path.isdir(os.path.join(ROOT_DIR, name))]
|
||||||
session.pop('website_dir', None) # Clear website_dir if exist
|
|
||||||
match session['requests']: #Match Nth page request
|
match session['requests']: #Match Nth page request
|
||||||
case 5:
|
case 5:
|
||||||
session['website_dir'] = (os.path.join(ROOT_DIR, 'Marvel'))
|
session['website_dir'] = 'Marvel'
|
||||||
case 6:
|
case 6:
|
||||||
session['website_dir'] = (os.path.join(ROOT_DIR, 'Escape'))
|
session['website_dir'] = 'Escape'
|
||||||
session.pop('requests', None) # Reset request count
|
session.pop('requests', None) # Reset request count
|
||||||
case _:
|
case _:
|
||||||
for dir in WEBSITE_DIRS:
|
for dir in WEBSITE_DIRS:
|
||||||
match dir: # Remove request specific pages
|
match dir: # Remove request specific pages
|
||||||
case 'Escape' | 'Marvel' | 'templates':
|
case 'Escape' | 'Marvel' | 'templates':
|
||||||
WEBSITE_DIRS.remove(dir)
|
WEBSITE_DIRS.remove(dir)
|
||||||
session['website_dir'] = random.choice(WEBSITE_DIRS)
|
website_choice = random.choice(WEBSITE_DIRS)
|
||||||
|
if 'website_dir' in session: # Don't serve templates back to back
|
||||||
|
while website_choice == session['website_dir']:
|
||||||
|
website_choice = random.choice(WEBSITE_DIRS)
|
||||||
|
session['website_dir'] = website_choice
|
||||||
|
else:
|
||||||
|
session['website_dir'] = website_choice
|
||||||
|
|
||||||
@app.route('/<path:filename>', methods=['GET']) # Make static files available
|
@Fyourday.route('/<path:filename>', methods=['GET']) # Make static files available
|
||||||
def static_proxy(filename):
|
def static_proxy(filename):
|
||||||
return send_from_directory(session['website_dir'], filename)
|
return send_from_directory(os.path.join(ROOT_DIR, session['website_dir']), filename)
|
||||||
|
|
||||||
@app.route('/', methods=['GET']) # Serve site index.html
|
@Fyourday.route('/', methods=['GET']) # Serve site index.html
|
||||||
def index():
|
def index():
|
||||||
if 'requests' in session: # init requests count
|
if 'requests' in session: # init requests count
|
||||||
session['requests'] += 1
|
session['requests'] += 1
|
||||||
else:
|
else:
|
||||||
session['requests'] = 1
|
session['requests'] = 1
|
||||||
current_website_dir() # Choose website dir
|
current_website_dir() # Choose website dir
|
||||||
return send_from_directory(session['website_dir'], 'index.html')
|
return send_from_directory(os.path.join(ROOT_DIR, session['website_dir']), 'index.html')
|
||||||
|
|
||||||
@app.errorhandler(Exception) # Handle uncaught exceptions per code type
|
@Fyourday.errorhandler(Exception) # Handle uncaught exceptions per code
|
||||||
def handle_error(e):
|
def handle_error(e):
|
||||||
try:
|
try:
|
||||||
if e.code < 400:
|
if e.code < 400:
|
||||||
|
@ -53,4 +58,4 @@ def handle_error(e):
|
||||||
return render_template('500.html', error='500'),500
|
return render_template('500.html', error='500'),500
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run()
|
Fyourday.run()
|
||||||
|
|
Loading…
Reference in New Issue