Simple interface for setting app config.

This adds a bare-bones HTML form for configuring the application config
parameters, i.e. client ID, service account key, rietveld server URL and
Rietveld nickname for the bot.

R=iannucci@chromium.org

Review URL: https://codereview.chromium.org/20514002

git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/reviewbot@216374 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/app_config.py b/app_config.py
new file mode 100644
index 0000000..1171517
--- /dev/null
+++ b/app_config.py
@@ -0,0 +1,69 @@
+# Copyright (c) 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Request handler for the /admin/app_config page.
+
+Allows admins to set configuration parameter via the appengine admin console.
+"""
+
+import cgi
+import webapp2
+
+import third_party  # pylint: disable=W0611
+
+import model.app_config
+import rietveld
+
+
+FIELDS = ('client_id', 'service_account_key', 'server_url', 'nickname')
+
+
+class AppConfigHandler(webapp2.RequestHandler):
+  """Handles /admin/appconfig."""
+
+  def post(self):
+    """Handles POST requests to update app config.
+
+    Parses the request data, writes it to the data store entity, and sends a
+    request to rietveld to update the app's nickname.
+    """
+    app_config = model.app_config.get()
+    for field in FIELDS:
+      setattr(app_config, field, self.request.get(field, None))
+    app_config.put()
+
+    # Set the nickname with rietveld.
+    rv = rietveld.Rietveld()
+    settings_payload = {
+        'column_width': 80,  # required field
+        'nickname': app_config.nickname,
+    }
+
+    try:
+      rv.post_data('settings', settings_payload)
+    except rietveld.RietveldRequestError as e:
+      # Redirect indicates success.
+      if e[1].status != 302:
+        raise e
+
+    self.RenderForm()
+
+  def get(self):
+    """Handles GET requests."""
+    self.RenderForm()
+
+  def RenderForm(self):
+    """Renders the app config form to the client."""
+    app_config = model.app_config.get()
+    self.response.write('<html><body><form action="%s" method="post"><table>' %
+                        self.request.path)
+    for field in FIELDS:
+      self.response.write(
+          '<tr><td>%s</td><td><textarea name="%s">%s</textarea></td></tr>' %
+          (field, field, cgi.escape(getattr(app_config, field, ''))))
+    self.response.write('<tr><td><input type="submit" value="Set"></td></tr>')
+    self.response.write('</table><form></body></body>')
+
+
+app = webapp2.WSGIApplication([('/admin/app_config', AppConfigHandler)])
diff --git a/third_party.py b/third_party.py
new file mode 100644
index 0000000..4e0507f
--- /dev/null
+++ b/third_party.py
@@ -0,0 +1,11 @@
+# Copyright (c) 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import os
+import sys
+
+def add_import_path(*args):
+  sys.path.append(os.path.join(*((os.path.dirname(__file__),) + args)))
+
+add_import_path('third_party', 'google-api-python-client')