Skip to content

Commit

Permalink
Merge pull request #436 from onaio/fix-total-valid-votes-on-candidate…
Browse files Browse the repository at this point in the history
…s-results-report

Fix logic for getting total votes per electrol race
  • Loading branch information
JohnMwashuma authored Nov 7, 2024
2 parents 14af9ff + 9661b44 commit 753cc1e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 47 deletions.
40 changes: 39 additions & 1 deletion tally_ho/apps/tally/tests/views/data/test_ballot_list_view.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import json
from django.test import RequestFactory

from tally_ho.apps.tally.views.data import ballot_list_view as views
from tally_ho.libs.permissions import groups
from tally_ho.libs.tests.test_base import create_tally, TestBase
from tally_ho.libs.tests.fixtures.electrol_race_data import electrol_races
from tally_ho.libs.tests.test_base import (
create_ballot, create_electrol_race, create_tally, TestBase
)


class TestBallotListView(TestBase):
Expand All @@ -22,3 +26,37 @@ def test_ballot_list_view(self):
response = view(request, tally_id=tally.pk)
self.assertContains(response, "Ballot List")
self.assertContains(response, "New Ballot")

def test_ballot_list_data_view(self):
"""
Test that ballot list data view returns correct data
"""
tally = create_tally()
tally.users.add(self.user)
electrol_race = create_electrol_race(
tally,
**electrol_races[0]
)
ballot = create_ballot(tally, electrol_race=electrol_race)
view = views.BallotListDataView.as_view()
request = self.factory.post('/ballot-list-data')
request.user = self.user
response = view(request, tally_id=tally.pk)
action_link =\
str(
'<a '
f'href="/super-administrator/edit-ballot/{tally.id}/{ballot.id}"'
' class="btn btn-default btn-small">Edit</a>'
)
mock_json_data =\
[
'1',
'True',
'Presidential',
'ballot_number_presidential',
ballot.modified_date.strftime('%a, %d %b %Y %H:%M:%S %Z'),
str(ballot.available_for_release),
action_link]
self.assertEqual(
mock_json_data,
json.loads(response.content.decode())['data'][0])
11 changes: 5 additions & 6 deletions tally_ho/apps/tally/tests/views/test_get_json_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def setUp(self):
@patch('tally_ho.apps.tally.views.reports.administrative_areas_reports.'
'results_queryset')
@patch('tally_ho.apps.tally.views.reports.administrative_areas_reports.'
'total_valid_votes_with_recon_forms_per_electrol_race')
@patch('tally_ho.apps.tally.views.reports.administrative_areas_reports.'
'total_valid_votes_with_no_recon_forms_per_electrol_race')
'get_total_valid_votes_per_electrol_race')
def test_get_results_success(
self, mock_no_recon, mock_with_recon, mock_results_queryset):
self,
mock_get_total_valid_votes_per_electrol_race,
mock_results_queryset):
mock_results_queryset.return_value = [
{
'candidate_number': 1,
Expand All @@ -47,8 +47,7 @@ def test_get_results_success(
'sub_con_name': 'Sub Con A',
}
]
mock_with_recon.return_value = 50
mock_no_recon.return_value = 50
mock_get_total_valid_votes_per_electrol_race.return_value = 100

request_data = json.dumps({'tally_id': self.tally.id})
response = self.client.get(self.url, {'data': request_data})
Expand Down
2 changes: 1 addition & 1 deletion tally_ho/apps/tally/views/data/ballot_list_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def render_column(self, row, column):

def filter_queryset(self, qs):
tally_id = self.kwargs.get('tally_id')
keyword = self.request.GET.get('search[value]', None)
keyword = self.request.POST.get('search[value]', None)

if tally_id:
qs = qs.filter(tally__id=tally_id)
Expand Down
51 changes: 12 additions & 39 deletions tally_ho/apps/tally/views/reports/administrative_areas_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,32 +539,17 @@ def stations_and_centers_queryset(

return qs

def total_valid_votes_with_recon_forms_per_electrol_race(
def get_total_valid_votes_per_electrol_race(
tally_id,
electral_race_id,
):
total_valid_votes =\
ReconciliationForm.objects.filter(
result_form__tally__id=tally_id,
active=True,
result_form__ballot__electrol_race__id=electral_race_id,
entry_version=EntryVersion.FINAL,
).values('result_form__ballot__electrol_race__id').aggregate(
total_valid_votes=Sum('number_valid_votes')
).get('total_valid_votes')
return total_valid_votes or 0

def total_valid_votes_with_no_recon_forms_per_electrol_race(
tally_id,
electral_race_id,
):
electral_race_id
):
total_valid_votes =\
Result.objects.filter(
result_form__tally__id=tally_id,
active=True,
result_form__reconciliationform__isnull=True,
result_form__ballot__electrol_race__id=electral_race_id,
entry_version=EntryVersion.FINAL,
result_form__form_state=FormState.ARCHIVED,
).values('result_form__ballot__electrol_race__id').aggregate(
total_valid_votes=Sum('votes'),
).get('total_valid_votes')
Expand Down Expand Up @@ -1739,11 +1724,7 @@ def create_ppt_export(

valid_votes_per_electrol_race_id =\
{
id: total_valid_votes_with_recon_forms_per_electrol_race(
tally_id,
id
) +
total_valid_votes_with_no_recon_forms_per_electrol_race(
id: get_total_valid_votes_per_electrol_race(
tally_id,
id
) for id in electrol_race_ids
Expand Down Expand Up @@ -2179,11 +2160,7 @@ def get_results(request):
list(set([result.get('electrol_race_id') for result in qs]))
valid_votes_per_electrol_race_id =\
{
id: total_valid_votes_with_recon_forms_per_electrol_race(
tally_id,
id
) +
total_valid_votes_with_no_recon_forms_per_electrol_race(
id: get_total_valid_votes_per_electrol_race(
tally_id,
id
) for id in electrol_race_ids
Expand Down Expand Up @@ -3761,16 +3738,12 @@ def get_initial_queryset(self):
electrol_race_ids =\
list(set([result.get('electrol_race_id') for result in qs]))
valid_votes_per_electrol_race_id =\
{
id: total_valid_votes_with_recon_forms_per_electrol_race(
tally_id,
id
) +
total_valid_votes_with_no_recon_forms_per_electrol_race(
tally_id,
id
) for id in electrol_race_ids
}
{
id: get_total_valid_votes_per_electrol_race(
tally_id,
id
) for id in electrol_race_ids
}
results =\
[{
'candidate_name': result.get('candidate_name'),
Expand Down

0 comments on commit 753cc1e

Please sign in to comment.