Skip to content

Commit

Permalink
Ability to edit recipes, an index page with all
Browse files Browse the repository at this point in the history
  • Loading branch information
z3ugma committed Jan 6, 2015
1 parent 8e5f99d commit 3b8779f
Show file tree
Hide file tree
Showing 4 changed files with 275 additions and 63 deletions.
67 changes: 64 additions & 3 deletions colorpicker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -146,7 +155,7 @@ def add():


@app.route('/<query>')
def index(query):
def recipe(query):
recipe = list(r.table('recipes').filter({'slug': query}).run(g.rdb_conn))

if recipe:
Expand All @@ -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('/<query>/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__':
Expand Down
73 changes: 13 additions & 60 deletions templates/index.html → index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,23 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Recipes</title>
<title>CarrotStick Dev - Home</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"></style>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css">

<link rel="stylesheet" href="lavish-bootstrap.css">

<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Ubuntu">
<style>
body {
font-family: 'Ubuntu', serif;
background-color: rgb{{avg[3]}};

}

.panel-primary{
border-color: rgb{{avg[1]}};

}

.panel-primary > .panel-heading {
background-color: rgb{{avg[1]}};
border-color: rgb{{avg[1]}};

}

kbd {

background-color: rgb{{avg[0]}}
}

.img-container { width: 100%; display: flex; }

img { width: 100%; }

.img-wrapper
{
flex: 1;
}

.overlay{
min-height: 20em;
height: 100%;
/*border-radius: 5px;*/
position: relative;
Expand All @@ -61,8 +36,6 @@


}




</style>
Expand All @@ -72,14 +45,14 @@


<div class="col-md-12">
<div class="header" style="background-color: rgb{{avg[0]}}; color: rgb{{avg[3]}}; margin-bottom: 20px; padding: 20px 10px;">
<h1>{{title}}</h1>
<div class="header" style="background-color: #6D2C38; color: #DBE0E4; margin-bottom: 20px; padding: 20px 10px;">
<h1>Zucchini and Onion Pie</h1>
</div>
</div>
<div class="col-md-3">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"></h3>Ingredients
<h3 class="panel-title"></h3>Debates You Might Like
</div>
<div class="panel-body">
<table class="table table-striped table-hover">
Expand All @@ -90,42 +63,22 @@ <h3 class="panel-title"></h3>Ingredients
</tr>
</thead>
<tbody>
{% for i in ingredients %}
<tr>
<td>{{i.amount}}</td>
<td>{{i.what}}</td>
<td>1 cup</td>
<td>butter</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<div class="col-md-9" style="font-size:150%">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"></h3> Directions </h3>
</div>
<div class="panel-body">
<table class="table table-striped table-hover">
<tbody>
{% for step in steps %}
<tr>
<td>{{loop.index}}</td>
<td>{{step}}</td>
<td>1 oz</td>
<td>vanilla</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<div class="container">
<div class="col-md-12 img-container">
{% for i in urls %}
<div class="img-wrapper"> <img src="{{i}}" /></div>
{% endfor%}
</div>
<div class="col-md-9">
<p></p>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.
</div>

</body>
</html>
88 changes: 88 additions & 0 deletions templates/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Edit {{recipe.title}}</title>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"></style>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css">


<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Ubuntu">

<style>
body {
font-family: 'Ubuntu', serif;

}
textarea {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
width: 100%;
height:100%;
}
</style>
</head>
<body>
<div class="container">


<form action="" role="form" method="POST" name="add_task">
{{form.hidden_tag()}}

<div class="row">
<div class="col-md-12">
<h1>Edit {{recipe.title}}</h1>

<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Title</h3>
</div>
<div class="panel-body">
{{form.title}}
</div>
</div>
</div>

</div>
<div class="row">

<div class="col-md-3">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Ingredients</h3>
</div>
<div class="panel-body" >
{{ form.ingredients(rows="10") }}
</div>
</div>
</div>
<div class="col-md-9">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"></h3> Directions </h3>
</div>
<div class="panel-body">
{{ form.directions(rows="10") }}
</div>
</div>
</div>

<input type="submit" value="Update Recipe" class="btn btn-default btn-md">
{% for error in form.errors.label %}
<span style="color: red;">{{ error }}</span>
{% endfor %}
</div>

</form>
</div>



</body>
</html>
Loading

0 comments on commit 3b8779f

Please sign in to comment.