diff --git a/decide/postproc/tests.py b/decide/postproc/tests.py index cf82807e83..814f73c774 100644 --- a/decide/postproc/tests.py +++ b/decide/postproc/tests.py @@ -444,4 +444,33 @@ def test_borda_different_size_preferences(self): values = response.json() self.assertEqual(values, expected_result) - \ No newline at end of file + + + 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) + diff --git a/decide/postproc/views.py b/decide/postproc/views.py index 69aaf82332..762ac4bd80 100644 --- a/decide/postproc/views.py +++ b/decide/postproc/views.py @@ -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 @@ -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): """ @@ -84,6 +118,16 @@ def post(self, request): ...extraparams } ] + + * type: EQUALITY + * options: [ + { + option: str, + number: int, + votes_men: int, + votes_women: int, + } + ] """ out = [] @@ -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)