Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validator file options #158

Merged
merged 2 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions bsyncviewer/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@ class LoadXMLFile(forms.Form):


class LoadXMLExample(forms.Form):
schema_version = forms.ModelChoiceField(queryset=Schema.objects.all(),
empty_label=None,
to_field_name='version',
initial=DEFAULT_SCHEMA_VERSION)
schema_version = forms.ModelChoiceField(
queryset=Schema.objects.all(),
empty_label=None,
to_field_name='version',
initial=DEFAULT_SCHEMA_VERSION,
widget=forms.Select(attrs={'id': 'schema_dropdown'})
)

file_name = forms.FilePathField(
path=os.path.join(
os.path.abspath(os.path.dirname(__file__)), 'lib', 'validator', 'examples', 'schema' + str(DEFAULT_SCHEMA_VERSION)
os.path.abspath(os.path.dirname(__file__)), 'lib', 'validator', 'examples'
),
recursive=False,
recursive=True,
match=r'\.xml$',
allow_files=True
allow_files=True,
widget=forms.Select(attrs={'id': 'file_dropdown'})
)

form_type = forms.CharField(widget=forms.HiddenInput(), initial='example')


Expand Down
27 changes: 27 additions & 0 deletions bsyncviewer/templates/validator.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,30 @@ <h3 class="panel-title">Explore Examples</h3>
</div>

{% endblock content %}

{% block base_extra_script %}
{{ schema_to_examples|json_script:"schema_to_examples" }}
<script>
/*
Set the file options in load_xml_example_form to the right files for the selected schema verisons.
Do so on page load and every change of the schema version drop down.
*/
function setFileOptions(){
fileDropdown = document.getElementById("file_dropdown")
schemaToExampleScript = document.getElementById('schema_to_examples')
schemaToExample = JSON.parse(schemaToExampleScript.textContent)

while (fileDropdown.options.length > 0) {
fileDropdown.remove(0);
}

schemaToExample[schema_dropdown.value].forEach(([abs_path, name]) => {
fileDropdown.add(new Option(abs_path, name))
})
}

schemaDropdown = document.getElementById("schema_dropdown")
schema_dropdown.addEventListener("change", setFileOptions)
setFileOptions();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noice

</script>
{% endblock base_extra_script %}
21 changes: 21 additions & 0 deletions bsyncviewer/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
import os
import pathlib
import subprocess
import tempfile
import zipfile
from typing import Dict, List

import semantic_version
from bsyncviewer import forms
Expand Down Expand Up @@ -412,10 +414,29 @@ def measures(request, version):
return render(request, 'enumerations.html', context)


def get_schema_to_examples() -> Dict[str, List[pathlib.Path]]:
"""
Create schema examples dictionary.

Create a dictionary were the keys are the schemas versions
and the values are a list of example files for that schema version

:return: dict
"""
validator_dir = pathlib.Path(__file__).parent / 'lib' / 'validator' / 'examples'

schema_to_examples = {}
for schema_dir in validator_dir.glob("*"):
schema_to_examples[schema_dir.name.replace("schema", "", 1)] = [(p.name, str(p)) for p in schema_dir.glob("*.xml")]

return schema_to_examples


def validator(request):
context = {
'load_xml_file_form': forms.LoadXMLFile(),
'load_xml_example_form': forms.LoadXMLExample(),
'schema_to_examples': get_schema_to_examples(),
'a_var': ''
}

Expand Down