Browse Source

adding pagination to resources

joel
Simon Bowie 1 year ago
parent
commit
3d8424a873
7 changed files with 114 additions and 50 deletions
  1. +29
    -18
      web/app/book.py
  2. +23
    -14
      web/app/practice.py
  3. +6
    -0
      web/app/relationships.py
  4. +6
    -0
      web/app/static/styles/main.css
  5. +2
    -2
      web/app/templates/base.html
  6. +21
    -0
      web/app/templates/resources.html
  7. +27
    -16
      web/app/tool.py

+ 29
- 18
web/app/book.py View File

@@ -21,15 +21,23 @@ book = Blueprint('book', __name__)
# route for displaying all books in database
@book.route('/books')
def get_books():
# GET PARAMETERS
# get URL parameters for views and pages
view = request.args.get('view')
page = request.args.get('page', 1, type=int)
# set resource type
resource_type = 'book'
# get introductory paragraph Markdown
with open('content/books.md', 'r') as f:
intro_text = f.read()
intro_text = markdown.markdown(intro_text)
view = request.args.get('view')
resource_type = 'book'
books_query = Resource.query.filter_by(type=resource_type)

# DATABASE QUERY
books_query = Resource.query.filter_by(type=resource_type).order_by(Resource.name)

# FILTERING
for key in request.args.keys():
if key != 'view':
if key != 'view' and key != 'page':
if (key == 'practice' and request.args.get(key) != ''):
books_1 = books_query.join(Relationship, Relationship.first_resource_id == Resource.id, isouter=True).filter(Relationship.second_resource_id==request.args.get(key))
books_2 = books_query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Relationship.first_resource_id==request.args.get(key))
@@ -37,26 +45,29 @@ def get_books():
if (key != 'practice' and request.args.get(key) != ''):
kwargs = {key: request.args.get(key)}
books_query = books_query.filter_by(**kwargs)
# finalise the query
books = books_query.all()
# get number of books
count = len(books)
# reorder books by book name
books = sorted(books, key=lambda d: d.__dict__['name'].lower())
# render Markdown as HTML
for book in books:
book.description = markdown.markdown(book.description)
if view != 'list':
# append relationships to each book
append_relationships_multiple(books)
# get values for filters

# finalise the query and add pagination
books = books_query.paginate(page=page, per_page=5)

# FILTERS MENU
# get values for filter menu dropdowns
# practices
practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name).all()
# year
year_filter = get_filter_values('year', resource_type)
# typology
typology_filter = get_filter_values('typology', resource_type)
return render_template('resources.html', resources=books, type=resource_type, practices_filter=practices_filter, year_filter=year_filter, typology_filter=typology_filter, count=count, view=view, intro_text=intro_text)

# POST-FILTERING PROCESSING
# if view is 'expanded' then append relationships
if view != 'list':
# append relationships to each book
append_relationships_multiple_paginated(books)
# render Markdown as HTML
for book in books:
book.description = markdown.markdown(book.description)
return render_template('resources.html', resources=books, type=resource_type, practices_filter=practices_filter, year_filter=year_filter, typology_filter=typology_filter, view=view, page=page, intro_text=intro_text)

# route for displaying a single book based on the ID in the database
@book.route('/books/<int:book_id>')

+ 23
- 14
web/app/practice.py View File

@@ -22,14 +22,22 @@ practice = Blueprint('practice', __name__)
# route for displaying all practices in database
@practice.route('/practices')
def get_practices():
# GET PARAMETERS
# get URL parameters for views and pages
view = request.args.get('view')
page = request.args.get('page', 1, type=int)
# set resource type
resource_type = 'practice'
# get introductory paragraph Markdown
with open('content/practices.md', 'r') as f:
with open('content/books.md', 'r') as f:
intro_text = f.read()
intro_text = markdown.markdown(intro_text)
view = request.args.get('view')
practices = Resource.query.filter_by(type='practice')
# temporarily removing incomplete practices from main list
practices = Resource.query.filter(

# DATABASE QUERY
practices_query = Resource.query.filter_by(type=resource_type).order_by(Resource.name)

# temporarily removing incomplete practices from main list
practices_query = Resource.query.filter(
or_(
Resource.id==53,
Resource.id==56,
@@ -39,16 +47,17 @@ def get_practices():
Resource.id==65,
Resource.id==66
))
# finalise the query
practices = practices.all()
# get number of practices
count = len(practices)
# reorder practices by practice name
practices = sorted(practices, key=lambda d: d.__dict__['name'].lower())
# finalise the query and add pagination
practices = practices_query.paginate(page=page, per_page=5)
# POST-FILTERING PROCESSING
# if view is 'expanded' then append relationships
if view != 'list':
# append relationships to each practice
append_relationships_multiple(practices)
return render_template('resources.html', resources=practices, type='practice', count=count, view=view, intro_text=intro_text)
# append relationships to each book
append_relationships_multiple_paginated(practices)

return render_template('resources.html', resources=practices, type='practice', view=view, page=page, intro_text=intro_text)

# route for displaying a single practice based on the ID in the database
@practice.route('/practices/<int:practice_id>')

+ 6
- 0
web/app/relationships.py View File

@@ -69,6 +69,12 @@ def append_relationships_multiple(resources):
resources[index] = append_relationships(resource)
return resources

# function to append relationships to a pagination object of resources
def append_relationships_multiple_paginated(pagination):
for item in (pagination.items):
item = append_relationships(item)
return pagination

# function to add a relationship to a linked resource
def add_relationship(resource_id, linked_resource_id):
first_resource_id = resource_id

+ 6
- 0
web/app/static/styles/main.css View File

@@ -1159,6 +1159,12 @@ h2,h3 {
hyphens: auto;
}

h2 {
font-family: 'ag-fett';
font-size: 1.5rem;
line-height: 1.1;
}

h3 {
font-family: 'ag-fett';
}

+ 2
- 2
web/app/templates/base.html View File

@@ -171,7 +171,7 @@

<div class="w-[4.5rem] shrink-0 lg:w-[12rem] text-center ">
{% if show_number %}
{{loop.index}} / {{count}}
{{ (page -1) * resources.per_page + loop.index }} / {{ resources.total }}
{% else %}
<div class="capitalize inline-block min-w-[5rem] px-4 ">{{ resource['type'] }}</div>
{% endif %}
@@ -198,7 +198,7 @@
<div class="grid lg:grid-rows-[auto,auto,auto] grid-flow-col w-fit pr-40 ">
<div class="w-[4.5rem] shrink-0 lg:w-[12rem] text-center px-2 py-4 ">
{% if show_number %}
<p>{{loop.index}} / {{count}}</p>
<p>{{ (page -1) * resources.per_page + loop.index }} / {{ resources.total }}</p>
{% else %}
<p class="capitalize">{{ resource['type'] }}</p>
{% endif %}

+ 21
- 0
web/app/templates/resources.html View File

@@ -106,6 +106,27 @@
{% endif %}
</div>

{% macro render_pagination(pagination) %}
<div class=page-items>
{{ pagination.first }} - {{ pagination.last }} of {{ pagination.total }}
</div>
<div class=pagination>
{% for page in pagination.iter_pages() %}
{% if page %}
{% if page != pagination.page %}
<a href="{{ url_for(request.endpoint, page=page) }}{% for key in request.args %}{% if key != 'page' %}&{{ key }}={{ request.args.get(key) }}{% endif %}{% endfor %}">{{ page }}</a>
{% else %}
<strong>{{ page }}</strong>
{% endif %}
{% else %}
<span class=ellipsis>…</span>
{% endif %}
{% endfor %}
</div>
{% endmacro %}

{{ render_pagination(resources) }}

</form>

{% endblock %}

+ 27
- 16
web/app/tool.py View File

@@ -21,15 +21,23 @@ tool = Blueprint('tool', __name__)
# route for displaying all tools in database
@tool.route('/tools')
def get_tools():
# GET PARAMETERS
# get URL parameters for views and pages
view = request.args.get('view')
page = request.args.get('page', 1, type=int)
# set resource type
resource_type = 'tool'
# get introductory paragraph Markdown
with open('content/tools.md', 'r') as f:
with open('content/books.md', 'r') as f:
intro_text = f.read()
intro_text = markdown.markdown(intro_text)
view = request.args.get('view')
resource_type = 'tool'
tools_query = Resource.query.filter_by(type=resource_type)

# DATABASE QUERY
tools_query = Resource.query.filter_by(type=resource_type).order_by(Resource.name)

# FILTERING
for key in request.args.keys():
if key != 'view':
if key != 'view' and key != 'page':
if (key == 'practice' and request.args.get(key) != ''):
tools_1 = tools_query.join(Relationship, Relationship.first_resource_id == Resource.id, isouter=True).filter(Relationship.second_resource_id==request.args.get(key))
tools_2 = tools_query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Relationship.first_resource_id==request.args.get(key))
@@ -40,16 +48,12 @@ def get_tools():
if ((key != 'practice' and key != 'scriptingLanguage') and request.args.get(key) != ''):
kwargs = {key: request.args.get(key)}
tools_query = tools_query.filter_by(**kwargs)
# finalise the query
tools = tools_query.all()
# get number of tools
count = len(tools)
# reorder tools by tools name
tools = sorted(tools, key=lambda d: d.__dict__['name'].lower())
if view != 'list':
# append relationships to each tool
append_relationships_multiple(tools)
# get values for filters

# finalise the query and add pagination
tools = tools_query.paginate(page=page, per_page=5)

# FILTERS MENU
# get values for filter menu dropdowns
# practices
practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name).all()
# license
@@ -58,7 +62,14 @@ def get_tools():
languages_filter = get_filter_values('scriptingLanguage', resource_type)
# status
status_filter = get_filter_values('status', resource_type)
return render_template('resources.html', resources=tools, type=resource_type, practices_filter=practices_filter, licenses_filter=licenses_filter, languages_filter=languages_filter, status_filter=status_filter, count=count, view=view, intro_text=intro_text)
# POST-FILTERING PROCESSING
# if view is 'expanded' then append relationships
if view != 'list':
# append relationships to each book
append_relationships_multiple_paginated(tools)

return render_template('resources.html', resources=tools, type=resource_type, practices_filter=practices_filter, licenses_filter=licenses_filter, languages_filter=languages_filter, status_filter=status_filter, view=view, page=page, intro_text=intro_text)

# route for displaying a single tool based on the ID in the database
@tool.route('/tools/<int:tool_id>')

Loading…
Cancel
Save