Commits

Kenneth Love committed cb3afa9

zsets works with multiple members

  • Participants
  • Parent commits ed989f8

Comments (0)

Files changed (4)

 app.debug = True
 
 # Forms
-class StringForm(Form):
+class KeyForm(Form):
     key_name = TextField('Key', validators=[Required()])
-    key_value = TextField('Value', validators=[Required()])
     key_ttl = IntegerField('TTL')
 
-class ListSetForm(Form):
+class StringForm(KeyForm):
+    key_value = TextField('Value', validators=[Required()])
+
+class ListSetForm(KeyForm):
     """
     Form for creating a new set or list
     """
-    key_name = TextField('Key', validators=[Required()])
     member = TextField('Member', validators=[Required()])
-    key_ttl = IntegerField('TTL (in seconds)')
 
-class HashForm(Form):
+class HashForm(KeyForm):
     """
     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()])
 
-class ZSetForm(ListSetForm):
+class ZSetForm(KeyForm):
     """
     Form for creating a ZSet
     """
-    score = FloatField('Score', validators=[Required()])
+    member_name = TextField('Member', validators=[Required()])
+    member_score = FloatField('Score', validators=[Required()])
 
 # Context processors
 @app.context_processor
     form = ZSetForm(request.form or None)
     if form.validate_on_submit():
         key = request.form['key_name']
-        member = request.form['member']
-        key_ttl = request.form['key_ttl']
-        score = request.form['score']
-
-        try:
-            score = float(score)
-        except ValueError:
-            flash('Score must be an int or float')
-            return render_template('new_zset.html', form=form)
+        ttl = int(request.form['key_ttl'])
 
         r = get_redis_connection(session)
         if not r:
             return redirect(url_for('setup'))
 
-        r.zadd(key, member, score)
+        for m in [k for k in request.form.keys() if k.startswith('member_name')]:
+            v = re.sub('name', 'score', m)
+            r.zadd(key, request.form[m], float(request.form[v]))
 
-        try:
-            key_ttl = int(key_ttl)
-            if key_ttl > 0:
-                r.expire(key, key_ttl)
-        except ValueError:
-            pass
+        if ttl and ttl != 0:
+            r.expire(key, ttl)
 
         flash('%s was created.' % key)
         return redirect('#%s' % key)

File templates/_partials.html

 		</thead>
 		<tbody>
 			<tr>
-				<td>{{ field1(**kwargs)|safe }}</td>
+				<td>{{ field1|safe }}</td>
 				<td>{{ field2(**kwargs)|safe }}</td>
 			</tr>
 		</tbody>
 	</table>
 </fieldset>
 {% endmacro %}
+
+{% macro key_list(key) %}
+<li>
+	<a href="{{ url_for('key', key=key) }}">{{ key }}</a>
+</li>
+{% endmacro %}

File templates/index.html

 	<h1>Keys</h1>
 </header>
 <ul id="keylist">
+	{% from "_partials.html" import key_list %}
 	{% for key in keys %}
-	<li><a href="{{ url_for('key', key=key) }}">{{ key }}</a></li>
+		{{ key_list(key) }}
 	{% endfor %}
 </ul>
 {% endblock %}

File templates/new_zset.html

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