Search engine almost done
This commit is contained in:
parent
ff68c1111f
commit
e5026f8c7a
85
search.py
85
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("<?xml version=\"1.0\" ?>\n")
|
||||
doc.asis("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">")
|
||||
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")
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<page title="Search TODO">
|
||||
<p>TODO</p>
|
||||
<page title="Search the Language Subtag Registry">
|
||||
<p>Type a code or a description (for instance, "<code>fr</code>", or "german"):</p>
|
||||
<form action="http://localhost:4000/" method="post">
|
||||
<p>Name: <input type="text" name="string"/></p>
|
||||
<p>Search in: <select name="what">
|
||||
|
Loading…
Reference in New Issue
Block a user