Commits

Kenneth Love committed 83edc94

add hashes like a boss

  • Participants
  • Parent commits 9342c89

Comments (0)

Files changed (6)

     member = TextField('Member', validators=[Required()])
     key_ttl = IntegerField('TTL (in seconds)')
 
+class HashForm(Form):
+    """
+    Form for creating a hash.
+    """
+    key_name = TextField('Key', validators=[Required()])
+    key_ttl = IntegerField('TTL (in seconds)')
+    member_name = TextField('Member', validators=[Required()])
+    member_value = TextField('Value', validators=[Required()])
+
 # Context processors
 @app.context_processor
 def get_db_size():
 
     return render_template('new_list.html', form=form)
 
+@app.route('/key/new/hash', methods=['GET', 'POST'])
+def new_hash():
+    """
+    View for creating a new list with members.
+    """
+    form = HashForm(request.form or None)
+    if form.validate_on_submit():
+        key = request.form['key_name']
+        ttl = int(request.form['key_ttl'])
+
+        r = get_redis_connection(session)
+        if not r:
+            return redirect(url_for('setup'))
+
+        for m in [k for k in request.form.keys() if k.startswith('member_name')]:
+            v = re.sub('name', 'value', m)
+            r.hset(key, request.form[m], request.form[v])
+
+        if ttl and ttl != 0:
+            r.expire(key, ttl)
+
+        flash('%s was created.' % key)
+        return redirect('#%s' % key)
+
+    return render_template('new_hash.html', form=form)
+
 @app.route('/key/delete/<key>', methods=['GET'])
 def delete(key):
     """ Delete key """

File static/css/style.css

       text-indent:-99999px;
     }
     #add_key,
-    .add_member {
+    .add_member,
+    .add_member_table {
       background:url('../img/add.png') center center no-repeat;
       display:block;
       float:left;
     #members {
       position:relative;
     }
-    .add_member {
+      #members table {
+        width:100%;
+      }
+    .add_member,
+    .add_member_table {
       bottom:10px;
       position:absolute;
       right:-30px;

File static/js/newkeys.js

 	$new_member.appendTo($("#members"));
 	$new_member.find('input').focus();
 });
+
+$(".add_member_table").live('click', function() {
+	var $member = $("table:eq(0)", "#members").parent('fieldset'),
+        $new_member = $member.clone(),
+        $members = $("#members"),
+		count = $("[id^=member]").length,
+		new_id = $new_member.find('input').attr('id') + '_' + count,
+		$insert_point = $("#key_ttl").parent('fieldset');
+	$("input", $new_member).each(function() {
+        $(this).attr({
+            'id': $(this).attr('id') + '_' + count,
+            'name': $(this).attr('name') + '_' + count,
+            'value': ''
+        });
+    });
+	$("label", $new_member).each(function() {
+        $(this).attr('for', $(this).attr('for') + '_' + count);
+    });
+	$new_member.appendTo($("#members"));
+	$new_member.find('input:eq(0)').focus();
+});

File static/js/redbeard.js

                 },
                 'Hash': {
                     'action': function() {
+                        window.location = '/key/new/hash';
                     }
                 },
                 'Set': {

File templates/_partials.html

 	{{ field(**kwargs)|safe }}
 </fieldset>
 {% endmacro %}
+
+{% macro render_keyvalue(field1, field2) %}
+<fieldset>
+	{% if field1.errors or field2.errors %}
+	<ul class="errors">
+		{% for error in field1.errors %}
+		<li>{{ error }}</li>
+		{% endfor %}
+		{% for error in field2.errors %}
+		<li>{{ error }}</li>
+		{% endfor %}
+	</ul>
+	{% endif %}
+	<table>
+		<thead>
+			<tr>
+				<th>{{ field1.label }}</th>
+				<th>{{ field2.label }}</th>
+			</tr>
+		</thead>
+		<tbody>
+			<tr>
+				<td>{{ field1(**kwargs)|safe }}</td>
+				<td>{{ field2(**kwargs)|safe }}</td>
+			</tr>
+		</tbody>
+	</table>
+</fieldset>
+{% endmacro %}

File templates/new_hash.html

+{% extends "layout.html" %}
+{% block leftblock %}
+<article>
+	<header>
+		<h1>Add New Set</h1>
+	</header>
+	{% from "_partials.html" import render_field, render_keyvalue %}
+	<form action="{{ url_for('new_hash') }}" method="post" id="new_key_form">
+		{{ form.csrf }}
+		{{ render_field(form.key_name) }}
+		<fieldset id="members">
+			<a class="add_member_table">+</a>
+			{{ render_keyvalue(form.member_name, form.member_value) }}
+		</fieldset>
+		{{ render_field(form.key_ttl, type="number", class="optional") }}
+		<a href="{{ url_for('index') }}" id="cancel">cancel</a>
+		<input type="submit" value="Submit">
+	</form>
+
+</article>
+{% endblock %}
+
+{% block extra_scripts %}
+<script src="{{ url_for('static', filename='js/newkeys.js') }}"></script>
+{% endblock %}