INPRO: Bugfixing and better returns

This commit is contained in:
2023-06-04 15:30:59 -07:00
parent a88774fc5b
commit 6c5717c4d1
5 changed files with 98 additions and 39 deletions
+18 -5
View File
@@ -1,5 +1,5 @@
# app.py
from flask import Flask, request, jsonify, send_file, render_template, redirect
from flask import Flask, request, jsonify, send_file, render_template, redirect, Response
import random
from datetime import date, datetime, timedelta # used for daily verse
import sqlite3
@@ -212,6 +212,8 @@ def parse_query(query_override:str = None):
query = query_override
if query:
query = query.strip()
if request.args.get("view", "rich") != "rich":
return jsonify({"Error":"/find endpoint only supports rich responses"})
logging.info(f"Query: {query}")
if not query:
return send_file("html/find.html")
@@ -434,7 +436,7 @@ def page_not_found(e):
@app.get("/<shortcode>")
def idx(shortcode=''):
if not sanity:
return("There is an issue with permissions. Check log, or please notify tyler@dinsmoor.us")
return("Sanity check failed. Check log, or please notify tyler@dinsmoor.us")
if shortcode:
return resolve_shortcode(shortcode)
logging.info("Index Served")
@@ -477,6 +479,17 @@ def verse_of_the_day():
response = jsonify(response_list)
logging.debug(f"Search Response: {response}")
return response
def make_view(response_list=[]):
#TODO: Maybe use flask session to make this easier to deal with
# https://stackoverflow.com/a/53152394
arg_view = request.args.get("view", None)
if arg_view == "plain":
response = "<br>".join(response_list)
elif arg_view == "rich":
response = "<br>".join(response_list)
else:
response = jsonify(response_list)
@app.get("/search")
def search():
keywords = request.args.get("kw", None)
@@ -504,9 +517,9 @@ def search():
response = jsonify(response_list)
logging.debug(f"Search Response: {response}")
return response
@app.get("/reader")
@app.get("/browse")
def bible_reader():
return send_file("html/reader.html")
return render_template("reader.html", books=cached.get("book_names"))
# We should return an index of all books with
# drop down menus of each chapter, and a search
# bar at the top for quick seeking, which can use
@@ -667,7 +680,7 @@ def resolve_shortcode(shortcode: str = ""):
logging.info(f"Resolved Shortcode {shortcode} for query '{query}'")
return redirect(f"/find?kw={query}&view=rich")
if __name__ == "__main__":
print("You must run this program within a uwsgi environment if you are running this for multiple users.")
print("Otherwise, you may end up with database corruption if multiple writes happen.")