Smarter research

This commit is contained in:
Stephane Bortzmeyer 2023-06-12 21:45:53 +02:00
parent f7393da541
commit c5ad7686e0
2 changed files with 27 additions and 10 deletions

View File

@ -1,13 +1,17 @@
#!/usr/bin/env python3
# You can test it with:
# curl --request POST --data name=foobar http://localhost:4000
# curl --request POST --data string=foobar\&what=language http://localhost:4000
import wsgiref.simple_server as server
import urllib.parse
# PostgreSQL interface
import psycopg2
# HTML templates http://www.yattag.org/
from yattag import Doc
PORT = 4000
def pack(start_response, status, data):
@ -36,23 +40,36 @@ def application(environ, start_response):
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"
term = form["string"].lower()
elif form["what"] == "script":
sql = "SELECT * FROM Scripts WHERE code = %s"
sql = "SELECT * 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)
# TODO junctions
# TODO search in descriptions
cursor.execute(sql, (form["string"], ))
cursor.execute(sql, (term, ))
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 ?
if mytuple is not None:
output = "TODO %s\r\n" % str(mytuple) # TODO display comments, preferredscript, addition date etc Or a redirect to mulhtml's result?
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;"
elif form["what"] == "script":
raise "TODO not implemented"
cursor.execute(sql, (term, ))
found = False
output = ""
for tuple in cursor.fetchall():
output += "TODO %s\r\n" % str(tuple)
found = True
if not found:
output = "\"%s\" not found\r\n" % term
# TODO HTML with a templating engine? Yattag, probably
# TODO link to mulhtml results?
return pack(start_response, status, output)
if __name__ == "__main__":

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<page title="Search TODO">
<p>TODO</p>
<form action="http://localhost:4000/" method="POST" name="Search">
<form action="http://localhost:4000/" method="post">
<p>Name: <input type="text" name="string"/></p>
<p>Search in: <select name="what">
<option value="language">Languages</option>