Created
August 26, 2012 12:25
-
-
Save pbanaszkiewicz/3478436 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_db(): | |
ctx = _app_ctx_stack.top | |
con = getattr(ctx, "sa_database", None) | |
if con is None: | |
con = SQLAlchemy(ctx.app) | |
ctx.sa_database = con | |
return con |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from .db import get_db | |
db = get_db() | |
class User(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
username = db.Column(db.String(80), unique=True) | |
email = db.Column(db.String(120), unique=True) | |
def __repr__(self): | |
return '<User %r>' % self.username |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def register_blueprints(app): | |
from .models import User | |
from .views import admin | |
app.register_blueprint(admin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Blueprint | |
from ..models import User | |
from ..db import get_db | |
# from werkzeug.local import LocalProxy | |
admin = Blueprint("admin", __name__, url_prefix="/admin") | |
db = LocalProxy(get_db) | |
# db = get_db() | |
@admin.route("/") | |
def index(): | |
# db = get_db() | |
u1 = User(username="Piotr", email="ex@example.org") | |
db.session.add(u1) | |
db.session.commit() | |
return "It Works!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment