exquisite_corpse

commit c26ea8e6521b02451c9483be533cf2d1f1f36070

tree

parent:
8e661ec815a12eaca8cf08b64a47fc9723e7507d

Nick Mykins <nick.mykins@gmail.com>

2013-12-22T20:57:19-05:00

initial commit

diff --git a/exquisite_corpse.py b/exquisite_corpse.py
new file mode 100644
index 0000000000000000000000000000000000000000..cf0ae271c5f7218368ad6104cb4ae20ab3eb185a
--- /dev/null
+++ b/exquisite_corpse.py
@@ -0,0 +1,107 @@
+# -*- coding: utf-8 -*-
+"""
+    Flaskr
+    ~~~~~~
+
+    A microblog example application written as Flask tutorial with
+    Flask and sqlite3.
+
+    :copyright: (c) 2010 by Armin Ronacher.
+    :license: BSD, see LICENSE for more details.
+"""
+
+from sqlite3 import dbapi2 as sqlite3
+from flask import Flask, request, session, g, redirect, url_for, abort, \
+     render_template, flash, make_response
+
+
+# create our little application :)
+app = Flask(__name__)
+
+# Load default config and override config from an environment variable
+app.config.update(dict(
+    DATABASE='/tmp/flaskr.db',
+    DEBUG=True,
+    SECRET_KEY='development key',
+))
+app.config.from_envvar('FLASKR_SETTINGS', silent=True)
+
+
+def connect_db():
+    """Connects to the specific database."""
+    rv = sqlite3.connect(app.config['DATABASE'])
+    rv.row_factory = sqlite3.Row
+    return rv
+
+
+def init_db():
+    """Creates the database tables."""
+    with app.app_context():
+        db = get_db()
+        with app.open_resource('schema.sql', mode='r') as f:
+            db.cursor().executescript(f.read())
+        db.commit()
+
+
+def get_db():
+    """Opens a new database connection if there is none yet for the
+    current application context.
+    """
+    if not hasattr(g, 'sqlite_db'):
+        g.sqlite_db = connect_db()
+    return g.sqlite_db
+
+def get_last_post_id():
+    db = get_db()
+    cur = db.execute('select max(id) from entries')
+    return cur.fetchall()[0]['max(id)']
+
+@app.route('/set_cookie')
+def cookie_insertion():
+    redirect_to_index = redirect(url_for('show_entries'))
+    response = app.make_response(redirect_to_index )  
+    response.set_cookie('test_cookie',value='test')
+    return response
+
+@app.teardown_appcontext
+def close_db(error):
+    """Closes the database again at the end of the request."""
+    if hasattr(g, 'sqlite_db'):
+        g.sqlite_db.close()
+
+
+@app.route('/')
+def show_entries():
+    db = get_db()
+    cur = db.execute('select text from entries order by id desc')
+    entries = cur.fetchall()
+    cookie_insertion()
+    return render_template('show_entries.html', entries=entries)
+
+
+@app.route('/add', methods=['POST'])
+def add_entry():
+    redirect_to_index = redirect(url_for('show_entries'))
+    response = app.make_response(redirect_to_index )  
+    
+    last_post_id = get_last_post_id()
+    my_last_post_id = int(request.cookies.get('my_last_post_id') 
+                          if request.cookies.get('my_last_post_id') is not None 
+                          else 0)
+    if my_last_post_id == last_post_id and last_post_id is not None:
+        flash('let someone else have a turn before you contribute again!')
+        return redirect(url_for('show_entries'))
+    else:
+        db = get_db()
+        db.execute('insert into entries (text) values (?)',
+                 [request.form['text']])
+        db.commit()
+        flash('thank you!')
+        with open('poem','a') as poemfile:
+            poemfile.write(request.form['text']+'\n')
+        response.set_cookie('my_last_post_id',value=str(get_last_post_id()))
+    return response
+
+if __name__ == '__main__':
+    init_db()
+    app.run()
diff --git a/schema.sql b/schema.sql
new file mode 100644
index 0000000000000000000000000000000000000000..daebb379c09bc389ac4330962b15d3ba042b7810
--- /dev/null
+++ b/schema.sql
@@ -0,0 +1,5 @@
+drop table if exists entries;
+create table entries (
+  id integer primary key autoincrement,
+  text text not null
+);
diff --git a/static/style.css b/static/style.css
new file mode 100644
index 0000000000000000000000000000000000000000..8d809eb535942372f7245af32c693ffa1b2bea2c
--- /dev/null
+++ b/static/style.css
@@ -0,0 +1,18 @@
+body            { font-family: sans-serif; background: black; }
+a, h1, h2       { color: #66FF00; }
+h1, h2          { font-family: 'Courier', sans-serif; margin: 0; color: #66FF00 }
+h1              { border-bottom: 2px solid #eee }
+h2              { font-size: 1.2em; }
+
+.page           { margin: 2em auto; width: 35em;
+                  padding: 0.8em; background: black; }
+.entries        { list-style: none; margin: 0; padding: 0; color: #ddd }
+.entries li     { margin: 0.8em 1.2em; }
+.entries li h2  { margin-left: -1em; }
+.add-entry      { font-size: 0.9em; color: #66FF00}
+.add-entry dl   { font-weight: bold; color: #66FF00}
+.metanav        { text-align: right; font-size: 0.8em; padding: 0.3em;
+                  margin-bottom: 1em; background: #fafafa; color: #66FF00}
+.flash          { background: black; padding: 0.5em;
+                  color: #66FF00;}
+.error          { background: #F0D6D6; padding: 0.5em; }
diff --git a/templates/layout.html b/templates/layout.html
new file mode 100644
index 0000000000000000000000000000000000000000..989ec483b8245dc005fe5dfe0bd4f0fe653f2581
--- /dev/null
+++ b/templates/layout.html
@@ -0,0 +1,14 @@
+<!doctype html>
+<title>exquisite corpse</title>
+<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
+<div class=page>
+  <h1>exquisite corpse</h1>
+  {% if get_flashed_messages()|length > 0 %}
+    {% for message in get_flashed_messages() %}
+        <div class=flash>{{ message }}</div>
+     {% endfor %}
+  {% else %}
+     <div class=flash>please contribute a line of poetry. the last line of the poem is:</div> 
+  {% endif %}
+  {% block body %}{% endblock %}
+</div>
diff --git a/templates/login.html b/templates/login.html
new file mode 100644
index 0000000000000000000000000000000000000000..6f70bb76f477e526f0667cc70d5db0733f1dbf70
--- /dev/null
+++ b/templates/login.html
@@ -0,0 +1,14 @@
+{% extends "layout.html" %}
+{% block body %}
+  <h2>Login</h2>
+  {% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}
+  <form action="{{ url_for('login') }}" method=post>
+    <dl>
+      <dt>Username:
+      <dd><input type=text name=username>
+      <dt>Password:
+      <dd><input type=password name=password>
+      <dd><input type=submit value=Login>
+    </dl>
+  </form>
+{% endblock %}
diff --git a/templates/show_entries.html b/templates/show_entries.html
new file mode 100644
index 0000000000000000000000000000000000000000..e54ee183e3d2fb6d0ce05183303cc334240b6281
--- /dev/null
+++ b/templates/show_entries.html
@@ -0,0 +1,17 @@
+{% extends "layout.html" %}
+{% block body %}
+  <ul class=entries>
+  {% for entry in entries %}
+    <li><p style="text align: center;">{{ entry.text|safe }}</p>
+  {% else %}
+    <li><em>this poem has not begun yet!</em>
+  {% endfor %}
+  </ul>
+    <form action="{{ url_for('add_entry') }}" method=post class=add-entry>
+      <dl>
+        <dt>next line:
+        <input type=text name="text" style="width:500px" maxlength=140>
+        <input type=submit value=share>
+      </dl>
+    </form>
+{% endblock %}