| @@ -20,18 +20,21 @@ book = Blueprint('book', __name__) | |||
| # route for displaying all books in database | |||
| @book.route('/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(): | |||
| 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) | |||
| 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 | |||
| # practices | |||
| 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 | |||
| @book.route('/books/<int:book_id>') | |||
| @@ -55,6 +58,10 @@ def edit_book(book_id): | |||
| else: | |||
| book = Resource.query.get(book_id) | |||
| 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'] | |||
| db.session.commit() | |||
| linked_resources = request.form.getlist('linked_resources') | |||
| @@ -47,6 +47,9 @@ class Resource(db.Model): | |||
| lessonsLearned = db.Column(db.Text) | |||
| references = db.Column(db.Text) | |||
| # books | |||
| author = db.Column(db.Text) | |||
| year = db.Column(db.Text) | |||
| bookUrl = db.Column(db.Text) | |||
| isbn = db.Column(db.Text) | |||
| # table for relationships | |||
| @@ -27,9 +27,9 @@ def delete_resource(resource_id): | |||
| flash('Successfully deleted!') | |||
| # function to get filters for a specific field | |||
| def get_filter_values(field): | |||
| def get_filter_values(field, type): | |||
| # 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 | |||
| field_filter = [i for i, in field_filter] | |||
| # split each element on '/' (useful for scriptingLanguage only) | |||
| @@ -43,7 +43,7 @@ def get_filter_values(field): | |||
| return field_filter | |||
| def get_book_data(isbn): | |||
| #try: | |||
| try: | |||
| book = meta(isbn) | |||
| description = {'desc': desc(isbn)} | |||
| book.update(description) | |||
| @@ -56,5 +56,5 @@ def get_book_data(isbn): | |||
| book_cover = {'thumbnail': openl_url} | |||
| book.update(book_cover) | |||
| return book | |||
| #except: | |||
| # pass | |||
| except: | |||
| pass | |||
| @@ -22,6 +22,16 @@ | |||
| <div class="col"> | |||
| <table class="table table-hover"> | |||
| <tbody> | |||
| {% if resource['bookUrl'] %} | |||
| <tr> | |||
| <th> | |||
| URL: | |||
| </th> | |||
| <td> | |||
| <a href="{{ resource['bookUrl'] }}">{{ resource['bookUrl'] }}</a> | |||
| </td> | |||
| </tr> | |||
| {% endif %} | |||
| {% if book %} | |||
| <!-- fields for books from isbntools --> | |||
| {% if book['Title'] %} | |||
| @@ -119,6 +129,26 @@ | |||
| </td> | |||
| </tr> | |||
| {% 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 %} | |||
| </tbody> | |||
| </table> | |||
| @@ -155,6 +155,27 @@ | |||
| </div> | |||
| {% 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"> | |||
| <label for="isbn">ISBN</label> | |||
| <input type="text" name="isbn" placeholder="ISBN" | |||
| @@ -47,6 +47,32 @@ | |||
| </div> | |||
| </div> | |||
| {% 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 class="row"> | |||
| {% if languages_filter %} | |||
| @@ -55,7 +81,7 @@ | |||
| <ul class="filter-items"> | |||
| {% for language in languages_filter %} | |||
| <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> | |||
| {% endfor %} | |||
| </ul> | |||
| @@ -20,26 +20,27 @@ tool = Blueprint('tool', __name__) | |||
| # route for displaying all tools in database | |||
| @tool.route('/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(): | |||
| 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) | |||
| elif key == 'scriptingLanguage': | |||
| 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: | |||
| kwargs = {'type': 'tool', key: request.args.get(key)} | |||
| kwargs = {'type': type, key: request.args.get(key)} | |||
| tools = Resource.query.filter_by(**kwargs) | |||
| # get filters | |||
| # practices | |||
| 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; | |||
| # license | |||
| licenses_filter = get_filter_values('license') | |||
| licenses_filter = get_filter_values('license', type) | |||
| # 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 | |||
| @tool.route('/tools/<int:tool_id>') | |||