From e5026f8c7a42ab0491b13725a791c6315f3856ca Mon Sep 17 00:00:00 2001 From: Stephane Bortzmeyer Date: Wed, 2 Aug 2023 14:47:24 +0200 Subject: [PATCH] Search engine almost done --- search.py | 85 ++++++++++++++++++++++++++++++++++++++---------------- search.xml | 4 +-- 2 files changed, 62 insertions(+), 27 deletions(-) diff --git a/search.py b/search.py index 31e4d24..b5f432d 100755 --- a/search.py +++ b/search.py @@ -13,12 +13,23 @@ import psycopg2 from yattag import Doc PORT = 4000 -#REDIRECTION_URL = "file:///home/stephane/src/Language-tags/GaBuZoMeu/registry-html" # Firefox does not accept a redirect to a file -REDIRECTION_URL = "https://www.langtag.net/registries/registry-html" +REDIRECTION_URL = "https://www.langtag.net/registries/registry-html" # For +# local testing: Firefox apparently does not accept a redirect to a +# file. -def pack(start_response, status, data, headers = []): - datae = data.encode() # Always encode to UTF-8 - response_headers = [("Content-type", "text/plain; charset=UTF-8"), # TODO allows the type HTML +def pack(start_response, status, title, body, headers = []): + doc, tag, text = Doc().tagtext() + doc.asis("\n") + doc.asis("") + with tag("html", ("xml:lang", "en"), lang = "en", xmlns = "http://www.w3.org/1999/xhtml"): + with tag("head"): + with tag("title"): + text(title) + with tag("body"): + doc.asis(body.getvalue()) + data = doc.getvalue() + datae = data.encode() + response_headers = [("Content-type", "text/html; charset=UTF-8"), ("Content-Length", str(len(datae)))] if headers != []: for header in headers: @@ -29,8 +40,11 @@ def pack(start_response, status, data, headers = []): def application(environ, start_response): if environ["REQUEST_METHOD"] != "POST": status = "405 Method unavailable" - output = "Unsupported HTTP method \"%s\"\r\n" % environ["REQUEST_METHOD"] - return pack(start_response, status, output) + msg = "Unsupported HTTP method \"%s\"\r\n" % environ["REQUEST_METHOD"] + doc, tag, text = Doc().tagtext() + with tag("p"): + text(msg) + return pack(start_response, status, msg, doc) try: body_size = int(environ.get("CONTENT_LENGTH", 0)) except (ValueError): @@ -44,40 +58,61 @@ def application(environ, start_response): for mandatory in ["string", "what"]: if mandatory not in form: status = "400 Invalid request" - output = "Missing \"%s\" in request\r\n" % mandatory - return pack(start_response, status, output) + msg = "Missing \"%s\" in request\r\n" % mandatory + doc, tag, text = Doc().tagtext() + with tag("p"): + text(msg) + return pack(start_response, status, msg, doc) if form["what"] == "language": - sql = "SELECT * FROM Languages WHERE code = %s" + sql = "SELECT code,suppressscript,preferredvalue,added,comments FROM Languages WHERE code = %s" term = form["string"].lower() elif form["what"] == "script": - sql = "SELECT * FROM Scripts WHERE code = %s" + sql = "SELECT code,added,comments FROM Scripts WHERE code = %s" term = form["string"].capitalize() else: status = "400 Invalid request" - output = "Unknown search category \"%s\"\r\n" % form["what"] - return pack(start_response, status, output) + msg = "Unknown search category \"%s\"\r\n" % form["what"] + doc, tag, text = Doc().tagtext() + with tag("p"): + text(msg) + return pack(start_response, status, msg, doc) cursor.execute(sql, (term, )) mytuple = cursor.fetchone() status = "200 OK" + doc, tag, text = Doc().tagtext() + title = "Result for %s" % term if mytuple is not None: status = "308 Redirect" - url = "%s/%s/%s.html" % (REDIRECTION_URL, form["what"], mytuple[1]) + url = "%s/%s/%s.html" % (REDIRECTION_URL, form["what"], mytuple[0]) headers.append(("Location", url)) - output = "Redirect to %s\r\n" % (url) + msg = "Redirect to %s\r\n" % (url) + with tag("p"): + text("Redirect to ") + with tag("a", href = url): + text(url) + text("\r\n") else: if form["what"] == "language": - sql = "SELECT * FROM Languages,Descriptions_Languages,Descriptions WHERE Descriptions_Languages.description = Descriptions.id AND Descriptions_Languages.lang = Languages.code AND position(%s in lower(Descriptions.description)) > 0;" + sql = "SELECT Languages.code, Descriptions.description FROM Languages,Descriptions_Languages,Descriptions WHERE Descriptions_Languages.description = Descriptions.id AND Descriptions_Languages.lang = Languages.code AND position(%s in lower(Descriptions.description)) > 0;" elif form["what"] == "script": - raise "TODO not implemented" - cursor.execute(sql, (term, )) + sql = "SELECT Scripts.code, Descriptions.description FROM Scripts,Descriptions_Scripts,Descriptions WHERE Descriptions_Scripts.description = Descriptions.id AND Descriptions_Scripts.script = Scripts.code AND position(%s in lower(Descriptions.description)) > 0;" + cursor.execute(sql, (form["string"], )) found = False - output = "" - for tuple in cursor.fetchall(): - output += "TODO %s\r\n" % str(tuple) # TODO make it a HTML list - found = True - if not found: - output = "\"%s\" not found\r\n" % term - return pack(start_response, status, output, headers) + with tag("h1"): + text("Results for %s" % form["string"]) + with tag("p"): + with tag("ul"): + for tuple in cursor.fetchall(): + code = tuple[0] + description = tuple[1] + with tag("li"): + with tag("a", href = "%s/%s/%s.html" % (REDIRECTION_URL, form["what"], code)): + text("%s: %s\r\n" % (code, description)) + found = True + if not found: + with tag("li"): + text("\"%s\" not found\r\n" % term) + return pack(start_response, status, title, doc, headers) if __name__ == "__main__": conn = psycopg2.connect("dbname=lsr") diff --git a/search.xml b/search.xml index 2275e5b..f1b1f93 100644 --- a/search.xml +++ b/search.xml @@ -1,6 +1,6 @@ - -

TODO

+ +

Type a code or a description (for instance, "fr", or "german"):

Name:

Search in: