Skip to content

Commit

Permalink
Broke production settings. Also removed webassets
Browse files Browse the repository at this point in the history
On our way to separation.
  • Loading branch information
shuhaowu committed May 13, 2014
1 parent 6ca0f3d commit c4c3d68
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 63 deletions.
2 changes: 0 additions & 2 deletions production-requirements.txt
@@ -1,6 +1,5 @@
Cython==0.19.1
Flask==0.10.1
-e git+https://github.com/miracle2k/flask-assets.git@5ea49d3bab5dbb8a9ca84069642fda6505423576#egg=flask-assets-dev
Flask-Login==0.2.10
Flask-SeaSurf==0.1.22
Jinja2==2.7.2
Expand All @@ -17,5 +16,4 @@ protobuf==2.4.1
misaka==1.0.2
requests==1.2.3
ujson==1.33
-e git+https://github.com/miracle2k/webassets.git@5d7381c797810a6f38efe5ab2fd88bd4d59f75e8#egg=webassets-dev
wsgiref==0.1.2
19 changes: 14 additions & 5 deletions projecto/__init__.py
Expand Up @@ -5,7 +5,7 @@
from flask.ext.login import current_user

from .blueprints import blueprints
from .extensions import login_manager, register_assets, partials
from .extensions import login_manager, build_partials, build_js_files, build_css_files
from .models import File
from settings import APP_FOLDER, STATIC_FOLDER, TEMPLATES_FOLDER, API, LOADED_MODULES

Expand All @@ -27,7 +27,10 @@
# App stuff
@app.before_request
def before_request():
app.jinja_env.globals["partials"] = partials()
app.jinja_env.globals["DEBUG"] = app.debug
app.jinja_env.globals["partials"] = partials
app.jinja_env.globals["css_files"] = css_files
app.jinja_env.globals["js_files"] = js_files
app.jinja_env.globals["SERVER_MODE"] = app.config["SERVER_MODE"]
app.jinja_env.globals["GOOGLE_ANALYTICS_DOMAIN"] = app.config["GOOGLE_ANALYTICS_DOMAIN"]
app.jinja_env.globals["GOOGLE_ANALYTICS_ID"] = app.config["GOOGLE_ANALYTICS_ID"]
Expand Down Expand Up @@ -70,6 +73,12 @@ def mainapp():
meta["url_prefix"] = api_route_base + meta["url_prefix"]
app.register_blueprint(api_module.blueprint, **meta)

# asset stuffs
# needs to be after all the blueprints are registered.
register_assets(app)
# This needs to happen after registering blueprint
if not app.debug:
partials = build_partials(app)
css_files = None
js_files = None
else:
partials = None
css_files = build_css_files(app)
js_files = build_js_files(app)
79 changes: 34 additions & 45 deletions projecto/extensions.py
Expand Up @@ -6,7 +6,7 @@

from .models import User
from .utils import jsonify
from settings import STATIC_FOLDER, APP_FOLDER, LOADED_MODULES, UGLIFYJS_BIN
from settings import STATIC_FOLDER, APP_FOLDER, LOADED_MODULES

login_manager = LoginManager()

Expand All @@ -29,8 +29,9 @@ def unauthorized():


# Assets
from flask.ext.assets import Environment, Bundle
assets = Environment()
# The code here is mainly to automatically discover files.
# Actual production file building is done via Grunt.
# Partials, however, is built here.


def get_files_from_module(module, type, ext=None, excludes=tuple()):
Expand All @@ -48,53 +49,41 @@ def get_files_in_directory(path, ext=None, excludes=[], fname_only=False):
for root, subdir, filenames in os.walk(os.path.join(STATIC_FOLDER, path)):
for fname in filenames:
if (ext is None or fname.endswith(ext)) and fname not in excludes:
yield fname if fname_only else root[prefix_length:] + "/" + fname
yield fname if fname_only else "/static/" + root[prefix_length:] + "/" + fname


_partials = None
partials = lambda: _partials


def register_assets(app):
_css_files = ["css/base.css", "css/app.css"]
def build_css_files(app):
css_files = ["/static/css/base.css", "/static/css/app.css"]
for module in LOADED_MODULES:
for fname in get_files_from_module(module, "css", "css"):
_css_files.append("api_v1_" + module + "/css/" + fname)
with app.test_request_context():
css_files.append(url_for("api_v1_" + module + ".static", filename="css/" + fname))

return css_files

css_all = Bundle(
*_css_files,
filters="cssmin",
output="css/app.min.css"
)

_js_files = list(get_files_in_directory("js/develop", ".js", excludes=["app.min.js", "app.js"]))
def build_js_files(app):
js_files = list(get_files_in_directory("js/develop", ".js", excludes=["app.min.js", "app.js"]))
js_files.insert(0, "/static/js/develop/app.js")
for module in LOADED_MODULES:
for fname in get_files_from_module(module, "js", "js"):
_js_files.append("api_v1_" + module + "/js/" + fname)

js_all = Bundle("js/develop/app.js", *_js_files, filters="uglifyjs", output="js/app.min.js")

assets.init_app(app)
# work around bug https://github.com/miracle2k/flask-assets/issues/54
assets.app = app
assets.auto_build = app.debug
assets.debug = app.debug
assets.config["uglifyjs_bin"] = UGLIFYJS_BIN
assets.register("js_all", js_all)
assets.register("css_all", css_all)
if not app.debug:
partials_template = '<script id="{path}" type="text/ng-template">{content}</script>'
global _partials
_partials = ""
for module in LOADED_MODULES:
for fname in get_files_from_module(module, "partials", "html"):
# TODO: look at this hack.
with app.test_request_context():
path = url_for("api_v1_" + module + ".static", filename="partials/" + fname)

with open(os.path.join(APP_FOLDER, "projecto", "apiv1", module, "static", "partials", fname)) as g:
content = g.read()
_partials += partials_template.format(path=path, content=content)

css_all.build()
js_all.build()
with app.test_request_context():
js_files.append(url_for("api_v1_" + module + ".static", filename="js/" + fname))

return js_files


def build_partials(app):
partials_template = '<script id="{path}" type="text/ng-template">{content}</script>'
partials = ""
for module in LOADED_MODULES:
for fname in get_files_from_module(module, "partials", "html"):
# TODO: look at this hack.
with app.test_request_context():
path = url_for("api_v1_" + module + ".static", filename="partials/" + fname)

with open(os.path.join(APP_FOLDER, "projecto", "apiv1", module, "static", "partials", fname)) as g:
content = g.read()
partials += partials_template.format(path=path, content=content)

return partials
2 changes: 0 additions & 2 deletions requirements.txt
@@ -1,5 +1,4 @@
Flask==0.10.1
-e git+https://github.com/miracle2k/flask-assets.git@5ea49d3bab5dbb8a9ca84069642fda6505423576#egg=flask-assets-dev
Flask-Login==0.2.10
Flask-SeaSurf==0.1.22
Jinja2==2.7.2
Expand All @@ -14,5 +13,4 @@ requests==1.2.3
riak==2.0.2
riak-pb==1.4.1.1
ujson==1.33
-e git+https://github.com/miracle2k/webassets.git@5d7381c797810a6f38efe5ab2fd88bd4d59f75e8#egg=webassets-dev
wsgiref==0.1.2
3 changes: 0 additions & 3 deletions settings.py
Expand Up @@ -55,9 +55,6 @@
GOOGLE_ANALYTICS_ID = ""
GOOGLE_ANALYTICS_DOMAIN = ""

# For production
UGLIFYJS_BIN = "uglifyjs"

try:
from settings_local import *
except ImportError:
Expand Down
10 changes: 7 additions & 3 deletions templates/app.html
Expand Up @@ -67,7 +67,11 @@
{% endautoescape %}

<script src="/static/js/vendor/angular.min.js"></script>
{% assets "js_all" %}
<script type="text/javascript" src="{{ ASSET_URL }}"></script>
{% endassets %}
{% if DEBUG %}
{% for url in js_files %}
<script type="text/javascript" src="{{ url }}"></script>
{% endfor %}
{% else %}
<script type="type/javascript" src="/static/js/app.min.js" />
{% endif %}
{% endblock %}
14 changes: 11 additions & 3 deletions templates/master.html
Expand Up @@ -6,9 +6,13 @@
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>{% block title %}Projecto{% endblock %}</title>
{% assets "css_all" %}
<link rel="stylesheet" href="{{ ASSET_URL }}" />
{% endassets %}
{% if DEBUG %}
{% for url in css_files %}
<link rel="stylesheet" href="{{ url }}" />
{% endfor %}
{% else %}
<link rel="stylesheet" href="/static/css/app.min.css" />
{% endif %}
<script type="application/x-javascript">
{% if current_user.is_authenticated() %}
{% autoescape off %}
Expand Down Expand Up @@ -70,7 +74,11 @@
</script>
<script src="/static/js/vendor/statusmsg.js"></script>
<script src="https://login.persona.org/include.js"></script>
{% if DEBUG %}
<script src="/static/js/login.js"></script>
{% else %}
<script src="/static/js/login.min.js"></script>
{% endif %}
{% block end %}
{% endblock %}

Expand Down

0 comments on commit c4c3d68

Please sign in to comment.