-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.py
56 lines (48 loc) · 1.72 KB
/
results.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from jinja2 import Environment, ChoiceLoader, PackageLoader, FileSystemLoader, select_autoescape
import sqlite3
import re
env = Environment(
loader=ChoiceLoader([FileSystemLoader("./templates"), PackageLoader("tna_frontend_jinja"), ]),
autoescape=select_autoescape()
)
def puid_exists(puid):
conn = sqlite3.connect("indexes")
cursor = conn.cursor()
cursor.execute("SELECT path from indexes where path = ?", (puid,))
rows = cursor.fetchall()
return len(rows) > 0
def search(search_string):
conn = sqlite3.connect('indexes')
cur = conn.cursor()
cur.execute('select path, name from indexes where field like ?', (f"%{search_string}%",))
rows = cur.fetchall()
cur.close()
return rows
def lambda_handler(event, _):
query_params = event.get("queryStringParameters", {})
search_term = query_params.get("q") if query_params else None
if re.search(r'^(x-)?fmt\/\d{1,5}$', search_term) is not None and puid_exists(search_term):
return {
"statusCode": 302,
"headers": {'Location': f'{search_term}.html'}
}
rows = search(search_term)
data = {f'{row[0]}': row[1] for row in rows}
index_template = env.get_template("index.html")
search_results = env.get_template("search_results.html")
content = search_results.render(data=data)
body = index_template.render(content=content)
try:
return {
"statusCode": 200,
"body": body,
"headers": {"Content-Type": "text/html"}
}
except Exception as e:
return {
"statusCode": 500,
"body": f"An error occurred: {str(e)}",
"headers": {
"Content-Type": "text/html"
}
}