diff --git a/the_works/__init__.py b/the_works/__init__.py index 0a6eb31..88fb91f 100644 --- a/the_works/__init__.py +++ b/the_works/__init__.py @@ -1,9 +1,11 @@ from flask import Flask -from the_works.database import init_db # this import is not strictly necessary but it forces pipreqs-to include dotenv when generating `requirements.txt` import dotenv +from the_works.database import init_db +from the_works.views import home, text, werk, verlag, sprache, textform, werksform, genre, pseudonym, reihe, herausgeber, veroeffentlichung, titelbild + #from flask_debugtoolbar import DebugToolbarExtension def create_app(config=None): @@ -28,7 +30,6 @@ def create_app(config=None): init_db(app) # register blueprints - from the_works.views import home, text, werk, verlag, sprache, textform, werksform, genre, pseudonym, reihe, herausgeber, veroeffentlichung, titelbild app.register_blueprint(genre.bp) app.register_blueprint(herausgeber.bp) app.register_blueprint(home.bp) @@ -51,7 +52,7 @@ def create_app(config=None): # helper function to print formatted file size; [source](https://stackoverflow.com/a/1094933) def sizeof_fmt(num: int | str, suffix: str = "B") -> str: - if type(num) == str: + if isinstance(num, str): num = int(num) for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"): if abs(num) < 1024.0: diff --git a/the_works/database.py b/the_works/database.py index dd04987..2c1fdf1 100644 --- a/the_works/database.py +++ b/the_works/database.py @@ -1,34 +1,13 @@ from flask_sqlalchemy import SQLAlchemy from the_works.models import Base -# Instantiate the SQLAlchemy around the database schema's declarative mapping +# Instantiate the SQLAlchemy around the database schema's declarative mapping db = SQLAlchemy(model_class=Base) def init_db(app): - # initialize the Flask app with the flask_sqlalchemy extension + # initialize the Flask app with the flask_sqlalchemy extension db.init_app(app) # create nonexistent tables in DB with app.app_context(): db.create_all() - -""" - # check if database is empty and if so, populate it with fresh tables - #TODO: does it make sense to try and create all tables by default? Existing tables wouldn't be overwritten but what about DB constraints ? - if not len(db.metadata.tables): - # import table classes from code that was generated via `sqlacodegen --generator tables sqlite:///the_works.sqlite > ./the_works/tables.py` in project root - import the_works.tables - - # filter the objects just imported for those of type sqlalchemy.Table - from sqlalchemy import Table - table_list = list(filter(lambda t: type(t) == Table, vars(the_works.tables).values())) - - # Table objects imported from sqlacodegen code are associated with a random MetaData() object, so we have to re-associate them with the DB's metadata - table_list = list(map(lambda t: t.to_metadata(db.metadata), table_list)) - - # create tables in DB ) - db.metadata.create_all(db.engine) - - # generate declarative table objects by reflecting the DB - db.reflect() - """ \ No newline at end of file diff --git a/the_works/views/genre.py b/the_works/views/genre.py index d813f92..7f6eaec 100644 --- a/the_works/views/genre.py +++ b/the_works/views/genre.py @@ -1,5 +1,5 @@ from flask import Blueprint, render_template, request, redirect, flash, url_for -from sqlalchemy import select, insert, update, delete +from sqlalchemy import select from the_works.database import db from the_works.models import Genre diff --git a/the_works/views/herausgeber.py b/the_works/views/herausgeber.py index 9ac1f22..c4132c0 100644 --- a/the_works/views/herausgeber.py +++ b/the_works/views/herausgeber.py @@ -1,5 +1,5 @@ from flask import Blueprint, render_template, request, redirect, flash, url_for -from sqlalchemy import select, insert, update, delete +from sqlalchemy import select from the_works.database import db from the_works.models import Herausgeber diff --git a/the_works/views/pseudonym.py b/the_works/views/pseudonym.py index 4326373..48e724b 100644 --- a/the_works/views/pseudonym.py +++ b/the_works/views/pseudonym.py @@ -1,5 +1,5 @@ from flask import Blueprint, render_template, request, redirect, flash, url_for -from sqlalchemy import select, insert, update, delete +from sqlalchemy import select from the_works.database import db from the_works.models import Pseudonym diff --git a/the_works/views/reihe.py b/the_works/views/reihe.py index 9de8788..48cebad 100644 --- a/the_works/views/reihe.py +++ b/the_works/views/reihe.py @@ -1,5 +1,5 @@ from flask import Blueprint, render_template, request, redirect, flash, url_for -from sqlalchemy import select, insert, update, delete +from sqlalchemy import select from the_works.database import db from the_works.models import Reihe, Verlag diff --git a/the_works/views/sprache.py b/the_works/views/sprache.py index 0e0a30f..2190460 100644 --- a/the_works/views/sprache.py +++ b/the_works/views/sprache.py @@ -1,5 +1,5 @@ from flask import Blueprint, render_template, request, redirect, flash, url_for -from sqlalchemy import select, insert, update, delete +from sqlalchemy import select from the_works.database import db from the_works.models import Sprache diff --git a/the_works/views/textform.py b/the_works/views/textform.py index 6cbd446..1f9e05e 100644 --- a/the_works/views/textform.py +++ b/the_works/views/textform.py @@ -1,5 +1,5 @@ from flask import Blueprint, render_template, request, redirect, flash, url_for -from sqlalchemy import select, insert, update, delete +from sqlalchemy import select from the_works.database import db from the_works.models import Textform diff --git a/the_works/views/verlag.py b/the_works/views/verlag.py index ae9cb57..f7389fa 100644 --- a/the_works/views/verlag.py +++ b/the_works/views/verlag.py @@ -1,5 +1,5 @@ from flask import Blueprint, render_template, request, redirect, flash, url_for -from sqlalchemy import select, insert, update, delete +from sqlalchemy import select from the_works.database import db from the_works.models import Verlag diff --git a/the_works/views/veroeffentlichung.py b/the_works/views/veroeffentlichung.py index 7ffcc9f..79b561b 100644 --- a/the_works/views/veroeffentlichung.py +++ b/the_works/views/veroeffentlichung.py @@ -1,5 +1,5 @@ from flask import Blueprint, render_template, request, redirect, flash, url_for -from sqlalchemy import select, insert, update, delete +from sqlalchemy import select from the_works.database import db from the_works.models import Veroeffentlichung, Text, Werk, Werksform, Pseudonym diff --git a/the_works/views/werksform.py b/the_works/views/werksform.py index 0a86304..41af10f 100644 --- a/the_works/views/werksform.py +++ b/the_works/views/werksform.py @@ -1,5 +1,5 @@ from flask import Blueprint, render_template, request, redirect, flash, url_for -from sqlalchemy import select, insert, update, delete +from sqlalchemy import select from the_works.database import db from the_works.models import Werksform