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

implemented comments for recipes

parent 09840533
......@@ -464,27 +464,16 @@ def recipe(id):
ingredients = list(set(ingredients))
# print ingredients
# form = CommentForm()
# if form.validate_on_submit():
# comment = Comment(body=form.body.data,
# recipe=recipe,
# author=current_user._get_current_object())
# db.session.add(comment)
# flash('Your comment has been published.')
# return redirect(url_for('.recipe', id=recipe.id, page=-1))
# page = request.args.get('page', 1, type=int)
# if page == -1:
# page = (recipe.comments.count() - 1) / \
# current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1
# pagination = recipe.comments.order_by(Comment.timestamp.asc()).paginate(
# page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
# error_out=False)
# comments = pagination.items
return render_template('recipe.html', recipes=[recipe], ingredients=ingredients)
form = CommentForm()
if form.validate_on_submit():
comment = Comment(body=form.body.data,
recipe=recipe,
author=current_user._get_current_object())
db.session.add(comment)
flash('Your comment has been published.')
return redirect(url_for('.recipe', id=recipe.id))
comments = Comment.query.filter_by(recipe=recipe).all()
return render_template('recipe.html', recipes=[recipe], ingredients=ingredients, form=form, comments=comments)
@main.route('/cuisine/<int:id>', methods=['GET', 'POST'])
def cuisine(id):
......
......@@ -296,9 +296,6 @@ class Recipe(db.Model):
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
comments = db.relationship('Comment', backref='recipe', lazy='dynamic')
# methods many to many
# cuisines many to many
@staticmethod
def generate_fake(count=100):
from random import seed, randint
......@@ -332,6 +329,7 @@ class RecipeSteps(db.Model):
step_id = db.Column(db.Integer)
step_text = db.Column(db.Text)
ingredients = db.relationship('Ingredient', secondary=stepingredients, backref=db.backref('recipesteps', lazy='dynamic'))
comments = db.relationship('Comment', backref='recipestep', lazy='dynamic')
class Cuisine(db.Model):
__tablename__ = 'cuisines'
......@@ -360,28 +358,6 @@ class Method(db.Model):
cuisines = db.relationship('Cuisine', secondary=cuisinemethods, backref=db.backref('methods', lazy='dynamic'))
class Comment(db.Model):
__tablename__ = 'comments'
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text)
body_html = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
disabled = db.Column(db.Boolean)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.id'))
cuisine_id = db.Column(db.Integer, db.ForeignKey('cuisines.id'))
method_id = db.Column(db.Integer, db.ForeignKey('methods.id'))
@staticmethod
def on_changed_body(target, value, oldvalue, initiator):
allowed_tags = ['a', 'abbr', 'acronym', 'b', 'code', 'em', 'i',
'strong']
target.body_html = bleach.linkify(bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags, strip=True))
db.event.listen(Comment.body, 'set', Comment.on_changed_body)
class Ingredient(db.Model):
__tablename__ = 'ingredients'
id = db.Column(db.Integer(), primary_key=True, unique=True)
......@@ -389,6 +365,7 @@ class Ingredient(db.Model):
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'))
comments = db.relationship('Comment', backref='ingredient', lazy='dynamic')
flavours = db.relationship('Flavour', backref='ingredient', lazy='dynamic')
......@@ -400,6 +377,7 @@ class Flavour(db.Model):
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'))
comments = db.relationship('Comment', backref='flavour', lazy='dynamic')
class FlavourType(db.Model):
......@@ -410,5 +388,31 @@ class FlavourType(db.Model):
colour = db.Column(db.String(6))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
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)
body = db.Column(db.Text)
body_html = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
disabled = db.Column(db.Boolean)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
recipe_id = db.Column(db.Integer, db.ForeignKey('recipes.id'))
cuisine_id = db.Column(db.Integer, db.ForeignKey('cuisines.id'))
method_id = db.Column(db.Integer, db.ForeignKey('methods.id'))
recipe_step_id = db.Column(db.Integer, db.ForeignKey('recipesteps.id'))
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'))
@staticmethod
def on_changed_body(target, value, oldvalue, initiator):
allowed_tags = ['a', 'abbr', 'acronym', 'b', 'code', 'em', 'i',
'strong']
target.body_html = bleach.linkify(bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags, strip=True))
db.event.listen(Comment.body, 'set', Comment.on_changed_body)
......@@ -79,6 +79,14 @@ Created by <a href="{{ url_for('.user', username=recipe.author.username) }}">{{
</div>
</div>
<h4 id="comments">Comments</h4>
{% if current_user.can(Permission.COMMENT) %}
<div class="comment-form">
{{ wtf.quick_form(form) }}
</div>
{% endif %}
{% include '_comments.html' %}
{% endfor %}
{% endblock %}
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