Skip to content

Commit ce0e21a

Browse files
committed
Trying again. Still having a problem with url_for being undefined.
1 parent 3e5d856 commit ce0e21a

30 files changed

Lines changed: 428 additions & 0 deletions
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Submittin Your Work
2+
3+
* Try to get your code running on your VM
4+
I was able to finally get flaskr working on my vm:
5+
http://block647052-gfz.blueboxgrid.com
6+
7+
* Add a README.txt file that discusses the experience.
8+
I decided to try and port the flaskr app to Bottle
9+
Project: flaskr_to_bottler
10+
11+
I started working on getting this all installed to my vm with a new virtualenv for Bottle. I was able to test the functionality by adding a simple Hello_World to confirm that it was working. I then copied the flaskr project over and began picking apart the differences.
12+
13+
Templates:
14+
I attempted to use jinja2_template from bottle in order to utilize the existing templates. Unfortunately, no matter where I place my *.html (views, templates), I get a 404 stating "Not found: '/'"
15+
I did try to use the TEMPLATE_PATH.append("./templates") to force my app to use that directory but no dice.
16+
Used the FlashPlugin: Imported bottle_flash
17+
Used the SQLitePlugin: This allow for a call to be made to the db any time a route requires it. Handy instead of using 'g'
18+
19+
Resources:
20+
You must the resources.add_path() method to define where files like schema.sql will live. This was a gotchya when I tried to reinitialize a new bottler.db. After doing this, I still found that my entries table was not created, I had have to perform this manually by using the sqlite3 command to open a connection to bottler.db and create the table.
21+
22+
* Commit your changes to your fork of the class repository and send me a pull
23+
request
24+
25+
26+
27+
187 Bytes
Binary file not shown.
187 Bytes
Binary file not shown.
185 Bytes
Binary file not shown.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import sqlite3
2+
from contextlib import closing
3+
4+
from bottle import Bottle
5+
from bottle import default_app
6+
from bottle import error
7+
from bottle import get
8+
from bottle import install
9+
from bottle import post
10+
from bottle import redirect
11+
from bottle import request
12+
from bottle import static_file
13+
from bottle import TEMPLATE_PATH
14+
from bottle import url
15+
from bottle import jinja2_template as template
16+
17+
from bottle_sqlite import SQLitePlugin
18+
from bottle_flash import FlashPlugin
19+
20+
# configuration goes here
21+
DATABASE = '/tmp/bottler.db'
22+
SECRET_KEY = 'development key'
23+
USERNAME = 'admin'
24+
PASSWORD = 'default'
25+
26+
app = Bottle(__name__)
27+
28+
# Resource configuration (relative to current file)
29+
app.resources.add_path('./', base=__file__)
30+
TEMPLATE_PATH.append("./templates")
31+
32+
app.install(SQLitePlugin(dbfile='/tmp/bottler.db'))
33+
app.install(FlashPlugin(secret='COOKIE_SECRET'))
34+
35+
def connect_db():
36+
return sqlite3.connect('/tmp/bottler.db')
37+
38+
39+
def init_db():
40+
with closing(connect_db()) as db:
41+
with app.resources.open('schema.sql') as f:
42+
db.cursor().executescript(f.read())
43+
db.commit()
44+
45+
#@app.before_request
46+
#def before_request():
47+
# g.db = connect_db()
48+
49+
50+
#@app.teardown_request
51+
#def teardown_request(exception):
52+
# g.db.close()
53+
54+
def write_entry(title, text):
55+
db.execute('insert into entries (title, text) values (?, ?)',
56+
[title, text])
57+
db.commit()
58+
59+
60+
def get_all_entries():
61+
db = connect_db()
62+
cur = db.execute('select title, text from entries order by id desc')
63+
entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
64+
return entries
65+
66+
@app.route('/')
67+
def show_entries():
68+
db = connect_db()
69+
cur = db.execute('select title, text from entries order by id desc')
70+
entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
71+
return template('show_entries.html', entries=entries)
72+
73+
##############################################################
74+
# TEST PASS
75+
#
76+
#@app.route('/')
77+
#@app.route('/hello/<name>')
78+
#def greet(name='Stranger'):
79+
# return template('home.html', name=name)
80+
##############################################################
81+
82+
@app.route('/add', methods=['POST'])
83+
def add_entry():
84+
try:
85+
write_entry(request.form['title'], request.form['text'])
86+
flash('New entry was successfully posted')
87+
except sqlite3.Error as e:
88+
flash('There was an error: %s' % e.args[0])
89+
return redirect(url('show_entries'))
90+
91+
92+
if __name__ == '__main__':
93+
app.run(debug=True)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
drop table if exists entries;
2+
create table entries (
3+
id integer primary key autoincrement,
4+
title string not null,
5+
text string not null
6+
);
186 Bytes
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
body { font-family: sans-serif; background: #eee; }
2+
a, h1, h2 { color: #377BA8; }
3+
h1, h2 { font-family: 'Georgia', serif; margin: 0; }
4+
h1 { border-bottom: 2px solid #eee; }
5+
h2 { font-size: 1.2em; }
6+
.page { margin: 2em auto; width: 35em; border: 5px solid #ccc;
7+
padding: 0.8em; background: white; }
8+
.entries { list-style: none; margin: 0; padding: 0; }
9+
.entries li { margin: 0.8em 1.2em; }
10+
.entries li h2 { margin-left: -1em; }
11+
.add-entry { font-size: 0.9em; border-bottom: 1px solid #ccc; }
12+
.add-entry dl { font-weight: bold; }
13+
.metanav { text-align: right; font-size: 0.8em; padding: 0.3em;
14+
margin-bottom: 1em; background: #fafafa; }
15+
.flash { background: #CEE5F5; padding: 0.5em;
16+
border: 1px solid #AACBE2; }
17+
.error { background: #F0D6D6; padding: 0.5em; }
185 Bytes
Binary file not shown.
184 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)