This is a reusable plain version the varia library website. You can host your own website of books using just a simple csv file
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.7 KiB

import csv
import os
import tomllib
import flask_apscheduler
11 months ago
from application.csvparser import CsvParser
from flask import Flask
from flask_bcrypt import Bcrypt
11 months ago
from flask_login import LoginManager
from flask_migrate import Migrate
11 months ago
from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect
from whoosh.fields import *
from whoosh.index import create_in
from whoosh.qparser import QueryParser
db = SQLAlchemy()
migrate = Migrate()
bcrypt = Bcrypt()
login_manager = LoginManager()
SCRIPT_DIR = os.path.dirname(__file__)
DATA_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "data"))
11 months ago
def create_app():
settings = settings_from_file()
APP = Flask(__name__, static_folder="static")
APP.config["SECRET_KEY"] = os.urandom(24)
APP.config["UPLOAD_FOLDER"] = "tmpupload"
APP.config["IMAGE_FOLDER"] = "static/images"
APP.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///libraryusers.db"
APP.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
APP.config["LIBRARY_FILENAME"] = settings["libaryfilename"]
APP.config["TITLE"] = settings["title"]
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)
scheduler = flask_apscheduler.APScheduler()
scheduler.api_enabled = False
scheduler.init_app(APP)
scheduler.start()
index_books(APP.config["LIBRARY_FILENAME"], APP.config["IMAGE_FOLDER"])
@scheduler.task("interval", id="update", minutes=10)
def update():
index_books(APP.config["LIBRARY_FILENAME"], APP.config["IMAGE_FOLDER"])
@APP.context_processor
def inject_title():
return dict(title=APP.config["TITLE"])
return APP
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)
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:
rowcontent = csvparser.concatenate_csv_row(row)
writer.add_document(title=row["Id"], path="/a", content=rowcontent)
writer.commit()
def settings_from_file():
settings = {}
7 months ago
if os.path.isfile("settings_development.toml"):
with open("settings_development.toml", "rb") as settings_file:
return tomllib.load(settings_file)
with open("settings.toml", "rb") as settings_file:
7 months ago
return tomllib.load(settings_file)