| @book.route('/books') | @book.route('/books') | ||||
| def get_books(): | def get_books(): | ||||
| type = 'book' | type = 'book' | ||||
| books = Resource.query.filter_by(type=type) | |||||
| books = Resource.query.filter_by(type=type).all() | |||||
| 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="' + type + '";' | |||||
| with db.engine.connect() as conn: | |||||
| books = conn.execute(text(query)) | |||||
| books = Resource.query.join(Relationship, Relationship.first_resource_id == Resource.id, isouter=True).filter(Resource.type==type, Relationship.second_resource_id==request.args.get(key)).all() | |||||
| also_books = Resource.query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Resource.type==type, Relationship.first_resource_id==request.args.get(key)).all() | |||||
| books = books + also_books | |||||
| else: | else: | ||||
| kwargs = {'type': type, key: request.args.get(key)} | kwargs = {'type': type, key: request.args.get(key)} | ||||
| books = Resource.query.filter_by(**kwargs) | |||||
| books = Resource.query.filter_by(**kwargs).all() | |||||
| # append relationships to each book | |||||
| append_relationships_multiple(books) | |||||
| # 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) |
| @main.route('/') | @main.route('/') | ||||
| def index(): | def index(): | ||||
| tools = Resource.query.filter_by(type='tool').order_by(func.random()).limit(6).all() | tools = Resource.query.filter_by(type='tool').order_by(func.random()).limit(6).all() | ||||
| # append relationships to each tool | |||||
| append_relationships_multiple(tools) | |||||
| books = Resource.query.filter_by(type='book').order_by(func.random()).limit(6).all() | books = Resource.query.filter_by(type='book').order_by(func.random()).limit(6).all() | ||||
| # append relationships to each book | |||||
| append_relationships_multiple(books) | |||||
| with open('content/home.md', 'r') as f: | with open('content/home.md', 'r') as f: | ||||
| text = f.read() | text = f.read() | ||||
| text = markdown.markdown(text) | text = markdown.markdown(text) | ||||
| # route for test page | # route for test page | ||||
| @main.route('/test') | @main.route('/test') | ||||
| def test(): | def test(): | ||||
| tool_id = '69' | |||||
| tool = get_full_resource(tool_id) | |||||
| return render_template('test.html', resource=tool) | |||||
| return render_template('test.html') | |||||
| # route for about page | # route for about page | ||||
| @main.route('/about') | @main.route('/about') |
| # 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(): | ||||
| practices = Resource.query.filter_by(type='practice') | |||||
| practices = Resource.query.filter_by(type='practice').all() | |||||
| # append relationships to each practice | |||||
| append_relationships_multiple(practices) | |||||
| return render_template('resources.html', resources=practices, type='practice') | return render_template('resources.html', resources=practices, type='practice') | ||||
| # 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 |
| for relationship in relationships: | for relationship in relationships: | ||||
| if relationship.type == 'tool': | if relationship.type == 'tool': | ||||
| if 'tools' not in resource.__dict__.keys(): | if 'tools' not in resource.__dict__.keys(): | ||||
| resource.__dict__['tools'] = relationship | |||||
| elif type(resource.__dict__['tools']) == list: | |||||
| resource.__dict__['tools'] = [] | |||||
| resource.__dict__['tools'].append(relationship) | resource.__dict__['tools'].append(relationship) | ||||
| else: | else: | ||||
| resource.__dict__['tools'] = [resource.__dict__['tools'], relationship] | |||||
| resource.__dict__['tools'].append(relationship) | |||||
| elif relationship.type == 'practice': | elif relationship.type == 'practice': | ||||
| if 'practices' not in resource.__dict__.keys(): | if 'practices' not in resource.__dict__.keys(): | ||||
| resource.__dict__['practices'] = relationship | |||||
| elif type(resource.__dict__['practices']) == list: | |||||
| resource.__dict__['practices'] = [] | |||||
| resource.__dict__['practices'].append(relationship) | resource.__dict__['practices'].append(relationship) | ||||
| else: | else: | ||||
| resource.__dict__['practices'] = [resource.__dict__['practices'], relationship] | |||||
| resource.__dict__['practices'].append(relationship) | |||||
| elif relationship.type == 'book': | elif relationship.type == 'book': | ||||
| if 'books' not in resource.__dict__.keys(): | if 'books' not in resource.__dict__.keys(): | ||||
| resource.__dict__['books'] = relationship | |||||
| elif type(resource.__dict__['books']) == list: | |||||
| resource.__dict__['books'] = [] | |||||
| resource.__dict__['books'].append(relationship) | resource.__dict__['books'].append(relationship) | ||||
| else: | else: | ||||
| resource.__dict__['books'] = [resource.__dict__['books'], relationship] | |||||
| resource.__dict__['books'].append(relationship) | |||||
| return resource | return resource | ||||
| else: | else: | ||||
| return resource | return resource | ||||
| # function to append relationships to a dictionary of resources | |||||
| def append_relationships_multiple(resources): | |||||
| for index, resource in enumerate(resources): | |||||
| resources[index] = append_relationships(resource) | |||||
| return resources | |||||
| # 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 |
| <div class="right lg:mt-16 max-w-[30rem] mx-auto p-4"> | <div class="right lg:mt-16 max-w-[30rem] mx-auto p-4"> | ||||
| {{ relationships_links(relationships) }} | {{ relationships_links(relationships) }} | ||||
| {% if resource.tools %} | |||||
| TOOLS: | |||||
| {% for tool in resource.tools %} | |||||
| {{ tool.name }}; | |||||
| {% endfor %} | |||||
| {% endif %} | |||||
| {% if resource.practices %} | |||||
| PRACTICES: | |||||
| {% for practice in resource.practices %} | |||||
| {{ practice.name }}; | |||||
| {% endfor %} | |||||
| {% endif %} | |||||
| {% if resource.books %} | |||||
| BOOKS: | |||||
| {% for book in resource.books %} | |||||
| {{ book.name }}; | |||||
| {% endfor %} | |||||
| {% endif %} | |||||
| </div> | </div> | ||||
| </div> | </div> | ||||
| {% extends 'base.html' %} | {% extends 'base.html' %} | ||||
| {% block content %} | {% block content %} | ||||
| <!-- <div class="cell-margin"> | <!-- <div class="cell-margin"> | ||||
| <div class="mb-8"> | <div class="mb-8"> | ||||
| <h2> | <h2> | ||||
| <div> | <div> | ||||
| {% for resource in resources %} | {% for resource in resources %} | ||||
| {{ resource_with_related(resource) }} | |||||
| {% if resource.tools %} | |||||
| TOOLS: | |||||
| {% for tool in resource.tools %} | |||||
| {{ tool.name }}; | |||||
| {% endfor %} | |||||
| {% endif %} | |||||
| {% if resource.practices %} | |||||
| PRACTICES: | |||||
| {% for practice in resource.practices %} | |||||
| {{ practice.name }}; | |||||
| {% endfor %} | |||||
| {% endif %} | |||||
| {% if resource.books %} | |||||
| BOOKS: | |||||
| {% for book in resource.books %} | |||||
| {{ book.name }}; | |||||
| {% endfor %} | |||||
| {% endif %} | |||||
| {{ resource_with_related(resource) }} | |||||
| {% endfor %} | {% endfor %} | ||||
| </div> | </div> | ||||
| </div> | </div> |
| @tool.route('/tools') | @tool.route('/tools') | ||||
| def get_tools(): | def get_tools(): | ||||
| type = 'tool' | type = 'tool' | ||||
| tools = Resource.query.filter_by(type=type) | |||||
| tools = Resource.query.filter_by(type=type).all() | |||||
| 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="' + type + '";' | |||||
| with db.engine.connect() as conn: | |||||
| tools = conn.execute(text(query)) | |||||
| tools = Resource.query.join(Relationship, Relationship.first_resource_id == Resource.id, isouter=True).filter(Resource.type==type, Relationship.second_resource_id==request.args.get(key)).all() | |||||
| also_tools = Resource.query.join(Relationship, Relationship.second_resource_id == Resource.id, isouter=True).filter(Resource.type==type, Relationship.first_resource_id==request.args.get(key)).all() | |||||
| tools = tools + also_tools | |||||
| 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=type).filter(Resource.scriptingLanguage.regexp_match(regex)) | |||||
| tools = Resource.query.filter_by(type=type).filter(Resource.scriptingLanguage.regexp_match(regex)).all() | |||||
| else: | else: | ||||
| kwargs = {'type': type, 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).all() | |||||
| # append relationships to each tool | |||||
| append_relationships_multiple(tools) | |||||
| # 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) |