def edit_election(el_id): db = get_db() el = db.get(doc_id=el_id) if el is None: abort(404) tt = time_type(el["times"]["start"], el["times"]["end"]) if tt != "future": abort(404) if request.method == "POST": if el["type"] == "ranked": succ, data = get_data_ranked() if succ: db.remove(doc_ids=[el_id]) db.insert(data) flash("Election updated successfully!", "success") return redirect(url_for("admin.elections")) else: flash(data, "error") elif el["type"] == "yesno": succ, data = get_data_yesno() if succ: db.remove(doc_ids=[el_id]) db.insert(data) flash("Election updated successfully!", "success") return redirect(url_for("admin.elections")) else: flash(data, "error") else: if el["type"] == "ranked": return render_template("admin/edit_ranked.html", el=el) elif el["type"] == "yesno": return render_template("admin/edit_yesno.html", el=el)
def results(el_id): db = get_db() el = db.get(doc_id=el_id) if el is None: abort(404) tt = time_type(el['times']['start'], el['times']['end']) if tt == "present": return redirect(url_for("vote.election", el_id=el_id)) elif tt != "past": abort(404) if el['type'] == "yesno": results = process_votes_yesno(el['votes']) voters = process_voters(el['voters']) return render_template("results/yesno.html", processed_results=results, results=el['votes'], voters=voters, el=el) elif el['type'] == "ranked": winner, results = process_votes_ranked(list(el['votes']), el) voters = process_voters(el['voters']) return render_template("results/ranked.html", winner=winner, processed_results=results, results=el['votes'], voters=voters, el=el) else: return render_template( "base.html", content="Oops, that election type is not supported")
def elections(): db = get_db() els = db.all() categories = {"past": [], "present": [], "future": []} for el in els: tt = time_type(el["times"]["start"], el["times"]["end"]) categories[tt].append(el) return render_template("admin/elections.html", **categories)
def delete_election(el_id): db = get_db() el = db.get(doc_id=el_id) if el is None: abort(404) tt = time_type(el["times"]["start"], el["times"]["end"]) if tt != "future": abort(404) if request.method == "POST": db.remove(doc_ids=[el_id]) flash("Election removed successfully", "success") return redirect(url_for("admin.elections")) return render_template("admin/delete_election.html", el=el)
def election_list(): db = get_db() elections = db.all() # we need to sort these into "past", "present" and "future" categories = {'past': [], 'present': [], 'future': []} for el in elections: tt = time_type(el['times']['start'], el['times']['end']) categories[tt].append(el) threshold = dt.datetime.now() - dt.timedelta(days=7 * 2) # 2 weeks ago print(threshold) for e in categories['past']: print(e['times']['end'], e['times']['end'] < threshold) categories['past'] = [ e for e in categories['past'] if e['times']['end'] > threshold ] return render_template("election_list/election_list.html", **categories)
def election(el_id): db = get_db() el = db.get(doc_id=el_id) if el is None: abort(404) else: tt = time_type(el['times']['start'], el['times']['end']) if tt == "past": return redirect(url_for("results.results", el_id=el_id)) elif tt == "present": voters = get_allowed_voters() if el['type'] == "yesno": return render_template("vote/yesno.html", el=el, el_id=el_id, voters=voters) elif el['type'] == "ranked": return render_template("vote/ranked.html", el=el, el_id=el_id, voters=voters) else: return render_template("base.html", content="Oops, that election type is not supported") else: abort(404)