|
-
-
-
-
-
-
-
-
- from flask import Blueprint, render_template, request, flash, redirect, url_for
- from flask_login import login_required, current_user
- from .models import Resource
- from .resources import *
- from .relationships import *
- from . import db
- import os
- from sqlalchemy.sql import func
- import markdown
-
- book = Blueprint('book', __name__)
-
-
- @book.route('/books')
- def get_books():
-
-
- view = request.args.get('view')
- page = request.args.get('page', 1, type=int)
-
- resource_type = 'book'
-
- with open('content/books.md', 'r') as f:
- intro_text = f.read()
- intro_text = markdown.markdown(intro_text)
-
-
- books_query = Resource.query.filter_by(type=resource_type).filter_by(published=True)
-
-
- for key in request.args.keys():
- 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))
- books_query = books_1.union(books_2)
- if (key != 'practice' and request.args.get(key) != ''):
- kwargs = {key: request.args.get(key)}
- books_query = books_query.filter_by(**kwargs)
-
-
- books = books_query.order_by(Resource.name).paginate(page=page, per_page=25)
-
-
-
-
- practices_filter = Resource.query.filter_by(type='practice').filter_by(published=True).with_entities(Resource.id, Resource.name).all()
-
- year_filter = get_filter_values('year', resource_type)
-
- typology_filter = get_filter_values('typology', resource_type)
-
-
-
- if view != 'list':
-
- append_relationships_multiple_paginated(books)
-
- 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)
-
-
- @book.route('/books/<int:book_id>')
- def show_book(book_id):
- book = get_full_resource(book_id)
- return render_template('book.html', resource=book)
-
-
- @book.route('/books/<int:book_id>/edit', methods=('GET', 'POST'))
- @login_required
- def edit_book(book_id):
- book = get_resource(book_id)
- resource_dropdown = Resource.query.order_by(Resource.name)
- existing_relationships = get_relationships(book_id)
-
- if request.method == 'POST':
- if not request.form['name']:
- flash('Name is required!')
- 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']
- book.typology = request.form['typology']
- db.session.commit()
- linked_resources = request.form.getlist('linked_tools') + request.form.getlist('linked_practices')
- remove_linked_resources = request.form.getlist('remove_linked_resources')
-
- edit_relationships(book_id, linked_resources, remove_linked_resources, existing_relationships)
-
- return redirect(url_for('book.get_books',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
-
- return render_template('edit.html', resource=book, resource_dropdown=resource_dropdown, relationships=existing_relationships)
-
-
-
- @book.route('/books/<int:book_id>/delete', methods=('POST',))
- @login_required
- def delete_book(book_id):
- delete_resource(book_id)
- return redirect(url_for('book.get_books',_external=True,_scheme=os.environ.get('SSL_SCHEME')))
|