diff --git a/app.py b/app.py index 0d35a72..9abbf4d 100644 --- a/app.py +++ b/app.py @@ -150,7 +150,20 @@ def vote_date(): """, (session["user"]["id"], meet_id,)) dates = csr.fetchall() - return render_template("date_vote_fragment.html", dates=dates) + + result = instantrunoff_forward(meet_id, "date") + log.debug("result = %s", result) + + return render_template("date_vote_fragment.html", dates=dates, result=result) + +@app.get("/result//date") +def result_date(meet_id): + result = instantrunoff_forward(meet_id, "date") + log.debug("result = %s", result) + + return render_template("date_result_fragment.html", result=result) + + @app.post("/vote/time") def vote_time(): @@ -186,7 +199,18 @@ def vote_time(): """, (session["user"]["id"], meet_id,)) times = csr.fetchall() - return render_template("time_vote_fragment.html", times=times) + + result = instantrunoff_forward(meet_id, "time") + log.debug("result = %s", result) + + return render_template("time_vote_fragment.html", times=times, result=result) + +@app.get("/result//time") +def result_time(meet_id): + result = instantrunoff_forward(meet_id, "time") + log.debug("result = %s", result) + + return render_template("time_result_fragment.html", result=result) @app.post("/vote/place") def vote_place(): @@ -222,7 +246,19 @@ def vote_place(): """, (session["user"]["id"], meet_id,)) places = csr.fetchall() - return render_template("place_vote_fragment.html", places=places) + + result = instantrunoff_forward(meet_id, "place") + log.debug("result = %s", result) + + return render_template("place_vote_fragment.html", places=places, result=result) + + +@app.get("/result//place") +def result_place(meet_id): + result = instantrunoff_forward(meet_id, "place") + log.debug("result = %s", result) + + return render_template("place_result_fragment.html", result=result) def send_mail(email_address, confirmation_url): @@ -235,6 +271,70 @@ def send_mail(email_address, confirmation_url): mta = smtplib.SMTP(host="localhost") mta.send_message(msg) +def get_ballots(meet_id, kind): + csr = get_cursor() + + q = f""" + select {kind}.*, bod, position + from {kind} join {kind}_vote on {kind}.id = {kind}_vote.{kind} + where meet = %s + order by bod, position + """ + csr.execute(q, (meet_id,)) + + last_bod = None + ballots = [] + for r in csr: + if r.bod != last_bod: + ballot = [] + ballots.append(ballot) + last_bod = r.bod + ballot.append(r) + return ballots + +def dump_ballots(ballots): + for ballot in ballots: + log.debug ("---") + for r in ballot: + log.debug(r) + +def runoff(ballots): + count = {} + candidates = {} + for ballot in ballots: + for r in ballot: + count[r.id] = 0 + candidates[r.id] = r + for ballot in ballots: + # The votes are sorted by position, so the first entry is the favourite + count[ballot[0].id] += 1 + result = sorted(count.keys(), key=lambda i: count[i]) + log.debug("result of this round:") + for r in result: + log.debug(r, count[r]) + log.debug("striking %d", result[0]) + loser = candidates[result[0]] + new_ballots = [ + [ + r for r in ballot if r.id != loser.id + ] for ballot in ballots + ] + return loser, new_ballots + +def instantrunoff_forward(meet_id, kind): + ballots = get_ballots(meet_id, kind) + + result = [] + while max(len(b) for b in ballots): + dump_ballots(ballots) + loser, ballots = runoff(ballots) + result.append(loser) + result = list(reversed(result)) + log.debug("final result") + for r in result: + log.debug(r) + return result + def get_cursor(): db = get_db() csr = db.cursor(row_factory=psycopg.rows.namedtuple_row) diff --git a/static/style.css b/static/style.css index 1ae6d10..93cf0b7 100644 --- a/static/style.css +++ b/static/style.css @@ -10,19 +10,31 @@ body { padding: 1em; border: #CCC 1px solid; border-radius: 0.2em; + cursor: grab; } .blue-background-class { background: #CDF; + cursor: grabbing; } body { display: grid; + grid-template-columns: 1fr 1fr; + grid-column-gap: 2em; } #hello { grid-column: 1 / 3; } -#h-day, #h-time, #h-place { +h1, h2 { grid-column: 1 / 3; } + +.result-item { + padding: 1em; + border: #CCC 1px solid; + border-radius: 0.2em; + color: #888; +} + diff --git a/templates/date_result_fragment.html b/templates/date_result_fragment.html new file mode 100644 index 0000000..8fedb36 --- /dev/null +++ b/templates/date_result_fragment.html @@ -0,0 +1,7 @@ +
+ {% for r in result %} +
+ {{ r.display or r.date }} +
+ {% endfor %} +
diff --git a/templates/date_vote_fragment.html b/templates/date_vote_fragment.html index a6316c6..5935ba7 100644 --- a/templates/date_vote_fragment.html +++ b/templates/date_vote_fragment.html @@ -4,3 +4,10 @@ {% endfor %} +
+ {% for r in result %} +
+ {{ r.display or r.date }} +
+ {% endfor %} +
diff --git a/templates/place_result_fragment.html b/templates/place_result_fragment.html new file mode 100644 index 0000000..bf65088 --- /dev/null +++ b/templates/place_result_fragment.html @@ -0,0 +1,7 @@ +
+ {% for r in result %} +
+ {{ r.name }} +
+ {% endfor %} +
diff --git a/templates/place_vote_fragment.html b/templates/place_vote_fragment.html index b4246e7..f74dc35 100644 --- a/templates/place_vote_fragment.html +++ b/templates/place_vote_fragment.html @@ -4,3 +4,10 @@ {% endfor %} +
+ {% for r in result %} +
+ {{ r.name }} +
+ {% endfor %} +
diff --git a/templates/register.html b/templates/register.html index f20ca2f..96af989 100644 --- a/templates/register.html +++ b/templates/register.html @@ -7,8 +7,8 @@

Registriere dich:

- - + +
diff --git a/templates/time_result_fragment.html b/templates/time_result_fragment.html new file mode 100644 index 0000000..1487c63 --- /dev/null +++ b/templates/time_result_fragment.html @@ -0,0 +1,7 @@ +
+ {% for r in result %} +
+ {{ r.display or r.time }} +
+ {% endfor %} +
diff --git a/templates/time_vote_fragment.html b/templates/time_vote_fragment.html index 6e0c0c1..d984c80 100644 --- a/templates/time_vote_fragment.html +++ b/templates/time_vote_fragment.html @@ -4,3 +4,10 @@ {% endfor %} +
+ {% for r in result %} +
+ {{ r.display or r.time }} +
+ {% endfor %} +
diff --git a/templates/vote.html b/templates/vote.html index 6d95a0f..31c348f 100644 --- a/templates/vote.html +++ b/templates/vote.html @@ -23,7 +23,7 @@ {% endfor %} -
+

Zu welcher Zeit? @@ -36,7 +36,7 @@

{% endfor %} -
+

An welchem Ort? @@ -49,7 +49,7 @@

{% endfor %} -
+