diff --git a/decide/decide/settings.py b/decide/decide/settings.py index 1d22b67324..38cd262138 100644 --- a/decide/decide/settings.py +++ b/decide/decide/settings.py @@ -1,11 +1,8 @@ """ Django settings for decide project. - Generated by 'django-admin startproject' using Django 2.0. - For more information on this file, see https://docs.djangoproject.com/en/2.0/topics/settings/ - For the full list of settings and their values, see https://docs.djangoproject.com/en/2.0/ref/settings/ """ @@ -180,3 +177,4 @@ INSTALLED_APPS = INSTALLED_APPS + MODULES + diff --git a/decide/postproc/tests.py b/decide/postproc/tests.py index 236e15d4f8..eb2bc20c5d 100644 --- a/decide/postproc/tests.py +++ b/decide/postproc/tests.py @@ -15,25 +15,24 @@ def setUp(self): def tearDown(self): self.client = None - def test_identity(self): +#test de la función de postproc Bipartisanship, +#comprueba que las dos opciones mayoritarias obtienen 25 y 15 escaños cada una + def test_bipartitanship(self): data = { - 'type': 'IDENTITY', + 'type': 'BIPARTISHANSHIP', + 'numEscanyos': 40, 'options': [ - { 'option': 'Option 1', 'number': 1, 'votes': 5 }, + { 'option': 'Option 1', 'number': 1, 'votes': 50 }, { 'option': 'Option 2', 'number': 2, 'votes': 0 }, - { 'option': 'Option 3', 'number': 3, 'votes': 3 }, - { 'option': 'Option 4', 'number': 4, 'votes': 2 }, - { 'option': 'Option 5', 'number': 5, 'votes': 5 }, - { 'option': 'Option 6', 'number': 6, 'votes': 1 }, + { 'option': 'Option 3', 'number': 3, 'votes': 30 }, + { 'option': 'Option 4', 'number': 4, 'votes': 20 }, ] } expected_result = [ - { 'option': 'Option 1', 'number': 1, 'votes': 5, 'postproc': 5 }, - { 'option': 'Option 5', 'number': 5, 'votes': 5, 'postproc': 5 }, - { 'option': 'Option 3', 'number': 3, 'votes': 3, 'postproc': 3 }, - { 'option': 'Option 4', 'number': 4, 'votes': 2, 'postproc': 2 }, - { 'option': 'Option 6', 'number': 6, 'votes': 1, 'postproc': 1 }, + { 'option': 'Option 1', 'number': 1, 'votes': 50, 'postproc': 25 }, + { 'option': 'Option 3', 'number': 3, 'votes': 30, 'postproc': 15 }, + { 'option': 'Option 4', 'number': 4, 'votes': 20, 'postproc': 0 }, { 'option': 'Option 2', 'number': 2, 'votes': 0, 'postproc': 0 }, ] def test_hamilton(self): diff --git a/decide/postproc/views.py b/decide/postproc/views.py index 9b16eeea62..aeac6c2b95 100644 --- a/decide/postproc/views.py +++ b/decide/postproc/views.py @@ -144,9 +144,39 @@ def dHont(self, options, numEscanyos): return Response(options) + + def bipartishanship(self, options, numEscanyos): + # función de ordenación + def sortByVotes(e): + return e['votes'] + # copiamos las opciones, ordenamos y obtenemos las dos con mayor número de votos + opts = options + opts.sort(reverse=True,key=sortByVotes) + option1=opts[0] + option2=opts[1] + #calculamos la proporción de peso por cada voto + votosTotales = option1['votes'] + option2['votes'] + proporcion = numEscanyos/votosTotales + escanyos1 = round(option1['votes']*proporcion) + escanyos2 = round(option2['votes']*proporcion) + sumaEscanyos = escanyos1 + escanyos2 + #si queda algún escaño sin asignar se lo damos al primero + if sumaEscanyos != numEscanyos: + sobrante = numEscanyos-sumaEscanyos + escanyos1 +=sobrante + #inicializamos y asignamos los escaños a los dos primeros + for op in opts: + op['postproc'] = 0 + opts[0]['postproc'] = escanyos1 + opts[1]['postproc'] = escanyos2 + #reemplazamos las opciones y devolvemos el resultado + options = opts + return Response(options) + + def post(self, request): """ - * type: IDENTITY | HUNTINGTONHILL | DHONT | HAMILTON + * type: IDENTITY | HUNTINGTONHILL | DHONT | HAMILTON | BIPARTITANSHIP * options: [ { option: str, @@ -163,6 +193,7 @@ def post(self, request): if t == 'IDENTITY': return self.identity(opts) + elif t=='HUNTINGTONHILL': return self.HuntingtonHill(options=opts, numEscanyos=numEscanyos) @@ -172,5 +203,8 @@ def post(self, request): elif t== 'HAMILTON': return self.hamilton(options=opts, numEscanyos=numEscanyos) + + elif t == 'BIPARTISHANSHIP': + return self.bipartishanship(options=opts, numEscanyos=numEscanyos) - return Response({}) + return Response({}) \ No newline at end of file diff --git a/decide/voting/migrations/0004_voting_tipo.py b/decide/voting/migrations/0004_voting_tipo.py index 67ddd1b346..34e7215aae 100644 --- a/decide/voting/migrations/0004_voting_tipo.py +++ b/decide/voting/migrations/0004_voting_tipo.py @@ -13,6 +13,6 @@ class Migration(migrations.Migration): migrations.AddField( model_name='voting', name='tipo', - field=models.CharField(choices=[('IDENTITY', 'IDENTITY'), ('HUNTINGTONHILL', 'HUNTINGTONHILL'), ('HAMILTON','HAMILTON')], default='IDENTITY', max_length=20, verbose_name='Count method'), + field=models.CharField(choices=[('IDENTITY', 'IDENTITY'), ('HUNTINGTONHILL', 'HUNTINGTONHILL'), ('HAMILTON','HAMILTON'), ('BIPARTITANSHIP', 'BIPARTITANSHIP')], default='IDENTITY', max_length=20, verbose_name='Count method'), ), ] diff --git a/decide/voting/models.py b/decide/voting/models.py index 4b962ef743..fcbfb01d85 100644 --- a/decide/voting/models.py +++ b/decide/voting/models.py @@ -37,7 +37,7 @@ class Voting(models.Model): end_date = models.DateTimeField(blank=True, null=True) - tipo_votacion = [("IDENTITY", "IDENTITY"),("HUNTINGTONHILL", "HUNTINGTONHILL"),("DHONT","DHONT"), ('HAMILTON', 'HAMILTON')] + tipo_votacion = [("IDENTITY", "IDENTITY"),("HUNTINGTONHILL", "HUNTINGTONHILL"),("DHONT","DHONT"), ('HAMILTON', 'HAMILTON'),("BIPARTISHANSHIP", "BIPARTISHANSHIP")] tipo = models.CharField(choices=tipo_votacion, max_length=20, default="IDENTITY", verbose_name='Count method')