diff --git a/squawker.db b/squawker.db new file mode 100644 index 0000000..7d0c385 Binary files /dev/null and b/squawker.db differ diff --git a/squawker/.DS_Store b/squawker/.DS_Store new file mode 100644 index 0000000..487abca Binary files /dev/null and b/squawker/.DS_Store differ diff --git a/squawker/__pycache__/__init__.cpython-35.pyc b/squawker/__pycache__/__init__.cpython-35.pyc new file mode 100644 index 0000000..4de11d6 Binary files /dev/null and b/squawker/__pycache__/__init__.cpython-35.pyc differ diff --git a/squawker/__pycache__/server.cpython-35.pyc b/squawker/__pycache__/server.cpython-35.pyc new file mode 100644 index 0000000..11e9241 Binary files /dev/null and b/squawker/__pycache__/server.cpython-35.pyc differ diff --git a/squawker/schema.sql b/squawker/schema.sql index 5e67ffb..d1ba9fa 100644 --- a/squawker/schema.sql +++ b/squawker/schema.sql @@ -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, + posts varchar(140), + timestamp datetime default CURRENT_TIMESTAMP +); diff --git a/squawker/server.py b/squawker/server.py index 6ff24ba..cbd5fed 100644 --- a/squawker/server.py +++ b/squawker/server.py @@ -1,4 +1,4 @@ -from flask import Flask, g +from flask import Flask, g, request, render_template import sqlite3 @@ -37,11 +37,40 @@ def close_connection(exception): # ------------------------------ -@app.route('/') +@app.route('/', methods=['GET', 'POST']) def root(): + error = "" + status = 200 conn = get_db() - # TODO change this - return "Hello World!" + # if click on the post button + if(request.method == "POST"): + # get the text from text area id as post_text + post_text = request.form['post_text'] + # if longer than 140c, shoot ERROR 400 + if len(post_text) > 140: + error = "Post too long, limit to 140" + status = 400 + # insert into the database + else: + c = conn.execute("INSERT INTO squawks (\'posts\') VALUES (\'" + post_text + "\')") + conn.commit() + c = conn.execute("SELECT COUNT(*) FROM squawks", ()) + count = c.fetchone()[0] + c.close() + return render_template("index.html", num_squawks=count), status + + +@app.context_processor +def utility_processor(): + def loadSquawks(): + conn = get_db() + # sort by DESC time + c = conn.execute("SELECT posts FROM squawks ORDER BY timestamp DESC ", ()) + squawks = c.fetchall() + c.close() + slen = len(squawks) + return squawks + return dict(loadSquawks=loadSquawks) if __name__ == '__main__': diff --git a/squawker/templates/.DS_Store b/squawker/templates/.DS_Store new file mode 100644 index 0000000..1a727a8 Binary files /dev/null and b/squawker/templates/.DS_Store differ diff --git a/squawker/templates/index.html b/squawker/templates/index.html new file mode 100644 index 0000000..021c62d --- /dev/null +++ b/squawker/templates/index.html @@ -0,0 +1,34 @@ + + + + Squawker + + + +
+
+ +
+ +
+
+ +
+
+ +
+

Total Squaks Counts: {{num_squawks}}

+
+ {% for squawk in loadSquawks() %} +
+
{{ squawk[0] }}
+ {% endfor %} + +
+ +

+ + + + + diff --git a/src/pip-delete-this-directory.txt b/src/pip-delete-this-directory.txt new file mode 100644 index 0000000..c8883ea --- /dev/null +++ b/src/pip-delete-this-directory.txt @@ -0,0 +1,5 @@ +This file is placed here by pip to indicate the source was put +here by pip. + +Once this package is successfully installed this source code will be +deleted (unless you remove this file). diff --git a/src/splinter b/src/splinter new file mode 160000 index 0000000..ba3afc8 --- /dev/null +++ b/src/splinter @@ -0,0 +1 @@ +Subproject commit ba3afc8a0750dcfea096f8f89adb58f8f8d78276