Skip to content

Commit

Permalink
Merge ac610d0 into cb37761
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-gaia committed Oct 20, 2016
2 parents cb37761 + ac610d0 commit 7fe14ae
Show file tree
Hide file tree
Showing 25 changed files with 1,481 additions and 367 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
define([
'jQuery',
'bootstrap-datetimepicker',
'select2'
'select2',
'bootstrap-3-typeahead'
], function($) {
"use strict";

return {
initialise: function() {
var $issueLicenceForm = $('#issueLicenceForm'),
$issueButton = $('#issue'),
$regionSelect = $issueLicenceForm.find('select'),
$regionSelect = $issueLicenceForm.find('#id_regions'),
$addAttachment = $('#add_attachment');

// initialise all datapickers
Expand All @@ -30,14 +31,38 @@ define([
}

$('#previewLicence').click(function(e) {
$(this).attr("href", this.href + '?' + $issueLicenceForm.serialize());
$issueLicenceForm.find('.extracted-checkbox').each(function() {
$issueLicenceForm.find('#' + this.id + 'Hidden').prop('disabled', this.checked);
});
var url = this.href + '?' + $issueLicenceForm.serialize();
window.open(url);
$issueLicenceForm.find("input:hidden").prop('disabled', false);
return false;
});

$regionSelect.select2({
placeholder: "Select applicable regions."
placeholder: 'Select applicable regions.'
});
$regionSelect.removeClass('hidden');

$('.species').each(function() {
var speciesTypeArg = '';
if($(this).attr('data-species-type') !== undefined) {
speciesTypeArg = '&type=' + $(this).attr('data-species-type');
}

// initialise species typeahead
$(this).typeahead({
minLength: 3,
items: 'all',
source: function (query, process) {
return $.get('/taxonomy/species_name?search=' + query + speciesTypeArg, function (data) {
return process(data);
});
}
});
});

$addAttachment.on('click', function (e) {
var inputNode = $('<input class="top-buffer" id="id_attach" name="attachments" type="file" multiple>');
e.preventDefault();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% if field.type == 'text' %}
<input class="form-control" type="{{ field.type }}" name="{{ field.name }}" value="{{ field.data }}"></input>
{% elif field.type == 'text_area' %}
<textarea rows="3" class="form-control" type="{{ field.type }}" name="{{ field.name }}" value="{{ field.data }}"></textarea>
{% elif field.type == 'number' %}
<input class="form-control" type="{{ field.type }}" name="{{ field.name }}" value="{{ field.data }}"></input>
{% elif field.type == 'date' %}
<input id="{{ field.name }}{{ group_index }}_date" class="form-control" name="{{ field.name }}" value="{{ field.data }}"></input>
{% elif field.type == 'species' %}
<input name="{{ field.name }}" class="form-control species" autocomplete="off" data-species-type="{{ field.speciesType }}" value="{{ field.data }}"/>
{% elif field.type == 'label' %}
{% for option in field.options %}
<div>
{% if group_index %}
<input id="{{ option.name }}{{ group_index }}Hidden" name="{{ option.name }}" type="hidden" value="off">
{% endif %}
<input name="{{ option.name }}" {% if group_index %} id="{{ option.name }}{{ group_index }}" class="extracted-checkbox"{% endif %}
type="checkbox" {% if option.data == 'on' %}checked{% endif %}>
{{ option.label }}
</div>
{% endfor %}
{% elif field.type == 'radiobuttons' %}
{% for option in field.options %}
<div class="radio">
<label>
<input name="{{ field.name }}" type="radio" value="{{ option.value}}" {% if option.value == field.data %}checked{% endif %}>
{{ option.label }}
</label>
</div>
{% endfor %}
{% elif field.type == 'select' %}
<select name="{{ field.name}}" class="form-control">
{% if field.defaultBlank %}
<option disabled {% if not field.data %}selected{% endif %}>Please Choose</option>
{% endif %}
{% for option in field.options %}
<option class="form-control" value="{{ option.value}}" {% if option.value == field.data %}selected{% endif %}>{{ option.label }}</option>
{% endfor %}
</select>
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

{% load jsonify %}

{% load application_filters %}

{% block extra_css %}
<link href="//static.dpaw.wa.gov.au/static/libs/select2/3.5.3/select2.min.css" rel="stylesheet"/>
<link href="//static.dpaw.wa.gov.au/static/libs/select2-bootstrap-css/1.4.6/select2-bootstrap.css"
Expand Down Expand Up @@ -144,6 +146,43 @@ <h3>{{ application.licence_type.name }}</h3>
<form method="post" id="issueLicenceForm" enctype="multipart/form-data">
{% csrf_token %}
{% bootstrap_form issue_licence_form %}
{% if extracted_fields %}
<div class="panel panel-default top-buffer">
<div class="panel-heading">
<h4 class="panel-title">Editable application fields (will appear on licence)</h4>
</div>
<div class="panel-body">
{% for item in extracted_fields %}
{% if not item.readonly %}
{% if not item.children %}
<div class="form-group">
<label class="control-label" for="id_{{ item.name }}">{{ item.label }}</label>
{% include 'wl/issue/extracted_field.html' with field=item %}
</div>
{% else %}
<label class="control-label" for="id_{{ item.name }}">{{ item.label }}</label>
{% for item_group in item.children %}
<div class="row">
{% with width=item_group|length|derive_col_width %}
{% for child_item in item_group %}
<div class="col-md-{{ width }}">
<div class="form-group">
{% if forloop.parentloop.first %}
<label class="control-label">{{ child_item.label }}</label>
{% endif %}
{% include 'wl/issue/extracted_field.html' with field=child_item group_index=forloop.parentloop.counter %}
</div>
</div>
{% endfor %}
{% endwith %}
</div>
{% endfor %}
{% endif %}
{% endif %}
{% endfor %}
</div>
</div>
{% endif %}
<a href="#" id="add_attachment" class="top-buffer">Add attachment</a>
</form>
</div>
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.template.defaulttags import register

MAX_COLS = 12


@register.filter
def derive_col_width(num_cols):
if num_cols == 0:
return 0
elif num_cols > MAX_COLS:
return 1
else:
return int(MAX_COLS / num_cols)
153 changes: 151 additions & 2 deletions wildlifelicensing/apps/applications/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def _extend_item_name(name, suffix, repetition):
return '{}{}-{}'.format(name, suffix, repetition)


def create_data_from_form(form_structure, post_data, file_data, post_data_index=None):
def create_data_from_form(schema, post_data, file_data, post_data_index=None):
data = []

for item in form_structure:
for item in schema:
data.append(_create_data_from_item(item, post_data, file_data, 0, ''))

return data
Expand Down Expand Up @@ -68,6 +68,155 @@ def _create_data_from_item(item, post_data, file_data, repetition, suffix):
return item_data


def extract_licence_fields(schema, data):
licence_fields = []

for item in schema:
_extract_licence_fields_from_item(item, data, licence_fields)

return licence_fields


def _extract_licence_fields_from_item(item, data, licence_fields):
children_extracted = False

if item.get('isLicenceField', False):
if 'children' not in item:
# label / checkbox types are extracted differently so skip here
if item['type'] not in ('label', 'checkbox'):
licence_field = {
'name': item['name'],
'label': item['licenceFieldLabel'] if 'licenceFieldLabel' in item else item['label'],
'type': item['type'],
'readonly': item.get('isLicenceFieldReadonly', False)
}

if 'options' in item:
licence_field['options'] = item['options']
licence_field['defaultBlank'] = item.get('defaultBlank', False)

licence_field['data'] = _extract_item_data(item['name'], data)

licence_fields.append(licence_field)
else:
licence_field = {
'name': item['name'],
'label': item['licenceFieldLabel'] if 'licenceFieldLabel' in item else item['label'],
'type': item['type'],
'readonly': item.get('isLicenceFieldReadonly', False),
'children': []
}

child_data = _extract_item_data(item['name'], data)

for index in range(len(child_data)):
group_licence_fields = []
for child_item in item.get('children'):
if child_item['type'] == 'label' and child_item.get('isLicenceField', False):
_extract_label_and_checkboxes(child_item, item.get('children'), child_data[index], group_licence_fields)

_extract_licence_fields_from_item(child_item, child_data[index], group_licence_fields)
licence_field['children'].append(group_licence_fields)

licence_fields.append(licence_field)

children_extracted = True

# extract licence fields from field's children
if 'children' in item and not children_extracted:
for child_item in item.get('children'):
if child_item['type'] == 'label' and child_item.get('isLicenceField', False):
_extract_label_and_checkboxes(child_item, item.get('children'), data, licence_fields)
_extract_licence_fields_from_item(child_item, data, licence_fields)

# extract licence fields from field's conditional children
if 'conditions' in item:
for condition in item['conditions'].keys():
for child_item in item['conditions'][condition]:
if child_item['type'] == 'label' and child_item.get('isLicenceField', False):
_extract_label_and_checkboxes(child_item, item['conditions'][condition], data, licence_fields)
_extract_licence_fields_from_item(child_item, data, licence_fields)


def _extract_label_and_checkboxes(current_item, items, data, licence_fields):
licence_field = {
'name': current_item['name'],
'label': current_item['licenceFieldLabel'] if 'licenceFieldLabel' in current_item else current_item['label'],
'type': current_item['type'],
'readonly': current_item.get('isLicenceFieldReadonly', False),
'options': []
}

# find index of first checkbox after checkbox label within current item list
checkbox_index = 0
while checkbox_index < len(items) and items[checkbox_index]['name'] != current_item['name']:
checkbox_index += 1
checkbox_index += 1

# add all checkboxes to licence field options
while checkbox_index < len(items) and items[checkbox_index]['type'] == 'checkbox':
name = items[checkbox_index]['name']
option = {
'name': name,
'label': items[checkbox_index]['label']
}
if name in data:
option['data'] = data[name]

licence_field['options'].append(option)

checkbox_index += 1

licence_fields.append(licence_field)


def _extract_item_data(name, data):
def ___extract_item_data(name, data):
if isinstance(data, dict):
if name in data:
return data[name]
else:
for value in data.values():
result = ___extract_item_data(name, value)
if result is not None:
return result
if isinstance(data, list):
for item in data:
result = ___extract_item_data(name, item)
if result is not None:
return result

result = ___extract_item_data(name, data)

return result if result is not None else ''


def update_licence_fields(licence_fields, post_data):
for field in licence_fields:
if 'children' not in field:
if field['type'] == 'label':
for option in field['options']:
if option['name'] in post_data:
option['data'] = post_data[option['name']]
else:
field['data'] = post_data.get(field['name'])
else:
for index, group in enumerate(field['children']):
for child_field in group:
if child_field['type'] == 'label':
for option in child_field['options']:
if option['name'] in post_data:
data_list = post_data.getlist(option['name'])
if index < len(data_list):
option['data'] = data_list[index]
else:
data_list = post_data.getlist(child_field['name'])
if index < len(data_list):
child_field['data'] = data_list[index]

return licence_fields


def convert_documents_to_url(data, document_queryset, suffix):
if isinstance(data, list):
for item in data:
Expand Down
Loading

0 comments on commit 7fe14ae

Please sign in to comment.