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
5 changes: 2 additions & 3 deletions squawker/schema.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
-- TODO change this
DROP TABLE IF EXISTS mytable;
CREATE TABLE mytable (id integer);
DROP TABLE IF EXISTS squaks;
CREATE TABLE squaks (id integer primary key, squak TEXT);
40 changes: 35 additions & 5 deletions squawker/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Flask, g
from __future__ import print_function
from flask import Flask, g, render_template, request, redirect, url_for
import sqlite3

import sys

# -- leave these lines intact --
app = Flask(__name__)
Expand Down Expand Up @@ -38,10 +39,39 @@ def close_connection(exception):


@app.route('/')
def root():
def root(scroll=0):
conn = get_db()
cursor = conn.cursor()
count = cursor.execute('SELECT COUNT(*) FROM squaks').fetchall()[0][0]
print("count", count, file=sys.stderr)
if request.args.get('scroll') is not None:
scroll = int(request.args.get('scroll'))
print("scroll", scroll, file=sys.stderr)
else:
scroll = 0
print("No scrolling")
if count:
cursor.execute('SELECT * FROM squaks')
squaks = list(reversed(cursor.fetchall()))
print(squaks, scroll, file=sys.stderr)
return render_template('index.html', squaks=squaks[scroll:scroll + 20], nextButton=(count > scroll + 20), scroll=scroll)
else:
return render_template('index.html', squaks=[], nextButton=False, scroll=0)


@app.route('/submit', methods=['POST'])
def newSquak():
newSquak = request.form['newSquak']
if not newSquak or len(newSquak) > 140:
return redirect(url_for('root'))
print(newSquak, file=sys.stderr)
conn = get_db()
# TODO change this
return "Hello World!"
cur = conn.cursor()
cur.execute('INSERT INTO squaks(squak) VALUES (?)', (newSquak,))
cur.execute('SELECT * from squaks')
conn.commit()
print(cur.fetchall(), file=sys.stderr)
return redirect(url_for('root'))


if __name__ == '__main__':
Expand Down
22 changes: 22 additions & 0 deletions squawker/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head></head>
<body>
<p>Squawk !</p>
<p>Squawk Page: {{ scroll }}</p>
<div>
<form action="submit" method="post">
<input name="newSquak" type="text" placeholder="Enter a squak" maxlength="140" required>
<input type="submit" value="newSquak">
</form>
</div>
<div>
{% for squak in squaks %}
<p>{{ squak[1] }}</p>
{% endfor %}
{% if nextButton %}
<a href="{{ url_for('root',scroll=scroll+20) }}" type="submit" method="post" name="scroll"><button type="button">Next</button></a>
{% endif %}
</div>
</body>
</html>