From 1b248f9ac4dd074cb53a216bd08f14b9ded64c93 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Fri, 28 Jun 2019 10:59:27 -0600 Subject: [PATCH] Add recipes.index view --- src/humulus/recipes.py | 22 +++++++++- src/humulus/templates/recipes/index.html | 56 ++++++++++++++++++++++++ tests/test_recipes.py | 52 ++++++++++++++++++++++ 3 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 src/humulus/templates/recipes/index.html diff --git a/src/humulus/recipes.py b/src/humulus/recipes.py index bc2ead0..5c10daf 100644 --- a/src/humulus/recipes.py +++ b/src/humulus/recipes.py @@ -22,7 +22,7 @@ from wtforms import (Form, StringField, DecimalField, TextAreaField, FieldList, FormField, SelectField) 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') @@ -185,6 +185,26 @@ class RecipeForm(FlaskForm): 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')) def create(): form = RecipeForm() diff --git a/src/humulus/templates/recipes/index.html b/src/humulus/templates/recipes/index.html new file mode 100644 index 0000000..544a8d2 --- /dev/null +++ b/src/humulus/templates/recipes/index.html @@ -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 %} +

Recipes

+ + + + + + + + + {% for row in rows %} + + + + + + {% endfor %} +
+ {% if sort_by == 'name' and descending %} + Name ↓ + {% elif sort_by == 'name' %} + Name ↑ + {% else %} + Name + {% endif %} + Batch Size + {% if sort_by == 'date' and descending %} + Created On ↓ + {% elif sort_by == 'date' %} + Created On ↑ + {% else %} + Created On + {% endif %} +
{{ row.doc.name }}{{ row.doc.volume }}{{ moment(row.doc.created) }}
+{% endblock %} diff --git a/tests/test_recipes.py b/tests/test_recipes.py index 54e26b6..259a114 100644 --- a/tests/test_recipes.py +++ b/tests/test_recipes.py @@ -18,6 +18,58 @@ from humulus.couch import get_db, get_doc, put_doc 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): """Test success in creating a recipe document.""" # Test GET