aboutsummaryrefslogtreecommitdiffstats
path: root/fatcat_scholar/web.py
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@archive.org>2020-05-21 14:23:31 -0700
committerBryan Newbold <bnewbold@archive.org>2020-05-21 14:23:41 -0700
commit01ae25c1bf24c8d9f7721f49122a15bf522bdbb4 (patch)
tree36add4d6c2c7050f646a14c2a0ffc5f03436ad03 /fatcat_scholar/web.py
parent6c4f539463074bcb563675b6e4f19464339e5641 (diff)
downloadfatcat-scholar-01ae25c1bf24c8d9f7721f49122a15bf522bdbb4.tar.gz
fatcat-scholar-01ae25c1bf24c8d9f7721f49122a15bf522bdbb4.zip
search query improvements
- wire up most of the filters and sort order - query sticks around in search box - crude error message (needs work)
Diffstat (limited to 'fatcat_scholar/web.py')
-rw-r--r--fatcat_scholar/web.py46
1 files changed, 34 insertions, 12 deletions
diff --git a/fatcat_scholar/web.py b/fatcat_scholar/web.py
index a148c8b..06d6a02 100644
--- a/fatcat_scholar/web.py
+++ b/fatcat_scholar/web.py
@@ -4,25 +4,23 @@ This contains the FastAPI web application and RESTful API.
So far there are few endpoints, so we just put them all here!
"""
+import sys
from enum import Enum
-
import babel.support
from fastapi import FastAPI, APIRouter, Request, Depends, Header
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse
-from pydantic import BaseModel
from dynaconf import settings
+from typing import List, Dict, Tuple, Optional, Any, Sequence
from fatcat_scholar.hacks import Jinja2Templates
-from fatcat_scholar.search import do_fulltext_search
+from fatcat_scholar.search import do_fulltext_search, FulltextQuery, FulltextHits
-print(settings.as_dict())
+print(f"dynaconf settings: {settings.as_dict()}", file=sys.stderr)
I18N_LANG_TRANSLATIONS = ["de", "zh"]
I18N_LANG_OPTIONS = I18N_LANG_TRANSLATIONS + [settings.I18N_LANG_DEFAULT,]
-class SearchParams(BaseModel):
- q: str = ""
class LangPrefix:
"""
@@ -61,7 +59,7 @@ async def home():
return {"endpoints": {"/": "this", "/search": "fulltext search"}}
@api.get("/search", operation_id="get_search")
-async def search(query: SearchParams = Depends(SearchParams)):
+async def search(query: FulltextQuery = Depends(FulltextQuery)):
return {"message": "search results would go here, I guess"}
web = APIRouter()
@@ -115,24 +113,48 @@ def load_i18n_templates():
i18n_templates = load_i18n_templates()
+
@web.get("/", include_in_schema=False)
async def web_home(request: Request, lang: LangPrefix = Depends(LangPrefix), content: ContentNegotiation = Depends(ContentNegotiation)):
if content.mimetype == "application/json":
return await home()
return i18n_templates[lang.code].TemplateResponse("home.html", {"request": request, "locale": lang.code, "lang_prefix": lang.prefix})
+
@web.get("/about", include_in_schema=False)
async def web_about(request: Request, lang: LangPrefix = Depends(LangPrefix)):
return i18n_templates[lang.code].TemplateResponse("about.html", {"request": request, "locale": lang.code, "lang_prefix": lang.prefix})
+
@web.get("/search", include_in_schema=False)
-async def web_search(request: Request, query: SearchParams = Depends(SearchParams), lang: LangPrefix = Depends(LangPrefix), content: ContentNegotiation = Depends(ContentNegotiation)):
+async def web_search(request: Request, query: FulltextQuery = Depends(FulltextQuery), lang: LangPrefix = Depends(LangPrefix), content: ContentNegotiation = Depends(ContentNegotiation)):
+
if content.mimetype == "application/json":
return await search(query)
- found = None
- if query.q:
- found = do_fulltext_search(query.q)
- return i18n_templates[lang.code].TemplateResponse("search.html", {"request": request, "locale": lang.code, "lang_prefix": lang.prefix, "found": found})
+ hits : Optional[FulltextHits] = None
+ search_error: Optional[str] = None
+ status_code: int = 200
+ if query.q is not None:
+ try:
+ hits = do_fulltext_search(query)
+ except ValueError as e:
+ search_error = str(e)
+ status_code = 400
+ except IOError as e:
+ search_error = str(e)
+ status_code = 500
+ return i18n_templates[lang.code].TemplateResponse(
+ "search.html",
+ {
+ "request": request,
+ "locale": lang.code,
+ "lang_prefix": lang.prefix,
+ "hits": hits,
+ "search_error": search_error,
+ "query": query,
+ },
+ status_code=status_code,
+ )
app = FastAPI(