Browse Source

get_full_resources function to combine data

joel
Simon Bowie 1 year ago
parent
commit
6eb228a208
7 changed files with 62 additions and 67 deletions
  1. +2
    -4
      web/app/book.py
  2. +5
    -4
      web/app/main.py
  3. +2
    -4
      web/app/practice.py
  4. +32
    -0
      web/app/resources.py
  5. +15
    -18
      web/app/templates/book.html
  6. +4
    -34
      web/app/templates/test.html
  7. +2
    -3
      web/app/tool.py

+ 2
- 4
web/app/book.py View File

# 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>')
def show_book(book_id): def show_book(book_id):
book = get_resource(book_id)
relationships = get_relationships(book_id)
book_data = get_book_data(book.isbn)
return render_template('book.html', resource=book, relationships=relationships, book=book_data)
book = get_full_resource(book_id)
return render_template('book.html', resource=book)


# route for editing a single book based on the ID in the database # route for editing a single book based on the ID in the database
@book.route('/books/<int:book_id>/edit', methods=('GET', 'POST')) @book.route('/books/<int:book_id>/edit', methods=('GET', 'POST'))

+ 5
- 4
web/app/main.py View File

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)
book_showcase = get_book('69')
book_showcase_relationships = get_relationships('69')
return render_template('index.html', text=text, tools=tools, books=books, book=book_showcase, book_relationships=book_showcase_relationships)
book_showcase = get_full_resource('69')
return render_template('index.html', text=text, tools=tools, books=books, book=book_showcase)


# route for profile page # route for profile page
@main.route('/profile') @main.route('/profile')
# route for test page # route for test page
@main.route('/test') @main.route('/test')
def test(): def test():
return render_template('test.html')
tool_id = '69'
tool = get_full_resource(tool_id)
return render_template('test.html', resource=tool)


# route for about page # route for about page
@main.route('/about') @main.route('/about')

+ 2
- 4
web/app/practice.py View File

# 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>')
def show_practice(practice_id): def show_practice(practice_id):
practice = get_resource(practice_id)
relationships = get_relationships(practice_id)
practice.references = replace_urls(practice.references)
return render_template('resource.html', resource=practice, relationships=relationships)
practice = get_full_resource(practice_id)
return render_template('resource.html', resource=practice)


# route for editing a single practice based on the ID in the database # route for editing a single practice based on the ID in the database
@practice.route('/practices/<int:practice_id>/edit', methods=('GET', 'POST')) @practice.route('/practices/<int:practice_id>/edit', methods=('GET', 'POST'))

+ 32
- 0
web/app/resources.py View File

from .models import Resource from .models import Resource
from werkzeug.exceptions import abort from werkzeug.exceptions import abort
from . import db from . import db
from .relationships import *
from isbntools.app import * from isbntools.app import *
import requests import requests
import re import re
abort(404) abort(404)
return resource return resource


# function to retrieve data about a resource and its relationships
def get_full_resource(resource_id):
resource = get_resource(resource_id)
relationships = get_relationships(resource_id)
for relationship in relationships:
if relationship.type == 'tool':
if 'tools' not in resource.__dict__.keys():
resource.__dict__['tools'] = relationship
elif type(resource.__dict__['tools']) == list:
resource.__dict__['tools'].append(relationship)
else:
resource.__dict__['tools'] = [resource.__dict__['tools'], relationship]
elif relationship.type == 'practice':
if 'practices' not in resource.__dict__.keys():
resource.__dict__['practices'] = relationship
elif type(resource.__dict__['practices']) == list:
resource.__dict__['practices'].append(relationship)
else:
resource.__dict__['practices'] = [resource.__dict__['practices'], relationship]
elif relationship.type == 'book':
if 'books' not in resource.__dict__.keys():
resource.__dict__['books'] = relationship
elif type(resource.__dict__['books']) == list:
resource.__dict__['books'].append(relationship)
else:
resource.__dict__['books'] = [resource.__dict__['books'], relationship]
if resource.type == 'book':
book_data = get_book_data(resource.isbn)
resource.__dict__.update(book_data)
return resource

# function to delete a single resource # function to delete a single resource
def delete_resource(resource_id): def delete_resource(resource_id):
deletion = Resource.query.get(resource_id) deletion = Resource.query.get(resource_id)

+ 15
- 18
web/app/templates/book.html View File

<div class="left"> <div class="left">
<div class="mb-8"> <div class="mb-8">
<div class="mb-2">Book</div> <div class="mb-2">Book</div>
{% if book['thumbnail'] %}
{% if resource['thumbnail'] %}
<div class="float-right"> <div class="float-right">
<img class="w-40 h-40 object-contain m-16 rotate-[15deg]" src={{ book['thumbnail'] }} alt="cover for {{ book['Title'] }}">
<img class="w-40 h-40 object-contain m-16 rotate-[15deg]" src={{ resource['thumbnail'] }} alt="cover for {{ resource['Title'] }}">
</div> </div>
{% endif %} {% endif %}


<h2 class="book mb-2 max-w-[30rem]">{% block title %} {{ book['Title'] or resource['name'] }} {% endblock %}</h2>
<h2 class="book mb-2 max-w-[30rem]">{% block title %} {{ resource['Title'] or resource['name'] }} {% endblock %}</h2>


{% if book['Year'] %}
{{ book['Year'] }}
{% if resource['Year'] %}
{{ resource['Year'] }}
{% endif %} {% endif %}
{% if book['Authors'] %}
{% if resource['Authors'] %}
<div class=""> <div class="">
{% if book['Authors']|length > 1 %}
{% if resource['Authors']|length > 1 %}
<div class=""> <div class="">
{% for author in book['Authors'] %}
{% for author in resource['Authors'] %}
{{ author }}</br> {{ author }}</br>
{% endfor %} {% endfor %}
</div> </div>
{% else %} {% else %}
<div> <div>
{% for author in book['Authors'] %}
{% for author in resource['Authors'] %}
{{ author }} {{ author }}
{% endfor %} {% endfor %}
</div> </div>
<a target="_blank" href="{{ resource['bookUrl'] }}">{{ resource['bookUrl'] }}</a> <a target="_blank" href="{{ resource['bookUrl'] }}">{{ resource['bookUrl'] }}</a>
</div> </div>
{% endif %} {% endif %}
{% if book %}
<!-- fields for books from isbntools --> <!-- fields for books from isbntools -->
{% if book['ISBN-13'] %}
{% if resource['ISBN-13'] %}
<div class=""> <div class="">
<h3> <h3>
ISBN-13 ISBN-13
</h3> </h3>
<div> <div>
{{ book['ISBN-13'] }}
{{ resource['ISBN-13'] }}
</div> </div>
</div> </div>
{% endif %} {% endif %}
{% if book['Publisher'] %}
{% if resource['Publisher'] %}
<div class=""> <div class="">
<h3> <h3>
Publisher Publisher
</h3> </h3>
<div> <div>
{{ book['Publisher'] }}
{{ resource['Publisher'] }}
</div> </div>
</div> </div>
{% endif %} {% endif %}
{% else %}
<!-- fields for books from database --> <!-- fields for books from database -->
{% if resource['name'] %} {% if resource['name'] %}
<!-- <div class=""> <!-- <div class="">
</div> </div>
</div> </div>
{% endif %} {% endif %}
{% endif %}
{% if book['desc'] %}
{% if resource['desc'] %}
<div class="col-span-2"> <div class="col-span-2">
<h3>Publisher's description</h3> <h3>Publisher's description</h3>
<div class="">{{ book['desc'] }}</div>
<div class="">{{ resource['desc'] }}</div>
</div> </div>
{% endif %} {% endif %}



+ 4
- 34
web/app/templates/test.html View File

{% extends 'base.html' %} {% extends 'base.html' %}


{% block content %} {% block content %}
<style>


.collapse_text {
padding: 0 18px;
background-color: #f1f1f1;
}
{{ resource.__dict__ }}


</style>

<h2>Collapsible text</h2>

<b>HackMD</b>'s online writing environment supports practices of <a data-bs-toggle="collapse" href="#collapse_editing" role="button" aria-expanded="false" aria-controls="collapse_editing">collaborative editing</a>, <a data-bs-toggle="collapse" href="#collapse_writing" role="button" aria-expanded="false" aria-controls="collapse_writing">collaborative writing</a>, and <a data-bs-toggle="collapse" href="#collapse_versioning" role="button" aria-expanded="false" aria-controls="collapse_versioning">versioning</a>.

<div class="collapse collapse_text" id="collapse_editing">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<div class="collapse collapse_text" id="collapse_writing">
<p>Exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>

<div class="collapse collapse_text" id="collapse_versioning">
<p>Versioning text goes here.</p>
</div>

<br><br>

<h2>Tooltips</h2>

<b>HackMD</b>'s online writing environment supports practices of <a href="#" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.">collaborative editing</a>, <a href="#" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.">collaborative writing</a>, and <a href="#" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Versioning text goes here.">versioning</a>.

<br><br>

<h2>Popover</h2>

<b>HackMD</b>'s online writing environment supports practices of <a href="#" data-bs-toggle="popover" title="collaborative editing description" data-bs-content="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.">collaborative editing</a>, <a href="#" data-bs-toggle="popover" title="collaborative writing description" data-bs-content="Exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.">collaborative writing</a>, and <a href="#" data-bs-toggle="popover" title="versioning description" data-bs-content="Versioning text goes here.">collaborative editing</a>versioning</a>.
{% for book in resource.books %}
{{ book.name }}
{% endfor %}


{% endblock %} {% endblock %}

+ 2
- 3
web/app/tool.py View File

# 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>')
def show_tool(tool_id): def show_tool(tool_id):
tool = get_resource(tool_id)
relationships = get_relationships(tool_id)
return render_template('resource.html', resource=tool, relationships=relationships)
tool = get_full_resource(tool_id)
return render_template('resource.html', resource=tool)


# route for editing a single tool based on the ID in the database # route for editing a single tool based on the ID in the database
@tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST')) @tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST'))

Loading…
Cancel
Save