diff --git a/powerdnsadmin/routes/domainconnect.py b/powerdnsadmin/routes/domainconnect.py index 9c70b7f24..8d3738d30 100644 --- a/powerdnsadmin/routes/domainconnect.py +++ b/powerdnsadmin/routes/domainconnect.py @@ -380,6 +380,16 @@ def templates(): return render_template('dc_template.html', templates=templlist.templates) +@dc_api_bp.route('/admin/templates/schema', methods=['GET']) +@operator_role_or_allow_user_manage_dc_templates_required +@login_required +def template_schema(): + templlist = DomainConnectTemplates(template_path=Setting().get('dc_template_folder')) + if templlist.schema is not None: + return templlist.schema + else: + return abort(404) + @dc_api_bp.route('/admin/templates/providers//services/', methods=['POST']) @dc_api_bp.route('/admin/templates/new', methods=['POST']) @operator_role_or_allow_user_manage_dc_templates_required @@ -391,6 +401,8 @@ def template_edit_post(provider_id=None, service_id=None): error = None templateerror = None variables = {} + groups = [] + group_variables = {} templ = templ = { "providerId": None, "serviceId": None, @@ -403,7 +415,10 @@ def template_edit_post(provider_id=None, service_id=None): variables = DomainConnectTemplates.get_variable_names(templ, request.form) except InvalidTemplate as tex: templateerror = f"{tex}" - + groups=DomainConnectTemplates.get_group_ids(templ) + group_variables = {} + for g in groups: + group_variables[g] = DomainConnectTemplates.get_variable_names(templ, request.form, g) if request.form["_test_template"] == "true": try: templlist.validate_template(templ) @@ -423,8 +438,9 @@ def template_edit_post(provider_id=None, service_id=None): return render_template('dc_template_edit.html', new=service_id is None or provider_id is None, template_raw=request.form["_template"], template=templ, params=variables, - groups=DomainConnectTemplates.get_group_ids(templ), + groups=groups, group_values=request.form.getlist('group'), + group_variables=group_variables, records=result, error=error, templateerror=templateerror) @@ -436,9 +452,15 @@ def template_edit(provider_id, service_id): dc = DomainConnect(provider_id, service_id, template_path=Setting().get('dc_template_folder'), redir_template_records=redir_template_records) template = dc.data + groups = DomainConnectTemplates.get_group_ids(template) + group_variables = {} + for g in groups: + group_variables[g] = DomainConnectTemplates.get_variable_names(template, {}, g) return render_template('dc_template_edit.html', new=False, template=template, params=DomainConnectTemplates.get_variable_names(template, {'domain': 'example.com'}), - groups=DomainConnectTemplates.get_group_ids(template)) + groups=groups, + group_values=[], + group_variables=group_variables) @dc_api_bp.route('/admin/templates/new', methods=['GET']) @@ -529,9 +551,15 @@ def template_new(): } ] } + groups = DomainConnectTemplates.get_group_ids(template) + group_variables = {} + for g in groups: + group_variables[g] = DomainConnectTemplates.get_variable_names(template, {}, g) return render_template('dc_template_edit.html', new=True, template=template, params=DomainConnectTemplates.get_variable_names(template, {'domain': 'example.com'}), - groups=DomainConnectTemplates.get_group_ids(template)) + groups=groups, + group_values=[], + group_variables=group_variables) @dc_api_bp.route('/admin/templates/providers//services//save', methods=['POST']) diff --git a/powerdnsadmin/templates/dc_template_edit.html b/powerdnsadmin/templates/dc_template_edit.html index ead3152d4..95f983fb6 100644 --- a/powerdnsadmin/templates/dc_template_edit.html +++ b/powerdnsadmin/templates/dc_template_edit.html @@ -58,6 +58,11 @@

Variables

Reload Params  +
+ +
@@ -65,24 +70,30 @@

Variables

{% for k, v in params.items() %} {% if k != "group" %} -
- -
- -
-
- {% else %} - {% for g in groups %} -
- +
+
- +
- {% endfor %} -
+ {% else %} + {% for g in groups %} +
+ +
+ +
+
+ {% endfor %} +
{% endif %} {% endfor %}
@@ -123,6 +134,14 @@

Variables

{% endfor %} +
+
 
+ +
 
+ +
{% endif %} {% if error %} @@ -143,8 +162,7 @@

Variables

window.editor = null; - var schema = $.getJSON('https://raw.githubusercontent.com/Domain-Connect/Templates/master/template.schema', function(data) { - console.log(data); + var schema = $.getJSON("{{ url_for('domainconnect.template_schema') }}", function(data) { // some hacks to make the schema working fine with avj delete data.$schema; delete data.definitions.DomainConnectTemplate.properties.logoUrl['qt-uri-protocols']; @@ -174,6 +192,8 @@

Variables

{% endif %} } }); + updateTestDropDown(); + updateVariableVisibility(); if (document.location.hash.startsWith('#')) { let element = document.getElementById(document.location.hash.substring(1)) if (element !== null) @@ -228,6 +248,110 @@

Variables

$("#variable_post").attr('action', '#tbl_records_and_errors'); $("#variable_post").submit(); }); + {% if params and records %} + $(document.body).on("click", "#save_test_btn", function(e) { + e.stopPropagation(); + var current_template = JSON.parse(window.editor.get().text); + const testName = $("#save_test_name").val(); + if (testName == "") { + alert("Set a name for test data set."); + $("#save_test_name").focus(); + return; + } + if (current_template.testData == null) { + current_template.testData = {} + } + + current_template.testData[testName] = { + 'variables': {{ params | tojson }}, + 'results': {{ records | tojson }} + } + current_template.testData[testName].variables.group = {{ group_values | tojson }} + for(k in current_template.testData[testName].variables) { + if (current_template.testData[testName].variables[k] == '') { + delete current_template.testData[testName].variables[k]; + } + } + window.editor.set({'text': JSON.stringify(current_template, null, 2)}); + updateTestDropDown(); + }); + {% endif %} + $(document.body).on("change", ".group-checkbox", function(e) { + updateVariableVisibility(); + }); + $(document.body).on("change", ".param-input, .param-input-cbx", function(e) { + $("#tbl_records_and_errors").hide(); + }); + $(document.body).on("keydown", ".param-input", function(e) { + $("#tbl_records_and_errors").hide(); + }); + $(document.body).on("change", "#test_selection", function(e) { + $("#tbl_records_and_errors").hide(); + $(".param-input").val(""); + $("#param_domain").val("example.com"); + $(".param-input-cbx").prop('checked', false); + var current_template = JSON.parse(window.editor.get().text); + if (current_template.testData != null) { + select = $("#test_selection").find(":selected").val(); + if (select in current_template.testData && current_template.testData[select].variables != null) { + for (k in current_template.testData[select].variables) + { + $("#param_" + k).val(current_template.testData[select].variables[k]); + } + if ("group" in current_template.testData[select].variables) { + for (g in current_template.testData[select].variables.group) { + $("#group-" + current_template.testData[select].variables.group[g]).prop('checked', true); + } + } + } + } + updateVariableVisibility(); + }); + const updateTestDropDown = function() { + var current_template = JSON.parse(window.editor.get().text); + if (current_template.testData != null) { + select = $("#test_selection")[0]; + $(".test-selection-option").remove(); + for(k in current_template.testData) { + const option = document.createElement('option') + option.innerHTML = k + option.setAttribute("value", k) + option.setAttribute("class", "test-selection-option") + select.appendChild(option) + }; + } + } + const updateVariableVisibility = function() { + const all_groups = {{ groups | tojson }}; + var_inputs = $(".variable-input") + var any_grp_selected = false; + for (grp in all_groups) { + if ($("#group-" + all_groups[grp])[0].checked) { + any_grp_selected = true; + break; + } + } + + for (i = 0; i < var_inputs.length; i++) { + input_field = var_inputs[i]; + var show = false; + var any_data = false; + if (any_grp_selected) { + for (grp in all_groups) { + if (input_field.getAttribute("data-group-" + all_groups[grp]) != null) { + any_data = true; + val = $("#group-" + all_groups[grp])[0].checked; + input_field.setAttribute("data-group-" + all_groups[grp], val); + show |= val + } + } + input_field.hidden = !show && any_data; + } + else { + input_field.hidden = false; + } + } + } {% endblock %} {% block modals %}