Skip to content
Open
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
49 changes: 49 additions & 0 deletions ynr/apps/bulk_adding/static/bulk_adding/js/bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,53 @@ $(function(){
$form.remove();
});
});


function tbody_is_empty($tbody) {
if ($tbody.find("input:not([type=hidden])").val() && $tbody.find("input:not([type=hidden])").val().length > 0) {
return false;
}
if ($tbody.find("option:not([value='']):selected").length > 0) {
return false;
}
return true;
}

// Remove empty fieldsets
var always_show_extra = $("form#bulk_add_form")[0].dataset.winnerCount * 3;
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't had chance to check this out locally to test, but I dont think this would fix the 500 error I mentioned in an old comment that happens when a ballot does not have winner_count set? I think that will need a change in the view.

There was also another bug that I came across when testing originally, that from memory, was that if I filled in multiple candidates using the new "add a row" button, and then submitting the form, only the last candidate that I entered was being created/shown for review. But as mentioned it was a while ago I tested it locally so will need to go through again when I have chance!

$("#bulk_add_form .sopn_adding_table tbody").each(function(i) {
var tbody = $(this);
if (tbody_is_empty(tbody)) {
if (i >= always_show_extra) {
tbody.hide();
tbody.prev("thead").hide();
}
} else {
always_show_extra++;
}
});


// Extra form rows on bulk adding form
var $table = $("#bulk_add_form .sopn_adding_table");
var add_extra_row = "<tbody class='add_extra_table'><tr><td></td><td><button type='button' id='add_row_button'>Add row</button></td></tr></tbody>";
$table.append(add_extra_row);
$("#add_row_button").on("click", function() {
var total_form_element = $('#id_form-TOTAL_FORMS');
var last_tbody = $table.find("tbody:last").prev("tbody");
var new_tbody = last_tbody.clone(true, true);
var formRegex = RegExp('form-[1-9]+-','g');
var new_form_id = $table.find(".ballot_group").length;
new_tbody.html(new_tbody.html().replace(formRegex, 'form-'+new_form_id+'-'));
var next_form_id = new_form_id += 1;
var next_form_label = $table.find("thead:visible:last").find("th").text().match("([0-9]+)")[0];
next_form_label = parseInt(next_form_label) + 1;
$("<thead><tr><th colspan='3' >Person "+ next_form_label +"</th></tr></thead>").insertBefore($(".add_extra_table"));
new_tbody.insertBefore($(".add_extra_table"));
new_tbody.find(".select2").remove();
populate_party_selects();
new_tbody.show();
total_form_element.val(next_form_id);
});

});
9 changes: 5 additions & 4 deletions ynr/apps/bulk_adding/templates/bulk_add/sopns/add_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ <h2>Add candidates to {{ election_obj.name }}: <a href="{{ ballot.get_absolute_u

{% include "bulk_add/sopns/_known-people.html" %}

<form method=POST id="bulk_add_form">
<form method=POST id="bulk_add_form" data-winner-count="{{ ballot.winner_count }}">
Copy link
Contributor

Choose a reason for hiding this comment

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

If winner_count is not set, this causes a 500 error

<button type=submit>Review</button>
{% csrf_token %}
{{ formset.management_form }}
Expand All @@ -82,9 +82,6 @@ <h2>Add candidates to {{ election_obj.name }}: <a href="{{ ballot.get_absolute_u

<table class="table sopn_adding_table" >
{% for form in formset %}
{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
<thead>
<tr>
<th colspan="3" {% if form.has_error %}class="form-error-summary"{% endif %}>
Expand All @@ -93,6 +90,9 @@ <h2>Add candidates to {{ election_obj.name }}: <a href="{{ ballot.get_absolute_u
</tr>
</thead>
<tbody class="ballot_group" data-party-register="{{ ballot.post.party_set.slug|upper }}">
{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
<tr>
<th>
{{ form.name.errors }}
Expand Down Expand Up @@ -133,6 +133,7 @@ <h2>Add candidates to {{ election_obj.name }}: <a href="{{ ballot.get_absolute_u
</td>
{% endif %}
</tr>
</tbody>
{% endfor %}
</table>
<button type=submit>Review</button>
Expand Down
1 change: 1 addition & 0 deletions ynr/apps/bulk_adding/views/sopns.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def get_context_data(self, **kwargs):
)
else:
context["formset"] = forms.BulkAddFormSetFactory(**form_kwargs)
context["formset"].extra = max(self.ballot.winner_count * 5, 15)

return context

Expand Down