Browse Source

index books onn applications startup and once every 10 minutes

master
crunk 10 months ago
parent
commit
ff7189af66
  1. 45
      library/app.py
  2. 16
      library/csvparser/csvparser.py
  3. 46
      library/page.py

45
library/app.py

@ -0,0 +1,45 @@
import csv
import os
import flask_apscheduler
from flask import Flask
from whoosh.fields import *
from whoosh.index import create_in
from whoosh.qparser import QueryParser
from csvparser.csvparser import concatenate_csv_row, getfullpublication
SCRIPT_DIR = os.path.dirname(__file__)
DATA_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "data"))
def create_app():
APP = Flask(__name__, static_folder="static")
APP.config["SECRET_KEY"] = "ty4425hk54a21eee5719b9s9df7sdfklx"
APP.config["UPLOAD_FOLDER"] = "tmpupload"
scheduler = flask_apscheduler.APScheduler()
scheduler.api_enabled = False
scheduler.init_app(APP)
scheduler.start()
index_books()
@scheduler.task("interval", id="update", minutes=10)
def update():
index_books()
return APP
def index_books():
filename = os.path.join(DATA_DIR, "varlib.csv")
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 = concatenate_csv_row(row)
writer.add_document(title=row["Id"], path="/a", content=rowcontent)
writer.commit()

16
library/csvparser/csvparser.py

@ -1,9 +1,9 @@
"""This parses the varlib.csv but only in a way
that is actually useful for the site"""
from tempfile import NamedTemporaryFile
import shutil
import csv
import os
import shutil
from tempfile import NamedTemporaryFile
script_dir = os.path.dirname(__file__)
data_dir = os.path.abspath(os.path.join(script_dir, "../data"))
@ -222,3 +222,15 @@ def editborrowedby(pubid, borrower):
csv_as_writer.writerow(row)
shutil.move(tempfile.name, filename)
def concatenate_csv_row(row):
rowcontent = []
rowcontent.append(row["Publication"])
rowcontent.append(row["Author"])
rowcontent.append(row["Fields"])
rowcontent.append(row["Type"])
rowcontent.append(row["Publishers"])
rowcontent.append(row["Highlights"])
rowcontent.append(row["Comments"])
return " ".join(rowcontent)

46
library/page.py

@ -1,44 +1,35 @@
"""This is the main flask library page"""
import os
import flask
from requests import get
from icalendar import Calendar
import datetime
import os
import bcrypt
from flask import (
render_template,
redirect,
request,
)
import flask
from flask import redirect, render_template, request
from flask_wtf.csrf import CSRFProtect
from werkzeug.utils import secure_filename
from icalendar import Calendar
from PIL import Image
from requests import get
from werkzeug.utils import secure_filename
from app import create_app
from borrowform import BorrowForm
from csvparser.csvparser import (editborrowedby, getfullpublication,
getlicenses, getpublications, gettypes,
getyears, writepublication)
from rnrfeed.rnrfeeder import getevents, getlatestevent
from uploadform import PublicationForm
from borrowform import BorrowForm
from csvparser.csvparser import (
getlicenses,
getpublications,
gettypes,
getyears,
getfullpublication,
writepublication,
editborrowedby,
)
APP = create_app()
csrf = CSRFProtect()
APP = flask.Flask(__name__, static_folder="static")
APP.config["SECRET_KEY"] = "ty4425hk54a21eee5719b9s9df7sdfklx"
APP.config["UPLOAD_FOLDER"] = "tmpupload"
csrf.init_app(APP)
@APP.route("/")
def index():
"""Main route, shows all the books and you can filter them, a bit"""
"""Main route, shows all the books and you can filter them
based on year, license, type"""
pubtypes = gettypes()
pubyears = getyears()
publicenses = getlicenses()
@ -96,6 +87,11 @@ def show_book(publicationID):
)
@APP.route("/search", methods=["GET"])
def searchbooks():
return
@APP.route("/pastevents")
def pastevents():
"""show past R&R events and book recommendations"""

Loading…
Cancel
Save