Hodfa71 commited on
Commit
02314c6
·
verified ·
1 Parent(s): e77fdce

Send participants directly to first task via /go/<project> route

Browse files
Files changed (1) hide show
  1. ls_proxy_hf.py +35 -1
ls_proxy_hf.py CHANGED
@@ -1722,6 +1722,40 @@ def proxy_session_check():
1722
  return Response(json.dumps(result, indent=2), content_type="application/json")
1723
 
1724
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1725
  @app.route("/start", methods=["GET"])
1726
  def prolific_start():
1727
  """Prolific entry point.
@@ -1788,7 +1822,7 @@ def prolific_start():
1788
  <b>Estimated time:</b> approximately {est_min} minutes<br>
1789
  <b>When done:</b> you will be sent back to Prolific automatically
1790
  </div>
1791
- <a class="task-btn" href="/autologin?pid={pid}&email={cred_email}&password={cred_pw}&next=/projects/{project}/">
1792
  {flag} Start &rarr; {task}
1793
  </a>
1794
  <p class="note">Your Prolific ID is recorded automatically. Do not share this link.</p>
 
1722
  return Response(json.dumps(result, indent=2), content_type="application/json")
1723
 
1724
 
1725
+ @app.route("/go/<int:project_id>/", methods=["GET"])
1726
+ @app.route("/go/<int:project_id>", methods=["GET"])
1727
+ def go_to_project(project_id):
1728
+ """After autologin, redirect participant straight to their first available task."""
1729
+ uid, _ = get_current_user(request)
1730
+ if not uid:
1731
+ return Response("", status=302, headers={"Location": f"/user/login/?next=/go/{project_id}/"})
1732
+ try:
1733
+ conn = sqlite3.connect(DB_PATH)
1734
+ row = conn.execute(
1735
+ "SELECT at.key FROM authtoken_token at "
1736
+ "JOIN htx_user u ON at.user_id = u.id "
1737
+ "WHERE u.is_superuser=1 ORDER BY u.id LIMIT 1"
1738
+ ).fetchone()
1739
+ conn.close()
1740
+ admin_token = row[0] if row else None
1741
+ except Exception:
1742
+ admin_token = None
1743
+ if admin_token:
1744
+ try:
1745
+ r = requests.get(
1746
+ f"{LS_URL}/api/projects/{project_id}/next/",
1747
+ headers={"Authorization": f"Token {admin_token}", "Host": "localhost:8080"},
1748
+ cookies=_ls_cookies(request), timeout=10,
1749
+ )
1750
+ d = r.json() if r.status_code == 200 else {}
1751
+ if d.get("id"):
1752
+ return Response("", status=302,
1753
+ headers={"Location": f"/projects/{project_id}/data?task={d['id']}"})
1754
+ except Exception:
1755
+ pass
1756
+ return Response("", status=302, headers={"Location": f"/projects/{project_id}/data"})
1757
+
1758
+
1759
  @app.route("/start", methods=["GET"])
1760
  def prolific_start():
1761
  """Prolific entry point.
 
1822
  <b>Estimated time:</b> approximately {est_min} minutes<br>
1823
  <b>When done:</b> you will be sent back to Prolific automatically
1824
  </div>
1825
+ <a class="task-btn" href="/autologin?pid={pid}&email={cred_email}&password={cred_pw}&next=/go/{project}/">
1826
  {flag} Start &rarr; {task}
1827
  </a>
1828
  <p class="note">Your Prolific ID is recorded automatically. Do not share this link.</p>