112 lines
4.1 KiB
Python
112 lines
4.1 KiB
Python
from fastapi import APIRouter, Request
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/ui/create-user")
|
|
def create_user_page(request: Request):
|
|
return """
|
|
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Create user & Take test</title>
|
|
</head>
|
|
<body>
|
|
<h1>Create user</h1>
|
|
<form id="userForm">
|
|
<label>First name: <input name="first_name" required></label><br>
|
|
<label>Last name: <input name="last_name" required></label><br>
|
|
<button type="submit">Create user</button>
|
|
</form>
|
|
|
|
<h2>Or create Holland test</h2>
|
|
<button id="createHolland">Create Holland test</button>
|
|
|
|
<script>
|
|
document.getElementById('userForm').addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
const fd = new FormData(e.target);
|
|
const data = { first_name: fd.get('first_name'), last_name: fd.get('last_name') };
|
|
const res = await fetch('/users/', { method: 'POST', headers: {'Content-Type':'application/json'}, body: JSON.stringify(data) });
|
|
if (!res.ok) { alert('Create user failed'); return; }
|
|
const js = await res.json();
|
|
const uid = js.id;
|
|
alert('User created: ' + uid);
|
|
// allow user to input poll id manually
|
|
const poll = prompt('Enter poll id to take (or leave empty to create Holland test)');
|
|
if (poll) {
|
|
location.href = '/ui/take-test?poll_id=' + encodeURIComponent(poll) + '&user_id=' + encodeURIComponent(uid);
|
|
} else {
|
|
// create Holland test then redirect
|
|
const r2 = await fetch('/polls/holland', { method: 'POST' });
|
|
const p = await r2.json();
|
|
location.href = '/ui/take-test?poll_id=' + encodeURIComponent(p.id) + '&user_id=' + encodeURIComponent(uid);
|
|
}
|
|
});
|
|
|
|
document.getElementById('createHolland').addEventListener('click', async () => {
|
|
const r = await fetch('/polls/holland', { method: 'POST' });
|
|
if (!r.ok) { alert('Create failed'); return; }
|
|
const p = await r.json();
|
|
alert('Created poll ' + p.id);
|
|
location.href = '/ui/take-test?poll_id=' + encodeURIComponent(p.id);
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
|
|
@router.get("/ui/take-test")
|
|
def take_test_page(request: Request, poll_id: str = None, user_id: str = None):
|
|
return f"""
|
|
<!doctype html>
|
|
<html>
|
|
<head><meta charset="utf-8"><title>Take test</title></head>
|
|
<body>
|
|
<h1>Take test</h1>
|
|
<div id="poll"></div>
|
|
<button id="submitBtn">Submit</button>
|
|
<script>
|
|
const pollId = '{poll_id or ''}';
|
|
const userId = '{user_id or ''}';
|
|
async function load() {{
|
|
if (!pollId) {{ document.getElementById('poll').innerText = 'No poll_id provided'; return; }}
|
|
const r = await fetch('/polls/' + encodeURIComponent(pollId));
|
|
if (!r.ok) {{ document.getElementById('poll').innerText = 'Failed to load poll'; return; }}
|
|
const p = await r.json();
|
|
const container = document.getElementById('poll');
|
|
const h = document.createElement('h2'); h.innerText = p.title; container.appendChild(h);
|
|
p.questions.forEach(q => {{
|
|
const div = document.createElement('div');
|
|
div.innerHTML = '<p>' + q.text + '</p>';
|
|
q.choices.forEach(c => {{
|
|
const id = 'q_' + q.id + '_c_' + c.id;
|
|
const input = `<label><input type="radio" name="${{q.id}}" value="${{c.id}}"> ${{c.text}}</label><br>`;
|
|
div.insertAdjacentHTML('beforeend', input);
|
|
}});
|
|
container.appendChild(div);
|
|
}});
|
|
}}
|
|
load();
|
|
|
|
document.getElementById('submitBtn').addEventListener('click', async () => {{
|
|
const answers = [];
|
|
const groups = new Set();
|
|
document.querySelectorAll('input[type=radio]').forEach(r => groups.add(r.name));
|
|
groups.forEach(name => {{
|
|
const checked = document.querySelector('input[name="' + name + '"]:checked');
|
|
if (checked) answers.push({{question_id: name, choice_id: checked.value}});
|
|
}});
|
|
const payload = {{ user_id: userId || null, answers }};
|
|
const res = await fetch('/polls/' + encodeURIComponent(pollId) + '/responses', {{ method: 'POST', headers: {{'Content-Type':'application/json'}}, body: JSON.stringify(payload) }});
|
|
if (!res.ok) {{ alert('Submit failed'); return; }}
|
|
const js = await res.json();
|
|
alert('Response saved: ' + js.id);
|
|
}});
|
|
</script>
|
|
</body>
|
|
</html>
|
|
"""
|