diff --git a/.gitignore b/.gitignore index 4a4c19a..8f5b1e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .venv config.yaml +shelf \ No newline at end of file diff --git a/app.py b/app.py index a974305..ca0fef1 100644 --- a/app.py +++ b/app.py @@ -1,14 +1,31 @@ import subprocess +import requests +import time from flask import Flask, render_template, request, redirect, url_for, session from authlib.integrations.flask_client import OAuth from time import sleep from dynaconf import Dynaconf import logging +import shelve logger = logging.getLogger(__name__) config = Dynaconf(settings_files = ['config.yaml']) +# Setup shelf. +with shelve.open('shelf', 'c', writeback=True) as shelf: + if 'logins' not in shelf.keys(): + shelf['logins'] = {} + + if 'checkins' not in shelf.keys(): + shelf['checkins'] = {} + + if 'admissions' not in shelf.keys(): + shelf['admissions'] = {} + + + + class Pinctrl: def __init__(self): pass @@ -31,6 +48,7 @@ class Pinctrl: pinctrl = Pinctrl() app = Flask(__name__) +app.secret_key = config.oauth.secret_key oauth = OAuth(app) oauth.register( @@ -43,24 +61,71 @@ for led, pin in config.pinout.led.items(): pinctrl.output(pin) pinctrl.off(pin) + @app.route('/') def homepage(): user = session.get('user') - return render_template('index.html', user=user) + if not user: + return redirect(url_for('login')) + + logging.info(f"User {user} is logged in.") + with shelve.open('shelf', 'r') as shelf: + return render_template( + 'index.html', + user=user, + logins=shelf.get('logins', {}), + checkins=shelf.get('checkins', {}), + admissions=shelf.get('admissions', {}), + ) + + @app.route('/login') def login(): + logging.info("Login requested") redirect_uri = url_for('auth', _external=True) return oauth.jimlab.authorize_redirect(redirect_uri) - @app.route('/auth') def auth(): token = oauth.jimlab.authorize_access_token() - session['user'] = token['userinfo'] + logger.critical(f"Token received: {token}") + + # Fetch user info + resp = oauth.jimlab.get('https://auth.jimlab.io/application/o/userinfo/') # or the correct user info endpoint + userinfo = resp.json() + logger.critical(f"Userinfo: {userinfo}") + + session['user'] = userinfo + + # Log the login. + with shelve.open('shelf', 'w', writeback=True) as shelf: + username = userinfo.get('preferred_username', 'unknown') + if username not in shelf['logins']: + shelf['logins'][username] = [] + shelf['logins'][username].append((userinfo, time.time())) + return redirect('/') +@app.route('/checkin', methods=['POST']) +def check_in(): + + if not session.get('user'): + return redirect(url_for('login')) + user = session['user'] + username = user.get('preferred_username', 'unknown') + logger.info(f"Check-in requested by user: {username}") + + with shelve.open('shelf', 'w', writeback=True) as shelf: + if username not in shelf['checkins']: + shelf['checkins'][username] = [] + shelf['checkins'][username].append(time.time()) + + return redirect(url_for('homepage')) + + + # @app.route('/') # def index(): # logger.info(oauth.jimlab.get('user')) @@ -106,4 +171,4 @@ if __name__ == '__main__': for led, pin in config.pinout.led.items(): pinctrl.off(pin) -#with shelve.open('db', 'w', writeback=True) as db \ No newline at end of file + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index dde9d99..fe95fcf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ flask authlib +requests dynaconf \ No newline at end of file diff --git a/templates/base.html b/templates/base.html deleted file mode 100644 index d9f2469..0000000 --- a/templates/base.html +++ /dev/null @@ -1,10 +0,0 @@ - - -
- -Visitors are currently {{ status }}
-{% endblock %} \ No newline at end of file + +| Visitor Name | +Check-in Time | +
|---|---|
| {{ username }} | +{{ checkins[username][-1] }} | +