Skip to content

Commit

Permalink
Merge pull request EGCETSII#29 from joszamama/G2-feature/Bipartisanship
Browse files Browse the repository at this point in the history
G2 feature/bipartisanship
  • Loading branch information
beallasai authored Dec 20, 2021
2 parents 74fa29d + 012e46b commit 06f0dbb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
4 changes: 1 addition & 3 deletions decide/decide/settings.py
Original file line number Diff line number Diff line change
@@ -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/
"""
Expand Down Expand Up @@ -180,3 +177,4 @@


INSTALLED_APPS = INSTALLED_APPS + MODULES

23 changes: 11 additions & 12 deletions decide/postproc/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
38 changes: 36 additions & 2 deletions decide/postproc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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({})
2 changes: 1 addition & 1 deletion decide/voting/migrations/0004_voting_tipo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
),
]
2 changes: 1 addition & 1 deletion decide/voting/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')


Expand Down

0 comments on commit 06f0dbb

Please sign in to comment.