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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ __pycache__/
*.db3
*.sqlite
*.sqlite3
.idea/
*/.idea/
8 changes: 5 additions & 3 deletions squawker/schema.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-- TODO change this
DROP TABLE IF EXISTS mytable;
CREATE TABLE mytable (id integer);
-- TODO CHANGE THIS
DROP TABLE IF EXISTS squawker;
CREATE TABLE squawker (id INTEGER PRIMARY KEY AUTOINCREMENT,
post VARCHAR(150),
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
62 changes: 57 additions & 5 deletions squawker/server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from flask import Flask, g
from flask import Flask, g, render_template, request, redirect, url_for, abort
import sqlite3

from datetime import datetime, timezone
import math

# -- leave these lines intact --
app = Flask(__name__)
FORMAT = "%Y-%m-%d %H:%M:%S"
PER_PAGE = 20


def get_db():
Expand Down Expand Up @@ -34,14 +37,63 @@ def close_connection(exception):
db = getattr(g, 'sqlite_db', None)
if db is not None:
db.close()


# ------------------------------


@app.route('/')
def root():
@app.route('/<cur_page>')
def root(cur_page=None):
if cur_page is None:
cur_page = 1

conn = get_db()
cur = conn.cursor()
cur.execute("SELECT count(*) FROM squawker ORDER BY ts DESC;")
page_count = math.ceil(cur.fetchone()[0] / 20)
print(page_count)

items = _get_one_page(cur_page)

return render_template('base.html', items=items, page=cur_page, page_count=page_count)


def _get_one_page(page):
conn = get_db()
# TODO change this
return "Hello World!"
cur = conn.cursor()
cur.execute("SELECT post, ts FROM squawker ORDER BY ts DESC LIMIT %d OFFSET %d;" % (
PER_PAGE, (int(page) - 1) * PER_PAGE))
items = cur.fetchall()
_ = map(lambda r: (r[0], _convert_time(r[1])), items)
return _


def _convert_time(ts):
dt = datetime.strptime(ts, FORMAT)
_ = dt.replace(tzinfo=timezone.utc).astimezone(tz=None)
return _.strftime(FORMAT)


@app.route('/post', methods=["POST"])
def do_post():
text = request.form['input-post']
print(text)

if is_valid(text):
conn = get_db()
cur = conn.cursor()
stmt = 'INSERT INTO squawker (post) VALUES ("{0}");'.format(text)
cur.execute(stmt)
conn.commit()

return redirect(url_for("root"))
else:
abort(400)


def is_valid(text):
return 0 < len(text.rstrip()) <= 140


if __name__ == '__main__':
Expand Down
82 changes: 82 additions & 0 deletions squawker/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Squawker</title>
</head>
<body style="font-size: 14pt">
<form action="/post" method="post" style="width: 80%;">
<div class="input" style="width: 80%; height: auto">
<textarea class="text" name="input-post"
placeholder="Type in your post here"
cols="60" rows="4"
maxlength="140"
></textarea>
<div style="width: 100%">
<button type="submit" style="font-size: 14pt; float: right"><span>Post</span></button>
</div>
</div>
</form>
<div style="height: 50px;"></div>
{% if items %}
<div class="feed">
<table>
<tr>
<th class="tb-content">Content</th>
<th class="tb-time">Time</th>
</tr>
{% for item, ts in items %}
<tr>
<td class="tb-content">
<div><p>{{ item }}</p>
</div>
</td>
<td class="tb-time">{{ ts }}</td>
</tr>
{% endfor %}
</table>
</div>
<div class="pagination">
<a href="/"><span>Prev</span></a>
{% for i in range(1,(page_count+1)) %}
<a href="/"><span>{{ i }}</span></a>
{% endfor %}
<a href="/"><span>Next</span></a>
</div>
{% else %}
<div><p>No posts yet.</p></div>
{% endif %}

<style scoped>
.text {
display: inline-block;
font-size: 14pt;
text-align: left;
padding-top: 5px
}

.feed {
width: 60%;
}

.tb-content {
width: 40%;
height: auto;
text-align: left;
font-family: sans-serif;
}

.tb-time {
width: 10%;
height: auto;
text-align: left;
font-family: sans-serif;
}

.pagination {
width: 60%;
text-align: center;
}
</style>
</body>
</html>