1
0
Fork 0
mirror of https://github.com/shouptech/humulus.git synced 2026-02-03 16:09:44 +00:00

Add sort by volume (#10)

This commit adds the ability to sort by volume.

Closes #8
This commit is contained in:
Emma 2019-07-06 09:17:25 -06:00 committed by GitHub
parent a7e04c4ec6
commit 42799be7bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 3 deletions

View file

@ -7,9 +7,12 @@
}, },
"by-name": { "by-name": {
"map": "function (doc) {\n if (doc.$type == \"recipe\" && doc.name) {\n emit(doc.name, doc.name)\n }\n}" "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": {}, "lists": {},
"indexes": {}, "indexes": {},
"shows": {} "shows": {}
} }

View file

@ -257,6 +257,8 @@ def index():
sort_by = request.args.get('sort_by', default='name', type=str) sort_by = request.args.get('sort_by', default='name', type=str)
if sort_by == 'date': if sort_by == 'date':
view = get_view('_design/recipes', 'by-date') view = get_view('_design/recipes', 'by-date')
elif sort_by == 'volume':
view = get_view('_design/recipes', 'by-volume')
else: else:
view = get_view('_design/recipes', 'by-name') view = get_view('_design/recipes', 'by-name')

View file

@ -40,7 +40,15 @@
<a class="text-dark" href="{{ url_for('recipes.index', descending='false', sort_by='name') }}">Name</a> <a class="text-dark" href="{{ url_for('recipes.index', descending='false', sort_by='name') }}">Name</a>
{% endif %} {% endif %}
</th> </th>
<th>Batch Size</th> <th>
{% if sort_by == 'volume' and descending %}
<a class="text-dark" href="{{ url_for('recipes.index', descending='false', sort_by='volume') }}">Batch Size &darr;</a>
{% elif sort_by == 'volume' %}
<a class="text-dark" href="{{ url_for('recipes.index', descending='true', sort_by='volume') }}">Batch Size &uarr;</a>
{% else %}
<a class="text-dark" href="{{ url_for('recipes.index', descending='false', sort_by='volume') }}">Batch Size</a>
{% endif %}
</th>
<th> <th>
{% if sort_by == 'date' and descending %} {% if sort_by == 'date' and descending %}
<a class="text-dark" href="{{ url_for('recipes.index', descending='false', sort_by='date') }}">Created On &darr;</a> <a class="text-dark" href="{{ url_for('recipes.index', descending='false', sort_by='date') }}">Created On &darr;</a>
@ -55,7 +63,7 @@
{% for row in rows %} {% for row in rows %}
<tr> <tr>
<td><a href="{{ url_for('recipes.info', id=row.id) }}">{{ row.doc.name }}</a></td> <td><a href="{{ url_for('recipes.info', id=row.id) }}">{{ row.doc.name }}</a></td>
<td>{{ row.doc.volume }}</td> <td>{{ row.doc.volume }} gal.</td>
<td>{{ moment(row.doc.created) }}</td> <td>{{ moment(row.doc.created) }}</td>
</tr> </tr>
{% endfor %} {% endfor %}

View file

@ -71,6 +71,28 @@ def test_index(client):
response.data response.data
) )
# Test sort by volume ascending
response = client.get('/recipes/?descending=false&sort_by=volume')
assert (
b'"/recipes/?descending=false&amp;sort_by=name">Name' in
response.data
)
assert (
b'"/recipes/?descending=true&amp;sort_by=volume">Batch Size &uarr;' in
response.data
)
# Test sort by volume descending
response = client.get('/recipes/?descending=true&sort_by=volume')
assert (
b'"/recipes/?descending=false&amp;sort_by=name">Name' in
response.data
)
assert (
b'"/recipes/?descending=false&amp;sort_by=volume">Batch Size &darr;' in
response.data
)
def test_create(client, app, auth): def test_create(client, app, auth):
"""Test success in creating a recipe document.""" """Test success in creating a recipe document."""