Chris Jones avatar Chris Jones committed 2dfbed1

Added zset form and creation view. New template. Needs a little more vaidation of the data and the ability to add multiple members but its a simple working version.

Comments (0)

Files changed (2)

 from flask import Flask, render_template, redirect, url_for, request, flash
 from flask import session, jsonify
 
-from flaskext.wtf import Form, TextField, Required, IntegerField
+from flaskext.wtf import Form, TextField, Required, IntegerField, FloatField
 
 import redis
 from redis.exceptions import ConnectionError, ResponseError
     member_name = TextField('Member', validators=[Required()])
     member_value = TextField('Value', validators=[Required()])
 
+class ZSetForm(StringForm):
+    """
+    Form for creating a ZSet
+    """
+    score = FloatField('Score', validators=[Required()])
+
 # Context processors
 @app.context_processor
 def get_db_size():
 
     return render_template('new_hash.html', form=form)
 
+@app.route('/key/new/zset', methods=['GET', 'POST'])
+def new_zset():
+    """
+    View for creating a new zset
+    """
+    form = ZSetForm(request.form or None)
+    if form.validate_on_submit():
+        key = request.form['key_name']
+        value = request.form['key_value']
+        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)
+
+        r = get_redis_connection(session)
+        if not r:
+            return redirect(url_for('setup'))
+
+        r.zadd(key, value, score)
+
+        try:
+            key_ttl = int(key_ttl)
+            r.expire(key, key_ttl)
+        except ValueError:
+            pass
+
+        flash('%s was created.' % key)
+        return redirect('#%s' % key)
+
+    return render_template('new_zset.html', form=form)
+
 @app.route('/key/delete/<key>', methods=['GET'])
 def delete(key):
     """ Delete key """

templates/new_zset.html

+{% extends "layout.html" %}
+{% block leftblock %}
+<article>
+	<header>
+		<h1>Add New ZSet</h1>
+	</header>
+	{% from "_partials.html" import render_field %}
+	<form action="{{ url_for('new_zset') }}" method="post" id="new_key_form">
+		{{ form.csrf }}
+		{{ render_field(form.key_name) }}
+		{{ render_field(form.key_value) }}
+		{{ render_field(form.score) }}
+		{{ 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 %}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.