Chris Mutel avatar Chris Mutel committed 940e9af

Added database import

Comments (0)

Files changed (2)

     JsonWrapper, reset_meta
 from bw2analyzer import ContributionAnalysis, DatabaseExplorer
 from bw2calc import LCA, ParallelMonteCarlo
+from bw2data.io import EcospoldImporter, EcospoldImpactAssessmentImporter
 from flask import Flask, url_for, render_template, request, redirect, abort
 from fuzzywuzzy import process
 from jobs import JobDispatch, InvalidJob
 
 @app.route("/fp-api", methods=["POST"])
 def fp_api():
+    full = bool(request.args.get("full", False))
     path = urllib2.unquote(request.form["dir"])
     try:
         root, dirs, files = os.walk(path).next()
         files = os.listdir(root)
     data = []
     files = [x for x in files if x[0] != "."]
-    if len(files) > 20:
+    if not full and len(files) > 20:
         files = files[:20] + ["(and %s more files...)" % (len(files) - 20)]
     for dir_name in dirs:
         if dir_name[0] == ".":
         f.write(dirpath)
 
     config.reset()
+    config.is_temp_dir = False
     config.create_basic_directories()
     reset_meta()
     return "1"
     You are currently saving your work in a temporary directory that can be deleted at any time. You can let Brightway2 create a workspace in your home directory, or you can specify a different directory by typing in the full path below:
     """
 
+#################
+### Importing ###
+#################
+
+
+@app.route("/import/database", methods=["GET", "POST"])
+def import_database():
+    if request.method == "GET":
+        return render_template("import-database.html")
+    else:
+        path = urllib2.unquote(request.form["path"])
+        name = urllib2.unquote(request.form["name"])
+        EcospoldImporter().importer(path, name)
+        return "1"
+
+
+@app.route("/import/method", methods=["GET", "POST"])
+def import_method():
+    if request.method == "GET":
+        return render_template("import-method.html")
+    else:
+        path = urllib2.unquote(request.form["path"])
+        EcospoldImpactAssessmentImporter().importer(path)
+        return "1"
 
 ###################
 ### Basic views ###
 
 @app.route('/')
 def index():
-    print config.p.get("temp_dir_ok", False)
-    print config.is_temp_dir and not config.p.get("temp_dir_ok", False)
     if config.is_temp_dir and not config.p.get("temp_dir_ok", False):
-        print "Trying to redirect..."
         return redirect(url_for('start'))
     dbs = [{
         "name": key,

bw2ui/web/templates/import-database.html

+{% extends "base.html" %}
+
+{% block extrahead %}
+<link rel="stylesheet" href="{{ url_for('static', filename="jqueryFileTree/jqueryFileTree.css") }}" type="text/css" media="screen, projection">
+{% endblock %}
+
+{% block body %}
+<div id="select-path">
+  <h1>Importing a database</h1>
+  <div class="span-15 colborder">
+    <h3 id="fp-selected">1. Click to select the directory that the XML files are in, or a single XML file.</h3>
+    <p>Note: Not all files are shown...</p>
+    <div id="fp"></div>
+  </div>
+  <div class="span-8 last">
+    <form name="path-form">
+      <h3>2. Then name the database:</h3>
+      <input id="dir-file" name="name" type="text" value=""><br>
+      <input id="hidden-path" name="path" type="hidden" value="">
+      <div id="missing-name"></div>
+      <button id="submit-path" type="submit" style="">Set Brightway2 data path</button>
+    </form>
+  </div>
+</div>
+
+<div id="in-submission" class="span-24 clear">
+  <h3>Your computer is now working as fast as it can importing your new database.</h3>
+  <p>Please be patient... you will be redirected to the home page as soon as the import is finished.</p>
+</div>
+
+<script type="text/javascript">
+function fpselector (a) {
+  $("#fp-selected").text("Currently selected: " + a.attr('rel'));
+  $("#hidden-path").val(a.attr('rel'));
+};
+</script>
+<script src="{{ url_for('static', filename="jqueryFileTree/jqueryFileTree.js") }}"></script>
+<script type="text/javascript">      
+$(document).ready( function() {        
+  $("#success").hide();
+
+  $('#fp').fileTree({
+    root: '/', 
+    script: '/fp-api', 
+    folderEvent: 'click', 
+    multiFolder: false 
+    }, 
+    function(file) { 
+      $("#fp-selected").text("Currently selected: " + file);
+      $("#hidden-path").val(file);
+    }
+  );
+
+  $('form[name=path-form]').submit( function (e) {
+    e.preventDefault();
+    if (($("#dir-file").val() != '') && ($("#hidden-path").val() != '')) {
+      console.log("ready");
+      console.log($(this).serialize());
+      $("#in-submission").show();
+      $("#select-path").hide();
+      $.ajax({
+        type: "POST",
+        url: '{{ url_for('import_database') }}',
+        data: $(this).serialize(),
+        success: function () {
+          document.location.href='{{ url_for('index') }}';
+        }
+      });
+    } else {
+      $("#missing-name").html('<p style="color: red">You must first select both a file/directory and enter a name</p>');
+    }
+  });
+});
+</script>
+{% endblock %}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.