Commit d33ec2ac authored by Francesca Sargent 's avatar Francesca Sargent
Browse files

Adding Recipe-Method-Cuisine functionality

parent efed3abe
from flask.ext.wtf import Form
import os
from wtforms import StringField, TextAreaField, BooleanField, SelectField,\
SubmitField, FormField
from wtforms.validators import Required, Length, Email, Regexp
from wtforms import ValidationError
from wtforms.validators import Required, Length, Email, Regexp, Optional
from wtforms import ValidationError, validators
from flask.ext.pagedown.fields import PageDownField
from .. import db
from ..models import Role, User, Cuisine, Recipe, RecipeSteps, Method
......@@ -59,14 +60,16 @@ class RecipeForm(Form):
name = StringField('Recipe Name', validators=[Required(), Length(0, 64)])
description = PageDownField('Recipe Description')
# recipestep = FormField(RecipeStepForm)
# cuisine = StringField('Cuisine')
# method = StringField('Method')
cuisines = StringField('Cuisine', validators=[Required()])
methods = StringField('Method', validators=[Required()])
submit = SubmitField('Submit')
# def __init__(self, *args, **kwargs):
# super(RecipeForm, self).__init__(*args, **kwargs)
# self.cuisine.choices = [(cuisine.id, cuisine.name)
# for cuisine in Cuisine.query.order_by(Cuisine.name).all()]
def __init__(self, *args, **kwargs):
super(RecipeForm, self).__init__(*args, **kwargs)
self.cuisines.choices = [(cuisine.id, cuisine.name)
for cuisine in Cuisine.query.order_by(Cuisine.name).all()]
class CuisineForm(Form):
name = TextAreaField("Cuisine Name", validators=[Required()])
......
......@@ -68,9 +68,20 @@ def poststuff(type):
form = RecipeForm()
if current_user.can(Permission.WRITE_ARTICLES) and \
form.validate_on_submit():
if Cuisine.query.filter_by(name=form.cuisines.data).first() is None:
cuisines = [Cuisine(name=form.cuisines.data, timestamp='now', author=current_user._get_current_object())]
else:
cuisines = [Cuisine.query.filter_by(name=form.cuisines.data).first()]
if Method.query.filter_by(name=form.methods.data).first() is None:
methods = [Method(name=form.methods.data, timestamp='now', author=current_user._get_current_object())]
else:
methods = [Method.query.filter_by(name=form.methods.data).first()]
recipe = Recipe(
name=form.name.data,
description=form.description.data,
cuisines = cuisines,
methods = methods,
author=current_user._get_current_object())
db.session.add(recipe)
return redirect(url_for('.index'))
......@@ -293,6 +304,19 @@ def editrecipe(id):
abort(403)
form = RecipeForm()
if form.validate_on_submit():
if Cuisine.query.filter_by(name=form.cuisines.data).first() is None:
cuisines = [Cuisine(name=form.cuisines.data, timestamp='now', author=current_user._get_current_object())]
else:
cuisines = [Cuisine.query.filter_by(name=form.cuisines.data).first()]
recipe.cuisines.extend(cuisines)
if Method.query.filter_by(name=form.methods.data).first() is None:
methods = [Method(name=form.methods.data, timestamp='now', author=current_user._get_current_object())]
else:
methods = [Method.query.filter_by(name=form.methods.data).first()]
recipe.methods.extend(methods)
recipe.name = form.name.data
recipe.description = form.description.data
db.session.add(recipe)
......
......@@ -11,6 +11,12 @@
<a href="{{ url_for('.user', username=recipe.author.username) }}">{{ recipe.author.username }}</a></div>
<div class="recipe-body">
{% if recipe.cuisines.count() > 0%}
<strong>Associated Cuisines: </strong> {% for cuisine in recipe.cuisines %} <span class="badge"> {{ cuisine.name }} </span>{% endfor %} <br/>
{% endif %}
{% if recipe.methods.count() > 0 %}
<strong>Associated Methods: </strong> {% for method in recipe.methods %} <span class="badge"> {{ method.name }} </span>{% endfor %} <br/>
{% endif %}
{% if recipe.description %}
{{ recipe.description }}
{% else %}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment