From c036e6f6f835ca8889ffc30bf614f08160dcbbf6 Mon Sep 17 00:00:00 2001 From: Mike Shoup Date: Sat, 6 Jul 2019 09:09:07 -0600 Subject: [PATCH] Add sort by volume --- src/humulus/designs/recipes.json | 5 ++++- src/humulus/recipes.py | 2 ++ src/humulus/templates/recipes/index.html | 12 ++++++++++-- tests/test_recipes.py | 22 ++++++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/humulus/designs/recipes.json b/src/humulus/designs/recipes.json index 6ff6edc..7ea1b59 100644 --- a/src/humulus/designs/recipes.json +++ b/src/humulus/designs/recipes.json @@ -7,9 +7,12 @@ }, "by-name": { "map": "function (doc) {\n if (doc.$type == \"recipe\" && doc.name) {\n emit(doc.name, doc.name)\n }\n}" + }, + "by-volume": { + "map": "function (doc) {\n if (doc.$type == \"recipe\" && doc.volume && doc.name) {\n emit(doc.volume, doc.name)\n }\n}" } }, "lists": {}, "indexes": {}, "shows": {} - } +} diff --git a/src/humulus/recipes.py b/src/humulus/recipes.py index 887cd75..4c1bf65 100644 --- a/src/humulus/recipes.py +++ b/src/humulus/recipes.py @@ -257,6 +257,8 @@ def index(): sort_by = request.args.get('sort_by', default='name', type=str) if sort_by == 'date': view = get_view('_design/recipes', 'by-date') + elif sort_by == 'volume': + view = get_view('_design/recipes', 'by-volume') else: view = get_view('_design/recipes', 'by-name') diff --git a/src/humulus/templates/recipes/index.html b/src/humulus/templates/recipes/index.html index 47db4d2..495e0b9 100644 --- a/src/humulus/templates/recipes/index.html +++ b/src/humulus/templates/recipes/index.html @@ -40,7 +40,15 @@ Name {% endif %} - Batch Size + + {% if sort_by == 'volume' and descending %} + Batch Size ↓ + {% elif sort_by == 'volume' %} + Batch Size ↑ + {% else %} + Batch Size + {% endif %} + {% if sort_by == 'date' and descending %} Created On ↓ @@ -55,7 +63,7 @@ {% for row in rows %} {{ row.doc.name }} - {{ row.doc.volume }} + {{ row.doc.volume }} gal. {{ moment(row.doc.created) }} {% endfor %} diff --git a/tests/test_recipes.py b/tests/test_recipes.py index d860fcd..d8ca6a4 100644 --- a/tests/test_recipes.py +++ b/tests/test_recipes.py @@ -71,6 +71,28 @@ def test_index(client): response.data ) + # Test sort by volume ascending + response = client.get('/recipes/?descending=false&sort_by=volume') + assert ( + b'"/recipes/?descending=false&sort_by=name">Name' in + response.data + ) + assert ( + b'"/recipes/?descending=true&sort_by=volume">Batch Size ↑' in + response.data + ) + + # Test sort by volume descending + response = client.get('/recipes/?descending=true&sort_by=volume') + assert ( + b'"/recipes/?descending=false&sort_by=name">Name' in + response.data + ) + assert ( + b'"/recipes/?descending=false&sort_by=volume">Batch Size ↓' in + response.data + ) + def test_create(client, app, auth): """Test success in creating a recipe document."""