Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions squawker/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from flask import Flask, g
from flask import Flask
from flask import g
from flask import render_template
from flask import request
import sqlite3

import time
import datetime
import random
import webbrowser

# -- leave these lines intact --
app = Flask(__name__)
Expand Down Expand Up @@ -37,11 +43,37 @@ def close_connection(exception):
# ------------------------------


@app.route('/')
@app.route('/', methods=['GET', 'POST'])
def root():
conn = get_db()
# TODO change this
return "Hello World!"
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS allSquawksIEverMade(datestamp TEXT, squawk TEXT)')
s = []
c.execute('SELECT * FROM allSquawksIEverMade')
for row in c.fetchall():
s.append(row[1])
t = []
for i in reversed(s):
t.append(i)
return render_template('Homepage.html', s=t)


@app.route('/send', methods=['GET', 'POST'])
def send():
conn = get_db()
c = conn.cursor()
if request.method == 'POST':
data = str(request.form['squawk'])
if len(data) > 140:
s = []
s.append('Error Code 400: Input correct form details')
return render_template('Homepage.html', s=s)
unix = time.time()
timestamp = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%m-%d %H:%M:%S'))
c.execute("INSERT INTO allSquawksIEverMade (datestamp, squawk) VALUES (?, ?)", (timestamp, data))
conn.commit()
webbrowser.open('http://localhost:5000/')
return 'all Ok'


if __name__ == '__main__':
Expand Down
35 changes: 35 additions & 0 deletions squawker/templates/Homepage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<title>Squawker Flask</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style media="screen" type="text/css">
#squawk-block{
padding: 50px;
}
#form-block{
padding: 50px;
}
</style>
</head>

<body>

<div id="squawk-block">
<h1>Squawks Today</h1>
{% for squawk in s %}
{{ squawk }}<br>
{% endfor %}
</div>

<div id="form-block">
<form method="POST" action="/send">
<div class="form-group">
<input type="text" name="squawk" required maxlength="140" >
</div>
<input class="btn btn-primary" type="submit" value="Post Squawk!">
</form>
</div>

</body>
</html>