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

Can now submit multiple Cuisines and Methods within one transaction

parent 0202b9f2
from flask.ext.wtf import Form
import os
import wtforms
from wtforms import StringField, TextAreaField, BooleanField, SelectField,\
SubmitField, FormField, IntegerField, FloatField, FieldList
SubmitField, FormField, IntegerField, FloatField, FieldList, Field
from wtforms.validators import Required, Length, Email, Regexp, Optional
from wtforms import ValidationError, validators
from wtforms import ValidationError, validators, widgets
from flask.ext.pagedown.fields import PageDownField
from .. import db
from ..models import Role, User, Cuisine, Recipe, RecipeSteps, Method
......@@ -52,21 +53,35 @@ class EditProfileAdminForm(Form):
raise ValidationError('Username already in use.')
class RecipeStepForm(Form):
step_id = IntegerField('Step Number', [validators.NumberRange(min=0, max=10)])
# step_id = IntegerField('Step Number', [validators.NumberRange(min=0, max=10)])
step_text = TextAreaField()
def __init__(self, *args, **kwargs):
kwargs['csrf_enabled'] = False
super(RecipeStepForm, self).__init__(*args, **kwargs)
class SplitStringField(StringField):
def _value(self):
if self.data:
return u', '.join(self.data)
else:
return u''
def process_formdata(self, valuelist):
if valuelist:
self.data = [x.strip() for x in valuelist[0].split(',')]
else:
self.data = []
class RecipeForm(Form):
name = StringField('Recipe Name', validators=[Required(), Length(0, 64)])
description = PageDownField('Recipe Description')
steps = FormField(RecipeStepForm)
cuisines = StringField('Cuisine', validators=[Required()])
methods = StringField('Method', validators=[Required()])
steps = FieldList(FormField(RecipeStepForm), min_entries=10)
cuisines = SplitStringField()
methods = SplitStringField()
submit = SubmitField('Submit')
def __init__(self, *args, **kwargs):
......@@ -79,7 +94,7 @@ class RecipeForm(Form):
class CuisineForm(Form):
name = TextAreaField("Cuisine Name", validators=[Required()])
description = PageDownField("Cuisine Description", validators=[Required()])
methods = StringField('Method')
methods = SplitStringField()
submit = SubmitField('Submit')
def __init__(self, *args, **kwargs):
......@@ -91,7 +106,7 @@ class MethodForm(Form):
name = StringField("Method Name", validators=[Required()])
description = PageDownField('Method Description')
method_text = PageDownField('Method Instructions')
cuisines = StringField('Cuisine')
cuisines = SplitStringField()
submit = SubmitField('Submit')
def __init__(self, *args, **kwargs):
......@@ -100,6 +115,7 @@ class MethodForm(Form):
for cuisine in Cuisine.query.order_by(Cuisine.name).all()]
class CommentForm(Form):
body = StringField('Enter your comment', validators=[Required()])
submit = SubmitField('Submit')
......@@ -64,25 +64,55 @@ def poststuff(type):
db.session.autoflush = True
def addCuisine(name):
if Cuisine.query.filter_by(name=name).first() is None:
cuisines = [Cuisine(name=name, timestamp='now', author=current_user._get_current_object())]
else:
cuisines = [Cuisine.query.filter_by(name=name).first()]
return cuisines
def addMethod(name):
if Method.query.filter_by(name=name).first() is None:
methods = [Method(name=name, timestamp='now', author=current_user._get_current_object())]
else:
methods = [Method.query.filter_by(name=name).first()]
return methods
if type == 'recipe':
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())]
recipe = Recipe()
recipe.name = form.name.data
recipe.description = form.description.data
if form.cuisines.data >= 1:
for i, cuisine in enumerate (form.cuisines.data):
addCuisine(cuisine)
if i is 0:
recipe.cuisines = addCuisine(cuisine)
else:
cuisine = addCuisine(cuisine)
recipe.cuisines.extend(cuisine)
else:
recipe.cuisines = addCuisine(form.cuisines.data)
if form.methods.data >= 1:
for i, method in enumerate (form.methods.data):
addMethod(method)
if i is 0:
recipe.methods = addMethod(method)
else:
method = addMethod(method)
recipe.methods.extend(method)
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())
recipe.methods = addCuisine(form.methods.data)
recipe.author = current_user._get_current_object()
db.session.add(recipe)
return redirect(url_for('.index'))
......@@ -91,14 +121,23 @@ def poststuff(type):
if current_user.can(Permission.WRITE_ARTICLES) and \
form.validate_on_submit():
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())]
cuisine = Cuisine()
cuisine.name = form.name.data
cuisine.description = form.description.data
if form.methods.data >= 1:
for i, method in enumerate (form.methods.data):
addMethod(method)
if i is 0:
cuisine.methods = addMethod(method)
else:
method = addMethod(method)
cuisine.methods.extend(method)
else:
methods = [Method.query.filter_by(name=form.methods.data).first()]
cuisine = Cuisine(name=form.name.data, description=form.description.data,
methods=methods,
author=current_user._get_current_object())
cuisine.methods = addCuisine(form.methods.data)
cuisine.author = current_user._get_current_object()
db.session.add(cuisine)
db.session.flush()
return redirect(url_for('.index'))
......@@ -108,17 +147,24 @@ def poststuff(type):
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())]
method = Method()
method.name = form.name.data
method.description = form.description.data
method.method_text = form.method_text.data
if form.cuisines.data >= 1:
for i, cuisine in enumerate (form.cuisines.data):
addCuisine(cuisine)
if i is 0:
method.cuisines = addCuisine(cuisine)
else:
cuisine = addCuisine(cuisine)
method.cuisines.extend(cuisine)
else:
cuisines = [Cuisine.query.filter_by(name=form.cuisines.data).first()]
method = Method(
name=form.name.data,
description=form.description.data,
method_text=form.method_text.data,
cuisines=cuisines,
author=current_user._get_current_object())
method.cuisines = addCuisine(form.cuisines.data)
method.author = current_user._get_current_object()
db.session.add(method)
db.session.flush()
return redirect(url_for('.index'))
......@@ -305,26 +351,39 @@ def editrecipe(id):
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)
for cuisine in form.cuisines.data:
if Cuisine.query.filter_by(name=cuisine).first() is None:
cuisines = [Cuisine(name=cuisine, timestamp='now', author=current_user._get_current_object())]
else:
cuisines = [Cuisine.query.filter_by(name=cuisine).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)
for method in form.methods.data:
if Method.query.filter_by(name=method).first() is None:
methods = [Method(name=method, timestamp='now', author=current_user._get_current_object())]
else:
methods = [Method.query.filter_by(name=method).first()]
recipe.methods.extend(methods)
recipe.name = form.name.data
recipe.description = form.description.data
recipe.steps = [RecipeSteps(step_id = form.steps.step_id.data, step_text = form.steps.step_text.data)]
db.session.add(recipe)
# recipe.steps = [RecipeSteps(step_id = form.steps.step_id.data, step_text = form.steps.step_text.data)]
for index,step_entry in enumerate(form.steps.entries):
step = RecipeSteps()
step.step_id = index
step.step_text = step_entry.step_text.data
# step.recipe_name = [Recipe.query.filter_by(name=form.name.data).first()]
db.session.merge(step)
db.session.flush()
db.session.add(recipe)
flash('The recipe has been updated.')
return redirect(url_for('.recipe', id=recipe.id))
form.description.data = recipe.description
form.name.data = recipe.name
form.steps.choices = {'a' : 'b'}
return render_template('edit_recipe.html', form=form)
@main.route('/method/<int:id>/edit', methods=['GET', 'POST'])
......@@ -338,10 +397,11 @@ def editmethod(id):
form = MethodForm()
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()]
for cuisine in form.cuisines.data:
if Cuisine.query.filter_by(name=cuisine).first() is None:
cuisines = [Cuisine(name=cuisine, timestamp='now', author=current_user._get_current_object())]
else:
cuisines = [Cuisine.query.filter_by(name=cuisine).first()]
method.name = form.name.data
method.description = form.description.data
......@@ -369,12 +429,13 @@ def editcuisine(id):
cuisine.name = form.name.data
cuisine.description = form.description.data
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()]
cuisine.methods.extend(methods)
for method in form.methods.data:
if Method.query.filter_by(name=method).first() is None:
methods = [Method(name=method, timestamp='now', author=current_user._get_current_object())]
else:
methods = [Method.query.filter_by(name=method).first()]
cuisine.methods.extend(methods)
db.session.add(cuisine)
flash('The cuisine has been updated.')
return redirect(url_for('.cuisine', id=cuisine.id))
......
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