File size: 7,340 Bytes
e2c12e7
 
 
 
 
 
720e020
 
e2c12e7
 
720e020
e2c12e7
 
 
720e020
e2c12e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
720e020
 
e2c12e7
 
 
 
 
 
 
 
 
 
 
 
 
720e020
e2c12e7
 
 
720e020
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2c12e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
720e020
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e2c12e7
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""
Lab-Setup  –  Multi-page lab portal (Hugging Face Space)
Stack: Flask Β· Jinja2 Β· vanilla JS/CSS Β· gunicorn
"""

import os
import sqlite3
from datetime import datetime, timezone
from flask import (
    Flask, render_template, request, redirect,
    url_for, session, flash, jsonify, g,
)
from werkzeug.middleware.proxy_fix import ProxyFix
from dotenv import load_dotenv
import markdown

load_dotenv()

app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)

app.secret_key = os.getenv("FLASK_SECRET", "change-me-in-prod")
app.config.update(
    SESSION_COOKIE_HTTPONLY=True,
    SESSION_COOKIE_SECURE=True,
    SESSION_COOKIE_SAMESITE="None",
)

ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "bpel123")

DB_PATH = os.getenv("DB_PATH", os.path.join(os.path.dirname(__file__), "lab.db"))

# ── oTree configuration ────────────────────────────────────────────
OTREE_SESSION_URL = os.getenv(
    "OTREE_SESSION_URL",
    "http://otree-lab-games-790d4693d333.herokuapp.com/room/bpel_lab",
)

# ─────────────────────────────────────────────────────────────────
# Sidebar page registry  –  add entries here to create new pages
# Each tuple: (endpoint, icon, label)
# The endpoint must match a route function name.
# ─────────────────────────────────────────────────────────────────
SIDEBAR_PAGES = [
    ("page_session", "πŸ–₯️", "Session"),
    ("page_logs",    "πŸ“", "Lab Logs"),
]


# ── SQLite helpers ──────────────────────────────────────────────────
def get_db():
    if "db" not in g:
        g.db = sqlite3.connect(DB_PATH)
        g.db.row_factory = sqlite3.Row
        g.db.execute("PRAGMA journal_mode=WAL")
    return g.db


@app.teardown_appcontext
def close_db(exc):
    db = g.pop("db", None)
    if db is not None:
        db.close()


def init_db():
    db = sqlite3.connect(DB_PATH)
    db.execute("""
        CREATE TABLE IF NOT EXISTS logs (
            id         INTEGER PRIMARY KEY AUTOINCREMENT,
            title      TEXT    NOT NULL,
            body       TEXT    NOT NULL DEFAULT '',
            author     TEXT    NOT NULL DEFAULT '',
            created_at TEXT    NOT NULL,
            updated_at TEXT    NOT NULL
        )
    """)
    db.commit()
    db.close()


init_db()


# ── Auth guard ──────────────────────────────────────────────────────
@app.before_request
def require_login():
    allowed = ("login", "static")
    if request.endpoint in allowed or (request.endpoint and request.endpoint.startswith("static")):
        return
    if not session.get("authenticated"):
        return redirect(url_for("login"))


# ── Auth routes ─────────────────────────────────────────────────────
@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        if request.form.get("password") == ADMIN_PASSWORD:
            session["authenticated"] = True
            return redirect(url_for("index"))
        flash("Incorrect password.", "error")
    return render_template("login.html")


@app.route("/logout")
def logout():
    session.clear()
    return redirect(url_for("login"))


# ── Context processor – injects sidebar into every template ─────────
@app.context_processor
def inject_sidebar():
    return dict(sidebar_pages=SIDEBAR_PAGES)


# ── Pages ───────────────────────────────────────────────────────────
@app.route("/")
def index():
    return redirect(url_for("page_session"))


@app.route("/session")
def page_session():
    return render_template(
        "pages/session.html",
        active_page="page_session",
        otree_url=OTREE_SESSION_URL,
    )


# ── Lab Logs page ───────────────────────────────────────────────────
@app.route("/logs")
def page_logs():
    db = get_db()
    rows = db.execute(
        "SELECT * FROM logs ORDER BY created_at DESC"
    ).fetchall()
    logs = []
    for r in rows:
        logs.append({
            "id": r["id"],
            "title": r["title"],
            "body": r["body"],
            "body_html": markdown.markdown(
                r["body"], extensions=["fenced_code", "tables", "nl2br"]
            ),
            "author": r["author"],
            "created_at": r["created_at"],
            "updated_at": r["updated_at"],
        })
    return render_template("pages/logs.html", active_page="page_logs", logs=logs)


# ── Lab Logs API ────────────────────────────────────────────────────
@app.route("/api/logs", methods=["POST"])
def api_logs_create():
    data = request.get_json(silent=True) or {}
    title = (data.get("title") or "").strip()
    if not title:
        return jsonify(error="Title is required."), 400
    body = (data.get("body") or "").strip()
    author = (data.get("author") or "").strip()
    now = datetime.now(timezone.utc).isoformat()
    db = get_db()
    cur = db.execute(
        "INSERT INTO logs (title, body, author, created_at, updated_at) VALUES (?,?,?,?,?)",
        (title, body, author, now, now),
    )
    db.commit()
    return jsonify(id=cur.lastrowid), 201


@app.route("/api/logs/<int:log_id>", methods=["PUT"])
def api_logs_update(log_id):
    data = request.get_json(silent=True) or {}
    title = (data.get("title") or "").strip()
    if not title:
        return jsonify(error="Title is required."), 400
    body = (data.get("body") or "").strip()
    author = (data.get("author") or "").strip()
    now = datetime.now(timezone.utc).isoformat()
    db = get_db()
    db.execute(
        "UPDATE logs SET title=?, body=?, author=?, updated_at=? WHERE id=?",
        (title, body, author, now, log_id),
    )
    db.commit()
    return jsonify(ok=True)


@app.route("/api/logs/<int:log_id>", methods=["DELETE"])
def api_logs_delete(log_id):
    db = get_db()
    db.execute("DELETE FROM logs WHERE id=?", (log_id,))
    db.commit()
    return jsonify(ok=True)


# ── API helpers ─────────────────────────────────────────────────────
@app.route("/api/health")
def api_health():
    return jsonify(status="ok")


# ── Dev server ──────────────────────────────────────────────────────
if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5111, debug=True)