|
@@ -0,0 +1,73 @@
|
|
|
+# app.py
|
|
|
+from flask import Flask, render_template, request, redirect, url_for, session, flash
|
|
|
+import requests.auth
|
|
|
+from lib.datetime import filter_accounts_current_month
|
|
|
+from lib.reqs import (get_urls, get_user_accounts)
|
|
|
+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
|
|
|
+
|
|
|
+@app.route('/')
|
|
|
+def home():
|
|
|
+ # If the user is logged in, redirect to a protected page like /accounts
|
|
|
+ if session.get('logged_in'):
|
|
|
+ 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('index.html')
|
|
|
+
|
|
|
+@app.route('/login', methods=['POST'])
|
|
|
+def login():
|
|
|
+ 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
|
|
|
+ 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)
|
|
|
+ )
|
|
|
+
|
|
|
+ # 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
|
|
|
+ else:
|
|
|
+ # Show error on the login page
|
|
|
+ error = "Invalid username or password. Please try again."
|
|
|
+ return render_template('index.html', error=error)
|
|
|
+
|
|
|
+@app.route('/urls', methods=['GET'])
|
|
|
+def urls():
|
|
|
+ # Check if the user is logged in
|
|
|
+ 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']))
|
|
|
+
|
|
|
+@app.route('/accounts', methods=['GET'])
|
|
|
+def user_accounts():
|
|
|
+ # Check if the user is logged in
|
|
|
+ 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']))
|
|
|
+
|
|
|
+if __name__ == '__main__':
|
|
|
+ app.run(debug=app.config['DEBUG'], host=app.config['HOST'], port=app.config['PORT'])
|