Commit 93c739ed authored by Francesca Sargent 's avatar Francesca Sargent
Browse files

Extremely simple Flavour functionality

parent 5c086901
...@@ -6,9 +6,10 @@ from wtforms import StringField, TextAreaField, BooleanField, SelectField,\ ...@@ -6,9 +6,10 @@ from wtforms import StringField, TextAreaField, BooleanField, SelectField,\
SubmitField, FormField, IntegerField, FloatField, FieldList, Field SubmitField, FormField, IntegerField, FloatField, FieldList, Field
from wtforms.validators import Required, Length, Email, Regexp, Optional from wtforms.validators import Required, Length, Email, Regexp, Optional
from wtforms import ValidationError, validators, widgets from wtforms import ValidationError, validators, widgets
from flask.ext.pagedown.fields import PageDownField from flask.ext.pagedown.fields import PageDownField
from .. import db from .. import db
from ..models import Role, User, Cuisine, Recipe, RecipeSteps, Method, Ingredient from ..models import Role, User, Cuisine, Recipe, RecipeSteps, Method, Ingredient, Flavour, FlavourType
def chomp(s): def chomp(s):
...@@ -20,6 +21,7 @@ class NameForm(Form): ...@@ -20,6 +21,7 @@ class NameForm(Form):
name = StringField('What is your name?', validators=[Required()]) name = StringField('What is your name?', validators=[Required()])
submit = SubmitField('Submit') submit = SubmitField('Submit')
class SplitStringField(StringField): class SplitStringField(StringField):
def process_formdata(self, valuelist): def process_formdata(self, valuelist):
...@@ -48,12 +50,41 @@ class EditProfileForm(Form): ...@@ -48,12 +50,41 @@ class EditProfileForm(Form):
about_me = TextAreaField('About me') about_me = TextAreaField('About me')
submit = SubmitField('Submit') submit = SubmitField('Submit')
class FlavourForm(Form):
flavourtype_id = SelectField(coerce=int)
ingredient_id = SelectField(coerce=int)
prevalence = IntegerField()
def __init__(self, *args, **kwargs):
kwargs['csrf_enabled'] = False
super(FlavourForm, self).__init__(*args, **kwargs)
self.flavourtype_id.choices = [(flavourtype.id, flavourtype.name)
for flavourtype in FlavourType.query.order_by(FlavourType.id).all()]
self.ingredient_id.choices = [(ingredient.id, ingredient.name)
for ingredient in Ingredient.query.order_by(Ingredient.id).all()]
class FlavourTypeForm(Form):
name = SelectField()
characteristics = TextAreaField('Flavour Characteristics')
flavours = FormField(FlavourForm)
def __init__(self, *args, **kwargs):
kwargs['csrf_enabled'] = False
super(FlavourTypeForm, self).__init__(*args, **kwargs)
self.name.choices = [(flavour.name, flavour.name)
for flavour in FlavourType.query.order_by(FlavourType.name).all()]
class IngredientForm(Form): class IngredientForm(Form):
name = StringField('Ingredient Name', validators=[Required(), Length(0,64)]) name = StringField('Ingredient Name', validators=[Required(), Length(0,64)])
description = TextAreaField('Description') description = TextAreaField('Description')
flavours = FormField(FlavourTypeForm)
submit = SubmitField('Submit') submit = SubmitField('Submit')
class EditProfileAdminForm(Form): class EditProfileAdminForm(Form):
email = StringField('Email', validators=[Required(), Length(1, 64), email = StringField('Email', validators=[Required(), Length(1, 64),
Email()]) Email()])
...@@ -144,8 +175,6 @@ class MethodForm(Form): ...@@ -144,8 +175,6 @@ class MethodForm(Form):
self.cuisines.choices = [(cuisine.id, cuisine.name) self.cuisines.choices = [(cuisine.id, cuisine.name)
for cuisine in Cuisine.query.order_by(Cuisine.name).all()] for cuisine in Cuisine.query.order_by(Cuisine.name).all()]
class CommentForm(Form): class CommentForm(Form):
body = StringField('Enter your comment', validators=[Required()]) body = StringField('Enter your comment', validators=[Required()])
submit = SubmitField('Submit') submit = SubmitField('Submit')
...@@ -10,9 +10,9 @@ from wtforms import ValidationError, validators, widgets ...@@ -10,9 +10,9 @@ from wtforms import ValidationError, validators, widgets
from flask.ext.pagedown.fields import PageDownField from flask.ext.pagedown.fields import PageDownField
from .forms import EditProfileForm, EditProfileAdminForm, RecipeForm,\ from .forms import EditProfileForm, EditProfileAdminForm, RecipeForm,\
CommentForm, CuisineForm, RecipeStepForm, MethodForm, RecipeEditForm, SplitStringField, \ CommentForm, CuisineForm, RecipeStepForm, MethodForm, RecipeEditForm, SplitStringField, \
IngredientForm IngredientForm, FlavourTypeForm, FlavourForm
from .. import db from .. import db
from ..models import Permission, Role, User, Recipe, Comment, Cuisine, RecipeSteps, Method, Ingredient from ..models import Permission, Role, User, Recipe, Comment, Cuisine, RecipeSteps, Method, Ingredient, FlavourType, Flavour
from ..decorators import admin_required, permission_required from ..decorators import admin_required, permission_required
...@@ -565,6 +565,16 @@ def editcuisine(id): ...@@ -565,6 +565,16 @@ def editcuisine(id):
form.methods.data = returnListed(cuisine.methods) form.methods.data = returnListed(cuisine.methods)
return render_template('edit_recipe.html', form=form, type='cuisine') return render_template('edit_recipe.html', form=form, type='cuisine')
def addFlavourType(namedata, characteristicdata):
flavourtype = FlavourType.query.filter_by(name=namedata).first()
flavourtype.name = namedata
flavourtype.characteristics = characteristicdata
db.session.add(flavourtype)
def addFlavour(flavour_ingredient_id, flavour_flavourtype_id, flavour_prevalence):
flavour = Flavour(id=str(flavour_flavourtype_id) + str(flavour_ingredient_id), flavourtype_id=flavour_flavourtype_id, ingredient_id=flavour_ingredient_id, prevalence=flavour_prevalence, timestamp='now', author=current_user._get_current_object())
db.session.add(flavour)
@main.route('/ingredient/<int:id>/edit', methods=['GET', 'POST']) @main.route('/ingredient/<int:id>/edit', methods=['GET', 'POST'])
@login_required @login_required
def editingredient(id): def editingredient(id):
...@@ -578,13 +588,85 @@ def editingredient(id): ...@@ -578,13 +588,85 @@ def editingredient(id):
ingredient.name = form.name.data ingredient.name = form.name.data
ingredient.description = form.description.data ingredient.description = form.description.data
# Creating a Flavour Type (the general flavour), which will link to Specific Flavour Details
flavourtype_name = form.flavours['name'].data
flavourtype_characteristics = form.flavours['characteristics'].data
addFlavourType(flavourtype_name, flavourtype_characteristics)
# Creating a Flavour, linking to Flavour Type and Ingredient
flavour_ingredient_id = ingredient.id
flavour_flavourtype_id = form.flavours.flavours['flavourtype_id'].data
flavour_prevalence = form.flavours.flavours['prevalence'].data
addFlavour(flavour_ingredient_id, flavour_flavourtype_id, flavour_prevalence)
db.session.add(ingredient) db.session.add(ingredient)
flash('The ingredient has been updated.') flash('The ingredient has been updated.')
return redirect(url_for('.ingredient', id=ingredient.id)) return redirect(url_for('.ingredient', id=ingredient.id))
form.name.data = ingredient.name form.name.data = ingredient.name
form.description.data = ingredient.description form.description.data = ingredient.description
return render_template('edit_recipe.html', form=form, type='cuisine')
for flavour in ingredient.flavours:
print flavour
form.flavours.flavours['flavourtype_id'].data = flavour.flavourtype.name
form.flavours['characteristics'].data = flavour.flavourtype.characteristics
for flavour in flavour.flavourtype.flavours:
form.flavours.flavours['prevalence'].data = flavour.prevalence
form.flavours.flavours['ingredient_id'].data = ingredient.id
return render_template('edit_recipe.html', form=form, type='ingredient')
@main.route('/flavourtype/<int:id>/edit', methods=['GET', 'POST'])
@login_required
def editflavourtype(id):
db.session.autoflush = True
flavourtype = FlavourType.query.get_or_404(id)
if current_user != flavourtype.author and \
not current_user.can(Permission.ADMINISTER):
abort(403)
form = FlavourTypeForm()
if form.validate_on_submit():
flavourtype.name = form.name.data
flavourtype.characteristics = form.characteristics.data
db.session.add(flavourtype)
flash('The flavour type has been updated.')
return redirect(url_for('.index'))
form.name.data = flavourtype.name
form.characteristics.data = flavourtype.characteristics
return render_template('edit_recipe.html', form=form, type='flavourtype')
@main.route('/flavour/<int:id>/edit', methods=['GET', 'POST'])
@login_required
def editflavour(id):
db.session.autoflush = True
flavour = Flavour.query.get_or_404(id)
if current_user != flavour.author and \
not current_user.can(Permission.ADMINISTER):
abort(403)
form = FlavourForm()
if form.validate_on_submit():
flavour.flavourtype_id = form.flavourtype_id.data
flavour.ingredient_id = form.ingredient_id.data
flavour.prevalence = form.prevalence.data
db.session.add(flavour)
flash('The flavour has been updated.')
return redirect(url_for('.index'))
form.flavourtype_id.data = flavour.flavourtype_id
form.ingredient_id.data = flavour.ingredient_id
form.prevalence.data = flavour.prevalence
return render_template('edit_recipe.html', form=form, type='flavourtype')
@main.route('/follow/<username>') @main.route('/follow/<username>')
......
...@@ -76,6 +76,8 @@ class User(UserMixin, db.Model): ...@@ -76,6 +76,8 @@ class User(UserMixin, db.Model):
cuisines = db.relationship('Cuisine', backref='author', lazy='dynamic') cuisines = db.relationship('Cuisine', backref='author', lazy='dynamic')
methods = db.relationship('Method', backref='author', lazy='dynamic') methods = db.relationship('Method', backref='author', lazy='dynamic')
ingredients = db.relationship('Ingredient', backref='author', lazy='dynamic') ingredients = db.relationship('Ingredient', backref='author', lazy='dynamic')
flavours = db.relationship('Flavour', backref='author', lazy='dynamic')
flavourtypes = db.relationship('FlavourType', backref='author', lazy='dynamic')
followed = db.relationship('Follow', followed = db.relationship('Follow',
foreign_keys=[Follow.follower_id], foreign_keys=[Follow.follower_id],
backref=db.backref('follower', lazy='joined'), backref=db.backref('follower', lazy='joined'),
...@@ -386,4 +388,26 @@ class Ingredient(db.Model): ...@@ -386,4 +388,26 @@ class Ingredient(db.Model):
name = db.Column(db.String(64), unique=True) name = db.Column(db.String(64), unique=True)
description = db.Column(db.Text) description = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id')) author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
\ No newline at end of file
flavours = db.relationship('Flavour', backref='ingredient', lazy='dynamic')
class Flavour(db.Model):
__tablename__ = 'flavours'
id = db.Column(db.Integer, primary_key=True, unique=True)
flavourtype_id = db.Column(db.Integer, db.ForeignKey('flavourtypes.id'))
ingredient_id = db.Column(db.Integer, db.ForeignKey('ingredients.id'))
prevalence = db.Column(db.Integer)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
class FlavourType(db.Model):
__tablename__ = 'flavourtypes'
id = db.Column(db.Integer, primary_key=True, unique=True)
name = db.Column(db.String(64), unique=True)
characteristics = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
flavours = db.relationship('Flavour', backref='flavourtype', lazy='dynamic')
...@@ -225,3 +225,9 @@ input#steps-0-ingredients { ...@@ -225,3 +225,9 @@ input#steps-0-ingredients {
span a { span a {
color: #fff !important; color: #fff !important;
} }
body > div.container > div:nth-child(2) > form > fieldset > fieldset > div:nth-child(3),
body > div.container > div:nth-child(2) > form > fieldset > fieldset > div:nth-child(2),
body > div.container > div:nth-child(2) > form > fieldset > fieldset > legend {
display: none
}
#!/usr/bin/env python #!/usr/bin/env python
import os import os
from app import create_app, db from app import create_app, db
from app.models import User, Follow, Role, Permission, Recipe, Comment, Cuisine, RecipeSteps, Method, Ingredient from app.models import User, Follow, Role, Permission, Recipe, Comment, Cuisine, RecipeSteps, Method, Ingredient, Flavour, FlavourType
from flask.ext.script import Manager, Shell from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand from flask.ext.migrate import Migrate, MigrateCommand
...@@ -12,7 +12,7 @@ migrate = Migrate(app, db) ...@@ -12,7 +12,7 @@ migrate = Migrate(app, db)
def make_shell_context(): def make_shell_context():
return dict(app=app, db=db, User=User, Follow=Follow, Role=Role, Cuisine=Cuisine, return dict(app=app, db=db, User=User, Follow=Follow, Role=Role, Cuisine=Cuisine,
Permission=Permission, Recipe=Recipe, Comment=Comment, RecipeSteps=RecipeSteps, Method=Method, Ingredient=Ingredient) Permission=Permission, Recipe=Recipe, Comment=Comment, RecipeSteps=RecipeSteps, Method=Method, Ingredient=Ingredient, Flavour=Flavour, FlavourType=FlavourType)
manager.add_command("shell", Shell(make_context=make_shell_context)) manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand) manager.add_command('db', MigrateCommand)
......
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