From 3b8779f348491e855a1dc715a74d63872cba82e2 Mon Sep 17 00:00:00 2001 From: Fred Turkington Date: Mon, 5 Jan 2015 23:21:05 -0600 Subject: [PATCH] Ability to edit recipes, an index page with all --- colorpicker.py | 67 +++++++++++++++++- templates/index.html => index.html | 73 ++++--------------- templates/edit.html | 88 +++++++++++++++++++++++ templates/recipes.html | 110 +++++++++++++++++++++++++++++ 4 files changed, 275 insertions(+), 63 deletions(-) rename templates/index.html => index.html (66%) create mode 100644 templates/edit.html create mode 100644 templates/recipes.html diff --git a/colorpicker.py b/colorpicker.py index 0e1b631..d5e50c0 100644 --- a/colorpicker.py +++ b/colorpicker.py @@ -44,6 +44,9 @@ def slugify(text, delim=u'-'): result.append(word) return unicode(delim.join(result)) +def gettitle(recipe): + return recipe['title'] + from flask.ext.wtf import Form from wtforms.fields import TextField from wtforms.validators import Required @@ -99,6 +102,12 @@ def teardown_request(exception): pass +@app.route('/') +def index(): + allrecipes = list(r.table('recipes').run(g.rdb_conn)) + allrecipes.sort(key=gettitle) + return render_template("index.html", allrecipes=allrecipes) + @app.route('/favicon.ico') def favicon(): abort(404) @@ -137,7 +146,7 @@ def add(): r.table('recipes').insert(recipe).run(g.rdb_conn) - return redirect(url_for('index', query=slug)) + return redirect(url_for('recipe', query=slug)) #r.table('recipes').get('de670bed-791d-41d0-97cb-1ceaf9ba54ac').update({'time_updated': r.now(), 'slug': "broccoli-cheese-soup", 'urls': urls, 'avgcolors': [list(i) for i in rainbow]}).run(g.rdb_conn) @@ -146,7 +155,7 @@ def add(): @app.route('/') -def index(query): +def recipe(query): recipe = list(r.table('recipes').filter({'slug': query}).run(g.rdb_conn)) if recipe: @@ -162,7 +171,59 @@ def index(query): rainbow = [tuple(l) for l in recipe['avgcolors']] - return render_template("index.html", urls = recipe['urls'], avg=rainbow, ingredients = recipe['ingredients'], steps = steps, title=recipe['title']) + return render_template("recipes.html", urls = recipe['urls'], avg=rainbow, ingredients = recipe['ingredients'], steps = steps, title=recipe['title'], query=query) + + +@app.route('//edit', methods = ['GET', 'POST']) +def edit(query): + + + form = RecipeForm() + recipe = list(r.table('recipes').filter({'slug': query}).run(g.rdb_conn)) + + if recipe: + recipe = recipe[0] + else: + abort(404) + + if request.method == 'GET': + + form.ingredients.data = "\r\n".join([(i['amount'] + " " + i['what']) for i in recipe['ingredients']]) + form.directions.data = "\r\n".join(i for i in recipe['directions']) + form.title.data = recipe['title'] + + return render_template("edit.html", form=form, recipe=recipe) + + if request.method == 'POST' and form.validate(): + + lightness = [] + rainbow = [] + resp=[] + urls = get_gimages("+".join(form.title.data.split())) + for url in urls: + i = cStringIO.StringIO(urllib.urlopen(url).read()) + quiche = colorific.extract_colors(i, max_colors=5) + resp.extend([each.value for each in quiche.colors]) + resp = [(m,sqrt(0.299 * m[0]**2 + 0.587 * m[1]**2 + 0.114 * m[2]**2)) for m in resp] + lightness = sorted(resp,key=lambda x: x[1]) + lightness = [i[0] for i in lightness] + lightness.sort(key=lambda tup: colorsys.rgb_to_hsv(tup[0],tup[1],tup[2])[2]) + for each in chunks(lightness,10): + avg = tuple(map(lambda y: sum(y) / len(y), zip(*each))) + rainbow.append(avg) + + slug = slugify(form.title.data) + + id = recipe['id'] + + recipe = { 'title': form.title.data, 'ingredients': [{'amount': " ".join(ingredient.split()[0:2]), 'what': " ".join(ingredient.split()[2:])} for ingredient in form.ingredients.data.split('\r\n')], 'directions': form.directions.data.split('\r\n'), 'urls': urls, 'slug': slug, 'avgcolors': [list(i) for i in rainbow]} + + recipe['ingredients'] = [i for i in recipe['ingredients'] if not i['what']==''] + recipe['directions'] = [i for i in recipe['directions'] if not i==''] + + r.table('recipes').get(id).update(recipe).run(g.rdb_conn) + + return redirect(url_for('recipe', query=slug)) if __name__ == '__main__': diff --git a/templates/index.html b/index.html similarity index 66% rename from templates/index.html rename to index.html index 79e2987..adc0e9d 100644 --- a/templates/index.html +++ b/index.html @@ -4,48 +4,23 @@ - Recipes + CarrotStick Dev - Home + @@ -72,14 +45,14 @@
-
-

{{title}}

+
+

Zucchini and Onion Pie

-

Ingredients +

Debates You Might Like
@@ -90,42 +63,22 @@

Ingredients - {% for i in ingredients %} - - + + - {% endfor %} - -
{{i.amount}}{{i.what}}1 cupbutter
-
-
-
-
-
-
-

Directions -
-
- - - {% for step in steps %} - - + + - {% endfor %}
{{loop.index}}{{step}}1 ozvanilla
-
-
-
-{% for i in urls %} -
-{% endfor%}
+
+

bu maize bamboo shoot green bean swiss chard seakale pumpkin onion chickpea gram corn pea. Brussels sprout coriander water chestnut gourd swiss chard wakame kohlrabi beetroot carrot watercress. Corn amaranth salsify bunya nuts nori azuki bean chickweed potato bell pepper artichoke.
+ diff --git a/templates/edit.html b/templates/edit.html new file mode 100644 index 0000000..77217e7 --- /dev/null +++ b/templates/edit.html @@ -0,0 +1,88 @@ + + + + + + + Edit {{recipe.title}} + + + + + + + + + + + + +
+ + +
+ {{form.hidden_tag()}} + +
+
+

Edit {{recipe.title}}

+ +
+
+

Title

+
+
+ {{form.title}} +
+
+
+ +
+
+ +
+
+
+

Ingredients

+
+
+ {{ form.ingredients(rows="10") }} +
+
+
+
+
+
+

Directions +
+
+ {{ form.directions(rows="10") }} +
+
+
+ + + {% for error in form.errors.label %} + {{ error }} + {% endfor %} +
+ +
+
+ + + + + \ No newline at end of file diff --git a/templates/recipes.html b/templates/recipes.html new file mode 100644 index 0000000..6eee0ba --- /dev/null +++ b/templates/recipes.html @@ -0,0 +1,110 @@ + + + + + + + Recipes + + + + + + + + + + + + +
+
+

{{title}} Edit

+
+
+
+
+
+

Ingredients

+
+
+ + + + + + + + + {% for i in ingredients %} + + + + + {% endfor %} + +
AmountIngredient
{{i.amount}}{{i.what}}
+
+
+
+
+
+
+

Directions +
+
+ + + {% for step in steps %} + + + + + {% endfor %} + +
{{loop.index}}{{step}}
+
+
+
+
+
+{% for i in urls %} +
+{% endfor%} +
+
+ +