-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage_manage.py
More file actions
53 lines (41 loc) · 1.96 KB
/
page_manage.py
File metadata and controls
53 lines (41 loc) · 1.96 KB
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
from flask import Blueprint, render_template, request, abort
from . import config
from .poll_db import PollDB
bp = Blueprint("manage", __name__, url_prefix = "/manage")
pdb = PollDB()
def can_manage():
return request.remote_addr in config.settings["manage_ip_addresses"]
def verify_can_manage():
if not can_manage():
abort(403)
def handle_action(id, action):
if ((not id and action == "create") or id) and action:
poll = None
if id:
poll = pdb.get_by_id(id)
if poll.is_empty():
return ("danger", "The poll you are trying to manage was not found.")
generic_success = ("success", "The poll was successfully " + action + "d.")
generic_error = ("danger", "An error occurred trying to perform the specified action on the poll, please try again.")
if action == "create":
question = request.form.get("question")
responses = request.form.getlist("responses[]")
closes = request.form.get("closes")
if not question or not responses or not closes or len(responses) < 1:
return ("danger", "One or more fields that are required to create a poll were not supplied.")
return generic_success if pdb.create(question, closes, responses) else generic_error
elif action == "close":
if poll.is_closed():
return ("danger", "The poll you are trying to close is already closed.")
return generic_success if pdb.close(poll.id()) else generic_error
elif action == "delete":
return generic_success if pdb.delete(poll.id()) else generic_error
return None
@bp.route("/", methods = ("GET", "POST"))
def index():
verify_can_manage()
msg = None
if request.method == "POST":
msg = handle_action(request.form.get("id"), request.form.get("action"))
all_polls = pdb.get_all()
return render_template("manage.html", message = msg, polls = all_polls)