Skip to content

Commit

Permalink
Merge branch '1696'
Browse files Browse the repository at this point in the history
  • Loading branch information
r39132 committed Jul 29, 2016
2 parents cda943e + 3deb9de commit b9af444
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
33 changes: 33 additions & 0 deletions airflow/www/templates/airflow/variable_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{#
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

#}
{% extends 'admin/model/list.html' %}

{% block model_menu_bar %}
{% if admin_view.verbose_name_plural %}
<h2>{{ admin_view.verbose_name_plural|title }}</h2>
{% endif %}
<form class="form-inline" action="{{ url_for("airflow.varimport") }}" method=post enctype=multipart/form-data style="margin-top: 50px;">
{% if csrf_token %}
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
{% endif %}
<input class="form-control" type="file" name="file">
<input class="btn btn-default" type="submit" value="Import Variables"/>
</form>
<hr/>
{{ super() }}
{% endblock %}
39 changes: 38 additions & 1 deletion airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from sqlalchemy import or_, desc, and_, union_all

from flask import (
redirect, url_for, request, Markup, Response, current_app, render_template)
redirect, url_for, request, Markup, Response, current_app, render_template, make_response)
from flask_admin import BaseView, expose, AdminIndexView
from flask_admin.contrib.sqla import ModelView
from flask_admin.actions import action
Expand Down Expand Up @@ -1612,6 +1612,20 @@ def variables(self, form):
return ("Error: form airflow/variables/{}.html "
"not found.").format(form), 404

@expose('/varimport', methods=["GET", "POST"])
@login_required
@wwwutils.action_logging
def varimport(self):
try:
out = str(request.files['file'].read())
d = json.loads(out)
except Exception:
flash("Missing file or syntax error.")
else:
for k, v in d.items():
models.Variable.set(k, v, serialize_json=isinstance(v, dict))
flash("{} variable(s) successfully updated.".format(len(d)))
return redirect('/admin/variable')

class HomeView(AdminIndexView):
@expose("/")
Expand Down Expand Up @@ -1944,6 +1958,7 @@ class KnowEventTypeView(wwwutils.DataProfilingMixin, AirflowModelView):
class VariableView(wwwutils.LoginMixin, AirflowModelView):
verbose_name = "Variable"
verbose_name_plural = "Variables"
list_template = 'airflow/variable_list.html'

def hidden_field_formatter(view, context, model, name):
if should_hide_value_for_key(model.key):
Expand Down Expand Up @@ -1972,6 +1987,28 @@ def hidden_field_formatter(view, context, model, name):
'val': hidden_field_formatter
}

# Default flask-admin export functionality doesn't handle serialized json
@action('varexport', 'Export', None)
def action_varexport(self, ids):
V = models.Variable
session = settings.Session()
qry = session.query(V).filter(V.id.in_(ids)).all()
session.close()

var_dict = {}
d = json.JSONDecoder()
for var in qry:
val = None
try:
val = d.decode(var.val)
except:
val = var.val
var_dict[var.key] = val

response = make_response(json.dumps(var_dict, sort_keys=True, indent=4))
response.headers["Content-Disposition"] = "attachment; filename=variables.json"
return response

def on_form_prefill(self, form, id):
if should_hide_value_for_key(form.key.data):
form.val.data = '*' * 8
Expand Down

0 comments on commit b9af444

Please sign in to comment.