Commits

Chris Jones committed 28553a7

Updated requirements file to include WTForms and Flask-WTF. Started base for new key creation. New view and template. Successfully saving strings but all other types not yet supported. Errors also not yet working properly on the form. May switch to a new page for the new key creation to make it a little easier to deal with.

Comments (0)

Files changed (6)

 import re
+
 from flask import Flask, render_template, redirect, url_for, request, flash
 from flask import session, jsonify
+
+from flaskext.wtf import Form, TextField, Required
+
 import redis
 from redis.exceptions import ConnectionError
+
 import settings
 
 SECRET_KEY = '781b0650af13493089a6ffafac755a61'
 app.config.from_object(__name__)
 app.debug = True
 
+# Forms
+class StringForm(Form):
+    key_name = TextField('Key', validators=[Required()])
+    key_value = TextField('Value', validators=[Required()])
+
+@app.route('/new', methods=['GET', 'POST'])
+def new_key():
+    form = StringForm(request.form or None)
+    if request.method == 'POST':
+        if form.validate():
+            key = request.form['key_name']
+            value = request.form['key_value']
+
+            r = get_redis_connection(session)
+            if not r:
+                return redirect(url_for('setup'))
+
+            if r.exists(key):
+                return jsonify(flash=key + ' already exists.')
+
+            r.set(key, value)
+            return jsonify(flash=key + ' was saved successfully.')
+        else:
+            return jsonify(flash='Motherfuckin\' Errors!')
+
+    return render_template('new_key.html', form=form)
+
 def get_redis_connection(session):
     """
     Get Redis connection with session values. Ping Redis
 Werkzeug==0.6.2
 -e git+https://github.com/andymccurdy/redis-py.git@52f17891a5523505ea1c4881bffc650ad97035bf#egg=redis-2.0.1-py2.6-dev
 wsgiref==0.1.2
+Flask-WTF==0.3.4
+WTForms==0.6.1

static/css/style.css

     vertical-align:middle;
   }
 
+  #switch_db #new_key {
+    float:right;
+  }
+
 #flashes {
   background:#999;
   color:#fff;
     width:28px;
   }
 
-  #setup_form {
+  #setup_form, #new_key_form {
     margin:10px 0;
   }
-    #setup_form fieldset {
+    #setup_form fieldset, #new_key_form {
       font-weight:bold;
       margin-bottom:10px;
     }
-    #setup_form input[type=text] {
+    #setup_form input[type=text], #new_key_form input[type=text] {
       border:1px solid rgba(30,30,30,0.5);
       padding:1em;
       width:100%;

static/js/redbeard.js

         });
     });
 
+    $('#new_key').live('click', function(e) {
+        var link = $(this).attr('href');
+        e.preventDefault();
+        $.get(link, function(data) {
+            $('#right').html(data);
+        });
+    });
+
 });

templates/layout.html

 	<meta charset="utf-8">
 	<title>{% block title %}Redbeard{% endblock %}</title>
 	<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.png') }}">
-	<link rel="stylesheet" href="{{ url_for('static', filename='css/html5reset-1.6.1.css') }}" 
-	media="screen" charset="utf-8">
-	<link rel="stylesheet" href="{{ url_for('static', filename='css/grid.css') }}" media="screen" charset="utf-8">
+	<link rel="stylesheet" href="{{ url_for('static', filename='css/html5reset-1.6.1.css') }}" media="screen" charset="utf-8">
 	<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" media="screen" charset="utf-8">
 	<script type="text/javascript" src="{{ url_for('static', filename='js/jquery-1.4.4.min.js') }}"></script>
 	<script type="text/javascript" src="{{ url_for('static', filename='js/modernizr-1.6.min.js') }}"></script>
 				<option value="{{ number }}" {% if number == session.redis_db %}selected{% endif %}>{{ number }}</option>
 				{% endfor %}
 			</select>
+			<a href="/new" id="new_key">New Key</a>
 		</fieldset>
 	</form>
 

templates/new_key.html

+<article>
+	<header>
+		<h1>Add New Key</h1>
+	</header>
+	<form action="/new" method="post" id="new_key_form">
+		{{ form.csrf }}
+		{{ form.key_name.label }}: {{ form.key_name }}
+		<br>
+		{{ form.key_value.label }}: {{ form.key_value }}
+		<br>
+		<input type="submit" value="Submit">
+	</form>
+
+</article>
+
+<script>
+	$(function() {
+
+		$("#new_key_form").submit(function(e) {
+			e.preventDefault();
+
+			var link = $(this).attr("action"),
+				flashes = $("#flashes");
+
+			flashes.empty();
+
+			$.ajax({
+				url: link,
+				type: "POST",
+				data: {
+					"key_name": $(this).find("input#key_name").val(),
+					"key_value": $(this).find("input#key_value").val()
+				},
+				dataType: "json",
+				success: function(data) {
+					$("<li>").text(data['flash']).appendTo(flashes);
+				}
+			});
+		});
+
+	});
+
+</script>