From 5b4a2a555909244c0f2992d3a8d484da5e6692e5 Mon Sep 17 00:00:00 2001
From: felixwelter <felixwelter@gmail.com>
Date: Mon, 31 Aug 2020 13:32:45 +0200
Subject: [PATCH] Switch to new request format

---
 app.py               | 34 +++++++++++++++++++++++++---------
 templates/index.html | 15 +++++++++++++++
 2 files changed, 40 insertions(+), 9 deletions(-)
 create mode 100644 templates/index.html

diff --git a/app.py b/app.py
index dbbab1e..43580d7 100644
--- a/app.py
+++ b/app.py
@@ -1,45 +1,61 @@
-from flask import Flask, render_template, request, redirect, send_file
+from flask import Flask, render_template, request, redirect, send_file, jsonify
 from elasticsearch_dsl import connections, Search
-import os 
+import os
 
 app = Flask(__name__)
 
 connections.create_connection(hosts=[os.getenv('ES_HOST', 'localhost')], timeout=20)
 
 
-@app.route('/search')
+@app.route('/test')
+def test_page():
+    return render_template('index.html')
+
+
+def make_text_response(text):
+    print(text[:100])
+    return jsonify({
+        "type": "text",
+        "text": text
+    })
+
+
+@app.route('/search', methods=['POST'])
 def index():
-    term = request.args.get('term')
+    term = request.form.get('term')
+    print(term)
 
     s = Search(index="en_wiki") \
         .query("match", title=term) \
         .exclude("match", description="beta")
     response = s.execute()
     if len(response) > 0:
-        return response[0].text
+        return make_text_response(response[0].text)
 
     s = Search(index="en_wiki") \
         .query("match_phrase", title=term) \
         .exclude("match", description="beta")
     response = s.execute()
     if len(response) > 0:
-        return response[0].text
+        return make_text_response(response[0].text)
 
     s = Search(index="en_wiki") \
         .query("match", text=term) \
         .exclude("match", description="beta")
     response = s.execute()
     if len(response) > 0:
-        return response[0].text
+        return make_text_response(response[0].text)
 
     s = Search(index="en_wiki") \
         .query("match_phrase", text=term) \
         .exclude("match", description="beta")
     response = s.execute()
     if len(response) > 0:
-        return response[0].text
+        return make_text_response(response[0].text)
 
-    return "No hits"
+    return jsonify({
+        "type": "miss"
+    })
 
 
 if __name__ == '__main__':
diff --git a/templates/index.html b/templates/index.html
new file mode 100644
index 0000000..b232350
--- /dev/null
+++ b/templates/index.html
@@ -0,0 +1,15 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+    <title>Slide index</title></head>
+<body>
+
+<h1>Query</h1>
+<form action="search" method="post">
+    <input name="term" placeholder="term"><br>
+    <input name="context" placeholder="context"><br>
+    <input type="submit" value="Query">
+</form>
+
+</body>
+</html>
-- 
GitLab