Commits

Chris Mutel committed e4d353e

Add backup and delete buttons to database view

Comments (0)

Files changed (5)

bw2ui/bin/bw2-browser.py

 #!/usr/bin/env python
 # encoding: utf-8
 """Brightway2 database and activity browser.
-Developped by Bernhard Steubing and Chris Mutel, 2013
+Developed by Bernhard Steubing and Chris Mutel, 2013
 
 This is a command-line utility to browse, search, and filter databases.
 
         print("History exported to %(c)s%(fp)s%(R)s" % _({'fp': fp}))
 
 # SOME ADDITIONAL FUNCTIONALITY FROM THE FIRST VERSION, WHICH IS CURRENTLY NOT INCLUDED:
-# MAINLY FILTER AND UP- AND DOWNSTREAM ACTIVITIES OVER SEVERAL LEVELS 
+# MAINLY FILTER AND UP- AND DOWNSTREAM ACTIVITIES OVER SEVERAL LEVELS
     # def sort_by_name(self, unsorted_list):
     # EASIER:
     # Something like this: unsorted_list.sort(key=lambda x: x['name'])
     print data
     return render_template("edit-raw.html", data=data)
 
+
 @app.route("/view/<database>/<code>")
 def json_editor(database, code):
     if database not in databases:
         return abort(404)
     return render_template("jsoneditor.html", jsondata=json.dumps(data))
 
+
+@app.route("/delete/<database>", methods=["POST"])
+def delete_database(database):
+    if database not in databases:
+        return abort(404)
+    del databases[database]
+    return ''
+
+
+@app.route("/backup/<database>", methods=["POST"])
+def backup_database(database):
+    if database not in databases:
+        return abort(404)
+    return Database(database).backup()
+
 ###########
 ### LCA ###
 ###########
         meta=meta,
         name=name,
         depends=depends,
-        data=JsonWrapper.dumps(json_data)
+        data=JsonWrapper.dumps(json_data),
+        backup_url = url_for('backup_database', database=name),
+        delete_url = url_for('delete_database', database=name),
     )
 
 

bw2ui/web/templates/base.html

 	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
 	<script src="{{ url_for('static', filename="backgrid/backgrid.js") }}"></script>
 	<script src="{{ url_for('static', filename="backgrid/backgrid-table.js") }}"></script>
-	<!-- <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script> -->
+	<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
 	<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.9.4/jquery.dataTables.min.js"></script> -->
 	<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.9.4/css/jquery.dataTables.css" type="text/css" media="screen, projection"> -->
-	<!-- <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/flick/jquery-ui.css" type="text/css" media="screen, projection"> -->
+	<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/flick/jquery-ui.css" type="text/css" media="screen, projection">
 	<script src="http://d3js.org/d3.v3.min.js"></script>
 	{% block extrahead %}{% endblock %}
 </head>

bw2ui/web/templates/database.html

 </ul>
 {% endif %}
 
+<p>
+<div id="backup-div"><button id="backup-button" class="positive">Backup</button></div>
+<button id="delete-button" class="negative">Delete</button>
+</p>
+
 <div id="bgtable"></div>
 
+<div id="delete-confirm" title="Delete database">
+    <p>Are you sure you want to delete this database?</p>
+</div>
+
 <script type="text/javascript">
 var columns = [{
     name: "name",
 };
 
 BackgridTable(data, columns, "#bgtable", ['name'], "Filter by name", 50, callback);
+
+$(document).ready(function() {
+    $("#delete-confirm").dialog({
+        autoOpen: false,
+        modal: true,
+        buttons: {
+            "Cancel": function () {
+                $(this).dialog("close");
+            },
+            "Delete": function () {
+                var callback = function () {
+                    window.location.href = "/";
+                }
+                $.post("{{ delete_url }}", "", callback);
+            }
+        }
+    });
+});
+
+var backup_callback = function (fp) {
+    $("#backup-button").hide();
+    $("#backup-div").html("Database backup up to: " + fp);
+}
+
+$("#backup-button").click(function () {
+    $("#backup-button").text("working...");
+    $.post("{{ backup_url }}", "", backup_callback);
+});
+
+$("#delete-button").click(function () {
+    $("#delete-confirm").dialog("open");
+});
 </script>
 {% endblock %}
     author_email="cmutel@gmail.com",
     license=open('LICENSE.txt').read(),
     install_requires=["brightway2>=0.10"],
-    scripts=["bw2ui/bin/bw2-web.py", "bw2ui/bin/bw2-controller.py", "bw2ui/bin/bw2-browser.py"],
+    scripts=[
+        "bw2ui/bin/bw2-web.py",
+        "bw2ui/bin/bw2-controller.py",
+        "bw2ui/bin/bw2-browser.py"
+    ],
     url="https://bitbucket.org/cmutel/brightway2-ui",
     long_description=open('README.rst').read(),
     classifiers=[
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.