mirror of
https://github.com/shouptech/humulus.git
synced 2026-02-03 19:29:41 +00:00
Add recipes.index view
This commit is contained in:
parent
28b788fa76
commit
1b248f9ac4
3 changed files with 129 additions and 1 deletions
|
|
@ -22,7 +22,7 @@ from wtforms import (Form, StringField, DecimalField, TextAreaField, FieldList,
|
||||||
FormField, SelectField)
|
FormField, SelectField)
|
||||||
from wtforms.validators import DataRequired, Optional
|
from wtforms.validators import DataRequired, Optional
|
||||||
|
|
||||||
from humulus.couch import get_doc_or_404, put_doc, update_doc
|
from humulus.couch import get_doc_or_404, put_doc, update_doc, get_view
|
||||||
|
|
||||||
bp = Blueprint('recipes', __name__, url_prefix='/recipes')
|
bp = Blueprint('recipes', __name__, url_prefix='/recipes')
|
||||||
|
|
||||||
|
|
@ -185,6 +185,26 @@ class RecipeForm(FlaskForm):
|
||||||
return recipe
|
return recipe
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route('/')
|
||||||
|
def index():
|
||||||
|
descending = (
|
||||||
|
request.args.get('descending', default='false', type=str).lower() in
|
||||||
|
['true', 'yes']
|
||||||
|
)
|
||||||
|
sort_by = request.args.get('sort_by', default='name', type=str)
|
||||||
|
if sort_by == 'date':
|
||||||
|
view = get_view('_design/recipes', 'by-date')
|
||||||
|
else:
|
||||||
|
view = get_view('_design/recipes', 'by-name')
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
'recipes/index.html',
|
||||||
|
rows=view(include_docs=True, descending=descending)['rows'],
|
||||||
|
descending=descending,
|
||||||
|
sort_by=sort_by
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/create', methods=('GET', 'POST'))
|
@bp.route('/create', methods=('GET', 'POST'))
|
||||||
def create():
|
def create():
|
||||||
form = RecipeForm()
|
form = RecipeForm()
|
||||||
|
|
|
||||||
56
src/humulus/templates/recipes/index.html
Normal file
56
src/humulus/templates/recipes/index.html
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
{#-
|
||||||
|
Copyright 2019 Mike Shoup
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
-#}
|
||||||
|
{% from "_macros.html" import moment %}
|
||||||
|
{% from "recipes/_macros.html" import render_recipe_form %}
|
||||||
|
|
||||||
|
{% extends '_base.html' %}
|
||||||
|
{% block title %}Recipes{% endblock %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<div class="row"><h1>Recipes</h1></div>
|
||||||
|
<table class="table table-hover table-sm">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>
|
||||||
|
{% if sort_by == 'name' and descending %}
|
||||||
|
<a class="text-dark" href="{{ url_for('recipes.index', descending='false', sort_by='name') }}">Name ↓</a>
|
||||||
|
{% elif sort_by == 'name' %}
|
||||||
|
<a class="text-dark" href="{{ url_for('recipes.index', descending='true', sort_by='name') }}">Name ↑</a>
|
||||||
|
{% else %}
|
||||||
|
<a class="text-dark" href="{{ url_for('recipes.index', descending='false', sort_by='name') }}">Name</a>
|
||||||
|
{% endif %}
|
||||||
|
</th>
|
||||||
|
<th>Batch Size</th>
|
||||||
|
<th>
|
||||||
|
{% if sort_by == 'date' and descending %}
|
||||||
|
<a class="text-dark" href="{{ url_for('recipes.index', descending='false', sort_by='date') }}">Created On ↓</a>
|
||||||
|
{% elif sort_by == 'date' %}
|
||||||
|
<a class="text-dark" href="{{ url_for('recipes.index', descending='true', sort_by='date') }}">Created On ↑</a>
|
||||||
|
{% else %}
|
||||||
|
<a class="text-dark" href="{{ url_for('recipes.index', descending='false', sort_by='date') }}">Created On</a>
|
||||||
|
{% endif %}
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
{% for row in rows %}
|
||||||
|
<tr>
|
||||||
|
<td><a href="{{ url_for('recipes.info', id=row.id) }}">{{ row.doc.name }}</a></td>
|
||||||
|
<td>{{ row.doc.volume }}</td>
|
||||||
|
<td>{{ moment(row.doc.created) }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</table>
|
||||||
|
{% endblock %}
|
||||||
|
|
@ -18,6 +18,58 @@ from humulus.couch import get_db, get_doc, put_doc
|
||||||
from humulus.recipes import FermentableForm, HopForm, RecipeForm, YeastForm
|
from humulus.recipes import FermentableForm, HopForm, RecipeForm, YeastForm
|
||||||
|
|
||||||
|
|
||||||
|
def test_index(client):
|
||||||
|
"""Test success in retrieving index."""
|
||||||
|
# Verify defaults
|
||||||
|
response = client.get('/recipes/')
|
||||||
|
assert response.status_code == 200
|
||||||
|
# Assert recipes are returned
|
||||||
|
assert b'Awesome Lager' in response.data
|
||||||
|
assert b'Awesome Beer' in response.data
|
||||||
|
assert (
|
||||||
|
b'"/recipes/?descending=true&sort_by=name">Name ↑' in
|
||||||
|
response.data
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
b'"/recipes/?descending=false&sort_by=date">Created On' in
|
||||||
|
response.data
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# Test sort by name descending
|
||||||
|
response = client.get('/recipes/?descending=true&sort_by=name')
|
||||||
|
assert (
|
||||||
|
b'"/recipes/?descending=false&sort_by=name">Name ↓' in
|
||||||
|
response.data
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
b'"/recipes/?descending=false&sort_by=date">Created On' in
|
||||||
|
response.data
|
||||||
|
)
|
||||||
|
|
||||||
|
# Test sort by date ascending
|
||||||
|
response = client.get('/recipes/?descending=false&sort_by=date')
|
||||||
|
assert (
|
||||||
|
b'"/recipes/?descending=false&sort_by=name">Name' in
|
||||||
|
response.data
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
b'"/recipes/?descending=true&sort_by=date">Created On ↑' in
|
||||||
|
response.data
|
||||||
|
)
|
||||||
|
|
||||||
|
# Test sort by date descending
|
||||||
|
response = client.get('/recipes/?descending=true&sort_by=date')
|
||||||
|
assert (
|
||||||
|
b'"/recipes/?descending=false&sort_by=name">Name' in
|
||||||
|
response.data
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
b'"/recipes/?descending=false&sort_by=date">Created On ↓' in
|
||||||
|
response.data
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_create(client, app):
|
def test_create(client, app):
|
||||||
"""Test success in creating a recipe document."""
|
"""Test success in creating a recipe document."""
|
||||||
# Test GET
|
# Test GET
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue