Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
FoAM
open sauces
Commits
f11231b9
Commit
f11231b9
authored
Feb 23, 2015
by
Francesca Sargent
Browse files
implemented comments for recipes
parent
09840533
Changes
3
Hide whitespace changes
Inline
Side-by-side
flask/app/main/views.py
View file @
f11231b9
...
...
@@ -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
):
...
...
flask/app/models.py
View file @
f11231b9
...
...
@@ -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
)
flask/app/templates/recipe.html
View file @
f11231b9
...
...
@@ -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 %}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment