diff --git a/src/humulus/designs/recipes.json b/src/humulus/designs/recipes.json index 7ea1b59..28aca01 100644 --- a/src/humulus/designs/recipes.json +++ b/src/humulus/designs/recipes.json @@ -10,6 +10,9 @@ }, "by-volume": { "map": "function (doc) {\n if (doc.$type == \"recipe\" && doc.volume && doc.name) {\n emit(doc.volume, doc.name)\n }\n}" + }, + "by-type": { + "map": "function (doc) {\n if (doc.$type == \"recipe\" && doc.type && doc.name) {\n emit(doc.type, doc.name)\n }\n}" } }, "lists": {}, diff --git a/src/humulus/recipes.py b/src/humulus/recipes.py index 7aeefd3..cda4a3d 100644 --- a/src/humulus/recipes.py +++ b/src/humulus/recipes.py @@ -262,12 +262,7 @@ def index(): ['true', 'yes'] ) 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') + view = get_view('_design/recipes', 'by-{}'.format(sort_by)) return render_template( 'recipes/index.html', diff --git a/src/humulus/templates/recipes/index.html b/src/humulus/templates/recipes/index.html index 495e0b9..23647f8 100644 --- a/src/humulus/templates/recipes/index.html +++ b/src/humulus/templates/recipes/index.html @@ -49,6 +49,15 @@ Batch Size {% endif %} + + {% if sort_by == 'type' and descending %} + Type ↓ + {% elif sort_by == 'type' %} + Type ↑ + {% else %} + Type + {% endif %} + {% if sort_by == 'date' and descending %} Created On ↓ @@ -64,6 +73,7 @@ {{ row.doc.name }} {{ row.doc.volume }} gal. + {{ row.doc.type }} {{ moment(row.doc.created) }} {% endfor %} diff --git a/tests/test_recipes.py b/tests/test_recipes.py index 525cd52..d6d2f41 100644 --- a/tests/test_recipes.py +++ b/tests/test_recipes.py @@ -93,6 +93,27 @@ def test_index(client): response.data ) + # Test sort by type ascending + response = client.get('/recipes/?descending=false&sort_by=type') + assert ( + b'"/recipes/?descending=false&sort_by=name">Name' in + response.data + ) + assert ( + b'"/recipes/?descending=true&sort_by=type">Type ↑' in + response.data + ) + + # Test sort by type descending + response = client.get('/recipes/?descending=true&sort_by=type') + assert ( + b'"/recipes/?descending=false&sort_by=name">Name' in + response.data + ) + assert ( + b'"/recipes/?descending=false&sort_by=type">Type ↓' in + response.data + ) def test_create(client, app, auth): """Test success in creating a recipe document."""