import os
import json
import requests
import flask
APP = flask.Flask(__name__)
APP.secret_key = "some_secret"
TEMPLATE = """
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
- {{ message }}
{% endfor %}
{% endif %}
{% endwith %}
View existing domains
| Domain |
Aliases |
Routes |
Action |
{% if domains %}
{% for info in domains %}
| {{ info.domain }} |
{{ info.aliases|join(' ') }}
| {{ info.destinations|join(' ') }}
| |
{% endfor %}
{% else %}
| No domains are set up. |
{% endif %}
"""
@APP.route("/", methods=['GET', 'POST'])
def view():
auth = (os.environ["USER"], os.environ["PASSWORD"])
base = "https://%(server)s/cgi-bin/api?call=" % {"server":
os.environ["SERVER"]}
requests.packages.urllib3.disable_warnings()
if flask.request.method == "POST":
action = flask.request.form["action"]
domain = flask.request.form["domain"]
if (action == "add" and domain and flask.request.form["destination"]):
destinations = []
for destination in flask.request.form["destination"].split(","):
destinations.append(destination.split(":")
if ":" in destination else
(destination, 25))
data = json.dumps({domain: {"destinations": destinations}})
response = requests.get("%(base)sapi_add_delivery_domain&"
"data=%(data)s" %
{"base": base, "data": data}, auth=auth)
if response.ok:
flask.flash("Added %(domain)s" % {"domain": domain}, "info")
else:
flask.flash("An error occurred.", "error")
elif action == "remove" and domain:
response = requests.get("%(base)sapi_remove_domain&"
"domain=%(domain)s" %
{"base": base, "domain": domain},
auth=auth)
if response.ok:
flask.flash("Removed %(domain)s" % {"domain": domain}, "info")
else:
flask.flash("An error occurred.", "error")
return flask.redirect(flask.url_for("view"))
domains = requests.get("%(base)sapi_list_domains&format=json" %
{"base": base}, auth=auth).json()
return flask.render_template_string(TEMPLATE, domains=domains)