Advertisement
Guest User

sample-oython-se

a guest
Apr 25th, 2016
910
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import json
  3.  
  4. import requests
  5.  
  6. import flask
  7.  
  8.  
  9. APP = flask.Flask(__name__)
  10. APP.secret_key = "some_secret"
  11.  
  12. TEMPLATE = """<html><body>
  13. {% with messages = get_flashed_messages(with_categories=true) %}
  14.  {% if messages %}
  15.    <ul class=flashes>
  16.    {% for category, message in messages %}
  17.      <li class="{{ category }}">{{ message }}</li>
  18.    {% endfor %}
  19.    </ul>
  20.  {% endif %}
  21. {% endwith %}
  22.  
  23. <form method="post" action="">
  24.    <fieldset>
  25.        <legend><strong>Add new domain</strong></legend>
  26.        <input name="action" type="hidden" value="add" />
  27.        <label style="float:left;width:100px;text-align:right;">Domain: </label><input name="domain" type="text" /><br />
  28.        <label style="float:left;width:100px;text-align:right;">Route: </label><input name="destination" type="text" /><br />
  29.        <input style="margin-left:100px;" type="submit" value="Add" />
  30.    </fieldset>
  31. </form>
  32.  
  33. <h3>View existing domains</h3>
  34.  
  35. <table border="1" cellspacing="0" cellpadding="2">
  36. <tr>
  37.    <th>Domain</th>
  38.    <th>Aliases</th>
  39.    <th>Routes</th>
  40.    <th>Action</th>
  41. </tr>
  42. {% if domains %}
  43. {% for info in domains %}
  44.    <tr>
  45.        <td>{{ info.domain }}</td>
  46.        <td>{{ info.aliases|join('<br />') }}
  47.        <td>{{ info.destinations|join('<br />') }}
  48.        <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>
  49.    </tr>
  50. {% endfor %}
  51. {% else %}
  52.    <tr>
  53.        <td colspan="4">No domains are set up.</td>
  54.    </tr>
  55. {% endif %}
  56. </table></body></html>
  57. """
  58.  
  59.  
  60. @APP.route("/", methods=['GET', 'POST'])
  61. def view():
  62.     auth = (os.environ["USER"], os.environ["PASSWORD"])
  63.     base = "https://%(server)s/cgi-bin/api?call=" % {"server":
  64.                                                      os.environ["SERVER"]}
  65.     requests.packages.urllib3.disable_warnings()
  66.     if flask.request.method == "POST":
  67.         action = flask.request.form["action"]
  68.         domain = flask.request.form["domain"]
  69.         if (action == "add" and domain and flask.request.form["destination"]):
  70.             destinations = []
  71.             for destination in flask.request.form["destination"].split(","):
  72.                 destinations.append(destination.split(":")
  73.                                     if ":" in destination else
  74.                                     (destination, 25))
  75.             data = json.dumps({domain: {"destinations": destinations}})
  76.             response = requests.get("%(base)sapi_add_delivery_domain&"
  77.                                     "data=%(data)s" %
  78.                                     {"base": base, "data": data}, auth=auth)
  79.             if response.ok:
  80.                 flask.flash("Added %(domain)s" % {"domain": domain}, "info")
  81.             else:
  82.                 flask.flash("An error occurred.", "error")
  83.         elif action == "remove" and domain:
  84.             response = requests.get("%(base)sapi_remove_domain&"
  85.                                     "domain=%(domain)s" %
  86.                                     {"base": base, "domain": domain},
  87.                                     auth=auth)
  88.             if response.ok:
  89.                 flask.flash("Removed %(domain)s" % {"domain": domain}, "info")
  90.             else:
  91.                 flask.flash("An error occurred.", "error")
  92.         return flask.redirect(flask.url_for("view"))
  93.     domains = requests.get("%(base)sapi_list_domains&format=json" %
  94.                            {"base": base}, auth=auth).json()
  95.     return flask.render_template_string(TEMPLATE, domains=domains)
Advertisement
RAW Paste Data Copied
Advertisement