소스 검색

get_full_resources function to combine data

joel
Simon Bowie 1 년 전
부모
커밋
6eb228a208
7개의 변경된 파일62개의 추가작업 그리고 67개의 파일을 삭제
  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 파일 보기

@@ -42,10 +42,8 @@ def get_books():
# route for displaying a single book based on the ID in the database
@book.route('/books/<int: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
@book.route('/books/<int:book_id>/edit', methods=('GET', 'POST'))

+ 5
- 4
web/app/main.py 파일 보기

@@ -24,9 +24,8 @@ def index():
with open('content/home.md', 'r') as f:
text = f.read()
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
@main.route('/profile')
@@ -37,7 +36,9 @@ def profile():
# route for test page
@main.route('/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
@main.route('/about')

+ 2
- 4
web/app/practice.py 파일 보기

@@ -25,10 +25,8 @@ def get_practices():
# route for displaying a single practice based on the ID in the database
@practice.route('/practices/<int: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
@practice.route('/practices/<int:practice_id>/edit', methods=('GET', 'POST'))

+ 32
- 0
web/app/resources.py 파일 보기

@@ -11,6 +11,7 @@ from flask import Blueprint, render_template, request, flash, redirect, url_for
from .models import Resource
from werkzeug.exceptions import abort
from . import db
from .relationships import *
from isbntools.app import *
import requests
import re
@@ -22,6 +23,37 @@ def get_resource(resource_id):
abort(404)
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
def delete_resource(resource_id):
deletion = Resource.query.get(resource_id)

+ 15
- 18
web/app/templates/book.html 파일 보기

@@ -7,28 +7,28 @@
<div class="left">
<div class="mb-8">
<div class="mb-2">Book</div>
{% if book['thumbnail'] %}
{% if resource['thumbnail'] %}
<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>
{% 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 %}
{% if book['Authors'] %}
{% if resource['Authors'] %}
<div class="">
{% if book['Authors']|length > 1 %}
{% if resource['Authors']|length > 1 %}
<div class="">
{% for author in book['Authors'] %}
{% for author in resource['Authors'] %}
{{ author }}</br>
{% endfor %}
</div>
{% else %}
<div>
{% for author in book['Authors'] %}
{% for author in resource['Authors'] %}
{{ author }}
{% endfor %}
</div>
@@ -62,29 +62,27 @@
<a target="_blank" href="{{ resource['bookUrl'] }}">{{ resource['bookUrl'] }}</a>
</div>
{% endif %}
{% if book %}
<!-- fields for books from isbntools -->
{% if book['ISBN-13'] %}
{% if resource['ISBN-13'] %}
<div class="">
<h3>
ISBN-13
</h3>
<div>
{{ book['ISBN-13'] }}
{{ resource['ISBN-13'] }}
</div>
</div>
{% endif %}
{% if book['Publisher'] %}
{% if resource['Publisher'] %}
<div class="">
<h3>
Publisher
</h3>
<div>
{{ book['Publisher'] }}
{{ resource['Publisher'] }}
</div>
</div>
{% endif %}
{% else %}
<!-- fields for books from database -->
{% if resource['name'] %}
<!-- <div class="">
@@ -106,11 +104,10 @@
</div>
</div>
{% endif %}
{% endif %}
{% if book['desc'] %}
{% if resource['desc'] %}
<div class="col-span-2">
<h3>Publisher's description</h3>
<div class="">{{ book['desc'] }}</div>
<div class="">{{ resource['desc'] }}</div>
</div>
{% endif %}


+ 4
- 34
web/app/templates/test.html 파일 보기

@@ -1,41 +1,11 @@
{% extends 'base.html' %}

{% 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 %}

+ 2
- 3
web/app/tool.py 파일 보기

@@ -46,9 +46,8 @@ def get_tools():
# route for displaying a single tool based on the ID in the database
@tool.route('/tools/<int: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
@tool.route('/tools/<int:tool_id>/edit', methods=('GET', 'POST'))

Loading…
취소
저장