Commits

Kenneth Love  committed 594cd12

sets with multiple members works. sets and strings with ttl works. started on lists

  • Participants
  • Parent commits 0622e13

Comments (0)

Files changed (5)

     """
     key_name = TextField('Key', validators=[Required()])
     member = TextField('Member', validators=[Required()])
-    key_ttl = IntegerField('TTL')
+    key_ttl = IntegerField('TTL (in seconds)')
+
+class ListForm(Form):
+    """
+    Form for creating a new list
+    """
+    key_name = TextField('Key', validators=[Required()])
+    member = TextField('Member', validators=[Required()])
+    key_ttl = IntegerField('TTL (in seconds)')
 
 # Context processors
 @app.context_processor
     if form.validate_on_submit():
         key = request.form['key_name']
         value = request.form['key_value']
-        ttl = request.form['key_ttl']
+        ttl = int(request.form['key_ttl'])
 
         r = get_redis_connection(session)
         if not r:
     form = SetForm(request.form or None)
     if form.validate_on_submit():
         key = request.form['key_name']
-        member = request.form['member']
-        ttl = request.form['key_ttl']
+        ttl = int(request.form['key_ttl'])
 
         r = get_redis_connection(session)
         if not r:
             return redirect(url_for('setup'))
 
-        result = r.sadd(key, member)
+        for m in [k for k in request.form.keys() if k.startswith('member')]:
+           r.sadd(key, request.form[m])
 
         if ttl and ttl != 0:
             r.expire(key, ttl)
 
-        if result:
-            flash('%s was created.' % key)
-        else:
-            flash('%s already contains that member. Nothing changed.' % key)
+        flash('%s was created.' % key)
         return redirect('#%s' % key)
 
     return render_template('new_set.html', form=form)
 
+@app.route('/key/new/list', methods=['GET', 'POST'])
+def new_list():
+    """
+    View for creating a new list with members.
+    """
+    form = ListForm(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')]:
+           r.rpush(key, request.form[m])
+
+        if ttl and ttl != 0:
+            r.expire(key, ttl)
+
+        flash('%s was created.' % key)
+        return redirect('#%s' % key)
+
+    return render_template('new_list.html', form=form)
+
 @app.route('/key/delete/<key>', methods=['GET'])
 def delete(key):
     """ Delete key """

File requirements.txt

 Flask==0.6
 Jinja2==2.5.5
 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
+-e git+https://github.com/andymccurdy/redis-py.git@5f99011ae98809ffc569793cd9896c24d1d9ee6b#egg=redis-2.2.1-py2.6-dev

File static/js/redbeard.js

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

File templates/new_list.html

+{% extends "layout.html" %}
+{% block leftblock %}
+<article>
+	<header>
+		<h1>Add New List</h1>
+	</header>
+	{% from "_partials.html" import render_field %}
+	<form action="{{ url_for('new_list') }}" method="post" id="new_key_form">
+		{{ form.csrf }}
+		{{ render_field(form.key_name) }}
+		{{ render_field(form.member) }}
+		{{ 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>
+	var add_link = $('<a class="add_member">+</a>'),
+		$member = $("#member").parent('fieldset');
+	$(function() {
+		$member.append(add_link);
+		$(".add_member").live('click', function() {
+			var $new_member = $member.clone(),
+				count = $("[id^=member]").length,
+				new_id = $new_member.find('input').attr('id') + '_' + count,
+				$insert_point = $("#key_ttl").parent('fieldset');
+			$("input", $new_member).attr({'id': new_id, 'name': new_id, 'value': ''});
+			$("label", $new_member).attr('for', new_id);
+			$new_member.insertBefore($insert_point);
+			$(this).remove();
+		});
+	});
+</script>
+{% endblock %}

File templates/new_set.html

 		<h1>Add New Set</h1>
 	</header>
 	{% from "_partials.html" import render_field %}
-	<form action="/key/new/set" method="post" id="new_key_form">
+	<form action="{{ url_for('new_set') }}" method="post" id="new_key_form">
 		{{ form.csrf }}
 		{{ render_field(form.key_name) }}
 		{{ render_field(form.member) }}
 
 </article>
 {% endblock %}
+
+{% block extra_scripts %}
+<script>
+	var add_link = $('<a class="add_member">+</a>'),
+		$member = $("#member").parent('fieldset');
+	$(function() {
+		$member.append(add_link);
+		$(".add_member").live('click', function() {
+			var $new_member = $member.clone(),
+				count = $("[id^=member]").length,
+				new_id = $new_member.find('input').attr('id') + '_' + count,
+				$insert_point = $("#key_ttl").parent('fieldset');
+			$("input", $new_member).attr({'id': new_id, 'name': new_id, 'value': ''});
+			$("label", $new_member).attr('for', new_id);
+			$new_member.insertBefore($insert_point);
+			$(this).remove();
+		});
+	});
+</script>
+{% endblock %}