Permalink
Browse files

Database model, pull data from db

  • Loading branch information...
1 parent 80bdd98 commit 4397a16380a0ca1330eeeca6f7acff35e919fa40 @eleddy eleddy committed Jan 12, 2012
Showing with 117 additions and 8 deletions.
  1. +24 −0 .installed.cfg
  2. +67 −0 README.rst
  3. +1 −0 setup.py
  4. +5 −7 src/noiselist/__init__.py
  5. +19 −0 src/noiselist/model.py
  6. +1 −1 src/noiselist/script.py
View
@@ -95,3 +95,27 @@ location = /Users/eleddy/buildouts/flask-noiselist/parts/test
python = buildout
recipe = pbp.recipe.noserunner
script = /Users/eleddy/buildouts/flask-noiselist/bin/test
+
+[buildout]
+installed_develop_eggs = /Users/eleddy/buildouts/flask-noiselist/develop-eggs/noiselist.egg-link
+
+[buildout]
+parts = mkdirs deploy_ini deploy_cfg debug_ini debug_cfg test app
+
+[buildout]
+parts = deploy_ini deploy_cfg debug_ini debug_cfg test app mkdirs
+
+[buildout]
+parts = deploy_cfg debug_ini debug_cfg test app mkdirs deploy_ini
+
+[buildout]
+parts = debug_ini debug_cfg test app mkdirs deploy_ini deploy_cfg
+
+[buildout]
+parts = debug_cfg test app mkdirs deploy_ini deploy_cfg debug_ini
+
+[buildout]
+parts = test app mkdirs deploy_ini deploy_cfg debug_ini debug_cfg
+
+[buildout]
+parts = app mkdirs deploy_ini deploy_cfg debug_ini debug_cfg test
View
@@ -189,11 +189,78 @@ of "pass". The = is not required to display a variable either.
The Database
------------
+INTRO HERE
+Add the package for SQLAlchemy integration in setup.py of our package and rerun buildout.
+In flask-noiselist/setup.py::
+ install_requires=[
+ 'setuptools',
+ 'Flask',
+ 'Flask-SQLAlchemy',
+ ],
+
+Re-run buildout to pull in the new package::
+
+ > ./bin/buildout
+ > bin/flask-ctl debug fg
+
+Now that we have the new egg, we can import and use all the db connections. In
+SQLAlchemy, we need to define and initialize the model. Let's make a new file
+called model.py and keep all of our access info there::
+
+ > touch flask-noiselist/src/noilist/model.py
+
+In this model, we will create the same todo item that we did in the web2py app with
+a bit of a different twist. Edit model.py to say::
+
+ > XXX put stuff here
+
+
+Next we need to initialize the database. Initializeing the database will sync the model
+we created with the database, making sure that all the columns and tables we need are
+there and ready to use*. In __init__.py::
+
+ from model import db
+
+ ...
+
+ def init_db():
+ """ Initialize the database """
+ db.create_all()
+
+
+Unlike web2py, we must initialize the database manually every time we update the model.
+There are several reasons and potential conflicts with this but SQLAlchemy does its
+best to make it all magically work. To resync the db, stop the server and run::
+
+ > ./bin/flask-ctl debug initdb
+ # restart
+ > ./bin/flask-ctl debug fg
+
+
+Hang in there, we are almost there. Next let's pull our data from the database. In
+__init__.py::
+
+ from model import TodoItem
+ ...
+
+ @app.route('/')
+ def index():
+ todo_list = TodoItem.query.all()
+ return render_template('hello.html', todos=todo_list)
+
+Keep in mind that at this moment the db is empty so a reload should just show an
+empty list.
+
+Adding Data to the Database
+---------------------------
+
+
More Info
---------
* Flask Documentation: http://flask.pocoo.org/docs/
* About Jinja2: http://jinja.pocoo.org/docs/
* Bootstrap: http://twitter.github.com/bootstrap/
+ * SQLAlchemy: XXX
* For more info on this buildout itself, please see http://flask.pocoo.org/snippets/27/
View
@@ -28,6 +28,7 @@ def read(*rnames):
install_requires=[
'setuptools',
'Flask',
+ 'Flask-SQLAlchemy',
],
entry_points="""
[console_scripts]
View
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from flask import Flask, request
from flask import render_template
+from model import TodoItem
+from model import db
class _DefaultSettings(object):
@@ -16,15 +18,11 @@ class _DefaultSettings(object):
def init_db():
- """Create the database tables."""
- pass
-
+ """ Initialize the database """
+ db.create_all()
@app.route('/')
def index():
- todo_list = ["Watch TV",
- "Contemplate Work",
- "Go to Bed",
- ]
+ todo_list = TodoItem.query.all()
return render_template('hello.html', todos=todo_list)
View
@@ -0,0 +1,19 @@
+from flask import Flask
+from flaskext.sqlalchemy import SQLAlchemy
+
+
+app = Flask(__name__)
+app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
+db = SQLAlchemy(app)
+
+
+class TodoItem(db.Model):
+ id = db.Column(db.Integer, primary_key=True)
+ description = db.Column(db.String(240), unique=True)
+
+ def __init__(self, description):
+ self.description = todo_item
+
+
+ def __repr__(self):
+ return '<User %r>' % self.description
View
@@ -48,7 +48,6 @@ def make_shell():
reqctx = app.test_request_context
return locals()
-
def _init_db(debug=False, dry_run=False):
"""Initialize the database."""
from noiselist import init_db
@@ -127,3 +126,4 @@ def action_stop(dry_run=False):
"""Stop the application."""
_serve('stop', dry_run=dry_run)
werkzeug.script.run()
+

0 comments on commit 4397a16

Please sign in to comment.