# 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(): | ||||
books = Resource.query.filter_by(type='book') | |||||
type = 'book' | |||||
books = Resource.query.filter_by(type=type) | |||||
for key in request.args.keys(): | for key in request.args.keys(): | ||||
if key == 'practice': | if key == 'practice': | ||||
query = 'SELECT Resource.* FROM Resource LEFT JOIN Relationship ON Resource.id=Relationship.first_resource_id WHERE Relationship.second_resource_id=' + request.args.get(key) + ' AND Resource.type="book";' | |||||
query = 'SELECT Resource.* FROM Resource LEFT JOIN Relationship ON Resource.id=Relationship.first_resource_id WHERE Relationship.second_resource_id=' + request.args.get(key) + ' AND Resource.type="' + type + '";' | |||||
books = db.engine.execute(query) | books = db.engine.execute(query) | ||||
else: | else: | ||||
kwargs = {'type': 'book', key: request.args.get(key)} | |||||
tools = Resource.query.filter_by(**kwargs) | |||||
kwargs = {'type': type, key: request.args.get(key)} | |||||
books = Resource.query.filter_by(**kwargs) | |||||
# get filters | # get filters | ||||
# practices | # practices | ||||
practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name) | practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name) | ||||
return render_template('resources.html', resources=books, type='book', practices_filter=practices_filter) | |||||
# year | |||||
year_filter = get_filter_values('year', type) | |||||
return render_template('resources.html', resources=books, type=type, practices_filter=practices_filter, year_filter=year_filter) | |||||
# 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>') | ||||
else: | else: | ||||
book = Resource.query.get(book_id) | book = Resource.query.get(book_id) | ||||
book.name = request.form['name'] | book.name = request.form['name'] | ||||
book.description = request.form['description'] | |||||
book.author = request.form['author'] | |||||
book.year = request.form['year'] | |||||
book.bookUrl = request.form['bookUrl'] | |||||
book.isbn = request.form['isbn'] | book.isbn = request.form['isbn'] | ||||
db.session.commit() | db.session.commit() | ||||
linked_resources = request.form.getlist('linked_resources') | linked_resources = request.form.getlist('linked_resources') |
lessonsLearned = db.Column(db.Text) | lessonsLearned = db.Column(db.Text) | ||||
references = db.Column(db.Text) | references = db.Column(db.Text) | ||||
# books | # books | ||||
author = db.Column(db.Text) | |||||
year = db.Column(db.Text) | |||||
bookUrl = db.Column(db.Text) | |||||
isbn = db.Column(db.Text) | isbn = db.Column(db.Text) | ||||
# table for relationships | # table for relationships |
flash('Successfully deleted!') | flash('Successfully deleted!') | ||||
# function to get filters for a specific field | # function to get filters for a specific field | ||||
def get_filter_values(field): | |||||
def get_filter_values(field, type): | |||||
# get field values for filter | # get field values for filter | ||||
field_filter = Resource.query.filter_by(type='tool').with_entities(getattr(Resource, field)) | |||||
field_filter = Resource.query.filter_by(type=type).with_entities(getattr(Resource, field)) | |||||
# turn SQLAlchemy object into list | # turn SQLAlchemy object into list | ||||
field_filter = [i for i, in field_filter] | field_filter = [i for i, in field_filter] | ||||
# split each element on '/' (useful for scriptingLanguage only) | # split each element on '/' (useful for scriptingLanguage only) | ||||
return field_filter | return field_filter | ||||
def get_book_data(isbn): | def get_book_data(isbn): | ||||
#try: | |||||
try: | |||||
book = meta(isbn) | book = meta(isbn) | ||||
description = {'desc': desc(isbn)} | description = {'desc': desc(isbn)} | ||||
book.update(description) | book.update(description) | ||||
book_cover = {'thumbnail': openl_url} | book_cover = {'thumbnail': openl_url} | ||||
book.update(book_cover) | book.update(book_cover) | ||||
return book | return book | ||||
#except: | |||||
# pass | |||||
except: | |||||
pass |
<div class="col"> | <div class="col"> | ||||
<table class="table table-hover"> | <table class="table table-hover"> | ||||
<tbody> | <tbody> | ||||
{% if resource['bookUrl'] %} | |||||
<tr> | |||||
<th> | |||||
URL: | |||||
</th> | |||||
<td> | |||||
<a href="{{ resource['bookUrl'] }}">{{ resource['bookUrl'] }}</a> | |||||
</td> | |||||
</tr> | |||||
{% endif %} | |||||
{% if book %} | {% if book %} | ||||
<!-- fields for books from isbntools --> | <!-- fields for books from isbntools --> | ||||
{% if book['Title'] %} | {% if book['Title'] %} | ||||
</td> | </td> | ||||
</tr> | </tr> | ||||
{% endif %} | {% endif %} | ||||
{% if resource['year'] %} | |||||
<tr> | |||||
<th> | |||||
Publication year: | |||||
</th> | |||||
<td> | |||||
{{ resource['year'] }} | |||||
</td> | |||||
</tr> | |||||
{% endif %} | |||||
{% if resource['description'] %} | |||||
<tr> | |||||
<th> | |||||
Summary: | |||||
</th> | |||||
<td> | |||||
{{ resource['description'] }} | |||||
</td> | |||||
</tr> | |||||
{% endif %} | |||||
{% endif %} | {% endif %} | ||||
</tbody> | </tbody> | ||||
</table> | </table> |
</div> | </div> | ||||
{% elif resource['type'] == 'book' %} | {% elif resource['type'] == 'book' %} | ||||
<div class="mb-3 mt-3"> | |||||
<label for="author">Author</label> | |||||
<input type="text" name="author" placeholder="Author" | |||||
class="form-control" | |||||
value="{{ request.form['author'] or resource['author'] }}"> | |||||
</input> | |||||
</div> | |||||
<div class="mb-3 mt-3"> | |||||
<label for="year">Publication year</label> | |||||
<input type="text" name="year" placeholder="Publication year" | |||||
class="form-control" | |||||
value="{{ request.form['year'] or resource['year'] }}"> | |||||
</input> | |||||
</div> | |||||
<div class="mb-3 mt-3"> | |||||
<label for="bookUrl">URL</label> | |||||
<input type="text" name="bookUrl" placeholder="URL" | |||||
class="form-control" | |||||
value="{{ request.form['bookUrl'] or resource['bookUrl'] }}"> | |||||
</input> | |||||
</div> | |||||
<div class="mb-3 mt-3"> | <div class="mb-3 mt-3"> | ||||
<label for="isbn">ISBN</label> | <label for="isbn">ISBN</label> | ||||
<input type="text" name="isbn" placeholder="ISBN" | <input type="text" name="isbn" placeholder="ISBN" |
</div> | </div> | ||||
</div> | </div> | ||||
{% endif %} | {% endif %} | ||||
{% if year_filter %} | |||||
<div class="col-sm-6 d-md-block d-sm-none d-none"> | |||||
<div class="filter-title">Year</div> | |||||
<ul class="filter-items"> | |||||
{% for year in year_filter %} | |||||
<li {% if request.args.get('year') == year %} class="fw-bold"{% endif %}> | |||||
<a href="/books?year={{year}}">{{ year }}</a> | |||||
</li> | |||||
{% endfor %} | |||||
</ul> | |||||
</div> | |||||
<div class="d-md-none accordion"> | |||||
<div class="accordion-item"> | |||||
<div class="filter-title accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#filter-year">Year</div> | |||||
<div class="accordion-body"> | |||||
<ul id="filter-year" class="collapse filter-items"> | |||||
{% for year in year_filter %} | |||||
<li {% if request.args.get('year') == year %} class="fw-bold"{% endif %}> | |||||
<a href="/books?year={{year}}">{{ year }}</a> | |||||
</li> | |||||
{% endfor %} | |||||
</ul> | |||||
</div> | |||||
</div> | |||||
</div> | |||||
{% endif %} | |||||
</div> | </div> | ||||
<div class="row"> | <div class="row"> | ||||
{% if languages_filter %} | {% if languages_filter %} | ||||
<ul class="filter-items"> | <ul class="filter-items"> | ||||
{% for language in languages_filter %} | {% for language in languages_filter %} | ||||
<li {% if request.args.get('scriptingLanguage') == language %} class="fw-bold"{% endif %}> | <li {% if request.args.get('scriptingLanguage') == language %} class="fw-bold"{% endif %}> | ||||
<a href="/{{type}}?scriptingLanguage={{language}}">{{ language }}</a> | |||||
<a href="/tools?scriptingLanguage={{language}}">{{ language }}</a> | |||||
</li> | </li> | ||||
{% endfor %} | {% endfor %} | ||||
</ul> | </ul> |
# 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(): | ||||
tools = Resource.query.filter_by(type='tool') | |||||
type = 'tool' | |||||
tools = Resource.query.filter_by(type=type) | |||||
for key in request.args.keys(): | for key in request.args.keys(): | ||||
if key == 'practice': | if key == 'practice': | ||||
query = 'SELECT Resource.* FROM Resource LEFT JOIN Relationship ON Resource.id=Relationship.first_resource_id WHERE Relationship.second_resource_id=' + request.args.get(key) + ' AND Resource.type="tool";' | |||||
query = 'SELECT Resource.* FROM Resource LEFT JOIN Relationship ON Resource.id=Relationship.first_resource_id WHERE Relationship.second_resource_id=' + request.args.get(key) + ' AND Resource.type="' + type + '";' | |||||
tools = db.engine.execute(query) | tools = db.engine.execute(query) | ||||
elif key == 'scriptingLanguage': | elif key == 'scriptingLanguage': | ||||
regex = request.args.get(key) + "$|" + request.args.get(key) + "\s\/" | regex = request.args.get(key) + "$|" + request.args.get(key) + "\s\/" | ||||
tools = Resource.query.filter_by(type='tool').filter(Resource.scriptingLanguage.regexp_match(regex)) | |||||
tools = Resource.query.filter_by(type=type).filter(Resource.scriptingLanguage.regexp_match(regex)) | |||||
else: | else: | ||||
kwargs = {'type': 'tool', key: request.args.get(key)} | |||||
kwargs = {'type': type, key: request.args.get(key)} | |||||
tools = Resource.query.filter_by(**kwargs) | tools = Resource.query.filter_by(**kwargs) | ||||
# get filters | # get filters | ||||
# practices | # practices | ||||
practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name) | practices_filter = Resource.query.filter_by(type='practice').with_entities(Resource.id, Resource.name) | ||||
#FOR LATER: SELECT Resource.name, second.name FROM Resource LEFT JOIN Relationship ON Resource.id=Relationship.first_resource_id LEFT JOIN Resource second ON Relationship.second_resource_id=second.id; | #FOR LATER: SELECT Resource.name, second.name FROM Resource LEFT JOIN Relationship ON Resource.id=Relationship.first_resource_id LEFT JOIN Resource second ON Relationship.second_resource_id=second.id; | ||||
# license | # license | ||||
licenses_filter = get_filter_values('license') | |||||
licenses_filter = get_filter_values('license', type) | |||||
# language | # language | ||||
languages_filter = get_filter_values('scriptingLanguage') | |||||
return render_template('resources.html', resources=tools, type='tool', practices_filter=practices_filter, licenses_filter=licenses_filter, languages_filter=languages_filter) | |||||
languages_filter = get_filter_values('scriptingLanguage', type) | |||||
return render_template('resources.html', resources=tools, type=type, practices_filter=practices_filter, licenses_filter=licenses_filter, languages_filter=languages_filter) | |||||
# 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>') |