diff --git a/pyShelf.py b/pyShelf.py index b753012..d626abc 100755 --- a/pyShelf.py +++ b/pyShelf.py @@ -9,7 +9,6 @@ from src.backend.lib.storage import Storage from src.backend.pyShelf_MakeCollections import MakeCollections from src.backend.pyShelf_ScanLibrary import execute_scan from src.frontend.lib.FastAPIServer import FastAPIServer -from src.frontend.lib.objects import JSInterface # import websockets @@ -30,7 +29,6 @@ def run_import(): async def main(): """Program entrypoint.""" - JSInterface(config=config).install() Storage(config=config).create_tables() _import_thread = Thread(target=run_import) _import_thread.start() diff --git a/src/backend/lib/storage.py b/src/backend/lib/storage.py index 77ce9e1..0f9efb2 100644 --- a/src/backend/lib/storage.py +++ b/src/backend/lib/storage.py @@ -164,3 +164,27 @@ class Storage: f"Collection {_s} failed: {e}") _collections.append(_p) self.config.logger.info("Finished making collections.") + + def get_books(self, collection=None): + """Get books from database. + + Parameters + ---------- + collection : str + Collection to filter by. + + Returns + ------- + _result : ScalarResult Object + """ + session = Session(self.engine) + if collection: + _result = session.execute( + select(Book).join(Collection).where( + Collection.collection == collection + ) + ).all() + else: + _result = session.execute(select(Book)).all() + session.close() + return _result diff --git a/src/frontend/lib/FastAPIServer.py b/src/frontend/lib/FastAPIServer.py index 4dfe9a3..04cc637 100644 --- a/src/frontend/lib/FastAPIServer.py +++ b/src/frontend/lib/FastAPIServer.py @@ -1,11 +1,15 @@ """pyShelf's main frontend library.""" import uvicorn +import os import sass from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse from fastapi.routing import APIRoute from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates +from ...backend.lib.storage import Storage +from .objects import JSInterface +from ...backend.lib.config import Config app = FastAPI() templates = Jinja2Templates(directory="src/frontend/templates") @@ -23,6 +27,7 @@ class FastAPIServer(): self.fe_config = uvicorn.Config(app, port=8080, log_level="info", reload=True) self.fe_server = uvicorn.Server(self.fe_config) + self.JSInterface: JSInterface = JSInterface(self.config) self.compile_static_files() def compile_static_files(self): @@ -34,6 +39,7 @@ class FastAPIServer(): with open('src/frontend/static/styles/pyShelf.css', 'w') as _pyShelf: _pyShelf.write(_pyShelf_src[0]) _pyShelf.close() + self.JSInterface.install() return True def use_route_names_as_operation_ids(self, app: FastAPI) -> None: @@ -44,15 +50,12 @@ class FastAPIServer(): @app.get("/", response_class=HTMLResponse) async def index(request: Request): + storage = Storage(Config(os.path.abspath(os.getcwd()))) + books = storage.get_books() """Home page responder.""" - return templates.TemplateResponse( - "index.html", - { - "request": request, - "package": { - "test": "This is a test variable" - } - }) + # _books = self.storage.get_books() + context = {"request": request, "books": books} + return templates.TemplateResponse("index.html", context ) @app.get("/users/me") async def about_me(self): diff --git a/src/frontend/lib/objects.py b/src/frontend/lib/objects.py index c6e26b5..c7e12d3 100644 --- a/src/frontend/lib/objects.py +++ b/src/frontend/lib/objects.py @@ -17,7 +17,14 @@ class JSInterface(): def install(self): """Install the JavaScript dependencies.""" if which("npm"): + self.config.logger.info("Installing JavaScript dependencies...") run(["npm", "install"], cwd=self.package_json.parent) else: self.config.logger.error("npm is not installed.") exit(1) + if which("npx"): + self.config.logger.info("Compiling TypeScript...") + run(["npx", "tsc", "static/script/pyshelf.ts"], cwd=self.package_json.parent) + else: + self.config.logger.error("npx is not installed.") + exit(1) diff --git a/src/frontend/static/script/pyshelf.js b/src/frontend/static/script/pyshelf.js new file mode 100644 index 0000000..84b87dc --- /dev/null +++ b/src/frontend/static/script/pyshelf.js @@ -0,0 +1,21 @@ +function getScreenSize() { + return { + width: window.innerWidth, + height: window.innerHeight + }; +} +function sizeMaster() { + var size = getScreenSize(); + var navbar = $('#navbar-main').height(); + var footer = $('#footer-main').height(); + var master = $('#master').height(size.height - navbar - footer); +} +$(document).ready(function () { + // Get the current URL + var url = window.location.href; + // Get the last part of the URL + sizeMaster(); + $(window).on('resize', function () { + sizeMaster(); + }); +}); diff --git a/src/frontend/static/script/pyshelf.ts b/src/frontend/static/script/pyshelf.ts index 6d6a1ed..c119ca8 100644 --- a/src/frontend/static/script/pyshelf.ts +++ b/src/frontend/static/script/pyshelf.ts @@ -4,12 +4,20 @@ function getScreenSize() { height: window.innerHeight }; } -$(document).ready(function() { - // Get the current URL - var url = window.location.href; - // Get the last part of the URL - var screenSize = getScreenSize(); - var x: number = screenSize.width; - var y: number = screenSize.height; - console.log(x, y); + +function sizeMaster() { + var size = getScreenSize(); + var navbar = $('#navbar-main').height(); + var footer = $('#footer-main').height(); + var master = $('#master').height(size.height - navbar - footer); } + +$(document).ready(function() { + // Get the current URL + var url = window.location.href; + // Get the last part of the URL + sizeMaster(); + $(window).on('resize', function() { + sizeMaster(); + }); +}) diff --git a/src/frontend/templates/example.tpl b/src/frontend/templates/example.tpl new file mode 100644 index 0000000..0f060d5 --- /dev/null +++ b/src/frontend/templates/example.tpl @@ -0,0 +1,7 @@ + +{% include 'header.html' %} + {% include 'navigation.html' %} +
+ {{ package.test }} +
+{% include 'footer.html' %} diff --git a/src/frontend/templates/footer.html b/src/frontend/templates/footer.html index 7177b7b..b1b43f9 100644 --- a/src/frontend/templates/footer.html +++ b/src/frontend/templates/footer.html @@ -1,4 +1,4 @@ -