Updated to show collections in navigation.

This commit is contained in:
th3r00t
2023-11-10 00:53:32 -05:00
parent 551feb9e0e
commit 4e0997df06
6 changed files with 105 additions and 47 deletions

View File

@@ -13,7 +13,3 @@ repos:
rev: v0.4.0 rev: v0.4.0
hooks: hooks:
- id: autoflake8 - id: autoflake8
- repo: https://github.com/ambv/black
rev: 22.10.0
hooks:
- id: black

View File

@@ -101,14 +101,20 @@ def book_tojson(book) -> dumps:
"publisher": book[0].publisher, "publisher": book[0].publisher,
}) })
def tojson(obj) -> dumps:
return dumps(obj)
def collections_tojson(collection) -> dumps: def collections_tojson(collection) -> dumps:
"""Convert a collections object to json.""" """Convert a collections object to json."""
_collections = [] _collections = []
_collection_id_set = set()
for _collection in collection: for _collection in collection:
if _collection[0].id in _collection_id_set:
pass
else:
_collection_id_set.add(_collection[0].id)
_collections.append({ _collections.append({
"collection_id": _collection[0].collection_id, "collection_id": _collection[0].id,
"book_id": _collection[0].book_id,
"collection": _collection[0].collection, "collection": _collection[0].collection,
}) })
return dumps(_collections) return dumps(_collections)
@@ -117,6 +123,8 @@ def collections_tojson(collection) -> dumps:
templates.env.filters["b64decode"] = base64decode templates.env.filters["b64decode"] = base64decode
templates.env.filters["summarize"] = summarize templates.env.filters["summarize"] = summarize
templates.env.filters["books_tojson"] = books_tojson templates.env.filters["books_tojson"] = books_tojson
templates.env.filters["collections_tojson"] = collections_tojson
templates.env.filters["tojson"] = tojson
class FastAPIServer(): class FastAPIServer():
@@ -143,13 +151,6 @@ class FastAPIServer():
with open('src/frontend/static/styles/pyShelf.css', 'w') as _pyShelf: with open('src/frontend/static/styles/pyShelf.css', 'w') as _pyShelf:
_pyShelf.write(_pyShelf_src[0]) _pyShelf.write(_pyShelf_src[0])
#_bulma_src = sass.compile(
# filename='src/frontend/static/styles/bulma.sass',
# source_map_filename='src/frontend/static/styles/bulma.sass',
# output_style='compressed')
#with open('src/frontend/static/styles/bulma.css', 'w') as _bulma:
# _bulma.write(_bulma_src[0])
self.JSInterface.install() self.JSInterface.install()
return True return True
@@ -163,11 +164,12 @@ class FastAPIServer():
async def index(request: Request, skip: int = 0, limit: int = 10): async def index(request: Request, skip: int = 0, limit: int = 10):
storage = Storage(Config(os.path.abspath(os.getcwd()))) storage = Storage(Config(os.path.abspath(os.getcwd())))
books = storage.get_books(collection=None, skip=skip, limit=limit) books = storage.get_books(collection=None, skip=skip, limit=limit)
collections = storage.get_collections()
"""Home page responder.""" """Home page responder."""
context = {"request": request, "books": books} context = {"request": request, "books": books, "collections": collections}
return templates.TemplateResponse("index.html", context) return templates.TemplateResponse("index.html", context)
@app.get("/books", response_class=JSONResponse) @app.get("/api/books", response_class=JSONResponse)
async def books(request: Request, skip: int = 0, limit: int = 10, collection=None): async def books(request: Request, skip: int = 0, limit: int = 10, collection=None):
storage = Storage(Config(os.path.abspath(os.getcwd()))) storage = Storage(Config(os.path.abspath(os.getcwd())))
books = storage.get_books(collection, skip=skip, limit=limit) books = storage.get_books(collection, skip=skip, limit=limit)
@@ -176,14 +178,14 @@ class FastAPIServer():
return JSONResponse(content=books_tojson(books)) return JSONResponse(content=books_tojson(books))
# return JSONResponse(content=books) # return JSONResponse(content=books)
@app.get("/book/{book_id}", response_class=JSONResponse) @app.get("/api/book/{book_id}", response_class=JSONResponse)
async def book(request: Request, book_id: int): async def book(request: Request, book_id: int):
storage = Storage(Config(os.path.abspath(os.getcwd()))) storage = Storage(Config(os.path.abspath(os.getcwd())))
book = storage.get_book(book_id) book = storage.get_book(book_id)
"""Home page responder.""" """Home page responder."""
return JSONResponse(content=book_tojson(book)) return JSONResponse(content=book_tojson(book))
@app.get("/collections", response_class=JSONResponse) @app.get("/api/collections", response_class=JSONResponse)
async def collections(request: Request): async def collections(request: Request):
storage = Storage(Config(os.path.abspath(os.getcwd()))) storage = Storage(Config(os.path.abspath(os.getcwd())))
collections = storage.get_collections() collections = storage.get_collections()

View File

@@ -12,15 +12,38 @@ function sizeMaster() {
var master = $('#master').height(size.height - navbar - footer); var master = $('#master').height(size.height - navbar - footer);
} }
$(document).ready(function() { function getParams() {
// Get the current URL
var url = window.location.href; var url = window.location.href;
console.log(url); var params = url.split('?')[1];
// Get the last part of the URL if (params) {
var paramstr = params.split('&');
var paramsObj = {};
for (var i = 0; i < paramstr.length; i++) {
var param = paramstr[i].split('=');
paramsObj[param[0]] = param[1];
}
return paramsObj;
}
return null;
}
function localStorePutTest(data) {
console.log('localStorePutTest');
localStorage.setItem('test', JSON.stringify(data));
}
function localStoreGetTest() {
console.log('localStoreGetTest');
console.log(localStorage.getItem('test'));
}
$(document).ready(function() {
console.log(getParams());
$(".navbar-burger").click(function(){ $(".navbar-burger").click(function(){
$(".navbar-burger").toggleClass("is-active"); $(".navbar-burger").toggleClass("is-active");
$(".navbar-menu").toggleClass("is-active"); $(".navbar-menu").toggleClass("is-active");
}); });
sizeMaster(); sizeMaster();
$(window).on('resize', function() { $(window).on('resize', function() {
sizeMaster(); sizeMaster();

View File

@@ -101,3 +101,25 @@ $footer-padding: 0.5rem 0.5rem
background-repeat: no-repeat; background-repeat: no-repeat;
background-attachment: fixed; background-attachment: fixed;
background-size: cover; background-size: cover;
#master-container
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: space-between;
#book-shelf
display: grid;
justify-content: space-evenly;
align-items: center;
align-content: center;
padding: 1rem;
margin: 1rem;
// background-color: $ps-color-background;
.collection
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: center;
align-content: center;
background-color: $ps-color-background;

View File

@@ -2,7 +2,7 @@
{% block javascript %} {% block javascript %}
<script type="text/javascript" src=static/script/pako.min.js> <script type="text/javascript" src=static/script/pako.min.js>
<script type="text/javascript"> <script type="text/javascript">
var books = {{ books|books_tojson }}; const books = {{ books|books_tojson }};
let inflatedJSON = {}; let inflatedJSON = {};
const pako = require('pako'); const pako = require('pako');
inflatedJSON = JSON.parse(pako.inflate(books, { to: 'string'})); inflatedJSON = JSON.parse(pako.inflate(books, { to: 'string'}));
@@ -10,12 +10,12 @@
{% endblock %} {% endblock %}
{% include 'header.html' %} {% include 'header.html' %}
{% include 'navigation.html' %} {% include 'navigation.html' %}
<section id="master"> <section id="master-container">
<div class="container is-dark"> <div id="book-shelf" class="container is-dark">
{% for book in books %} {% for book in books %}
{% set cover = book[0].cover|b64decode %} {% set cover = book[0].cover|b64decode %}
{% if cover != 'None' %} {% if cover != 'None' %}
<div class="box is-dark book" id="{{book[0].id}}"> <div class="is-dark book" id="{{book[0].id}}">
<div class="image book-thumbnail"> <div class="image book-thumbnail">
<figure class="image is-4by3"> <figure class="image is-4by3">
<img src="data:;base64,{{ book[0].cover|b64decode }}" alt="{{ book[0].title }}"> <img src="data:;base64,{{ book[0].cover|b64decode }}" alt="{{ book[0].title }}">
@@ -23,7 +23,12 @@
</div> </div>
</div> </div>
{% else %} {% else %}
<div class="box is-dark book" id="{{book[0].id}}"> <div class="is-dark book" id="{{book[0].id}}">
<div class="image book-thumbnail">
<figure class="image is-4by3">
<img src="static/images/no-cover.jpg" alt="{{ book[0].title }}">
</figure>
</div>
<h3 class="title is-3">{{ book[0].title }}</h3> <h3 class="title is-3">{{ book[0].title }}</h3>
<h4 class="subtitle is-4">{{ book[0].author }}</h4> <h4 class="subtitle is-4">{{ book[0].author }}</h4>
<p class="content">{{ book[0].description|summarize }}</p> <p class="content">{{ book[0].description|summarize }}</p>

View File

@@ -1,3 +1,6 @@
<script type="text/javascript">
const collections = {{ collections|collections_tojson }};
</script>
<nav id="navbar-main" class="navbar is-fixed-top is-dark" role="navigation" aria-label="main navigation"> <nav id="navbar-main" class="navbar is-fixed-top is-dark" role="navigation" aria-label="main navigation">
<div class="navbar-brand"> <div class="navbar-brand">
<a class="navbar-item" href="https://github.com/th3r00t/pyShelf"> <a class="navbar-item" href="https://github.com/th3r00t/pyShelf">
@@ -43,6 +46,13 @@
</a> </a>
</div> </div>
</div> </div>
<div class="select is-small is-rounded is-link" id="collection_dropdown">
<select id="collection_select">
{% for collection in collections %}
<option value={{collection[0].id}} class="collection_selection">{{collection[0].collection}}</option>
{% endfor %}
</select>
</div>
</div> </div>
<div class="navbar-end"> <div class="navbar-end">
<div class="navbar-item"> <div class="navbar-item">