Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import json
- import requests
- import flask
- APP = flask.Flask(__name__)
- APP.secret_key = "some_secret"
- TEMPLATE = """<html><body>
- {% with messages = get_flashed_messages(with_categories=true) %}
- {% if messages %}
- <ul class=flashes>
- {% for category, message in messages %}
- <li class="{{ category }}">{{ message }}</li>
- {% endfor %}
- </ul>
- {% endif %}
- {% endwith %}
- <form method="post" action="">
- <fieldset>
- <legend><strong>Add new domain</strong></legend>
- <input name="action" type="hidden" value="add" />
- <label style="float:left;width:100px;text-align:right;">Domain: </label><input name="domain" type="text" /><br />
- <label style="float:left;width:100px;text-align:right;">Route: </label><input name="destination" type="text" /><br />
- <input style="margin-left:100px;" type="submit" value="Add" />
- </fieldset>
- </form>
- <h3>View existing domains</h3>
- <table border="1" cellspacing="0" cellpadding="2">
- <tr>
- <th>Domain</th>
- <th>Aliases</th>
- <th>Routes</th>
- <th>Action</th>
- </tr>
- {% if domains %}
- {% for info in domains %}
- <tr>
- <td>{{ info.domain }}</td>
- <td>{{ info.aliases|join('<br />') }}
- <td>{{ info.destinations|join('<br />') }}
- <td><form method="post" action=""><input name="action" type="hidden" value="remove" /><input name="domain" type="hidden" value="{{ info.domain }}" /><input type="submit" value="remove" /></form></td>
- </tr>
- {% endfor %}
- {% else %}
- <tr>
- <td colspan="4">No domains are set up.</td>
- </tr>
- {% endif %}
- </table></body></html>
- """
- @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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement