Karl Hudgell il y a 4 semaines
Parent
commit
860f6ab356
2 fichiers modifiés avec 88 ajouts et 56 suppressions
  1. 88 56
      app.py
  2. 0 0
      static/favicon.ico

+ 88 - 56
app.py

@@ -2,109 +2,141 @@
 from flask import Flask, render_template, request, redirect, url_for, session, flash
 from flask_caching import Cache
 import requests.auth
+import os
 from lib.datetime import filter_accounts_current_month
-from lib.reqs import (get_urls, get_user_accounts, add_user_account, delete_user_account)
+from lib.reqs import get_urls, get_user_accounts, add_user_account, delete_user_account
+from flask import send_from_directory
 import requests
 import base64
 from flask import Flask
 from config import DevelopmentConfig  # or ProductionConfig
 
 app = Flask(__name__)
-app.config.from_object(DevelopmentConfig)  # Use DevelopmentConfig or ProductionConfig as needed
-cache = Cache(app, config={'CACHE_TYPE': 'SimpleCache'})
+app.config.from_object(
+    DevelopmentConfig
+)  # Use DevelopmentConfig or ProductionConfig as needed
+cache = Cache(app, config={"CACHE_TYPE": "SimpleCache"})
 
-@app.route('/')
+
+@app.route("/favicon.ico")
+def favicon():
+    return send_from_directory(
+        os.path.join(app.root_path, "static"),
+        "favicon.ico",
+        mimetype="image/vnd.microsoft.icon",
+    )
+
+
+@app.route("/")
 def index():
     # If the user is logged in, redirect to a protected page like /accounts
-    if session.get('logged_in'):
-        return redirect(url_for('home'))
-    return render_template('index.html')
+    if session.get("logged_in"):
+        return redirect(url_for("home"))
+    return render_template("index.html")
+
 
-@app.route('/home')
+@app.route("/home")
 @cache.cached(timeout=60)  # cache for 120 seconds
 def home():
-    base_url = app.config['BASE_URL']  # Access base_url from the config
-    all_accounts = get_user_accounts(base_url, session['auth_credentials'])
+    base_url = app.config["BASE_URL"]  # Access base_url from the config
+    all_accounts = get_user_accounts(base_url, session["auth_credentials"])
     current_month_accounts = filter_accounts_current_month(all_accounts)
-    return render_template('home.html',  username=session['username'], accounts=get_user_accounts(base_url, session['auth_credentials']), current_month_accounts=current_month_accounts)
+    return render_template(
+        "home.html",
+        username=session["username"],
+        accounts=get_user_accounts(base_url, session["auth_credentials"]),
+        current_month_accounts=current_month_accounts,
+    )
+
 
-@app.route('/login', methods=['POST'])
+@app.route("/login", methods=["POST"])
 def login():
-    username = request.form['username']
-    password = request.form['password']
-    
+    username = request.form["username"]
+    password = request.form["password"]
+
     # Encode the username and password in Base64
     credentials = f"{username}:{password}"
     encoded_credentials = base64.b64encode(credentials.encode()).decode()
-    
-    base_url = app.config['BASE_URL']  # Access base_url from the config
+
+    base_url = app.config["BASE_URL"]  # Access base_url from the config
     login_url = f"{base_url}/Login"  # Construct the full URL
 
     # Send GET request to the external login API with Basic Auth
     response = requests.get(
-        login_url,
-        auth=requests.auth.HTTPBasicAuth(username, password)
+        login_url, auth=requests.auth.HTTPBasicAuth(username, password)
     )
 
     # Check if login was successful
     if response.status_code == 200 and response.json().get("auth") == "Success":
         # Set session variable to indicate the user is logged in
-        session['logged_in'] = True
-        session['username'] = username
-        session['auth_credentials'] = encoded_credentials
-        return redirect(url_for('home'))  # Redirect to the Accounts page
+        session["logged_in"] = True
+        session["username"] = username
+        session["auth_credentials"] = encoded_credentials
+        return redirect(url_for("home"))  # Redirect to the Accounts page
     else:
         # Show error on the login page
         error = "Invalid username or password. Please try again."
-        return render_template('index.html', error=error)
+        return render_template("index.html", error=error)
 
-@app.route('/urls', methods=['GET'])
+
+@app.route("/urls", methods=["GET"])
 @cache.cached(timeout=300)  # cache for 5 minutes
 def urls():
     # Check if the user is logged in
-    if not session.get('logged_in'):
-        return redirect(url_for('home'))
+    if not session.get("logged_in"):
+        return redirect(url_for("home"))
     # Placeholder content for Accounts page
-    base_url = app.config['BASE_URL']  # Access base_url from the config
-    return render_template('urls.html', urls=get_urls(base_url, session['auth_credentials']))
+    base_url = app.config["BASE_URL"]  # Access base_url from the config
+    return render_template(
+        "urls.html", urls=get_urls(base_url, session["auth_credentials"])
+    )
 
-@app.route('/accounts', methods=['GET'])
+
+@app.route("/accounts", methods=["GET"])
 @cache.cached(timeout=120)  # cache for 120 seconds
 def user_accounts():
     # Check if the user is logged in
-    if not session.get('logged_in'):
-        return redirect(url_for('home'))
+    if not session.get("logged_in"):
+        return redirect(url_for("home"))
     # Placeholder content for Accounts page
-    base_url = app.config['BASE_URL']  # Access base_url from the config    
-    return render_template('user_accounts.html', username=session['username'], user_accounts=get_user_accounts(base_url, session['auth_credentials']), auth=session['auth_credentials'])
+    base_url = app.config["BASE_URL"]  # Access base_url from the config
+    return render_template(
+        "user_accounts.html",
+        username=session["username"],
+        user_accounts=get_user_accounts(base_url, session["auth_credentials"]),
+        auth=session["auth_credentials"],
+    )
 
 
-@app.route('/accounts/add', methods=['GET', 'POST'])
+@app.route("/accounts/add", methods=["GET", "POST"])
 def add_account():
-    base_url = app.config['BASE_URL']  # Access base_url from the config
-    if request.method == 'POST':
-        username = request.form['username']
-        password = request.form['password']
-        stream = request.form['stream']
-        
-        if add_user_account(base_url, session['auth_credentials'], username, password, stream):
+    base_url = app.config["BASE_URL"]  # Access base_url from the config
+    if request.method == "POST":
+        username = request.form["username"]
+        password = request.form["password"]
+        stream = request.form["stream"]
+
+        if add_user_account(
+            base_url, session["auth_credentials"], username, password, stream
+        ):
             cache.clear()  # Clears all cache entries
-            return redirect(url_for('user_accounts'))
-        return render_template('add_account.html')
-    
-    return render_template('add_account.html')
+            return redirect(url_for("user_accounts"))
+        return render_template("add_account.html")
+
+    return render_template("add_account.html")
 
-@app.route('/accounts/delete', methods=['POST'])
+
+@app.route("/accounts/delete", methods=["POST"])
 def delete_account():
-    stream = request.form.get('stream')
-    username = request.form.get('username')
-    base_url = app.config['BASE_URL']
-    
-    if delete_user_account(base_url,session['auth_credentials'], stream, username):
+    stream = request.form.get("stream")
+    username = request.form.get("username")
+    base_url = app.config["BASE_URL"]
+
+    if delete_user_account(base_url, session["auth_credentials"], stream, username):
         cache.clear()  # Clears all cache entries
-        return redirect(url_for('user_accounts'))
-    return redirect(url_for('user_accounts'))
+        return redirect(url_for("user_accounts"))
+    return redirect(url_for("user_accounts"))
 
 
-if __name__ == '__main__':
-    app.run(debug=app.config['DEBUG'], host=app.config['HOST'], port=app.config['PORT'])
+if __name__ == "__main__":
+    app.run(debug=app.config["DEBUG"], host=app.config["HOST"], port=app.config["PORT"])

+ 0 - 0
favicon.ico → static/favicon.ico