Skip to content

Instantly share code, notes, and snippets.

@MariusSchiffer
Created September 13, 2011 13:29
Show Gist options
  • Save MariusSchiffer/1213804 to your computer and use it in GitHub Desktop.
Save MariusSchiffer/1213804 to your computer and use it in GitHub Desktop.
Test
# skysim/skysimulation/__init__.py
import database
# skysim/__init__.py
from flask import Flask
app = Flask(__name__)
from skysimulation.database import init_db,db_session
skysimulation.database.init_db()
@app.teardown_request
def shutdown_session(exception=None):
db_session.remove()
import view_index # all views here
app.secret_key = 'asdf'
if __name__ == "__main__":
app.debug = True
app.run(host="0.0.0.0")
# skysim/skysimulation/database.py
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('blabla connect string here', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
# import all modules here that might define models so that
# they will be registered properly on the metadata. Otherwise
# you will have to import them first before calling init_db()
import tables
Base.metadata.create_all(bind=engine)
/
run_server.py
skysim/
__init__.py
view_index.py
logincheck.py
generalview.py
skysimulation/
__init__.py
database.py
user.py
# skysim/general_view.py
from flask import render_template, make_response
from flask.views import View
from logincheck import LoginCheck
class GeneralView(View, LoginCheck):
context = {}
methods = ['GET','POST']
def render_template(self):
return render_template(self.template_name,**self.context)
def before_request(self):
super(GeneralView,self).before_request()
def dispatch_request(self):
self.before_request()
return self.after_request(make_response(self.dispatch()))
def after_request(self,resp):
return super(GeneralView,self).after_request(resp)
# skysim/logincheck.py
from flask import session,request
import skysimulation
import skysimulation.user as User
# User just has some functions for checking username/password combinations and stuff
class LoginCheck(object):
REQUIRES_LOGIN = False
def before_request(self):
session.permanent = True
if 'username' in session and 'password' in session:
result = User.checkLogin(session['username'],session['password'])
if result > 0:
self.context['logged_in'] = True
session['id_user'] = result
session['logged_in'] = True
return
session['logged_in'] = False
self.context['logged_in'] = False
if self.REQUIRES_LOGIN:
pass # TODO redirect to login
def after_request(self,resp):
return resp # FIXME not needed anymore
# run_server.py
from skysim import app
app.run(debug=True, host="0.0.0.0")
# skysim/view_index.py
from skysim import app
import generalview
class MainView(generalview.GeneralView):
def __init__(self):
self.template_name = "index.html"
def dispatch(self):
return self.render_template()
app.add_url_rule('/', view_func=MainView.as_view('index'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment