1. Chris Mutel
  2. brightway2-web-reports


brightway2-web-reports / bw2webreports / app.py

# -*- coding: utf-8 -*-
from __future__ import division
from flask import Flask, render_template, request, abort
import json
import os

app = Flask(__name__)

def page_not_found(e):
    return "Resource not found", 404

def internal_error(e):
    return "Application error", 500

@app.route("/upload", methods=["POST"])
def upload():
    data = request.json
    if not data or not isinstance(data, dict) or "metadata" not in data \
            or not data["metadata"].get("uuid", None) \
            or not data["metadata"].get("type", None) == \
            u"Brightway2 serialized LCA report":
    filepath = os.path.join(app.config["DATA_DIR"],
        "%s.json" % data["metadata"]["uuid"])
    with open(filepath, "w") as f:
        json.dump(data, f)
    return "OK"

def report(uuid):
        data = open(os.path.join(app.config["DATA_DIR"], "%s.json" % uuid)).read()
        return render_template('404.html'), 404
    return render_template("report.html", data=data)

def status():
    return json.dumps({'reports': len(os.listdir(app.config["DATA_DIR"]))})

# TODO: Prevent large uploads
# TODO: Prevent overwriting of existing reports