Commit 79d710ad authored by Francesca Sargent 's avatar Francesca Sargent
Browse files

Can create new Flavours via Ingredient interface, also added slider

parent 93c739ed
...@@ -51,21 +51,22 @@ class EditProfileForm(Form): ...@@ -51,21 +51,22 @@ class EditProfileForm(Form):
submit = SubmitField('Submit') submit = SubmitField('Submit')
class FlavourForm(Form): class FlavourForm(Form):
flavourtype_id = SelectField(coerce=int) flavourtype_name = StringField()
ingredient_id = SelectField(coerce=int) ingredient_id = SelectField(coerce=int)
prevalence = IntegerField() prevalence = IntegerField('Prevalence out of 10', [
validators.NumberRange(message='Range should be between 1 and 10.',
min=1, max=10)
])
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
kwargs['csrf_enabled'] = False kwargs['csrf_enabled'] = False
super(FlavourForm, self).__init__(*args, **kwargs) 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) self.ingredient_id.choices = [(ingredient.id, ingredient.name)
for ingredient in Ingredient.query.order_by(Ingredient.id).all()] for ingredient in Ingredient.query.order_by(Ingredient.id).all()]
class FlavourTypeForm(Form): class FlavourTypeForm(Form):
name = SelectField() name = StringField('Flavour Name', validators=[Length(0, 64)])
characteristics = TextAreaField('Flavour Characteristics') characteristics = TextAreaField('Flavour Characteristics')
flavours = FormField(FlavourForm) flavours = FormField(FlavourForm)
......
...@@ -110,7 +110,7 @@ def postall(type): ...@@ -110,7 +110,7 @@ def postall(type):
return render_template('all_ingredients.html', user=user, ingredients=ingredients, return render_template('all_ingredients.html', user=user, ingredients=ingredients,
pagination=pagination, type=type) pagination=pagination, type=type)
@main.route('/post/<type>/', methods=['GET', 'POST']) @main.route('/post/<type>/', methods=['GET', 'POST'])
def poststuff(type): def poststuff(type):
...@@ -566,14 +566,34 @@ def editcuisine(id): ...@@ -566,14 +566,34 @@ def editcuisine(id):
return render_template('edit_recipe.html', form=form, type='cuisine') return render_template('edit_recipe.html', form=form, type='cuisine')
def addFlavourType(namedata, characteristicdata): def addFlavourType(namedata, characteristicdata):
flavourtype = FlavourType.query.filter_by(name=namedata).first() print '@@@@@', namedata
if FlavourType.query.filter_by(name=namedata).first() != None:
flavourtype = FlavourType.query.filter_by(name=namedata).first()
else:
flavourtype = FlavourType()
flavourtype.name = namedata flavourtype.name = namedata
flavourtype.characteristics = characteristicdata flavourtype.characteristics = characteristicdata
flavourtype.author = current_user._get_current_object()
db.session.add(flavourtype) db.session.add(flavourtype)
def addFlavour(flavour_ingredient_id, flavour_flavourtype_id, flavour_prevalence): def addFlavour(flavour_ingredient_id, flavour_flavourtype_name, 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()) new_id = flavour_flavourtype_name + str(flavour_ingredient_id)
db.session.add(flavour)
if Flavour.query.get(new_id) != None:
flavour = Flavour.query.get(new_id)
else:
flavour = Flavour()
flavour.id = new_id
flavour.flavourtype_name = flavour_flavourtype_name
flavour.ingredient_id = flavour_ingredient_id
flavour.prevalence = flavour_prevalence
flavour.timestamp = 'now'
flavour.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
...@@ -589,28 +609,34 @@ def editingredient(id): ...@@ -589,28 +609,34 @@ def editingredient(id):
ingredient.name = form.name.data ingredient.name = form.name.data
ingredient.description = form.description.data ingredient.description = form.description.data
print form.flavours.data
# Creating a Flavour Type (the general flavour), which will link to Specific Flavour Details # Creating a Flavour Type (the general flavour), which will link to Specific Flavour Details
flavourtype_name = form.flavours['name'].data flavourtype_name = form.flavours['name'].data
flavourtype_characteristics = form.flavours['characteristics'].data flavourtype_characteristics = form.flavours['characteristics'].data
print flavourtype_name
addFlavourType(flavourtype_name, flavourtype_characteristics) addFlavourType(flavourtype_name, flavourtype_characteristics)
# Creating a Flavour, linking to Flavour Type and Ingredient # Creating a Flavour, linking to Flavour Type and Ingredient
flavour_ingredient_id = ingredient.id flavour_ingredient_id = ingredient.id
flavour_flavourtype_id = form.flavours.flavours['flavourtype_id'].data flavour_flavourtype_name = form.flavours['name'].data
flavour_prevalence = form.flavours.flavours['prevalence'].data flavour_prevalence = form.flavours.flavours['prevalence'].data
addFlavour(flavour_ingredient_id, flavour_flavourtype_id, flavour_prevalence) addFlavour(flavour_ingredient_id, flavour_flavourtype_name, 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
for flavour in ingredient.flavours: for flavour in ingredient.flavours:
print flavour # form.flavours.flavours['flavourtype_name'].data = flavour.flavourtype_name
form.flavours.flavours['flavourtype_id'].data = flavour.flavourtype.name form.flavours['characteristics'].data = flavour.flavourtype.characteristics
form.flavours['characteristics'].data = flavour.flavourtype.characteristics form.flavours['name'].data = flavour.flavourtype_name
for flavour in flavour.flavourtype.flavours: for flavour in flavour.flavourtype.flavours:
...@@ -654,7 +680,7 @@ def editflavour(id): ...@@ -654,7 +680,7 @@ def editflavour(id):
form = FlavourForm() form = FlavourForm()
if form.validate_on_submit(): if form.validate_on_submit():
flavour.flavourtype_id = form.flavourtype_id.data flavour.flavourtype_name = form.flavourtype_name.data
flavour.ingredient_id = form.ingredient_id.data flavour.ingredient_id = form.ingredient_id.data
flavour.prevalence = form.prevalence.data flavour.prevalence = form.prevalence.data
...@@ -662,7 +688,7 @@ def editflavour(id): ...@@ -662,7 +688,7 @@ def editflavour(id):
flash('The flavour has been updated.') flash('The flavour has been updated.')
return redirect(url_for('.index')) return redirect(url_for('.index'))
form.flavourtype_id.data = flavour.flavourtype_id form.flavourtype_name.data = flavour.flavourtype_name
form.ingredient_id.data = flavour.ingredient_id form.ingredient_id.data = flavour.ingredient_id
form.prevalence.data = flavour.prevalence form.prevalence.data = flavour.prevalence
......
...@@ -384,7 +384,7 @@ db.event.listen(Comment.body, 'set', Comment.on_changed_body) ...@@ -384,7 +384,7 @@ db.event.listen(Comment.body, 'set', Comment.on_changed_body)
class Ingredient(db.Model): class Ingredient(db.Model):
__tablename__ = 'ingredients' __tablename__ = 'ingredients'
id = db.Column(db.Integer, primary_key=True, unique=True) id = db.Column(db.Integer(), primary_key=True, unique=True)
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)
...@@ -394,8 +394,8 @@ class Ingredient(db.Model): ...@@ -394,8 +394,8 @@ class Ingredient(db.Model):
class Flavour(db.Model): class Flavour(db.Model):
__tablename__ = 'flavours' __tablename__ = 'flavours'
id = db.Column(db.Integer, primary_key=True, unique=True) id = db.Column(db.String(64), primary_key=True, unique=True)
flavourtype_id = db.Column(db.Integer, db.ForeignKey('flavourtypes.id')) flavourtype_name = db.Column(db.String(64), db.ForeignKey('flavourtypes.name'))
ingredient_id = db.Column(db.Integer, db.ForeignKey('ingredients.id')) ingredient_id = db.Column(db.Integer, db.ForeignKey('ingredients.id'))
prevalence = db.Column(db.Integer) prevalence = db.Column(db.Integer)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
...@@ -404,7 +404,7 @@ class Flavour(db.Model): ...@@ -404,7 +404,7 @@ class Flavour(db.Model):
class FlavourType(db.Model): class FlavourType(db.Model):
__tablename__ = 'flavourtypes' __tablename__ = 'flavourtypes'
id = db.Column(db.Integer, primary_key=True, unique=True) id = db.Column(db.Integer(), primary_key=True, unique=True)
name = db.Column(db.String(64), unique=True) name = db.Column(db.String(64), unique=True)
characteristics = db.Column(db.Text) characteristics = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
......
...@@ -231,3 +231,98 @@ body > div.container > div:nth-child(2) > form > fieldset > fieldset > div:nth-c ...@@ -231,3 +231,98 @@ body > div.container > div:nth-child(2) > form > fieldset > fieldset > div:nth-c
body > div.container > div:nth-child(2) > form > fieldset > fieldset > legend { body > div.container > div:nth-child(2) > form > fieldset > fieldset > legend {
display: none display: none
} }
input#flavours-flavours-prevalence {
box-shadow: none;
border: 0px;
}
input[type=range] {
-webkit-appearance: none;
margin: 18px 0;
width: 100%;
}
input[type=range]:focus {
outline: none;
}
input[type=range]::-webkit-slider-runnable-track {
width: 100%;
height: 8.4px;
cursor: pointer;
animate: 0.2s;
/*box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;*/
background: #f39c12;
border-radius: 1.3px;
border: 0.2px solid #010101;
}
input[type=range]::-webkit-slider-thumb {
box-shadow: 1px 1px 1px #d35400, 0px 0px 1px #d35400;
border: 1px solid #d35400;
height: 36px;
width: 16px;
border-radius: 3px;
background: #e67e22;
cursor: pointer;
-webkit-appearance: none;
margin-top: -14px;
}
input[type=range]:focus::-webkit-slider-runnable-track {
background: #f39c12;
}
input[type=range]::-moz-range-track {
width: 100%;
height: 8.4px;
cursor: pointer;
animate: 0.2s;
box-shadow: 1px 1px 1px #d35400, 0px 0px 1px #d35400;
background: #3071a9;
border-radius: 1.3px;
border: 0.2px solid #010101;
}
input[type=range]::-moz-range-thumb {
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
border: 1px solid #000000;
height: 36px;
width: 16px;
border-radius: 3px;
background: #ffffff;
cursor: pointer;
}
input[type=range]::-ms-track {
width: 100%;
height: 8.4px;
cursor: pointer;
animate: 0.2s;
background: transparent;
border-color: transparent;
border-width: 16px 0;
color: transparent;
}
input[type=range]::-ms-fill-lower {
background: #2a6495;
border: 0.2px solid #010101;
border-radius: 2.6px;
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
}
input[type=range]::-ms-fill-upper {
background: #3071a9;
border: 0.2px solid #010101;
border-radius: 2.6px;
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
}
input[type=range]::-ms-thumb {
box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
border: 1px solid #000000;
height: 36px;
width: 16px;
border-radius: 3px;
background: #ffffff;
cursor: pointer;
}
input[type=range]:focus::-ms-fill-lower {
background: #3071a9;
}
input[type=range]:focus::-ms-fill-upper {
background: #367ebd;
}
\ No newline at end of file
...@@ -141,7 +141,9 @@ ...@@ -141,7 +141,9 @@
}); });
$('#flavours-flavours-prevalence').attr('type', 'range');
$('#flavours-flavours-prevalence').attr('min', '1');
$('#flavours-flavours-prevalence').attr('max', '10');
</script> </script>
...@@ -198,6 +200,7 @@ $("div.remove_this").click(function(e) { ...@@ -198,6 +200,7 @@ $("div.remove_this").click(function(e) {
}); });
function clone_field_list(selector) { function clone_field_list(selector) {
var new_element = $(selector).clone(true); var new_element = $(selector).clone(true);
var elem_id = new_element.find(':input')[0].id; var elem_id = new_element.find(':input')[0].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