app.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # app.py
  2. from flask import Flask, render_template, request, redirect, url_for, session, flash
  3. from flask_caching import Cache
  4. import requests.auth
  5. from lib.datetime import filter_accounts_current_month
  6. from lib.reqs import (get_urls, get_user_accounts)
  7. import requests
  8. import base64
  9. from flask import Flask
  10. from config import DevelopmentConfig # or ProductionConfig
  11. app = Flask(__name__)
  12. app.config.from_object(DevelopmentConfig) # Use DevelopmentConfig or ProductionConfig as needed
  13. cache = Cache(app, config={'CACHE_TYPE': 'SimpleCache'})
  14. @app.route('/')
  15. @cache.cached(timeout=60) # cache for 120 seconds
  16. def home():
  17. # If the user is logged in, redirect to a protected page like /accounts
  18. if session.get('logged_in'):
  19. base_url = app.config['BASE_URL'] # Access base_url from the config
  20. all_accounts = get_user_accounts(base_url, session['auth_credentials'])
  21. current_month_accounts = filter_accounts_current_month(all_accounts)
  22. return render_template('home.html', username=session['username'], accounts=get_user_accounts(base_url, session['auth_credentials']), current_month_accounts=current_month_accounts)
  23. return render_template('index.html')
  24. @app.route('/login', methods=['POST'])
  25. def login():
  26. username = request.form['username']
  27. password = request.form['password']
  28. # Encode the username and password in Base64
  29. credentials = f"{username}:{password}"
  30. encoded_credentials = base64.b64encode(credentials.encode()).decode()
  31. base_url = app.config['BASE_URL'] # Access base_url from the config
  32. login_url = f"{base_url}/Login" # Construct the full URL
  33. # Send GET request to the external login API with Basic Auth
  34. response = requests.get(
  35. login_url,
  36. auth=requests.auth.HTTPBasicAuth(username, password)
  37. )
  38. # Check if login was successful
  39. if response.status_code == 200 and response.json().get("auth") == "Success":
  40. # Set session variable to indicate the user is logged in
  41. session['logged_in'] = True
  42. session['username'] = username
  43. session['auth_credentials'] = encoded_credentials
  44. return redirect(url_for('home')) # Redirect to the Accounts page
  45. else:
  46. # Show error on the login page
  47. error = "Invalid username or password. Please try again."
  48. return render_template('index.html', error=error)
  49. @app.route('/urls', methods=['GET'])
  50. @cache.cached(timeout=300) # cache for 5 minutes
  51. def urls():
  52. # Check if the user is logged in
  53. if not session.get('logged_in'):
  54. return redirect(url_for('home'))
  55. # Placeholder content for Accounts page
  56. base_url = app.config['BASE_URL'] # Access base_url from the config
  57. return render_template('urls.html', urls=get_urls(base_url, session['auth_credentials']))
  58. @app.route('/accounts', methods=['GET'])
  59. @cache.cached(timeout=120) # cache for 120 seconds
  60. def user_accounts():
  61. # Check if the user is logged in
  62. if not session.get('logged_in'):
  63. return redirect(url_for('home'))
  64. # Placeholder content for Accounts page
  65. base_url = app.config['BASE_URL'] # Access base_url from the config
  66. return render_template('user_accounts.html', username=session['username'], user_accounts=get_user_accounts(base_url, session['auth_credentials']), auth=session['auth_credentials'])
  67. if __name__ == '__main__':
  68. app.run(debug=app.config['DEBUG'], host=app.config['HOST'], port=app.config['PORT'])