Commit 9563f370 authored by Francesca Sargent 's avatar Francesca Sargent
Browse files

Adding Menu

parent 768989dd
......@@ -9,7 +9,7 @@ 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, Flavour, FlavourType
from ..models import Role, User, Cuisine, Recipe, RecipeSteps, Method, Ingredient, Flavour, FlavourType, Menu
def chomp(s):
......@@ -197,3 +197,19 @@ class StepCommentForm(Form):
recipe_step_id = IntegerField(validators=[Required()])
submit = SubmitField('Add')
class MenuRecipeForm(Form):
recipe_id = SelectField(coerce=int)
def __init__(self, *args, **kwargs):
kwargs['csrf_enabled'] = False
super(MenuRecipeForm, self).__init__(*args, **kwargs)
self.recipe_id.choices = [(recipe.id, recipe.name)
for recipe in Recipe.query.order_by(Recipe.id).all()]
class MenuForm(Form):
title = StringField('Menu Title', validators=[Required(), Length(0, 64)])
description = TextAreaField('Description/Story')
recipes = FieldList(FormField(MenuRecipeForm), min_entries=1)
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, FlavourTypeForm, FlavourForm, IngredientPostForm, FlavourEditForm, StepCommentForm
IngredientForm, FlavourTypeForm, FlavourForm, IngredientPostForm, FlavourEditForm, StepCommentForm, MenuForm, MenuRecipeForm
from .. import db
from ..models import Permission, Role, User, Recipe, Comment, Cuisine, RecipeSteps, Method, Ingredient, FlavourType, Flavour
from ..models import Permission, Role, User, Recipe, Comment, Cuisine, RecipeSteps, Method, Ingredient, FlavourType, Flavour, Menu
from ..decorators import admin_required, permission_required
......@@ -32,6 +32,14 @@ def addMethod(name):
return methods
def addRecipe(id):
if Recipe.query.get(id) is None:
print "None"
else:
recipes = [Recipe.query.get(id)]
return recipes
def addIngredient(name):
if name is '':
return
......@@ -345,6 +353,36 @@ def poststuff(type):
flash('Your flavour has been added.')
return redirect(url_for('.index'))
if type == 'menu':
form = MenuForm()
if current_user.can(Permission.WRITE_ARTICLES) and \
form.validate_on_submit():
menu = Menu()
menu.title = form.title.data
menu.description = form.description.data
for index,recipe_entry in enumerate(form.recipes.entries):
if recipe_entry.recipe_id == '':
pass
else:
recipe = Recipe.query.filter_by(id=recipe_entry.recipe_id).first()
recipe_id = recipe_entry.recipe_id.data
print "@@@", recipe_id
if index is 0:
menu.recipes = addRecipe(recipe_id)
else:
recipe = addRecipe(recipe_id)
menu.recipes.extend(recipe)
menu.author = current_user._get_current_object()
db.session.add(menu)
db.session.flush()
flash('Your menu has been added.')
return redirect(url_for('.index'))
db.session.commit()
return render_template('post.html', form=form,
......
......@@ -78,6 +78,7 @@ class User(UserMixin, db.Model):
ingredients = db.relationship('Ingredient', backref='author', lazy='dynamic')
flavours = db.relationship('Flavour', backref='author', lazy='dynamic')
flavourtypes = db.relationship('FlavourType', backref='author', lazy='dynamic')
menus = db.relationship('Menu', backref='author', lazy='dynamic')
followed = db.relationship('Follow',
foreign_keys=[Follow.follower_id],
backref=db.backref('follower', lazy='joined'),
......@@ -284,6 +285,12 @@ stepingredients = db.Table('stepingredients',
db.Column('ingredient_id', db.Integer, db.ForeignKey('ingredients.id'))
)
menurecipes = db.Table('menurecipes',
db.Column('id', db.Integer, primary_key=True, unique=True),
db.Column('recipe_id', db.Integer, db.ForeignKey('recipes.id')),
db.Column('menu_id', db.Integer, db.ForeignKey('menus.id'))
)
class Recipe(db.Model):
__tablename__ = 'recipes'
id = db.Column(db.Integer, primary_key=True)
......@@ -322,6 +329,16 @@ class Recipe(db.Model):
db.event.listen(Recipe.body, 'set', Recipe.on_changed_body)
class Menu(db.Model):
__tablename__ = 'menus'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(64), unique=True, index=True)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
recipes = db.relationship('Recipe', secondary=menurecipes, backref=db.backref('menus', lazy='dynamic'))
description = db.Column(db.Text)
comments = db.relationship('Comment', backref='menu', lazy='dynamic')
class RecipeSteps(db.Model):
__tablename__ = 'recipesteps'
id = db.Column(db.Integer, primary_key=True)
......@@ -391,6 +408,7 @@ class FlavourType(db.Model):
comments = db.relationship('Comment', backref='flavourtype', lazy='dynamic')
flavours = db.relationship('Flavour', backref='flavourtype', lazy='dynamic')
class Comment(db.Model):
__tablename__ = 'comments'
id = db.Column(db.Integer, primary_key=True)
......@@ -406,6 +424,7 @@ class Comment(db.Model):
ingredient_id = db.Column(db.Integer, db.ForeignKey('ingredients.id'))
flavour_id = db.Column(db.Integer, db.ForeignKey('flavours.id'))
flavourtype_id = db.Column(db.Integer, db.ForeignKey('flavourtypes.id'))
menu_id = db.Column(db.Integer, db.ForeignKey('menus.id'))
@staticmethod
def on_changed_body(target, value, oldvalue, initiator):
......@@ -416,3 +435,5 @@ class Comment(db.Model):
tags=allowed_tags, strip=True))
db.event.listen(Comment.body, 'set', Comment.on_changed_body)
......@@ -166,7 +166,6 @@
{{ moment.include_moment() }}
{% if type == 'recipe' %}
<script>
console.log('recipe')
var recipecount = parseInt('{% if recipes %}{%for recipe in recipes%}{{recipe.steps.count()}}{% endfor %}{% else %}0{% endif %}')
console.log(recipecount);
......@@ -233,8 +232,6 @@ function clone_field_list(selector) {
});
$(selector).after(new_element)
}
......@@ -366,5 +363,77 @@ function clone_field_list(selector) {
</script>
{% endif %}
{% endblock %}
{% if type == 'menu' %}
<script>
var recipecount = parseInt('{% if menu %}{{menu.recipes.count()}}{% else %}0{% endif %}')
$('#recipes').css({
'border' : '0px',
'list-style' : 'none',
'display' : 'block',
'box-shadow' : '0px 0px 0px 0px rgba(255,255,255, 255)'
});
$('#recipes > li').css({
'display' : 'block'
})
for (i=0; i<=recipecount; i++) {
$('#recipes > li > label').hide()
$('#recipes-'+i+' > tbody > tr:nth-child(1) > th > label').hide()
$('label[for="recipes-'+i+'-recipe_id"]').text("Recipe ")
}
$('ul#recipes').after('<div id="add_another_button">Add</div><div class="remove_this">Remove</div>')
$('div#add_another_button').click(function () {
console.log('clicked');
last_li = $(this).prev('li')
clone_field_list('li:last');
var lis = document.querySelectorAll('#recipes > li').length;
console.log(lis)
if (lis > 0) {
$('div.remove_this').fadeIn()
}
});
$("div.remove_this").click(function(e) {
$('ul#recipes li:last').remove();
e.preventDefault();
var lis = document.querySelectorAll('.steptext').length;
if (lis === 1) {
$(this).fadeOut();
}
});
function clone_field_list(selector) {
var new_element = $(selector).clone(true);
var elem_id = new_element.find(':input')[0].id;
var elem_num = parseInt(elem_id.replace(/.*-(\d{1,4})-.*/m, '$1')) + 1;
new_element.find(':input').each(function() {
var id = $(this).attr('id', function(i,txt) {return txt.replace(/\d+/, elem_num); });
var name = $(this).attr('name', function(i,txt) {return txt.replace(/\d+/, elem_num); });
$(this).val('2')
});
new_element.find('label').each(function() {
var new_for = $(this).attr('for').replace('-' + (elem_num - 1) + '-', '-' + elem_num + '-');
$(this).attr('for', new_for);
$(this).html('Recipe ' + (elem_num + 1))
});
$(selector).after(new_element)
}
</script>
{% endif %}
{% endblock %}
......@@ -10,18 +10,8 @@
<h1>Post {% if type == 'ingredient' %}an {% else %}a {% endif %} {{ type }}</h1>
</div>
<div>
{% if type == 'cuisine' %}
{% if current_user.can(Permission.WRITE_ARTICLES) %}
{{ wtf.quick_form(form) }}
{% endif %}
{% elif type == 'recipe' %}
{{ wtf.quick_form(form) }}
{% elif type == 'method' %}
{{ wtf.quick_form(form) }}
{% elif type == 'ingredient' %}
{{ wtf.quick_form(form) }}
{% elif type == 'flavour' %}
{{ wtf.quick_form(form) }}
{% if current_user.can(Permission.WRITE_ARTICLES) %}
{{ wtf.quick_form(form) }}
{% endif %}
</div>
......
#!/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, Flavour, FlavourType
from app.models import User, Follow, Role, Permission, Recipe, Comment, Cuisine, RecipeSteps, Method, Ingredient, Flavour, FlavourType, Menu
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, Flavour=Flavour, FlavourType=FlavourType)
Permission=Permission, Recipe=Recipe, Comment=Comment, RecipeSteps=RecipeSteps, Method=Method, Ingredient=Ingredient, Flavour=Flavour, FlavourType=FlavourType, Menu=Menu)
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