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,\
SubmitField, FormField, IntegerField, FloatField, FieldList, Field
from wtforms.validators import Required, Length, Email, Regexp, Optional
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, Ingredient
from ..models import Role, User, Cuisine, Recipe, RecipeSteps, Method, Ingredient, Flavour, FlavourType
def chomp(s):
......@@ -20,6 +21,7 @@ class NameForm(Form):
name = StringField('What is your name?', validators=[Required()])
submit = SubmitField('Submit')
class SplitStringField(StringField):
def process_formdata(self, valuelist):
......@@ -48,12 +50,41 @@ class EditProfileForm(Form):
about_me = TextAreaField('About me')
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):
name = StringField('Ingredient Name', validators=[Required(), Length(0,64)])
description = TextAreaField('Description')
flavours = FormField(FlavourTypeForm)
submit = SubmitField('Submit')
class EditProfileAdminForm(Form):
email = StringField('Email', validators=[Required(), Length(1, 64),
Email()])
......@@ -144,8 +175,6 @@ class MethodForm(Form):
self.cuisines.choices = [(cuisine.id, cuisine.name)
for cuisine in Cuisine.query.order_by(Cuisine.name).all()]
class CommentForm(Form):
body = StringField('Enter your comment', validators=[Required()])
submit = SubmitField('Submit')
......@@ -10,9 +10,9 @@ from wtforms import ValidationError, validators, widgets
from flask.ext.pagedown.fields import PageDownField
from .forms import EditProfileForm, EditProfileAdminForm, RecipeForm,\
CommentForm, CuisineForm, RecipeStepForm, MethodForm, RecipeEditForm, SplitStringField, \
IngredientForm
IngredientForm, FlavourTypeForm, FlavourForm
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
......@@ -565,6 +565,16 @@ def editcuisine(id):
form.methods.data = returnListed(cuisine.methods)
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'])
@login_required
def editingredient(id):
......@@ -578,13 +588,85 @@ def editingredient(id):
ingredient.name = form.name.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)
flash('The ingredient has been updated.')
return redirect(url_for('.ingredient', id=ingredient.id))
form.name.data = ingredient.name
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>')
......
......@@ -76,6 +76,8 @@ class User(UserMixin, db.Model):
cuisines = db.relationship('Cuisine', backref='author', lazy='dynamic')
methods = db.relationship('Method', 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',
foreign_keys=[Follow.follower_id],
backref=db.backref('follower', lazy='joined'),
......@@ -386,4 +388,26 @@ class Ingredient(db.Model):
name = db.Column(db.String(64), unique=True)
description = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
\ No newline at end of file
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
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 {
span a {
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
import os
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.migrate import Migrate, MigrateCommand
......@@ -12,7 +12,7 @@ migrate = Migrate(app, db)
def make_shell_context():
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('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