mirror of
https://github.com/shouptech/humulus.git
synced 2026-02-03 17:19:42 +00:00
Add style list view
This commit is contained in:
parent
3005c6e44d
commit
7be88cd934
5 changed files with 159 additions and 5 deletions
|
|
@ -33,9 +33,6 @@ def create_app(test_config=None):
|
||||||
from . import couch
|
from . import couch
|
||||||
couch.init_app(app)
|
couch.init_app(app)
|
||||||
|
|
||||||
from . import styles
|
|
||||||
styles.init_app(app)
|
|
||||||
|
|
||||||
# Register blueprint for index page
|
# Register blueprint for index page
|
||||||
from . import home
|
from . import home
|
||||||
app.register_blueprint(home.bp)
|
app.register_blueprint(home.bp)
|
||||||
|
|
@ -49,6 +46,11 @@ def create_app(test_config=None):
|
||||||
from . import auth
|
from . import auth
|
||||||
app.register_blueprint(auth.bp)
|
app.register_blueprint(auth.bp)
|
||||||
|
|
||||||
|
# Register styles blueprint and cli commands
|
||||||
|
from . import styles
|
||||||
|
styles.init_app(app)
|
||||||
|
app.register_blueprint(styles.bp)
|
||||||
|
|
||||||
# Register custom filters
|
# Register custom filters
|
||||||
from . import filters
|
from . import filters
|
||||||
filters.create_filters(app)
|
filters.create_filters(app)
|
||||||
|
|
|
||||||
|
|
@ -14,14 +14,19 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
import math
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
import click
|
import click
|
||||||
import requests
|
import requests
|
||||||
from flask import current_app
|
from flask import Blueprint, abort, current_app, render_template, request
|
||||||
from flask.cli import with_appcontext
|
from flask.cli import with_appcontext
|
||||||
|
|
||||||
from humulus.couch import get_db, put_doc
|
from humulus.auth import login_required
|
||||||
|
from humulus.couch import get_db, put_doc, get_view
|
||||||
|
|
||||||
|
bp = Blueprint('styles', __name__, url_prefix='/styles')
|
||||||
|
|
||||||
|
|
||||||
def sub_to_doc(sub):
|
def sub_to_doc(sub):
|
||||||
"""Coverts sub (XML) to a dictionary document.
|
"""Coverts sub (XML) to a dictionary document.
|
||||||
|
|
@ -114,3 +119,31 @@ def import_command():
|
||||||
def init_app(app):
|
def init_app(app):
|
||||||
"""Register the CLI command with the app."""
|
"""Register the CLI command with the app."""
|
||||||
app.cli.add_command(import_command)
|
app.cli.add_command(import_command)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route('/')
|
||||||
|
@login_required
|
||||||
|
def index():
|
||||||
|
descending = (
|
||||||
|
request.args.get('descending', default='false', type=str).lower() in
|
||||||
|
['true', 'yes']
|
||||||
|
)
|
||||||
|
sort_by = request.args.get('sort_by', default='category', type=str)
|
||||||
|
page = request.args.get('page', default=1, type=int)
|
||||||
|
limit = request.args.get('limit', default=20, type=int)
|
||||||
|
|
||||||
|
view = get_view('_design/styles', 'by-{}'.format(sort_by))
|
||||||
|
try:
|
||||||
|
rows = view(include_docs=True, descending=descending)['rows']
|
||||||
|
except requests.exceptions.HTTPError:
|
||||||
|
abort(400)
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
'styles/index.html',
|
||||||
|
rows=rows[(page-1)*limit:page*limit],
|
||||||
|
descending=descending,
|
||||||
|
sort_by=sort_by,
|
||||||
|
page=page,
|
||||||
|
num_pages=math.ceil(len(rows)/limit),
|
||||||
|
limit=limit
|
||||||
|
)
|
||||||
|
|
|
||||||
84
src/humulus/templates/styles/index.html
Normal file
84
src/humulus/templates/styles/index.html
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
{#-
|
||||||
|
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.
|
||||||
|
-#}
|
||||||
|
|
||||||
|
{% extends '_base.html' %}
|
||||||
|
{% block title %}Styles{% endblock %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<div class="row"><h1>Styles</h1></div>
|
||||||
|
<div class="row">
|
||||||
|
<table class="table table-hover table-sm">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>
|
||||||
|
{% if sort_by == 'category' and descending %}
|
||||||
|
<a class="text-dark" href="{{ url_for('styles.index', descending='false', sort_by='category', limit=limit) }}">Category ↓</a>
|
||||||
|
{% elif sort_by == 'category' %}
|
||||||
|
<a class="text-dark" href="{{ url_for('styles.index', descending='true', sort_by='category', limit=limit) }}">Category ↑</a>
|
||||||
|
{% else %}
|
||||||
|
<a class="text-dark" href="{{ url_for('styles.index', descending='false', sort_by='category', limit=limit) }}">Category</a>
|
||||||
|
{% endif %}
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
{% if sort_by == 'name' and descending %}
|
||||||
|
<a class="text-dark" href="{{ url_for('styles.index', descending='false', sort_by='name', limit=limit) }}">Name ↓</a>
|
||||||
|
{% elif sort_by == 'name' %}
|
||||||
|
<a class="text-dark" href="{{ url_for('styles.index', descending='true', sort_by='name', limit=limit) }}">Name ↑</a>
|
||||||
|
{% else %}
|
||||||
|
<a class="text-dark" href="{{ url_for('styles.index', descending='false', sort_by='name', limit=limit) }}">Name</a>
|
||||||
|
{% endif %}
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
{% for row in rows %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ row.doc.id }}</td>
|
||||||
|
<td>{{ row.doc.name }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<nav>
|
||||||
|
<ul class="pagination flex-wrap">
|
||||||
|
<li class="page-item{% if page == 1 %} disabled {% endif %}">
|
||||||
|
<a class="page-link" href="{{ url_for('styles.index', descending=descending, sort_by=sort_by, page=page-1, limit=limit) }}">Previous</a>
|
||||||
|
</li>
|
||||||
|
{% for num in range(1, num_pages+1) %}
|
||||||
|
<li class="page-item{% if num == page %} active{% endif %}">
|
||||||
|
<a class="page-link" href="{{ url_for('styles.index', descending=descending, sort_by=sort_by, page=num, limit=limit) }}">{{ num }}</a>
|
||||||
|
</li>
|
||||||
|
{% endfor %}
|
||||||
|
<li class="page-item{% if page == num_pages %} disabled {% endif %}">
|
||||||
|
<a class="page-link" href="{{ url_for('styles.index', descending=descending, sort_by=sort_by, page=page+1, limit=limit) }}">Next</a>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
<div class="dropdown">
|
||||||
|
<button class="btn btn-secondary dropdown-toggle ml-2" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
||||||
|
Limit
|
||||||
|
</button>
|
||||||
|
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
|
||||||
|
<a class="dropdown-item{% if limit == 5 %} active {% endif %}" href="{{ url_for('styles.index', descending=descending, sort_by=sort_by, page=page, limit=5) }}">5</a>
|
||||||
|
<a class="dropdown-item{% if limit == 10 %} active {% endif %}" href="{{ url_for('styles.index', descending=descending, sort_by=sort_by, page=page, limit=10) }}">10</a>
|
||||||
|
<a class="dropdown-item{% if limit == 20 %} active {% endif %}" href="{{ url_for('styles.index', descending=descending, sort_by=sort_by, page=page, limit=20) }}">20</a>
|
||||||
|
<a class="dropdown-item{% if limit == 50 %} active {% endif %}" href="{{ url_for('styles.index', descending=descending, sort_by=sort_by, page=page, limit=50) }}">50</a>
|
||||||
|
<a class="dropdown-item{% if limit == 100 %} active {% endif %}" href="{{ url_for('styles.index', descending=descending, sort_by=sort_by, page=page, limit=100) }}">100</a>
|
||||||
|
<a class="dropdown-item{% if limit == 200 %} active {% endif %}" href="{{ url_for('styles.index', descending=descending, sort_by=sort_by, page=page, limit=200) }}">200</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
|
|
@ -122,6 +122,23 @@ def app():
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# Add a test style
|
||||||
|
put_doc({'$type': 'style',
|
||||||
|
'_id': 'style_1A',
|
||||||
|
'abv': {'high': '100', 'low': '0'},
|
||||||
|
'appearance': 'Good looking',
|
||||||
|
'aroma': 'Smelly',
|
||||||
|
'fg': {'high': '1.2', 'low': '1.0'},
|
||||||
|
'flavor': 'Good tasting',
|
||||||
|
'ibu': {'high': '100', 'low': '0'},
|
||||||
|
'id': '1A',
|
||||||
|
'impression': 'Refreshing',
|
||||||
|
'mouthfeel': 'Good feeling',
|
||||||
|
'name': 'Test Style',
|
||||||
|
'og': {'high': '1.2', 'low': '1.0'},
|
||||||
|
'srm': {'high': '100', 'low': '0'}
|
||||||
|
})
|
||||||
|
|
||||||
yield app
|
yield app
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
|
|
|
||||||
|
|
@ -180,3 +180,21 @@ def test_import_command(runner, monkeypatch):
|
||||||
result = runner.invoke(args=['import-styles'])
|
result = runner.invoke(args=['import-styles'])
|
||||||
assert Recorder.called
|
assert Recorder.called
|
||||||
assert 'Imported BJCP styles.' in result.output
|
assert 'Imported BJCP styles.' in result.output
|
||||||
|
|
||||||
|
|
||||||
|
def test_index(auth, client):
|
||||||
|
"""Test success in retrieving index."""
|
||||||
|
# Test not logged in
|
||||||
|
response = client.get('/styles/')
|
||||||
|
assert response.status_code == 302
|
||||||
|
|
||||||
|
# Login and test get
|
||||||
|
auth.login()
|
||||||
|
response = client.get('/styles/')
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert b'1A' in response.data
|
||||||
|
assert b'Test Style' in response.data
|
||||||
|
|
||||||
|
# Test for bad request
|
||||||
|
response = client.get('/styles/?sort_by=foobar')
|
||||||
|
assert response.status_code == 400
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue