Commits

Kenneth Love committed f34f74c

added TTL form field and some styles. reordered redbeard.py file to be a bit more logical

  • Participants
  • Parent commits 1c87dfd

Comments (0)

Files changed (4)

 from flask import Flask, render_template, redirect, url_for, request, flash
 from flask import session, jsonify
 
-from flaskext.wtf import Form, TextField, Required
+from flaskext.wtf import Form, TextField, Required, IntegerField
 
 import redis
 from redis.exceptions import ConnectionError, ResponseError
 class StringForm(Form):
     key_name = TextField('Key', validators=[Required()])
     key_value = TextField('Value', validators=[Required()])
+    key_ttl = IntegerField('TTL')
 
 class SetForm(Form):
     """
     key_name = TextField('Key', validators=[Required()])
     member = TextField('Member', validators=[Required()])
 
+# Context processors
 @app.context_processor
 def get_db_size():
     r = get_redis_connection(session)
 
     return dict(db_size=r.dbsize())
 
-@app.route('/new', methods=['GET', 'POST'])
-def new_key():
-    form = StringForm(request.form or None)
-    if form.validate_on_submit():
-        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.')
-
-        try:
-            r.set(key, value)
-            flash('%s was saved successfully.' % key)
-            return redirect('#%s' % key)
-        except:
-            return jsonify(flash=key + ' was not saved successfully.')
-
-    return render_template('new_key.html', form=form)
-
+# Control app flow
 def get_redis_connection(session):
     """
     Get Redis connection with session values. Ping Redis
             flash('Redis DB changed to ' + str(db))
     return redirect(request.referrer)
 
-@app.route('/info/')
-def info():
-    """ View for info about your redis set up. """
-    r = get_redis_connection(session)
-    if not r:
-        return redirect(url_for('setup'))
-
-    info = r.info().items()
-
-    return render_template('info.html', info=info)
-
 @app.route('/setup/', methods=['GET', 'POST'])
 def setup():
     """
 
     return render_template('setup.html')
 
+# Static views
+@app.route('/info/')
+def info():
+    """ View for info about your redis set up. """
+    r = get_redis_connection(session)
+    if not r:
+        return redirect(url_for('setup'))
+
+    info = r.info().items()
+
+    return render_template('info.html', info=info)
+
 @app.route('/')
 def index():
     """ All available keys. """
     else:
         return render_template('no_key.html', key=key)
 
+# Read/write views
+@app.route('/new', methods=['GET', 'POST'])
+def new_key():
+    form = StringForm(request.form or None)
+    if form.validate_on_submit():
+        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.')
+
+        try:
+            r.set(key, value)
+            flash('%s was saved successfully.' % key)
+            return redirect('#%s' % key)
+        except:
+            return jsonify(flash=key + ' was not saved successfully.')
+
+    return render_template('new_key.html', form=form)
+
 @app.route('/key/save/<key>', methods=['POST'])
 def save(key):
     """ Update the value of a key. """

File static/css/style.css

       font-weight:bold;
       margin-bottom:10px;
     }
-    #setup_form input[type=text], #setup_form input[type=password], #new_key_form input[type=text] {
+    #setup_form input[type=text],
+    #setup_form input[type=password],
+    #new_key_form input[type=text],
+    #new_key_form input[type=number] {
       border:1px solid rgba(30,30,30,0.5);
       padding:1em 2%;
       width:96%;
     }
 
+    input.optional,
+    textarea.optional {
+      color:#666;
+      border-color:rgba(150, 150, 150, 0.5) !important;
+    }
+
 footer {
   clear:both;
   color:rgba(0,0,0,0.75);

File static/js/redbeard.js

 });
 
 $(function() {
-	listFilter($('#keyheader'), $('#keylist'));
+    listFilter($('#keyheader'), $('#keylist'));
     $(".filterform").live('submit', function(e) { e.preventDefault(); });
 
     if (window.location.hash) {
             window.location.hash = '';
         });
     });
-	$("#add_key").live('click', function(e) {
-		e.stopPropagation();
-		e.preventDefault();
-		var link = $(this).attr("href");
-		$.confirm({
-			'title': 'Add new key',
-			'message': 'Choose the new key type',
-			'buttons': {
-				'String': {
-					'action': function() {
+    $("#add_key").live('click', function(e) {
+        e.stopPropagation();
+        e.preventDefault();
+        var link = $(this).attr("href");
+        $.confirm({
+            'title': 'Add new key',
+            'message': 'Choose the new key type',
+            'buttons': {
+                'String': {
+                    'action': function() {
                         window.location = '/key/new/string';
                     }
-				},
-				'List': {
-					'action': function() {
+                },
+                'List': {
+                    'action': function() {
                     }
-				},
-				'Hash': {
-					'action': function() {
-					}
-				},
-				'Set': {
-					'action': function() {
+                },
+                'Hash': {
+                    'action': function() {
+                    }
+                },
+                'Set': {
+                    'action': function() {
                         window.location = '/key/new/set';
                     }
-				},
-				'Sorted Set': {
-					'action': function() {
-					}
-				}
-			}
-		});
-	});
+                },
+                'Sorted Set': {
+                    'action': function() {
+                    }
+                }
+            }
+        });
+    });
 });

File templates/new_string.html

 		{{ form.csrf }}
 		{{ render_field(form.key_name) }}
 		{{ render_field(form.key_value) }}
+		{{ render_field(form.key_ttl, type="number", class="optional") }}
 		<a href="{{ url_for('index') }}" id="cancel">cancel</a>
 		<input type="submit" value="Submit">
 	</form>