2023-06-12 09:11:18 +02:00
#!/usr/bin/env python3
# You can test it with:
2023-06-12 21:45:53 +02:00
# curl --request POST --data string=foobar\&what=language http://localhost:4000
2023-06-12 09:11:18 +02:00
import urllib . parse
2023-06-12 21:45:53 +02:00
# PostgreSQL interface
2023-06-12 09:11:18 +02:00
import psycopg2
2023-06-12 21:45:53 +02:00
# HTML templates http://www.yattag.org/
from yattag import Doc
2023-08-03 16:40:18 +02:00
# TODO include the standard elements such as CSS and footer
2023-06-12 21:45:53 +02:00
2023-08-02 14:47:24 +02:00
REDIRECTION_URL = " https://www.langtag.net/registries/registry-html " # For
# local testing: Firefox apparently does not accept a redirect to a
# file.
2023-06-12 09:11:18 +02:00
2023-08-02 14:47:24 +02:00
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 " ) ,
2023-06-12 09:11:18 +02:00
( " Content-Length " , str ( len ( datae ) ) ) ]
2023-08-01 20:58:15 +02:00
if headers != [ ] :
for header in headers :
response_headers . append ( header )
2023-06-12 09:11:18 +02:00
start_response ( status , response_headers )
return [ datae ]
def application ( environ , start_response ) :
2023-06-12 18:34:27 +02:00
if environ [ " REQUEST_METHOD " ] != " POST " :
status = " 405 Method unavailable "
2023-08-02 14:47:24 +02:00
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 )
2023-06-12 09:11:18 +02:00
try :
body_size = int ( environ . get ( " CONTENT_LENGTH " , 0 ) )
except ( ValueError ) :
body_size = 0
body = environ [ " wsgi.input " ] . read ( body_size )
query = urllib . parse . parse_qsl ( body . decode ( ) )
2023-06-12 18:34:27 +02:00
form = { }
2023-08-01 20:58:15 +02:00
headers = [ ]
2023-06-12 18:34:27 +02:00
for q in query :
form [ q [ 0 ] ] = q [ 1 ]
for mandatory in [ " string " , " what " ] :
if mandatory not in form :
status = " 400 Invalid request "
2023-08-02 14:47:24 +02:00
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 )
2023-06-12 18:34:27 +02:00
if form [ " what " ] == " language " :
2023-08-02 14:47:24 +02:00
sql = " SELECT code,suppressscript,preferredvalue,added,comments FROM Languages WHERE code = %s "
2023-06-12 21:45:53 +02:00
term = form [ " string " ] . lower ( )
2023-06-12 18:34:27 +02:00
elif form [ " what " ] == " script " :
2023-08-02 14:47:24 +02:00
sql = " SELECT code,added,comments FROM Scripts WHERE code = %s "
2023-06-12 21:45:53 +02:00
term = form [ " string " ] . capitalize ( )
2023-08-02 17:52:44 +02:00
elif form [ " what " ] == " region " :
sql = " SELECT code,added,comments FROM Regions WHERE code = %s "
term = form [ " string " ] . upper ( )
2023-10-01 11:40:28 +02:00
elif form [ " what " ] == " variant " :
sql = " SELECT code,added,comments FROM Variants WHERE code = %s "
term = form [ " string " ] . lower ( )
2023-06-12 18:34:27 +02:00
else :
status = " 400 Invalid request "
2023-08-02 14:47:24 +02:00
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 )
2023-06-12 21:45:53 +02:00
cursor . execute ( sql , ( term , ) )
2023-06-12 18:34:27 +02:00
mytuple = cursor . fetchone ( )
status = " 200 OK "
2023-08-02 14:47:24 +02:00
doc , tag , text = Doc ( ) . tagtext ( )
title = " Result for %s " % term
2023-08-01 20:58:15 +02:00
if mytuple is not None :
2023-08-02 18:43:06 +02:00
status = " 307 Redirect "
2023-08-02 14:47:24 +02:00
url = " %s / %s / %s .html " % ( REDIRECTION_URL , form [ " what " ] , mytuple [ 0 ] )
2023-08-01 20:58:15 +02:00
headers . append ( ( " Location " , url ) )
2023-08-02 14:47:24 +02:00
msg = " Redirect to %s \r \n " % ( url )
with tag ( " p " ) :
text ( " Redirect to " )
with tag ( " a " , href = url ) :
text ( url )
text ( " \r \n " )
2023-06-12 21:45:53 +02:00
else :
if form [ " what " ] == " language " :
2023-08-02 14:47:24 +02:00
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; "
2023-06-12 21:45:53 +02:00
elif form [ " what " ] == " script " :
2023-08-02 14:47:24 +02:00
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; "
2023-08-02 17:52:44 +02:00
elif form [ " what " ] == " region " :
sql = " SELECT Regions.code, Descriptions.description FROM Regions,Descriptions_Regions,Descriptions WHERE Descriptions_Regions.description = Descriptions.id AND Descriptions_Regions.region = Regions.code AND position( %s in lower(Descriptions.description)) > 0; "
2023-10-01 11:40:28 +02:00
elif form [ " what " ] == " variant " :
sql = " SELECT Variants.code, Descriptions.description FROM Variants,Descriptions_Variants,Descriptions WHERE Descriptions_Variants.description = Descriptions.id AND Descriptions_Variants.variant = Variants.code AND position( %s in lower(Descriptions.description)) > 0; "
2023-08-02 14:47:24 +02:00
cursor . execute ( sql , ( form [ " string " ] , ) )
2023-06-12 21:45:53 +02:00
found = False
2023-08-02 14:47:24 +02:00
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 )
2023-06-12 09:11:18 +02:00
2023-08-02 18:27:04 +02:00
# Initialize
conn = psycopg2 . connect ( " dbname=lsr " )
cursor = conn . cursor ( )