Search engine a bit better

This commit is contained in:
Stephane Bortzmeyer 2023-06-12 18:34:27 +02:00
parent 4ffae2f437
commit 83519f0d2b
2 changed files with 34 additions and 10 deletions

View File

@ -18,21 +18,41 @@ def pack(start_response, status, data):
return [datae]
def application(environ, start_response):
status = "200 OK"
output = "TODO\r\n"
# REQUEST_METHOD
for variable in environ:
print("%s: %s" % (variable, environ[variable]))
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)
try:
body_size = int(environ.get("CONTENT_LENGTH", 0))
except (ValueError):
body_size = 0
body = environ["wsgi.input"].read(body_size)
print("")
print("Body \"%s\"" % body.decode())
query = urllib.parse.parse_qsl(body.decode())
print("")
print("Query \"%s\"" % query)
form = {}
for q in query:
form[q[0]] = q[1]
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)
# TODO case-insensitive (scripts are capitalized)
if form["what"] == "language":
sql = "SELECT * FROM Languages WHERE code = %s"
elif form["what"] == "script":
sql = "SELECT * FROM Scripts WHERE code = %s"
else:
status = "400 Invalid request"
output = "Unknown search category \"%s\"\r\n" % form["what"]
return pack(start_response, status, output)
# TODO junctions
# TODO search in descriptions
cursor.execute(sql, (form["string"], ))
mytuple = cursor.fetchone()
status = "200 OK"
output = "TODO %s\r\n" % str(mytuple) # TODO display comments, preferredscript, addition date etc
# TODO HTML with a templating engine?
# TODO link to mulhtml ?
return pack(start_response, status, output)
if __name__ == "__main__":

View File

@ -2,7 +2,11 @@
<page title="Search TODO">
<p>TODO</p>
<form action="http://localhost:4000/" method="POST" name="Search">
<p>Name: <input type="text" name="name"/></p>
<p>Name: <input type="text" name="string"/></p>
<p>Search in: <select name="what">
<option value="language">Languages</option>
<option value="script">Scripts</option>
</select></p>
<p><input type="submit" value="Search"/></p>
</form>
</page>