Compare commits
No commits in common. "b0f065d976d5ec72e3ee6e08a629dc7c1b9f8c1c" and "36c129b9391542099e00af591c9afdb11d25c2a8" have entirely different histories.
b0f065d976
...
36c129b939
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,2 @@
|
||||
.venv
|
||||
config.yaml
|
||||
shelf
|
||||
@ -1,31 +1,14 @@
|
||||
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
|
||||
@ -48,7 +31,6 @@ class Pinctrl:
|
||||
pinctrl = Pinctrl()
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = config.oauth.secret_key
|
||||
oauth = OAuth(app)
|
||||
|
||||
oauth.register(
|
||||
@ -61,71 +43,24 @@ for led, pin in config.pinout.led.items():
|
||||
pinctrl.output(pin)
|
||||
pinctrl.off(pin)
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def homepage():
|
||||
user = session.get('user')
|
||||
if not user:
|
||||
return redirect(url_for('login'))
|
||||
|
||||
logging.info(f"User {user} is logged in.")
|
||||
return render_template('index.html', user=user)
|
||||
|
||||
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()
|
||||
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()))
|
||||
|
||||
session['user'] = token['userinfo']
|
||||
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'))
|
||||
@ -171,4 +106,4 @@ if __name__ == '__main__':
|
||||
for led, pin in config.pinout.led.items():
|
||||
pinctrl.off(pin)
|
||||
|
||||
|
||||
#with shelve.open('db', 'w', writeback=True) as db
|
||||
@ -1,4 +1,3 @@
|
||||
flask
|
||||
authlib
|
||||
requests
|
||||
dynaconf
|
||||
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 876 KiB |
10
templates/base.html
Normal file
10
templates/base.html
Normal file
@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>{{ title }}</title>
|
||||
</head>
|
||||
<body style="text-align: center;">
|
||||
{% block content %}{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
@ -1,32 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Bellflower</title>
|
||||
</head>
|
||||
<body style="text-align: center;">
|
||||
<h1>Welcome, {{user.name}}</h1>
|
||||
{% extends "base.html" %}
|
||||
{% set title = "Visitor Check-in System" %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Welcome to Visitor Check-in System</h1>
|
||||
<p>Visitors are currently {{ status }}</p>
|
||||
<form action="/checkin" method="post">
|
||||
<input type="submit" value="Check In Visitor">
|
||||
</form>
|
||||
|
||||
<table id="checkins">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Visitor Name</th>
|
||||
<th>Check-in Time</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for username in checkins.keys() %}
|
||||
<tr>
|
||||
<td>{{ username }}</td>
|
||||
<td>{{ checkins[username][-1] }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
{% endblock %}
|
||||
11
templates/status.html
Normal file
11
templates/status.html
Normal file
@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Status</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Current Status: {{ status }}</h1>
|
||||
<a href="/">Go back to main page</a>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
x
Reference in New Issue
Block a user