def download(iuid, ext): """Download the content of the dataset as JSON or CSV file. This is for use in the HTML pages, not for API calls. """ try: dataset = get_dataset(iuid) except ValueError as error: utils.flash_error(str(error)) return flask.redirect(utils.url_referrer()) if not allow_view(dataset): utils.flash_error("View access to dataset is not allowed.") return flask.redirect(utils.url_referrer()) if not dataset.get("_attachments", None): utils.flash_error("Dataset does not contain any data.") return flask.redirect(utils.url_referrer()) if ext == "json": outfile = flask.g.db.get_attachment(dataset, "data.json") response = flask.make_response(outfile.read()) response.headers.set("Content-Type", constants.JSON_MIMETYPE) elif ext == "csv": outfile = flask.g.db.get_attachment(dataset, "data.csv") response = flask.make_response(outfile.read()) response.headers.set("Content-Type", constants.CSV_MIMETYPE) else: utils.flash_error("Invalid file type requested.") return flask.redirect(utils.url_referrer()) slug = utils.slugify(dataset['title']) response.headers.set("Content-Disposition", "attachment", filename=f"{slug}.{ext}") return response
def serve(iuid): "Return graphic information, update it, or delete it." try: graphic = get_graphic(iuid) except ValueError as error: flask.abort(http.client.NOT_FOUND) if utils.http_GET(): if not allow_view(graphic): flask.abort(http.client.FORBIDDEN) set_links(graphic) return utils.jsonify(graphic, schema=flask.url_for("api_schema.graphic", _external=True)) elif utils.http_POST(csrf=False): if not allow_edit(graphic): flask.abort(http.client.FORBIDDEN) try: data = flask.request.get_json() with GraphicSaver(graphic) as saver: try: saver.set_title(data["title"]) except KeyError: pass try: saver.set_description(data["description"]) except KeyError: pass try: saver.set_public(data["public"]) except KeyError: pass try: saver.set_specification(data["specification"]) except KeyError: pass except ValueError as error: return str(error), http.client.BAD_REQUEST graphic = saver.doc set_links(graphic) return utils.jsonify(graphic, schema=flask.url_for("api_schema.graphic", _external=True)) elif utils.http_DELETE(): if not allow_delete(graphic): flask.abort(http.client.FORBIDDEN) flask.g.db.delete(graphic) for log in utils.get_logs(graphic["_id"], cleanup=False): flask.g.db.delete(log) return "", http.client.NO_CONTENT
def get_graphics(dataset): """Get the graphics entities the dataset is used for. Exclude those this user is not allowed to view. """ from datagraphics.graphic import allow_view result = [] for row in flask.g.db.view("graphics", "dataset", key=dataset["_id"], include_docs=True): if allow_view(row.doc): flask.g.cache[row.doc["_id"]] = row.doc result.append(row.doc) return sorted(result, key=lambda g: g["title"])
def logs(iuid): "Display the log records of the given dataset." try: dataset = get_dataset(iuid) except ValueError as error: utils.flash_error(str(error)) return flask.redirect(utils.url_referrer()) if not allow_view(dataset): utils.flash_error("View access to dataset not allowed.") return flask.redirect(utils.url_referrer()) return flask.render_template( "logs.html", title=f"Dataset {dataset['title'] or 'No title'}", cancel_url=flask.url_for(".display", iuid=iuid), logs=utils.get_logs(iuid))
def copy(iuid): "Copy the dataset, including its data content." try: dataset = get_dataset(iuid) except ValueError as error: utils.flash_error(str(error)) return flask.redirect(utils.url_referrer()) if not allow_view(dataset): utils.flash_error("View access to dataset not allowed.") return flask.redirect(flask.url_for(".display", iuid=iuid)) try: with DatasetSaver() as saver: saver.copy(dataset) except ValueError as error: utils.flash_error(str(error)) return flask.redirect(utils.url_referrer()) return flask.redirect(flask.url_for(".display", iuid=saver.doc["_id"]))
def logs(iuid): "Return all log entries for the given graphic." try: graphic = get_graphic(iuid) except ValueError as error: flask.abort(http.client.NOT_FOUND) if not allow_view(graphic): flask.abort(http.client.FORBIDDEN) entity = { "type": "graphic", "iuid": iuid, "href": flask.url_for("api_graphic.serve", iuid=iuid, _external=True) } return utils.jsonify( { "entity": entity, "logs": utils.get_logs(graphic["_id"]) }, schema=flask.url_for("api_schema.logs", _external=True))
def data(iuid): "Display the data contents of the dataset." try: dataset = get_dataset(iuid) except ValueError as error: utils.flash_error(str(error)) return flask.redirect(flask.url_for("home")) if not allow_view(dataset): utils.flash_error("View access to dataset not allowed.") return flask.redirect(utils.url_referrer()) outfile = flask.g.db.get_attachment(dataset, "data.json") data = json.load(outfile) max_records = flask.current_app.config["MAX_RECORDS_INSPECT"] if len(data) > max_records: data = data[:max_records] utils.flash_message( f"Only the first {max_records} records are displayed.") return flask.render_template("dataset/data.html", dataset=dataset, data=data)
def display(iuid): "Display the dataset." try: dataset = get_dataset(iuid) except ValueError as error: utils.flash_error(str(error)) return flask.redirect(flask.url_for("home")) if not allow_view(dataset): utils.flash_error("View access to dataset not allowed.") return flask.redirect(utils.url_referrer()) storage = sum( [s['length'] for s in dataset.get('_attachments', {}).values()]) return flask.render_template("dataset/display.html", dataset=dataset, graphics=get_graphics(dataset), storage=storage, am_owner=am_owner(dataset), allow_edit=allow_edit(dataset), allow_delete=allow_delete(dataset), possible_delete=possible_delete(dataset), commands=get_commands(dataset))