frc_timecards/app/views.py
Quantum adced1e3c1 Fixed timekeeping + header color
pretty sure it was the indent, not the est but whatever, I did both
2024-10-03 23:51:36 -04:00

271 lines
8 KiB
Python

from app import app, db
from app.tables import Swipe, User
from flask import request, redirect, url_for, render_template
from sqlalchemy import desc
from pytz import timezone
from datetime import datetime
from datetime import date
import json
# from flask_login import login_user, login_required, logout_user
after_date = '2024-08-22'
@app.route('/')
def index():
return 'No.'
'''
@app.route('/auth', methods=['POST'])
def auth():
data = request.get_json(force=True)
print(data)
if 'uuid' in data:
print(data['uuid'])
uuid = AllowedUUIDs.query.filter_by(uuid=data['uuid']).first()
print(uuid)
if uuid:
print('login')
login_user(uuid)
return ''
'''
@app.route('/users', methods=['GET'])
def users():
users = db.session.query(User)
return render_template('users.html', users=users)
@app.route('/users/modify', methods=['POST'])
def users_modify():
data = request.form
if data['action'] == 'swipe':
swipe = Swipe(data['uuid'])
db.session.add(swipe)
db.session.commit()
if data['action'] == 'set_name':
if data['first_name'] == '' or data['last_name'] == '':
return 'Fill out the form...'
if data['uuid'] == '0000005222':
return 'No.'
user = User.query.filter_by(uuid=data['uuid']).first()
user.first_name = data['first_name']
user.last_name = data['last_name']
db.session.commit()
elif data['action'] == 'set_uid':
if data['uuid'] == '':
return 'Fill out the form...'
if data['uuid'] == '0000005222':
return 'No.'
user = User.query.filter_by(uuid=data['uuid']).first()
user.uuid = data['new_uuid']
db.session.commit()
elif data['action'] == 'delete':
if data['uuid'] == '0000005222':
return 'No.'
user = User.query.filter_by(uuid=data['uuid']).first()
db.session.delete(user)
db.session.commit()
elif data['action'] == 'new_user':
if data['new_uuid'] == '0000005222':
return 'No.'
user = User()
user.uuid = data['new_uuid']
user.first_name = data['first_name']
user.last_name = data['last_name']
user.role = data['role']
db.session.add(user)
db.session.commit()
return redirect(url_for('users'), code=302)
@app.route('/swipe', methods=['POST'])
def swipe():
data = request.get_json(force=True)
uuid = data.get('uuid')
if not uuid:
return 'NACK'
print(uuid)
swipe = Swipe(uuid)
db.session.add(swipe)
db.session.commit()
return 'OK'
@app.route('/log', methods=['GET', 'POST'])
def log():
global after_date
if request.method == 'POST':
after_date = request.form['after_date']
swipes = db.session.query(Swipe).filter(Swipe.time >= after_date)
result = []
for swipe in swipes:
print(swipe.__dict__)
first_name = 'WHO DIS'
last_name = swipe.uuid
names = User.query.filter_by(uuid=swipe.uuid).first()
if names:
first_name = names.first_name
last_name = names.last_name
result.append({
'time': swipe.time,
'first_name': first_name,
'last_name': last_name,
'id': swipe.id})
return render_template('log.html', swipes=result, after_date=after_date)
@app.route('/log/modify', methods=['POST'])
def log_modify():
data = request.form
if 'delete_id' in data:
swipe = Swipe.query.filter_by(id=data['delete_id']).first()
db.session.delete(swipe)
db.session.commit()
return redirect(url_for('log'), code=302)
@app.route('/kiosk', methods=['GET'])
def kiosk():
return render_template('kiosk.html')
@app.route('/kiosk/data', methods=['GET'])
def kiosk_data():
global after_date
swipes = db.session.query(Swipe).filter(Swipe.time >= after_date)
counts = {}
last_swipes = {}
for swipe in swipes:
if swipe.uuid not in counts:
counts[swipe.uuid] = 0
counts[swipe.uuid] += 1
last_swipes[swipe.uuid] = swipe
res = []
for uuid, count in counts.items():
# User already signed out
if count % 2 == 0:
continue
swipe = last_swipes[uuid]
dt = swipe.time
tz = timezone('EST')
est = dt.astimezone(tz)
time_here = datetime.now() - dt
time_here_str = f'{time_here.seconds//3600}:{(time_here.seconds//60)%60:02d}'
print(swipe.uuid)
user = db.session.query(User).filter(User.uuid == swipe.uuid).first()
if user:
rec = {
'first_name': user.first_name,
'last_name': user.last_name,
'time_here': time_here_str
}
res.append(rec)
return json.dumps(res)
@app.route('/report', methods=['GET', 'POST'])
def report():
global after_date
if request.method == 'POST':
after_date = request.form['after_date']
days = {}
swipes = db.session.query(Swipe).filter(Swipe.time >= after_date)
for swipe in swipes:
dt = swipe.time
tz = timezone('EST')
est = dt.astimezone(tz)
date = est.date()
if date not in days:
days[date] = {}
name_dict = days[date]
if swipe.uuid not in name_dict:
name_dict[swipe.uuid] = []
name_list = name_dict[swipe.uuid]
name_list.append(est)
result = '<link rel="stylesheet" type="text/css" href="/static/css/main.css">'
result += f'<form action="report" method="POST"><input type="date" name="after_date" value="{after_date}" /><input type="submit" /></form>'
result += '<table border=1 style="table-layout:fixed;">'
name_query = db.session.query(User)
names = []
result += '<thead><tr><th></th>'
for name_obj in name_query:
name = f'{name_obj.first_name} {name_obj.last_name}'
names.append(name)
result += f'<th style="width: 100px; position: sticky; top: 0; background: #242424;">{name}</th>'
result += '</tr></thead>'
totals = {}
last_weekday = 0
for date, name_dict in days.items():
day = date.isoformat()
column = {}
for uuid, times in name_dict.items():
names_query = User.query.filter_by(uuid=uuid).first()
if names_query:
name = f'{names_query.first_name} {names_query.last_name}'
else:
name = f'Unknown: {uuid}'
# result += f'{swipe.time} | WHO DIS {swipe.uuid}<br>'
# result += f'{day} {uuid}: {times}<br>'
if len(times) % 2 == 1:
# result += f'{day} | {name} uneven scans<br>'
column[name] = 'X'
continue
total_hours = 0.0
for i in range(len(times) // 2):
in_ = times[i * 2 + 0]
out = times[i * 2 + 1]
dt = out - in_
total_hours += dt.seconds / 60 / 60
if name not in totals:
totals[name] = 0.0
totals[name] += total_hours
# result += f'{day} | {name}: {total_hours:0.2f}<br>'
column[name] = f'{total_hours:0.2f}'
if date.weekday() < last_weekday:
result += f'<tr><td colspan="{len(names)+1}">'
result += '<hr style="margin: 0; border: none; background: black; height: 10px" />'
result += '</td></tr>'
last_weekday = date.weekday()
result += f'<tr><td style="white-space: nowrap;">{day}</td>'
for name in names:
if name in column:
result += f'<td>{column[name]}</td>'
else:
result += '<td></td>'
result += '</tr>'
result += '<tr><td>Totals</td>'
for name_obj in name_query:
name = f'{name_obj.first_name} {name_obj.last_name}'
if name in totals:
total = totals[name]
else:
total = 0.0
result += f'<td>{total:0.2f}</td>'
result += '</tr>'
result += '</table>'
return result