| # route for displaying all books in database | # route for displaying all books in database | ||||
| @book.route('/books') | @book.route('/books') | ||||
| def get_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 | # get introductory paragraph Markdown | ||||
| with open('content/books.md', 'r') as f: | with open('content/books.md', 'r') as f: | ||||
| intro_text = f.read() | intro_text = f.read() | ||||
| intro_text = markdown.markdown(intro_text) | 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(): | for key in request.args.keys(): | ||||
| if key != 'view': | |||||
| if key != 'view' and key != 'page': | |||||
| if (key == 'practice' and request.args.get(key) != ''): | 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_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)) | books_2 = books_query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Relationship.first_resource_id==request.args.get(key)) | ||||
| if (key != 'practice' and request.args.get(key) != ''): | if (key != 'practice' and request.args.get(key) != ''): | ||||
| kwargs = {key: request.args.get(key)} | kwargs = {key: request.args.get(key)} | ||||
| books_query = books_query.filter_by(**kwargs) | 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 | ||||
| practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name).all() | practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name).all() | ||||
| # year | # year | ||||
| year_filter = get_filter_values('year', resource_type) | year_filter = get_filter_values('year', resource_type) | ||||
| # typology | # typology | ||||
| typology_filter = get_filter_values('typology', resource_type) | 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 | # route for displaying a single book based on the ID in the database | ||||
| @book.route('/books/<int:book_id>') | @book.route('/books/<int:book_id>') |
| # route for displaying all practices in database | # route for displaying all practices in database | ||||
| @practice.route('/practices') | @practice.route('/practices') | ||||
| def get_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 | # 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 = f.read() | ||||
| intro_text = markdown.markdown(intro_text) | 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_( | or_( | ||||
| Resource.id==53, | Resource.id==53, | ||||
| Resource.id==56, | Resource.id==56, | ||||
| Resource.id==65, | Resource.id==65, | ||||
| Resource.id==66 | 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': | 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 | # route for displaying a single practice based on the ID in the database | ||||
| @practice.route('/practices/<int:practice_id>') | @practice.route('/practices/<int:practice_id>') |
| resources[index] = append_relationships(resource) | resources[index] = append_relationships(resource) | ||||
| return resources | 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 | # function to add a relationship to a linked resource | ||||
| def add_relationship(resource_id, linked_resource_id): | def add_relationship(resource_id, linked_resource_id): | ||||
| first_resource_id = resource_id | first_resource_id = resource_id |
| hyphens: auto; | hyphens: auto; | ||||
| } | } | ||||
| h2 { | |||||
| font-family: 'ag-fett'; | |||||
| font-size: 1.5rem; | |||||
| line-height: 1.1; | |||||
| } | |||||
| h3 { | h3 { | ||||
| font-family: 'ag-fett'; | font-family: 'ag-fett'; | ||||
| } | } |
| <div class="w-[4.5rem] shrink-0 lg:w-[12rem] text-center "> | <div class="w-[4.5rem] shrink-0 lg:w-[12rem] text-center "> | ||||
| {% if show_number %} | {% if show_number %} | ||||
| {{loop.index}} / {{count}} | |||||
| {{ (page -1) * resources.per_page + loop.index }} / {{ resources.total }} | |||||
| {% else %} | {% else %} | ||||
| <div class="capitalize inline-block min-w-[5rem] px-4 ">{{ resource['type'] }}</div> | <div class="capitalize inline-block min-w-[5rem] px-4 ">{{ resource['type'] }}</div> | ||||
| {% endif %} | {% endif %} | ||||
| <div class="grid lg:grid-rows-[auto,auto,auto] grid-flow-col w-fit pr-40 "> | <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 "> | <div class="w-[4.5rem] shrink-0 lg:w-[12rem] text-center px-2 py-4 "> | ||||
| {% if show_number %} | {% if show_number %} | ||||
| <p>{{loop.index}} / {{count}}</p> | |||||
| <p>{{ (page -1) * resources.per_page + loop.index }} / {{ resources.total }}</p> | |||||
| {% else %} | {% else %} | ||||
| <p class="capitalize">{{ resource['type'] }}</p> | <p class="capitalize">{{ resource['type'] }}</p> | ||||
| {% endif %} | {% endif %} |
| {% endif %} | {% endif %} | ||||
| </div> | </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> | </form> | ||||
| {% endblock %} | {% endblock %} |
| # route for displaying all tools in database | # route for displaying all tools in database | ||||
| @tool.route('/tools') | @tool.route('/tools') | ||||
| def get_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 | # 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 = f.read() | ||||
| intro_text = markdown.markdown(intro_text) | 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(): | for key in request.args.keys(): | ||||
| if key != 'view': | |||||
| if key != 'view' and key != 'page': | |||||
| if (key == 'practice' and request.args.get(key) != ''): | 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_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)) | tools_2 = tools_query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Relationship.first_resource_id==request.args.get(key)) | ||||
| if ((key != 'practice' and key != 'scriptingLanguage') and request.args.get(key) != ''): | if ((key != 'practice' and key != 'scriptingLanguage') and request.args.get(key) != ''): | ||||
| kwargs = {key: request.args.get(key)} | kwargs = {key: request.args.get(key)} | ||||
| tools_query = tools_query.filter_by(**kwargs) | 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 | ||||
| practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name).all() | practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name).all() | ||||
| # license | # license | ||||
| languages_filter = get_filter_values('scriptingLanguage', resource_type) | languages_filter = get_filter_values('scriptingLanguage', resource_type) | ||||
| # status | # status | ||||
| status_filter = get_filter_values('status', resource_type) | 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 | # route for displaying a single tool based on the ID in the database | ||||
| @tool.route('/tools/<int:tool_id>') | @tool.route('/tools/<int:tool_id>') |