pharmacy dashboard
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
# ================================================================
|
||||
# ✅ ADMIN DASHBOARD — paste this block into app.py
|
||||
# ================================================================
|
||||
# 1. Add this import near your other imports at the top of app.py:
|
||||
# from admin_stats import build_admin_stats, build_summary
|
||||
#
|
||||
# 2. Put admin_stats.py in the same folder as app.py.
|
||||
#
|
||||
# 3. Add this migration call right after `init_db()` is called (the
|
||||
# existing init_db() only runs on a brand-new DB, so a separate
|
||||
# migration step is needed to add the column to your existing users.db):
|
||||
|
||||
import datetime
|
||||
from dbm import sqlite3
|
||||
from functools import wraps
|
||||
|
||||
from flask import app, redirect, render_template, url_for
|
||||
|
||||
|
||||
from admin_stats import build_admin_stats, build_summary
|
||||
from app import get_current_user, get_db, login_required
|
||||
|
||||
|
||||
def migrate_db():
|
||||
"""Idempotent schema migrations for an existing database."""
|
||||
conn = get_db()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("ALTER TABLE users ADD COLUMN is_admin INTEGER DEFAULT 0")
|
||||
conn.commit()
|
||||
print("✅ Added is_admin column to users table")
|
||||
except sqlite3.OperationalError:
|
||||
pass # column already exists
|
||||
conn.close()
|
||||
|
||||
migrate_db()
|
||||
|
||||
# 4. Add this decorator near login_required:
|
||||
|
||||
def admin_required(f):
|
||||
"""Requires an is_admin=1 user. Use together with @login_required,
|
||||
@login_required first so an anonymous visitor is sent to /login
|
||||
rather than redirected in a way that looks like a missing page."""
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
user = get_current_user()
|
||||
if not user or not user['is_admin']:
|
||||
return redirect(url_for('login'))
|
||||
return f(*args, **kwargs)
|
||||
return decorated_function
|
||||
|
||||
# 5. Add the route itself:
|
||||
|
||||
@app.route('/admin')
|
||||
@login_required
|
||||
@admin_required
|
||||
def admin_dashboard():
|
||||
"""Per-day, per-user counts of invoices processed and item codes found."""
|
||||
conn = get_db()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
SELECT e.user_id, u.username, e.created_at, e.extraction_data
|
||||
FROM extractions e
|
||||
JOIN users u ON u.id = e.user_id
|
||||
ORDER BY e.created_at DESC
|
||||
''')
|
||||
rows = cursor.fetchall()
|
||||
conn.close()
|
||||
|
||||
stats = build_admin_stats(rows)
|
||||
today_str = datetime.now().strftime('%Y-%m-%d')
|
||||
summary = build_summary(stats, today_str)
|
||||
|
||||
return render_template('admin_dashboard.html', stats=stats, summary=summary, today=today_str)
|
||||
|
||||
|
||||
# ================================================================
|
||||
# ✅ MAKING A USER AN ADMIN
|
||||
# ================================================================
|
||||
# There's no signup flow for this, so set it directly once:
|
||||
#
|
||||
# python3 -c "
|
||||
# import sqlite3
|
||||
# conn = sqlite3.connect('users.db')
|
||||
# conn.execute(\"UPDATE users SET is_admin = 1 WHERE username = 'YOUR_USERNAME'\")
|
||||
# conn.commit()
|
||||
# "
|
||||
#
|
||||
# Run that once from the same folder as users.db, after you've registered
|
||||
# the account you want as admin.
|
||||
Reference in New Issue
Block a user