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
d33ec2ac
Commit
d33ec2ac
authored
Jan 22, 2015
by
Francesca Sargent
Browse files
Adding Recipe-Method-Cuisine functionality
parent
efed3abe
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
8 deletions
+41
-8
flask/app/main/forms.py
flask/app/main/forms.py
+11
-8
flask/app/main/views.py
flask/app/main/views.py
+24
-0
flask/app/templates/_recipes.html
flask/app/templates/_recipes.html
+6
-0
No files found.
flask/app/main/forms.py
View file @
d33ec2ac
from
flask.ext.wtf
import
Form
import
os
from
wtforms
import
StringField
,
TextAreaField
,
BooleanField
,
SelectField
,
\
SubmitField
,
FormField
from
wtforms.validators
import
Required
,
Length
,
Email
,
Regexp
from
wtforms
import
ValidationError
from
wtforms.validators
import
Required
,
Length
,
Email
,
Regexp
,
Optional
from
wtforms
import
ValidationError
,
validators
from
flask.ext.pagedown.fields
import
PageDownField
from
..
import
db
from
..models
import
Role
,
User
,
Cuisine
,
Recipe
,
RecipeSteps
,
Method
...
...
@@ -59,14 +60,16 @@ class RecipeForm(Form):
name
=
StringField
(
'Recipe Name'
,
validators
=
[
Required
(),
Length
(
0
,
64
)])
description
=
PageDownField
(
'Recipe Description'
)
# recipestep = FormField(RecipeStepForm)
#
cuisine = StringField('Cuisine')
#
method = StringField('Method')
cuisine
s
=
StringField
(
'Cuisine'
,
validators
=
[
Required
()]
)
method
s
=
StringField
(
'Method'
,
validators
=
[
Required
()]
)
submit
=
SubmitField
(
'Submit'
)
# def __init__(self, *args, **kwargs):
# super(RecipeForm, self).__init__(*args, **kwargs)
# self.cuisine.choices = [(cuisine.id, cuisine.name)
# for cuisine in Cuisine.query.order_by(Cuisine.name).all()]
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
(
RecipeForm
,
self
).
__init__
(
*
args
,
**
kwargs
)
self
.
cuisines
.
choices
=
[(
cuisine
.
id
,
cuisine
.
name
)
for
cuisine
in
Cuisine
.
query
.
order_by
(
Cuisine
.
name
).
all
()]
class
CuisineForm
(
Form
):
name
=
TextAreaField
(
"Cuisine Name"
,
validators
=
[
Required
()])
...
...
flask/app/main/views.py
View file @
d33ec2ac
...
...
@@ -68,9 +68,20 @@ def poststuff(type):
form
=
RecipeForm
()
if
current_user
.
can
(
Permission
.
WRITE_ARTICLES
)
and
\
form
.
validate_on_submit
():
if
Cuisine
.
query
.
filter_by
(
name
=
form
.
cuisines
.
data
).
first
()
is
None
:
cuisines
=
[
Cuisine
(
name
=
form
.
cuisines
.
data
,
timestamp
=
'now'
,
author
=
current_user
.
_get_current_object
())]
else
:
cuisines
=
[
Cuisine
.
query
.
filter_by
(
name
=
form
.
cuisines
.
data
).
first
()]
if
Method
.
query
.
filter_by
(
name
=
form
.
methods
.
data
).
first
()
is
None
:
methods
=
[
Method
(
name
=
form
.
methods
.
data
,
timestamp
=
'now'
,
author
=
current_user
.
_get_current_object
())]
else
:
methods
=
[
Method
.
query
.
filter_by
(
name
=
form
.
methods
.
data
).
first
()]
recipe
=
Recipe
(
name
=
form
.
name
.
data
,
description
=
form
.
description
.
data
,
cuisines
=
cuisines
,
methods
=
methods
,
author
=
current_user
.
_get_current_object
())
db
.
session
.
add
(
recipe
)
return
redirect
(
url_for
(
'.index'
))
...
...
@@ -293,6 +304,19 @@ def editrecipe(id):
abort
(
403
)
form
=
RecipeForm
()
if
form
.
validate_on_submit
():
if
Cuisine
.
query
.
filter_by
(
name
=
form
.
cuisines
.
data
).
first
()
is
None
:
cuisines
=
[
Cuisine
(
name
=
form
.
cuisines
.
data
,
timestamp
=
'now'
,
author
=
current_user
.
_get_current_object
())]
else
:
cuisines
=
[
Cuisine
.
query
.
filter_by
(
name
=
form
.
cuisines
.
data
).
first
()]
recipe
.
cuisines
.
extend
(
cuisines
)
if
Method
.
query
.
filter_by
(
name
=
form
.
methods
.
data
).
first
()
is
None
:
methods
=
[
Method
(
name
=
form
.
methods
.
data
,
timestamp
=
'now'
,
author
=
current_user
.
_get_current_object
())]
else
:
methods
=
[
Method
.
query
.
filter_by
(
name
=
form
.
methods
.
data
).
first
()]
recipe
.
methods
.
extend
(
methods
)
recipe
.
name
=
form
.
name
.
data
recipe
.
description
=
form
.
description
.
data
db
.
session
.
add
(
recipe
)
...
...
flask/app/templates/_recipes.html
View file @
d33ec2ac
...
...
@@ -11,6 +11,12 @@
<a
href=
"{{ url_for('.user', username=recipe.author.username) }}"
>
{{ recipe.author.username }}
</a></div>
<div
class=
"recipe-body"
>
{% if recipe.cuisines.count() > 0%}
<strong>
Associated Cuisines:
</strong>
{% for cuisine in recipe.cuisines %}
<span
class=
"badge"
>
{{ cuisine.name }}
</span>
{% endfor %}
<br/>
{% endif %}
{% if recipe.methods.count() > 0 %}
<strong>
Associated Methods:
</strong>
{% for method in recipe.methods %}
<span
class=
"badge"
>
{{ method.name }}
</span>
{% endfor %}
<br/>
{% endif %}
{% if recipe.description %}
{{ recipe.description }}
{% else %}
...
...
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