2023-07-14 12:54:43 +02:00
|
|
|
import csv
|
|
|
|
import os
|
2023-11-26 17:52:21 +01:00
|
|
|
import tomllib
|
2023-07-14 12:54:43 +02:00
|
|
|
|
|
|
|
import flask_apscheduler
|
2023-12-03 14:19:53 +01:00
|
|
|
from application.csvparser import CsvParser
|
2023-07-14 12:54:43 +02:00
|
|
|
from flask import Flask
|
2023-12-03 14:16:58 +01:00
|
|
|
from flask_bcrypt import Bcrypt
|
2023-12-03 14:19:53 +01:00
|
|
|
from flask_login import LoginManager
|
2023-12-03 14:16:58 +01:00
|
|
|
from flask_migrate import Migrate
|
2023-12-03 14:19:53 +01:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
2023-12-03 14:16:58 +01:00
|
|
|
from flask_wtf.csrf import CSRFProtect
|
2023-07-14 12:54:43 +02:00
|
|
|
from whoosh.fields import *
|
|
|
|
from whoosh.index import create_in
|
|
|
|
from whoosh.qparser import QueryParser
|
|
|
|
|
2023-12-03 14:16:58 +01:00
|
|
|
db = SQLAlchemy()
|
|
|
|
migrate = Migrate()
|
|
|
|
bcrypt = Bcrypt()
|
|
|
|
login_manager = LoginManager()
|
|
|
|
|
2023-07-14 12:54:43 +02:00
|
|
|
SCRIPT_DIR = os.path.dirname(__file__)
|
|
|
|
DATA_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "data"))
|
|
|
|
|
2023-12-03 14:19:53 +01:00
|
|
|
|
2023-07-14 12:54:43 +02:00
|
|
|
def create_app():
|
2023-11-26 17:52:21 +01:00
|
|
|
settings = settings_from_file()
|
|
|
|
|
2023-07-14 12:54:43 +02:00
|
|
|
APP = Flask(__name__, static_folder="static")
|
2023-12-03 14:16:58 +01:00
|
|
|
APP.config["SECRET_KEY"] = os.urandom(24)
|
2023-07-14 12:54:43 +02:00
|
|
|
APP.config["UPLOAD_FOLDER"] = "tmpupload"
|
2023-11-26 17:52:21 +01:00
|
|
|
APP.config["IMAGE_FOLDER"] = "static/images"
|
2023-12-03 14:16:58 +01:00
|
|
|
|
|
|
|
APP.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///libraryusers.db"
|
|
|
|
APP.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
|
|
|
|
|
|
|
|
APP.config["LIBRARY_FILENAME"] = settings["libaryfilename"]
|
2023-11-26 18:15:22 +01:00
|
|
|
APP.config["TITLE"] = settings["title"]
|
2023-12-03 14:16:58 +01:00
|
|
|
|
|
|
|
csrf = CSRFProtect(APP)
|
|
|
|
csrf.init_app(APP)
|
|
|
|
login_manager.init_app(APP)
|
|
|
|
db.init_app(APP)
|
|
|
|
migrate.init_app(APP, db, render_as_batch=True)
|
|
|
|
bcrypt.init_app(APP)
|
|
|
|
|
2023-07-14 12:54:43 +02:00
|
|
|
scheduler = flask_apscheduler.APScheduler()
|
|
|
|
scheduler.api_enabled = False
|
|
|
|
scheduler.init_app(APP)
|
|
|
|
scheduler.start()
|
2023-11-26 17:52:21 +01:00
|
|
|
index_books(APP.config["LIBRARY_FILENAME"], APP.config["IMAGE_FOLDER"])
|
2023-07-14 12:54:43 +02:00
|
|
|
|
|
|
|
@scheduler.task("interval", id="update", minutes=10)
|
|
|
|
def update():
|
2023-11-26 17:52:21 +01:00
|
|
|
index_books(APP.config["LIBRARY_FILENAME"], APP.config["IMAGE_FOLDER"])
|
2023-07-14 12:54:43 +02:00
|
|
|
|
|
|
|
return APP
|
|
|
|
|
|
|
|
|
2023-11-26 17:52:21 +01:00
|
|
|
def get_app():
|
|
|
|
APP = Flask(__name__, static_folder="static")
|
|
|
|
return APP
|
|
|
|
|
|
|
|
|
|
|
|
def index_books(filename: str, image_folder: str):
|
|
|
|
csvparser = CsvParser(filename, image_folder)
|
|
|
|
filename = os.path.join(DATA_DIR, csvparser.csv_file)
|
2023-07-14 12:54:43 +02:00
|
|
|
schema = Schema(
|
|
|
|
title=TEXT(stored=True), path=ID(stored=True), content=TEXT
|
|
|
|
)
|
|
|
|
ix = create_in(DATA_DIR, schema)
|
|
|
|
writer = ix.writer()
|
|
|
|
with open(filename, "r", encoding="utf_8_sig") as libcsv:
|
|
|
|
csv_as_dict = csv.DictReader(libcsv)
|
|
|
|
for row in csv_as_dict:
|
2023-11-26 17:52:21 +01:00
|
|
|
rowcontent = csvparser.concatenate_csv_row(row)
|
2023-07-14 12:54:43 +02:00
|
|
|
writer.add_document(title=row["Id"], path="/a", content=rowcontent)
|
|
|
|
writer.commit()
|
2023-11-26 17:52:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def settings_from_file():
|
|
|
|
settings = {}
|
2024-03-30 12:00:47 +01:00
|
|
|
if os.path.isfile("settings_development.toml"):
|
|
|
|
with open("settings_development.toml", "rb") as settings_file:
|
|
|
|
return tomllib.load(settings_file)
|
2023-11-26 17:52:21 +01:00
|
|
|
with open("settings.toml", "rb") as settings_file:
|
2024-03-30 12:00:47 +01:00
|
|
|
return tomllib.load(settings_file)
|