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

Commit

Permalink
[WIP]#26
Browse files Browse the repository at this point in the history
  • Loading branch information
romifz committed Jan 28, 2020
1 parent dc476fa commit 5bdd989
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 60 deletions.
93 changes: 89 additions & 4 deletions default/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,57 @@ def _get_extension_keys(data):
yield key


class OptionClassCheckboxSelectMultiple(forms.CheckboxSelectMultiple):
template_name = 'default/widget/checkbox-multiple.html'
option_template_name = 'default/widget/checkbox.html'


class MappingSheetOptionsForm(forms.Form):
type = forms.ChoiceField(choices=(('select', _('Select a schema and version')),
('url', _('Provide a URL')),
('file', _('Upload a file')),
('extension', _('For an OCDS Extension'))),
initial='select',
error_messages={'required': _('Please choose an operation type')})
select_url = forms.URLField(required=False, label=_('Select a schema and version'))
custom_url = forms.URLField(required=False, label=_('Provide the URL to a custom schema below'))
custom_file = forms.FileField(required=False, label=_('Upload a file'))
version = forms.ChoiceField(required=False, label=_('Please select an OCDS version'),
select_url = forms.ChoiceField(required=False,
label=_('Select a schema and version'),
choices=(
('1.1', (
('https://standard.open-contracting.org/1.1/en/release-schema.json',
'(1.1) Release'),
('https://standard.open-contracting.org/1.1/en/release-package-schema.json',
'(1.1) Release Package'),
('https://standard.open-contracting.org/1.1/en/record-package-schema.json',
'(1.1) Record Package'),
)),
('1.1 (Español)', (
('http://standard.open-contracting.org/1.1/es/release-schema.json',
'(1.1) (Español) Release'),
('http://standard.open-contracting.org/1.1/es/release-schema.json',
'(1.1) (Español) Paquete de Release'),
('http://standard.open-contracting.org/1.1/es/record-package-schema.json',
'(1.1) (Español) Paquete de Record'),
)),
('1.0', (
('https://standard.open-contracting.org/schema/1__0__3/release-schema.json',
'(1.0) Release'),
(
'https://standard.open-contracting.org/schema/1__0__3/'
+ 'release-package-schema.json',
'(1.0) Release Package'),
(
'https://standard.open-contracting.org/schema/1__0__3/'
+ 'record-package-schema.json',
'(1.0) Record Package'),
)),
),
widget=forms.Select(attrs={'class': 'form-control'}))
custom_url = forms.URLField(required=False,
label=_('Provide the URL to a custom schema below'))
custom_file = forms.FileField(required=False,
label=_('Upload a file'))
version = forms.ChoiceField(required=False,
label=_('Please select an OCDS version'),
choices=[(tag, tag.replace('__', '.')) for tag in _get_tags()],
widget=forms.Select(attrs={'class': 'form-control'}))

Expand Down Expand Up @@ -59,3 +99,48 @@ def clean(self):
def get_extension_fields(self):
# this method returns a list of BoundField and it is used in the template
return list(filter(lambda field: field.name.startswith('extension_url_'), [field for field in self]))


class UnflattenOptionsForm(forms.Form):
schema = forms.ChoiceField(required=False,
label=_('Schema version'),
initial='1.1',
choices=(
('https://standard.open-contracting.org/1.1/en/release-schema.json',
'1.1'),
('http://standard.open-contracting.org/1.1/es/release-schema.json',
'1.1 (Español)'),
('https://standard.open-contracting.org/schema/1__0__3/release-schema.json',
'1.0')),
widget=forms.Select(attrs={'class': 'form-control'}))
output_format = forms.MultipleChoiceField(required=True,
label=_('Output formats'),
initial=('csv', 'xlsx'),
choices=(('csv', 'CSV'),
('xlsx', 'Excel')),
widget=OptionClassCheckboxSelectMultiple(
attrs={'option_class': 'checkbox-inline'})
)
use_titles = forms.ChoiceField(required=True,
label=_('Use titles instead of field names'),
choices=(('yes', _('Yes')), ('no', _('No'))),
widget=forms.Select(attrs={'class': 'form-control'}),
initial='no')
filter_field = forms.CharField(required=False,
help_text=_('Choose a field'),
widget=forms.TextInput(attrs={'class': 'form-control'}))
filter_value = forms.CharField(required=False,
help_text=_('Input a value'),
widget=forms.TextInput(attrs={'class': 'form-control'}))
preserve_fields = forms.CharField(required=False,
label=_('Include the following fields only'),
widget=forms.Textarea(attrs={'class': 'form-control'}),
help_text=_('Specify each path and field separated by a newline'))
remove_empty_schema_columns = forms.ChoiceField(required=True,
label=_('Remove empty schema columns'),
choices=(('yes', _('Yes')), ('no', _('No'))),
initial='no',
widget=forms.Select(attrs={'class': 'form-control'}))

def clean_output_format(self):
return 'all' if len(self.cleaned_data['output_format']) > 1 else self.cleaned_data['output_format'][0]
6 changes: 6 additions & 0 deletions default/static/css/uploader.css
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,9 @@
#publishedDate {
width: 220px;
}
.options-link {
padding: 0 5px;
}
.options-link:hover, .options-link:active {
text-decoration: None;
}
61 changes: 61 additions & 0 deletions default/static/js/to-spreadsheet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
(function(){
var fileUploadObj = $('#fileupload'), dropArea = $('.drop-area'), uploadButton = $('#upload-button'),
processingModal = $('#processing-modal');
var selectedFile;

function transformInServer() {
// mask the page
processingModal.modal('show');
$.ajax('/to-spreadsheet/go/', {
'dataType': 'json',
'data': $('#unflatten-options').serialize(),
'method': 'POST'
})
.done(showLinksToResults)
.fail(showErrors)
;
}

function showLinksToResults(data) {
if (data.xlsx) {
$('.response-success .download-xlsx').attr('href', data.xlsx.url);
$('.response-success .file-size-xlsx').html(utils.readableFileSize(data.xlsx.size));
}
if (data.xlsx) {
$('.response-success .download-csv').attr('href', data.csv.url);
$('.response-success .file-size-csv').html(utils.readableFileSize(data.csv.size));
}
$('.response-success').removeClass('hidden');
}

function showErrors() {

}

/* listen when a file is selected or dropped in the designated area */
fileUploadObj.bind('fileuploadadd', function (e, data) {
dropArea.removeClass('empty');
// hide default message
dropArea.children('.msg-empty').addClass('hidden');
// fill area with file's name and size
dropArea.children('file-selected')
.prepend(data.files[0].name)
.children('small')
.html(utils.readableFileSize(data.files[0].size))
;
// show "Start" button
uploadButton.removeClass('hidden');
selectedFile = data;
});

/* "Start" button click listener */
uploadButton.click(function () {
// we don't want to add any more files
fileUploadObj.fileupload('disable');

selectedFile.submit() // send the file
.done()
;
});

})();
10 changes: 1 addition & 9 deletions default/templates/default/mapping_sheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,7 @@
class="panel-collapse collapse {% if form.type.value == 'select' %}in{% endif %} toucan-nav-select">
<div class="form-group {% if form.select_url.errors %}has-error{% endif %}">
<label for="url">{{ form.select_url.label }}</label>
<select name="select_url" class="form-control">
{% for version, schemas in versionOptions.items %}
<optgroup label="{{ version }}">
{% for label, url in schemas.items %}
<option value="{{ url }}">{{ version }}-{{ label }}</option>
{% endfor %}
</optgroup>
{% endfor %}
</select>
{{ form.select_url }}
{% if form.select_url.errors %}
<div class="help-block">{{ form.select_url.errors }}</div>{% endif %}
</div>
Expand Down
70 changes: 70 additions & 0 deletions default/templates/default/snippets/unflatten-options-modal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{% load i18n %}

<div class="modal fade" tabindex="-1" role="dialog" id="unflatten-options-modal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
</button>
<h4 class="modal-title">{% trans "Options" %}</h4>
</div>
<div class="modal-body form-horizontal">
<form id="unflatten-options">
<input type="hidden" name="csrfmiddlewaretoken" value="{{csrf_token}}">
<div class="form-group">
<label for="{{ form.schema_version.id_for_label }}" class="col-sm-4">
{{ form.schema_version.label }}
</label>
<div class="col-sm-8">
{{ form.schema_version }}
</div>
</div>
<div class="form-group">
<label for="{{ form.output_format.id_for_label }}" class="col-sm-4">
{{ form.output_format.label }}
</label>
<div class="col-sm-8">
{{ form.output_format }}
</div>
</div>
<div class="form-group">
<label for="{{ form.use_titles.id_for_label }}"
class="col-sm-4">{{ form.use_titles.label }}</label>
<div class="col-sm-8">
{{ form.use_titles }}
</div>
</div>
<div class="form-group">
<label class="col-sm-4">{% trans "Filter data by field" %}</label>
<div class="col-sm-8">
{{ form.filter_field }}
<span class="help-block">{{ form.filter_field.help_text }}</span>
{{ form.filter_value }}
<span class="help-block"> {{ form.filter_value.help_text }}</span>
</div>
</div>
<div class="form-group">
<label for="{{ form.preserve_fields.id_for_label }}" class="col-sm-4">
{{ form.preserve_fields.label }}
</label>
<div class="col-sm-8">
{{ form.preserve_fields }}
<span class="help-block"> {{ form.preserve_fields.help_text }}</span>
</div>
</div>
<div class="form-group">
<label for="{{ form.remove_empty_schema_columns.id_for_label }}" class="col-sm-4">
{{ form.remove_empty_schema_columns.label }}
</label>
<div class="col-sm-8">
{{ form.remove_empty_schema_columns }}
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">{% trans "Close" %}</button>
</div>
</div>
</div>
</div>
49 changes: 35 additions & 14 deletions default/templates/default/to-spreadsheet.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
</p>
</div>
</div>
<form
id="fileupload"
method="POST"
<form
id="fileupload"
method="POST"
action="/upload/"
data-form-data='{"csrfmiddlewaretoken": "{{csrf_token}}", "type": "release-package"}'
data-auto-upload="false"
Expand All @@ -48,16 +48,28 @@
<div class="drop-area empty single">
<span class="file-selector-empty">
<input type="file" name="file" id="file" accept=".json" multiple/>
<span class="drop-area-msg">
<span class="msg-empty">
<label for="file">{% trans "Add a file" %}</label>
{% trans "or drag and drop here." %}
</span>
<span class="file-selected">

</span>
</span>
</div>
<button type="button" class="btn btn-primary pull-right hidden" id="upload-button">
<i class="glyphicon glyphicon-cloud-upload"></i>
<span>{% trans "Start" %}</span>
</button>
<div class="actions">
<div class="buttons">
<button type="button" class="btn btn-default" id="options-button"
data-toggle="modal" data-target="#unflatten-options-modal">
<i class="glyphicon glyphicon-cog"></i>
<span>{% trans "Options" %}</span>
</button>
<button type="button" class="btn btn-primary pull-right hidden" id="upload-button">
<i class="glyphicon glyphicon-cloud-upload"></i>
<span>{% trans "Start" %}</span>
</button>
</div>
</div>
</form>
<!-- Processing modal -->
<div id="processing-modal" class="modal fade" tabindex="-1" role="dialog" data-backdrop="static" data-keyboard="false">
Expand All @@ -73,6 +85,7 @@
</div>
</div>
</div>
{% include "default/snippets/unflatten-options-modal.html" %}
{% endblock %}

{% block scripts %}
Expand All @@ -86,7 +99,7 @@

$('#fileupload').bind('fileuploadadd', function(e, data){
$('.drop-area').removeClass('empty');
$('.drop-area-msg').html('<strong>{% trans "Added:" %}</strong> ' {# not very nice to do this here #}
$('.drop-area-msg').html(
+ data.files[0].name
+ ' <small>('
+ utils.readableFileSize(data.files[0].size)
Expand All @@ -102,12 +115,20 @@
.submit()
.done(function(){
$('#processing-modal').modal('show');
$.ajax('/to-spreadsheet/go/', { 'dataType': 'json' })
$.ajax('/to-spreadsheet/go/', {
'dataType': 'json',
'data': $('#unflatten-options').serialize(),
'method': 'POST'
})
.done(function(data){
$('.response-success .download-xlsx').attr('href', data.xlsx.url);
$('.response-success .download-csv').attr('href', data.csv.url);
$('.response-success .file-size-xlsx').html(utils.readableFileSize(data.xlsx.size));
$('.response-success .file-size-csv').html(utils.readableFileSize(data.csv.size));
if (data.xlsx) {
$('.response-success .download-xlsx').attr('href', data.xlsx.url);
$('.response-success .file-size-xlsx').html(utils.readableFileSize(data.xlsx.size));
}
if (data.xlsx) {
$('.response-success .download-csv').attr('href', data.csv.url);
$('.response-success .file-size-csv').html(utils.readableFileSize(data.csv.size));
}
$('.response-success').removeClass('hidden');
})
.fail(function(){
Expand Down
15 changes: 15 additions & 0 deletions default/templates/default/widget/checkbox-multiple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% with id=widget.attrs.id %}
<div{% if id %} id="{{ id }}"{% endif %}{% if widget.attrs.class %} class="{{ widget.attrs.class }}"{% endif %}>
{% for group, options, index in widget.optgroups %}
{% if group %}
<div>{{ group }}<div{% if id %} id="{{ id }}_{{ index }}"{% endif %}>
{% endif %}
{% for option in options %}
{% include option.template_name with widget=option %}
{% endfor %}
{% if group %}
</div></div>
{% endif %}
{% endfor %}
</div>
{% endwith %}
5 changes: 5 additions & 0 deletions default/templates/default/widget/checkbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<label {% if widget.attrs.id %}for="{{ widget.attrs.id }}" {% endif %}
{% if widget.attrs.option_class %} class="{{ widget.attrs.option_class }}"{% endif %}>
{% include "django/forms/widgets/input.html" %}
{{ widget.label }}
</label>
Loading

0 comments on commit 5bdd989

Please sign in to comment.