From d0f08ed8699a2eda867bdab7c89628132bf90a3e Mon Sep 17 00:00:00 2001 From: Willem Renes Date: Wed, 12 May 2021 00:32:14 +0200 Subject: adding the initial files --- static/hf.css | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ templates/base.html | 12 +++++++++ templates/entry.html | 17 ++++++++++++ templates/results.html | 17 ++++++++++++ templates/viewlog.html | 22 ++++++++++++++++ vsearch4web.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 207 insertions(+) create mode 100644 static/hf.css create mode 100644 templates/base.html create mode 100644 templates/entry.html create mode 100644 templates/results.html create mode 100644 templates/viewlog.html create mode 100644 vsearch4web.py diff --git a/static/hf.css b/static/hf.css new file mode 100644 index 0000000..b02d621 --- /dev/null +++ b/static/hf.css @@ -0,0 +1,71 @@ +body { + font-family: Verdana, Geneva, Arial, sans-serif; + font-size: medium; + background-color: tan; + margin-top: 5%; + margin-bottom: 5%; + margin-left: 10%; + margin-right: 10%; + padding: 10px 10px 10px 10px; +} +a { + text-decoration: none; + font-weight: 600; +} +a:hover { + text-decoration: underline; +} +a img { + border: 0; +} +h2 { + font-size: 150%; +} +table { + margin-left: 20px; + margin-right: 20px; + caption-side: bottom; + border-collapse: collapse; +} +td, th { + padding: 5px; + text-align: left; +} +.copyright { + font-size: 75%; + font-style: italic; +} +.slogan { + font-size: 75%; + font-style: italic; +} +.confirmentry { + font-weight: 600; +} + +/*** Tables ***/ + +table { +font-size: 1em; +background-color: #fafcff; +border: 1px solid #909090; +color: #2a2a2a; +padding: 5px 5px 2px; +border-collapse: collapse; +} + +td, th { +border: thin dotted gray; +} + +/*** Inputs ***/ +input[type=text] { + font-size: 115%; + width: 30em; +} +input[type=submit] { + font-size: 125%; +} +select { + font-size: 125%; +} diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..98eef27 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,12 @@ + + + + {{ the_title }} + + + + {% block body %} + + {% endblock %} + + diff --git a/templates/entry.html b/templates/entry.html new file mode 100644 index 0000000..c54a766 --- /dev/null +++ b/templates/entry.html @@ -0,0 +1,17 @@ +{% extends 'base.html' %} + +{% block body %} + +

{{ the_title }}

+ +
+ +

Use this form to submit a search request:

+ + +
Phrase:
Letters:
+

When you're ready, click this button:

+

+
+ +{% endblock %} diff --git a/templates/results.html b/templates/results.html new file mode 100644 index 0000000..462bfb0 --- /dev/null +++ b/templates/results.html @@ -0,0 +1,17 @@ +{% extends 'base.html' %} + +{% block body %} + +

{{ the_title }}

+ +

You submitted the following data:

+ + + +
Phrase:{{ the_phrase }}
Letters:{{ the_letters }}
+ +

When "{{the_phrase }}" is searched for "{{ the_letters }}", the following +results are returned:

+

{{ the_results }}

+ +{% endblock %} diff --git a/templates/viewlog.html b/templates/viewlog.html new file mode 100644 index 0000000..8a47ccc --- /dev/null +++ b/templates/viewlog.html @@ -0,0 +1,22 @@ +{% extends 'base.html' %} + +{% block body %} + +

{{ the_title }}

+ + + + {% for row_title in the_row_titles %} + + {% endfor %} + + {% for log_row in the_data %} + + {% for item in log_row %} + + {% endfor %} + + {% endfor %} +
{{row_title}}
{{item}}
+ +{% endblock %} diff --git a/vsearch4web.py b/vsearch4web.py new file mode 100644 index 0000000..3eed663 --- /dev/null +++ b/vsearch4web.py @@ -0,0 +1,68 @@ +from flask import Flask, render_template, request, escape +from vsearch import search4letters + +from DBcm import UseDatabase + +app = Flask(__name__) + +app.config['dbconfig'] = {'host': '127.0.0.1', + 'user': 'vsearch', + 'password': 'vsearchpasswd', + 'database': 'vsearchlogDB', } + + +def log_request(req: 'flask_request', res: str) -> None: + """Log details of the web request and the results.""" + + with UseDatabase(app.config['dbconfig']) as cursor: + _SQL = """insert into log + (phrase, letters, ip, browser_string, results) + values + (%s, %s, %s, %s, %s)""" + cursor.execute(_SQL, (req.form['phrase'], + req.form['letters'], + req.remote_addr, + req.user_agent.browser, + res, )) + + +@app.route('/search4', methods=['POST']) +def do_search() -> 'html': + """Extract the posted data; perform the search; return results.""" + phrase = request.form['phrase'] + letters = request.form['letters'] + title = 'Here are your results:' + results = str(search4letters(phrase, letters)) + log_request(request, results) + return render_template('results.html', + the_title=title, + the_phrase=phrase, + the_letters=letters, + the_results=results,) + + +@app.route('/') +@app.route('/entry') +def entry_page() -> 'html': + """Display this webapp's HTML form.""" + return render_template('entry.html', + the_title='Welcome to search4letters on the web!') + + +@app.route('/viewlog') +def view_the_log() -> 'html': + """Display the contents of the log file as a HTML table.""" + with UseDatabase(app.config['dbconfig']) as cursor: + _SQL = """select phrase, letters, ip, browser_string, results + from log""" + cursor.execute(_SQL) + contents = cursor.fetchall() + titles = ('Phrase', 'Letters', 'Remote_addr', 'User_agent', 'Results') + return render_template('viewlog.html', + the_title='View Log', + the_row_titles=titles, + the_data=contents,) + + +if __name__ == '__main__': + app.run(debug=True) -- cgit v1.2.3-70-g09d2