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(): 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) user = db.session.query(User).filter(User.uuid == swipe.uuid).first() time_here = datetime.now() - dt time_here_str = f'{time_here.seconds//3600}:{(time_here.seconds//60)%60:02d}' 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(swipe.time) result = '' result += f'
' result += '' name_query = db.session.query(User) names = [] result += '' for name_obj in name_query: name = f'{name_obj.first_name} {name_obj.last_name}' names.append(name) result += f'' result += '' 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}
' # result += f'{day} {uuid}: {times}
' if len(times) % 2 == 1: # result += f'{day} | {name} uneven scans
' 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}
' column[name] = f'{total_hours:0.2f}' if date.weekday() < last_weekday: result += f'' last_weekday = date.weekday() result += f'' for name in names: if name in column: result += f'' else: result += '' result += '' result += '' 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'' result += '' result += '
{name}
' result += '
' result += '
{day}{column[name]}
Totals{total:0.2f}
' return result