diff --git a/search.py b/search.py index 5cd9d31..f626d8c 100755 --- a/search.py +++ b/search.py @@ -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__": diff --git a/search.xml b/search.xml index 3a26fca..2275e5b 100644 --- a/search.xml +++ b/search.xml @@ -1,7 +1,7 @@

TODO

-
+

Name:

Search in: