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
8 changes: 6 additions & 2 deletions squawker/schema.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
-- TODO change this
DROP TABLE IF EXISTS mytable;
CREATE TABLE mytable (id integer);
DROP TABLE IF EXISTS squawks;
CREATE TABLE squawks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
squawk VARCHAR(140),
time_stamp DATETIME
);
21 changes: 16 additions & 5 deletions squawker/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import Flask, g
from flask import Flask, g, request, render_template, abort
import sqlite3

import datetime

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


@app.route('/')
@app.route('/', methods=['POST', 'GET'])
def root():
conn = get_db()
cursor = conn.cursor()
# TODO change this
return "Hello World!"

if request.method == "POST":
msg = request.form["content"]
if len(msg) <= 140:
query = "INSERT INTO squawks (squawk, time_stamp) VALUES (?, ?)"
time = datetime.datetime.now()
cursor.execute(query, (msg, time))
conn.commit()
else:
abort(400)
cursor.execute("SELECT squawk FROM squawks ORDER BY time_stamp DESC")
all_squawks = cursor.fetchall()
return render_template("index.html", squawks=all_squawks)

if __name__ == '__main__':
app.run()
18 changes: 18 additions & 0 deletions squawker/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Squawkers</title>
</head>

<body>
<h1>Squawkers</h1>
<form method="post">
<textarea name="content" maxlength="140" placeholder="max length is 140 characters"></textarea>
<input type="submit">
<h2>Start squawking</h2>
{% for s in squawks %}
<p>{{s[0]}}</p>
{% endfor %}
</form>
</body>
</html>