Browse Source

Added ability to specify an alternate template/assets dir to override default ones file-by-file.
refs #16

Jocelyn Delande 9 years ago
parent
commit
7b324e6fdd
2 changed files with 46 additions and 12 deletions
  1. 20 0
      README.md
  2. 26 12
      backend.py

+ 20 - 0
README.md

@@ -52,3 +52,23 @@ Drop the database
     $ rm db.sqlite3
 
 What else ?
+
+Customizing appearance
+======================
+
+Wether you like or not balloons, you may want to override some templates and/or
+static files.
+
+You can mention a `CUSTOMIZATION_DIR` as environ variable. In that dir, you can
+create *assets* and *views* subdirs, containing files with the name of the
+original files you want to override from default *assets* and *views*.
+
+For example to override only *main.css* and *base.tpl*, you would set
+`CUSTOMIZATION_DIR=/home/alice/my-fancy-isp-theme` and use the following directory
+layout :
+
+    /home/alice/my-fancy-isp-theme/
+    ├── assets
+    │   └── main.css
+    └── views
+        └── base.tpl

+ 26 - 12
backend.py

@@ -8,9 +8,9 @@ import urlparse
 import datetime
 import json
 from email import utils
-from os.path import join, dirname
-
+from os.path import join, dirname, exists
 
+import bottle
 from bottle import route, run, static_file, request, template, FormsDict, redirect, response, Bottle
 
 URL_PREFIX = os.environ.get('URL_PREFIX', '')
@@ -210,8 +210,11 @@ def wifi_form_thanks():
 
 @app.route('/assets/<filename:path>')
 def send_asset(filename):
-    return static_file(filename, root=join(dirname(__file__), 'assets'))
-
+     for i in STATIC_DIRS:
+          path = join(i, filename)
+          if exists(path):
+               return static_file(filename, root=i)
+     raise bottle.HTTPError(404)
 
 @app.route('/legal')
 def legal():
@@ -368,6 +371,9 @@ DEBUG = bool(os.environ.get('DEBUG', False))
 LISTEN_ADDR= os.environ.get('BIND_ADDR', 'localhost')
 LISTEN_PORT= int(os.environ.get('BIND_PORT', 8080))
 URL_PREFIX = os.environ.get('URL_PREFIX', '').strip('/')
+CUSTOMIZATION_DIR = os.environ.get('CUSTOMIZATION_DIR', None)
+STATIC_DIRS = [join(dirname(__file__), 'assets')]
+
 
 if __name__ == '__main__':
     if len(sys.argv) > 1:
@@ -376,11 +382,19 @@ if __name__ == '__main__':
         if sys.argv[1] == 'buildgeojson':
             build_geojson()
     else:
-         if URL_PREFIX:
-              print('Using url prefix "{}"'.format(URL_PREFIX))
-              root_app = Bottle()
-              root_app.mount('/{}/'.format(URL_PREFIX), app)
-              run(root_app, host=LISTEN_ADDR, port=LISTEN_PORT, reloader=DEBUG)
-         else:
-              run(app, host=LISTEN_ADDR, port=LISTEN_PORT, reloader=DEBUG)
-         DB.close()
+        if URL_PREFIX:
+            print('Using url prefix "{}"'.format(URL_PREFIX))
+            root_app = Bottle()
+            root_app.mount('/{}/'.format(URL_PREFIX), app)
+            run(root_app, host=LISTEN_ADDR, port=LISTEN_PORT, reloader=DEBUG)
+
+        if CUSTOMIZATION_DIR:
+             custom_templates_dir = join(CUSTOMIZATION_DIR, 'views')
+             if exists(custom_templates_dir):
+                 bottle.TEMPLATE_PATH.insert(0, custom_templates_dir)
+             custom_assets_dir = join(CUSTOMIZATION_DIR, 'assets')
+             if exists(custom_assets_dir):
+                 STATIC_DIRS.insert(0, custom_assets_dir)
+
+        run(app, host=LISTEN_ADDR, port=LISTEN_PORT, reloader=DEBUG)
+        DB.close()