Source

brightway2-ui / bw2ui / web / app.py

Diff from to

bw2ui/web/app.py

-# -*- coding: utf-8 -*
-from brightway2 import config, databases, methods, Database, Method
+# -*- coding: utf-8 -*-
+import base64
+from brightway2 import config, databases, methods, Database, Method, \
+    JsonWrapper
 from bw2analyzer import ContributionAnalysis
 from bw2calc import LCA
-from flask import Flask, url_for, render_template
-import base64
+from flask import Flask, url_for, render_template, request, redirect, abort
 import json
+import os
 
 app = Flask(__name__)
 
 
-def template_context():
-    return {
-        'blueprint_screen': url_for('static', filename="blueprint/screen.css"),
-        'blueprint_print': url_for('static', filename="blueprint/print.css"),
-        'blueprint_ie': url_for('static', filename="blueprint/ie.css"),
-    }
+@app.errorhandler(404)
+def page_not_found(e):
+    # Log error?
+    return render_template('404.html'), 404
+
+
+@app.errorhandler(500)
+def internal_error(e):
+    # Log error?
+    return render_template('500.html'), 500
 
 
 @app.route('/')
 def index():
-    context = template_context()
-    print context
-    return "Success"
+    context = {
+        'databases': databases,
+        'methods': methods,
+        'config': config
+        }
+    if config.is_temp_dir:
+        context["redirect"] = url_for(start_bw)
     return render_template("index.html", **context)
 
 
+@app.route('/error')
+def error_t():
+    abort(404)
+
+
 @app.route('/calculate/lca')
 @app.route('/calculate/lca/<process>/<method>')
 def lca(process=None, method=None):
-    context = template_context()
+    context = {}
     if process:
         method = eval(base64.urlsafe_b64decode(str(method)), None, None)
         process = eval(base64.urlsafe_b64decode(str(process)), None, None)
         return render_template("lca.html", **context)
     else:
         return "No parameters"
+
+import uuid
+
+
+def get_job_id():
+    return uuid.uuid4().hex
+
+
+@app.route('/start', methods=["GET", "POST"])
+def start_bw():
+    """Start Brightway"""
+    job_id = get_job_id()
+    if request.method == "GET":
+        pass
+    elif not request.form["confirm"] == "false":
+        return redirect(url_for(index) + "?temp_dir_ok=True")
+    # Starting Brightway
+    # If POST[action] = "get_biosphere":
+    set_job_status(job_id, {"action": "biosphere-import", "finished": False})
+    # Download file to disk
+    DatabaseImporter().importer(biosphere)
+    set_job_status(job_id, {"action": "biosphere-import", "finished": True})
+
+    # Step two
+    # Import IA methods
+    # Can also be from dropbox
+
+    # Step three
+    # Import Ecoinvent or other LCI database
+
+    # Step four
+    # There is no step four!
+    # Actually, there is: go to the normal homepage
+
+jobs_dir = config.request_dir("jobs")
+
+
+def set_job_status(job, status):
+    filepath = os.path.join(jobs_dir, "%s.json" % job)
+    JsonWrapper.dump(status, filepath)
+
+# Normal homepage:
+# Think of what should be here
+# At a minimum, a table of databases
+# and IA methods
+# A quick entry to LCA (define function units)
+
+# use werkzeug.utils.secure_filename to check uploaded file names
+# http://werkzeug.pocoo.org/docs/utils/
+
+# to send static files not from 'static': send_from_directory
+# http://flask.pocoo.org/docs/api/#flask.send_from_directory
+# http://stackoverflow.com/questions/9513072/more-than-one-static-path-in-local-flask-instance