Skip to content

Commit

Permalink
Merge pull request #30 from egc-sierrezuela1/feature/postproc-5
Browse files Browse the repository at this point in the history
INTEGRAR RAMA feature/postproc-5 EN DEVELOP
  • Loading branch information
Servandofg12 authored Dec 26, 2021
2 parents cb07e5c + c12f5a9 commit 025c8db
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
31 changes: 30 additions & 1 deletion decide/postproc/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,33 @@ def test_borda_different_size_preferences(self):

values = response.json()
self.assertEqual(values, expected_result)



def test_equality(self):
data = {
'type': 'EQUALITY',
'options': [
{ 'option': 'Option 1', 'number': 1, 'votes_men': 2, 'votes_women': 3 },
{ 'option': 'Option 2', 'number': 2, 'votes_men': 0, 'votes_women': 4 },
{ 'option': 'Option 3', 'number': 3, 'votes_men': 3, 'votes_women': 1 },
{ 'option': 'Option 4', 'number': 4, 'votes_men': 1, 'votes_women': 0 },
{ 'option': 'Option 5', 'number': 5, 'votes_men': 1, 'votes_women': 3 },
{ 'option': 'Option 6', 'number': 6, 'votes_men': 1, 'votes_women': 1 },
]
}

expected_result = [
{ 'option': 'Option 1', 'number': 1, 'votes_men': 2, 'votes_women': 3, 'postproc': 4 },
{ 'option': 'Option 3', 'number': 3, 'votes_men': 3, 'votes_women': 1, 'postproc': 4 },
{ 'option': 'Option 2', 'number': 2, 'votes_men': 0, 'votes_women': 4, 'postproc': 3 },
{ 'option': 'Option 5', 'number': 5, 'votes_men': 1, 'votes_women': 3, 'postproc': 3 },
{ 'option': 'Option 6', 'number': 6, 'votes_men': 1, 'votes_women': 1, 'postproc': 2 },
{ 'option': 'Option 4', 'number': 4, 'votes_men': 1, 'votes_women': 0, 'postproc': 1 },
]

response = self.client.post('/postproc/', data, format='json')
self.assertEqual(response.status_code, 200)

values = response.json()
self.assertEqual(values, expected_result)

49 changes: 46 additions & 3 deletions decide/postproc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,40 @@ def identity(self, options):
out.append({
**opt,
'postproc': opt['votes'],
});
})

out.sort(key=lambda x: -x['postproc'])
return Response(out)


def equality(self, options):
out = []
n_women = 0
n_men = 0
rel = 0.
is_men_greater = False

for opt in options:
n_women += opt['votes_women']
n_men += opt['votes_men']

if n_women > n_men:
rel = n_men/n_women
else:
rel = n_women/n_men
is_men_greater = True

for opt in options:
votes = 0
if is_men_greater:
votes = opt['votes_women'] + opt['votes_men']*rel
else:
votes = opt['votes_men'] + opt['votes_women']*rel

out.append({
**opt,
'postproc': round(votes),
})

out.sort(key=lambda x: -x['postproc'])
return out
Expand Down Expand Up @@ -72,6 +105,7 @@ def proportional_representation(self, options, type): #EGC-GUADALENTIN
out.sort(key=lambda x: (-x['postproc'], -x['votes']))
return out


def post(self, request):

"""
Expand All @@ -84,6 +118,16 @@ def post(self, request):
...extraparams
}
]
* type: EQUALITY
* options: [
{
option: str,
number: int,
votes_men: int,
votes_women: int,
}
]
"""

out = []
Expand All @@ -102,8 +146,7 @@ def post(self, request):
result = self.equality(opts)
if t == 'SAINTE_LAGUE' or t == 'HONDT':
result = self.proportional_representation(opts, t)

out.append({'type': t, 'options': result})


return Response(out)

0 comments on commit 025c8db

Please sign in to comment.