Pastebin
Paste #3186: flask structure
< previous paste - next paste>
Pasted by tdn
====== __init__.py
(empty)
====== config.py
#!/usr/bin/env python
#encoding=utf8
cfg = {}
#cfg['runmode'] = "dev"
#cfg['runmode'] = "test"
cfg['runmode'] = "prod"
if cfg['runmode'] == "prod":
cfg['host'] = '0.0.0.0'
cfg['app_root'] = "/data/www/virtual/...../myapp"
cfg['static_root'] = "%s/static" % (cfg['app_root'])
cfg["site_url"] = "http://example.com/"
cfg['dbhost'] = "localhost"
cfg['dbport'] = "5432"
cfg['dbname'] = "appprod"
cfg['dbuser'] = "appprod"
cfg['dbpass'] = "**********"
elif cfg['runmode'] == "test":
cfg['host'] = '0.0.0.0'
cfg['app_root'] = "/data/www/virtual/...../myapp"
cfg['static_root'] = "%s/static" % (cfg['app_root'])
cfg["site_url"] = "http://example.com/"
cfg['dbhost'] = "localhost"
cfg['dbport'] = "5432"
cfg['dbname'] = "apptest"
cfg['dbuser'] = "apptest"
cfg['dbpass'] = "**********"
elif cfg['runmode'] == "dev":
cfg['host'] = '127.0.0.1'
cfg['app_root'] = "/data/www/virtual/...../myapp"
cfg['static_root'] = "%s/static" % (cfg['app_root'])
cfg["site_url"] = "http://example.com/"
cfg['dbhost'] = "localhost"
cfg['dbport'] = "5432"
cfg['dbname'] = "appdev"
cfg['dbuser'] = "appdev"
cfg['dbpass'] = "**********"
else:
print "ERROR: Unknown runmode"
import sys
sys.exit(1)
LANGUAGES = {
"da" : "Dansk",
"en" : "English",
"de" : "Deutsch"
}
========== web.py
#!/usr/bin/env python
#encoding=utf8
import hashlib
from flask import Flask
from flask.ext.babel import Babel
from flask.ext.babel import gettext, ngettext
from flask.ext.sqlalchemy import SQLAlchemy
from jinja2 import BaseLoader, ChoiceLoader, TemplateNotFound
from config import LANGUAGES, cfg
class DatabaseLoader(BaseLoader):
def get_source(self, environment, template):
to = TemplateObjectInDb.query.get(template)
ref_checksum = to.hash
if to is None:
raise TemplateNotFound(template)
# This works in SQLAlchemy efficiently because the session caches
# well. Might not work in other systems.
def check_if_up_to_date():
to = TemplateObjectInDb.query.get(template)
return to.hash == ref_checksum
return to.source, 'db:'+template, check_if_up_to_date
def create_global_jinja_loader(self):
database_loader = DatabaseLoader()
return ChoiceLoader(
[ DatabaseLoader(), Flask.create_global_jinja_loader(self) ]
)
app = Flask(__name__)
app.config.from_object('config-web')
app.config["SQLALCHEMY_DATABASE_URI"] = cfg["db_uri"]
db = SQLAlchemy(app)
# import views
from views import *
babel = Babel(app)
def get_locale():
# if a user is logged in, use the locale from the user settings
user = getattr(g, 'user', None)
if user is not None:
return user.locale
# otherwise try to guess the language from the user accept header
# we support en/da/de
return request.accept_languages.best_match(LANGUAGES.keys())
@babel.timezoneselector
def get_timezone():
user = getattr(g, 'user', None)
if user is not None:
return user.timezone
#@babel.localeselector
if __name__ == "__main__":
app.debug = True
app.run(host = cfg['host'])
============ app.wsgi
import os
import sys
# Change working directory so relative paths (and template lookup) work again
os.chdir(os.path.abspath(os.path.dirname(__file__)))
activate_this = os.path.join(os.path.realpath(os.path.join(os.path.dirname(__file__))), "venv", "bin", "activate_this.py")
execfile(activate_this, dict(__file__=activate_this))
EXTRA_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__)))
if EXTRA_DIR not in sys.path:
sys.path.append(EXTRA_DIR)
from web import app as application
New Paste
Go to most recent paste.