diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..184a677 Binary files /dev/null and b/.DS_Store differ diff --git a/requirements.txt b/requirements.txt index 3e0178d..918fef3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ Flask~=0.11.1 pep8~=1.7.0 pytest~=3.0.3 pytest-json~=0.4.0 --e git+https://github.com/startup-systems/splinter.git@ba3afc8a0750dcfea096f8f89adb58f8f8d78276#egg=splinter[flask] +splinter[flask]~=0.7.5 diff --git a/squawker/schema.sql b/squawker/schema.sql index 5e67ffb..66d1518 100644 --- a/squawker/schema.sql +++ b/squawker/schema.sql @@ -1,3 +1,3 @@ -- 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, body TEXT); diff --git a/squawker/server.py b/squawker/server.py index 6ff24ba..9ff3d9d 100644 --- a/squawker/server.py +++ b/squawker/server.py @@ -1,4 +1,4 @@ -from flask import Flask, g +from flask import Flask, g, render_template, request, redirect, url_for import sqlite3 @@ -36,12 +36,40 @@ def close_connection(exception): db.close() # ------------------------------ +# Add posts +def addPost(data): + conn = get_db() + cur = conn.cursor() + cur.execute("INSERT INTO squawks (body) VALUES(?)", (data,)) + conn.commit() -@app.route('/') -def root(): +def getPosts(): conn = get_db() - # TODO change this - return "Hello World!" + cur = conn.cursor() + cur.execute("""SELECT id, body FROM + squawks + ORDER BY id DESC """) + temp = cur.fetchall() + data = [] + idx = [] + cur.close() + return temp + +# Routes + +# Index +@app.route('/') +def root(): + squawks = getPosts() + return render_template('index.html', squawks=squawks) + +@app.route('/add/', methods=['POST']) # Add +def add(): + # Check if post is 140 characters + if (len(request.form["new_body"]) > 140): + return abort(400) + addPost(request.form["new_body"]) + return redirect(url_for('root')) if __name__ == '__main__': diff --git a/squawker/templates/index.html b/squawker/templates/index.html new file mode 100644 index 0000000..2a826fa --- /dev/null +++ b/squawker/templates/index.html @@ -0,0 +1,35 @@ + + + + + + + Squawker + + + + +
+

Squawker

+ + {% for squawk in squawks %} +
+

{{ squawk[0] }}

+

{{ squawk[1] }}

+
+ {% endfor %} + +
+
+
+ + + + + +
+
+
+
+ + \ No newline at end of file diff --git a/tests/flaskclient_fix.py b/tests/flaskclient_fix.py new file mode 100644 index 0000000..de222fb --- /dev/null +++ b/tests/flaskclient_fix.py @@ -0,0 +1,74 @@ +# https://github.com/cobrateam/splinter/issues/515 + +from urllib import parse + +from splinter.browser import _DRIVERS +from splinter.driver import flaskclient + +__all__ = ['FlaskClient'] + + +class FlaskClient(flaskclient.FlaskClient): + """ + A patched `FlaskClient` driver that implements more standard `302`/`303` + behaviour and that sets data for `GET` requests against the URL. + """ + + driver_name = 'flask' + + def _do_method(self, method, url, data=None): + + # Set the initial URL and client/HTTP method + self._url = url + func_method = getattr(self._browser, method.lower()) + + # Continue to make requests until a non 30X response is recieved + while True: + self._last_urls.append(url) + + # If we're making a GET request set the data against the URL as a + # query. + if method.lower() == 'get': + + # Parse the existing URL and it's query + url_parts = parse.urlparse(url) + url_params = parse.parse_qs(url_parts.query) + + # Update any existing query dictionary with the `data` argument + url_params.update(data or {}) + query = parse.urlencode(url_params, doseq=True) + url_parts = url_parts._replace(query=query) + + # Rebuild the URL + url = parse.urlunparse(url_parts) + + # As the `data` argument will be passed as a keyword argument to + # the `func_method` we set it `None` to prevent it populating + # `flask.request.form` on `GET` requests. + data = None + + # Call the flask client + self._response = func_method( + url, + headers=self._custom_headers, + data=data, + follow_redirects=False + ) + + # Implement more standard `302`/`303` behaviour + if self._response.status_code in (302, 303): + func_method = getattr(self._browser, 'get') + + # If the response was not in the `30X` range we're done + if self._response.status_code not in (301, 302, 303, 305, 307): + break + + # If the response was in the `30X` range get next URL to request + url = self._response.headers['Location'] + + self._url = self._last_urls[-1] + self._post_load() + + +# Patch the default `FlaskClient` driver +_DRIVERS['flask'] = FlaskClient diff --git a/tests/test_squawker.py b/tests/test_squawker.py index 5deda48..b31bc14 100644 --- a/tests/test_squawker.py +++ b/tests/test_squawker.py @@ -7,6 +7,7 @@ import string import tempfile import time +from . import flaskclient_fix URL = '/' @@ -143,7 +144,7 @@ def test_server_side_validation(browser): @pytest.mark.score(5) @pytest.mark.xfail def test_page_size_limit(browser): - bodies = create_squawks(browser, PAGE_SIZE + 1) + bodies = create_squawks(browser, PAGE_SIZE + 1, delay=1) browser.visit(URL) assert browser.is_text_not_present(bodies[0]) @@ -175,7 +176,7 @@ def test_next_not_present_on_last_page(browser): @pytest.mark.score(5) @pytest.mark.xfail def test_pagination(browser): - bodies = create_squawks(browser, PAGE_SIZE + 1) + bodies = create_squawks(browser, PAGE_SIZE + 1, delay=1) browser.visit(URL) browser.find_by_xpath(NEXT_XPATH).first.click()