Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
One step toward user-submitted URLs for mapping-sheet, #12
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Sep 27, 2019
1 parent 58470ee commit a0cce37
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion default/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


class MappingSheetOptionsForm(forms.Form):
version = forms.CharField()
url = forms.CharField()
12 changes: 6 additions & 6 deletions default/templates/default/mapping_sheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
</div>
<form method="POST" class="form-inline">{% csrf_token %}
<div class="form-group">
<label for="version">{% trans "Select an schema and version" %}:</label>
<select name="version" class="form-control">
{% for version, schema_files in versionOptions.items %}
<optgroup label="{{version}}">
{% for schema_file, url in schema_files.items %}
<option value="{{version}}-{{schema_file}}">{{ version }}-{{schema_file}}</li>
<label for="url">{% trans "Select a schema and version" %}:</label>
<select name="url" class="form-control">
{% for version, schemas in versionOptions.items %}
<optgroup label="{{ version }}">
{% for label, url in schemas.items %}
<option value="{{ url }}">{{ version }}-{{ label }}</li>
{% endfor %}
</optgroup>
{% endfor %}
Expand Down
19 changes: 8 additions & 11 deletions default/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,23 +116,20 @@ def perform_compile(request, published_date=''):


def mapping_sheet(request):
options = django_settings.OCDS_TOUCAN_SCHEMA_OPTIONS
context = {
'versionOptions': options,
'versionOptions': django_settings.OCDS_TOUCAN_SCHEMA_OPTIONS,
}

if request.method == 'POST':
form = MappingSheetOptionsForm(request.POST)
if form.is_valid():
file_type, ocds_version = form.cleaned_data['version'].split('-', 1)
if file_type in options and ocds_version in options[file_type]:
io = StringIO()
schema = jsonref.load_uri(options[file_type][ocds_version])
mapping_sheet_method(schema, io, infer_required=True)

response = HttpResponse(io.getvalue(), content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="mapping-sheet.csv"'
return response
io = StringIO()
schema = jsonref.load_uri(form.cleaned_data['url'])
mapping_sheet_method(schema, io, infer_required=True)

response = HttpResponse(io.getvalue(), content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="mapping-sheet.csv"'
return response

context['error'] = _('Invalid option! Please verify and try again')

Expand Down
2 changes: 1 addition & 1 deletion locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ msgstr ""
"su versión en planilla."

#: default/templates/default/mapping_sheet.html:13
msgid "Select an schema and version"
msgid "Select a schema and version"
msgstr "Seleccione un esquema y versión"

#: default/templates/default/mapping_sheet.html:22
Expand Down
8 changes: 7 additions & 1 deletion tests/test_mapping_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@ class MappingSheetTestCase(TestCase):

def test_get(self):
response = self.client.get(self.url)

self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')

def test_post_with_version(self):
response = self.client.post(self.url, {'version': '1.1-Release'})
response = self.client.post(self.url, {
'url': 'https://standard.open-contracting.org/latest/en/release-schema.json',
})

self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'text/csv')
self.assertEqual(response['Content-Disposition'], 'attachment; filename="mapping-sheet.csv"')
self.assertEqual(response.content.decode('utf-8').replace('\r\n', '\n'), read('results/mapping-sheet.csv'))

def test_post_without_version(self):
response = self.client.post(self.url)

self.assertEqual(response.status_code, 200)
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')

content = response.content.decode('utf-8')

self.assertIn('<div class="alert alert-warning">', content)
self.assertIn('Invalid option! Please verify and try again', content)

0 comments on commit a0cce37

Please sign in to comment.