Ejemplo n.º 1
0
def results_total_xls(request):
    """Downloads the combined results from all districts as an Excel
    spreadsheet with different districts in different sheets."""
    sheets = []

    session = DBSession()
    districts = session.query(District).order_by(District.code)

    for district in districts:
        if district.code == 0:
            continue
        title = u'{0} ({1} kansanedustajapaikka'.format(district.name, district.quota)
        title += ')' if district.quota == 1 else 'a)'
        rows = [
            (title, u'', u'', u'', u''),
            (u'', u'', u'', u'', u''),
            (u'Valitut ehdokkaat', u'', u'', u'', u''),
            (u'Numero', u'Nimi', u'Puolue', u'Äänimäärä', u'Vertailuluku'),
        ]
        for i, record in enumerate(dhont_selection(district)):

            # Add a separator between the selected and other candidates.
            if i == district.quota:
                rows.append((u'', u'', u'', u'', u''))
                rows.append((u'Valitsematta jääneet ehdokkaat', u'', u'', u'', u''))
                rows.append((u'Numero', u'Nimi', u'Puolue', u'Äänimäärä', u'Vertailuluku'))

            candidate = record['candidate']
            rows.append((candidate.number,
                         candidate.fullname(),
                         candidate.party.name,
                         record['absolute_votes'],
                         record['proportional_votes']))
        sheets.append((district.name, rows))

    request.add_response_callback(disable_caching_explorer)
    return xls_response_multiple(sheets,
                                 filename='nuorisovaalit2011-valtakunnalliset-tulokset.xls',
                                 num_formats=(None, '@', '@', None, None),
                                 col_widths=(None, 8000, 14000, 3000, 3000))
Ejemplo n.º 2
0
def results_xls(request):
    """Downloads the district specific combined results as an Excel sheet.
    """
    user = authenticated_user(request)
    if user is None:
        raise Forbidden

    district = user.school.district

    title = u'{0} ({1} kansanedustajapaikka'.format(district.name, district.quota)
    title += ')' if district.quota == 1 else 'a)'
    rows = [
        (title, u'', u'', u'', u''),
        (u'', u'', u'', u'', u''),
        (u'Valitut ehdokkaat', u'', u'', u'', u''),
        (u'Numero', u'Nimi', u'Puolue', u'Äänimäärä', u'Vertailuluku'),
    ]

    for i, record in enumerate(dhont_selection(user.school.district)):

        # Add a separator between the selected and other candidates.
        if i == district.quota:
            rows.append((u'', u'', u'', u'', u''))
            rows.append((u'Valitsematta jääneet ehdokkaat', u'', u'', u'', u''))
            rows.append((u'Numero', u'Nimi', u'Puolue', u'Äänimäärä', u'Vertailuluku'))

        candidate = record['candidate']
        rows.append((candidate.number,
                     candidate.fullname(),
                     candidate.party.name,
                     record['absolute_votes'],
                     record['proportional_votes']))

    request.add_response_callback(disable_caching_explorer)
    return xls_response(rows,
                        filename='nuorisovaalit2011-tulokset.xls',
                        num_formats=(None, '@', '@', None, None),
                        col_widths=(None, 8000, 14000, 3000, 3000))
    def test_no_votes(self):
        """Test for a case when no candidates have received any votes.

        Not likely to happen in reality.
        """
        from nuorisovaalit.results import dhont_selection

        district = self._populate(quota=8)

        # group 1
        self._add_votes(district.candidates[0], 0)
        self._add_votes(district.candidates[1], 0)

        # group 2
        self._add_votes(district.candidates[2], 0)
        self._add_votes(district.candidates[3], 0)

        # group 3
        self._add_votes(district.candidates[4], 0)
        self._add_votes(district.candidates[5], 0)
        self._add_votes(district.candidates[6], 0)

        # group 4
        self._add_votes(district.candidates[7], 0)

        winners = dhont_selection(district)

        # Assert quota
        self.assertEquals(8, len(winners))

        # Assert the secondary sorting key ordering. The expected ordering
        # was determined manually by evaluating the hashes in an interpreter.
        self.assertOrdering(
            district.candidates[2],
            district.candidates[6],
            district.candidates[5],
            district.candidates[7],
            district.candidates[0],
            district.candidates[4],
            district.candidates[3],
            district.candidates[1],
            )

        # Because all the vote counts are zero the ordering of the candidates
        # is based entirely on the secondary sorting key.

        # First selected candidate
        self.assertCandidate(winners[0], {
            'name': u'2, Candidate',
            'proportional_votes': Decimal('0'),
            'absolute_votes': 0,
            })

        # Second selected candidate
        self.assertCandidate(winners[1], {
            'name': u'6, Candidate',
            'proportional_votes': Decimal('0'),
            'absolute_votes': 0,
            })

        # Third selected candidate
        self.assertCandidate(winners[2], {
            'name': u'5, Candidate',
            'proportional_votes': Decimal('0'),
            'absolute_votes': 0,
            })

        # Fourth selected candidate
        self.assertCandidate(winners[3], {
            'name': u'7, Candidate',
            'proportional_votes': Decimal('0'),
            'absolute_votes': 0,
            })

        # Fifth selected candidate
        self.assertCandidate(winners[4], {
            'name': u'0, Candidate',
            'proportional_votes': Decimal('0'),
            'absolute_votes': 0,
            })

        # Sixth selected candidate
        self.assertCandidate(winners[5], {
            'name': u'4, Candidate',
            'proportional_votes': Decimal('0'),
            'absolute_votes': 0,
            })

        # Seventh selected candidate
        self.assertCandidate(winners[6], {
            'name': u'3, Candidate',
            'proportional_votes': Decimal('0'),
            'absolute_votes': 0,
            })

        # Eighth selected candidate
        self.assertCandidate(winners[7], {
            'name': u'1, Candidate',
            'proportional_votes': Decimal('0'),
            'absolute_votes': 0,
            })
    def test_tie_in_both_vote_types(self):
        """Tests for the case when there are ties in both the absolute votes
        and the proportional votes.
        """
        from nuorisovaalit.results import dhont_selection

        district = self._populate(quota=3)
        # Votes are created so that they result in ties.
        # The second and third group both have a total sum of 6 votes in
        # addition to the individual candidates having the same number of
        # votes.

        # group 1
        self._add_votes(district.candidates[0], 1)
        self._add_votes(district.candidates[1], 1)

        # group 2
        self._add_votes(district.candidates[2], 3)
        self._add_votes(district.candidates[3], 3)

        # group 3
        self._add_votes(district.candidates[4], 2)
        self._add_votes(district.candidates[5], 2)
        self._add_votes(district.candidates[6], 2)

        # group 4
        self._add_votes(district.candidates[7], 3)

        winners = dhont_selection(district)

        self.assertEquals(8, len(winners))

        # Assert the secondary sorting key ordering. The expected ordering
        # was determined manually by evaluating the hashes in an interpreter.
        self.assertOrdering(
            district.candidates[2],
            district.candidates[6],
            district.candidates[5],
            district.candidates[7],
            district.candidates[0],
            district.candidates[4],
            district.candidates[3],
            district.candidates[1],
            )

        # First selected candidate. The first position is a tie between the
        # second and third group. In each group the candidates have tied also.
        # Of the five possible candidates the secondary sorting key favors
        # candidate 3.
        self.assertCandidate(winners[0], {
            'name': u'2, Candidate',
            'proportional_votes': Decimal('6'),
            'absolute_votes': 3,
            })

        # Second selected candidate.
        self.assertCandidate(winners[1], {
            'name': u'6, Candidate',
            'proportional_votes': Decimal('6'),
            'absolute_votes': 2,
            })

        # Third selected candidate. The third position is a tie between
        # the left overs from groups two and three with group four. Out of
        # these candidates the secondary sorting key favors candidate 5.
        self.assertCandidate(winners[2], {
            'name': u'5, Candidate',
            'proportional_votes': Decimal('3'),
            'absolute_votes': 2,
            })

        # The remaining candidates that did not fit the quota.

        self.assertCandidate(winners[3], {
            'name': u'7, Candidate',
            'proportional_votes': Decimal('3'),
            'absolute_votes': 3,
            })
        self.assertCandidate(winners[4], {
            'name': u'3, Candidate',
            'proportional_votes': Decimal('3'),
            'absolute_votes': 3,
            })
        self.assertCandidate(winners[5], {
            'name': u'0, Candidate',
            'proportional_votes': Decimal('2'),
            'absolute_votes': 1,
            })
        self.assertCandidate(winners[6], {
            'name': u'4, Candidate',
            'proportional_votes': Decimal('2'),
            'absolute_votes': 2,
            })
        self.assertCandidate(winners[7], {
            'name': u'1, Candidate',
            'proportional_votes': Decimal('1'),
            'absolute_votes': 1,
            })
    def test_tie_in_proportional_votes(self):
        """Tests for the case when two or more candidates get the same amount
        of proportional votes.
        """
        from nuorisovaalit.results import dhont_selection

        district = self._populate(quota=3)
        # Votes are distributed so that each group has the same number of
        # total votes resulting in the tie in the proportional votes.

        # group 1
        self._add_votes(district.candidates[0], 2)
        self._add_votes(district.candidates[1], 6)

        # group 2
        self._add_votes(district.candidates[2], 5)
        self._add_votes(district.candidates[3], 3)

        # group 3
        self._add_votes(district.candidates[4], 2)
        self._add_votes(district.candidates[5], 2)
        self._add_votes(district.candidates[6], 4)

        # group 4
        self._add_votes(district.candidates[7], 8)

        winners = dhont_selection(district)

        self.assertEquals(8, len(winners))

        # Assert the secondary sorting key ordering. The expected ordering
        # was determined manually by evaluating the hashes in an interpreter.
        self.assertOrdering(
            district.candidates[2],
            district.candidates[6],
            district.candidates[5],
            district.candidates[7],
            district.candidates[0],
            district.candidates[4],
            district.candidates[3],
            district.candidates[1],
            )

        # First selected candidate.
        self.assertCandidate(winners[0], {
            'name': u'2, Candidate',
            'proportional_votes': Decimal('8'),
            'absolute_votes': 5,
            })

        # Second selected candidate.
        self.assertCandidate(winners[1], {
            'name': u'6, Candidate',
            'proportional_votes': Decimal('8'),
            'absolute_votes': 4,
            })

        # Third selected candidate.
        self.assertCandidate(winners[2], {
            'name': u'7, Candidate',
            'proportional_votes': Decimal('8'),
            'absolute_votes': 8,
            })

        self.assertCandidate(winners[3], {
            'name': u'1, Candidate',
            'proportional_votes': Decimal('8'),
            'absolute_votes': 6,
            })
        self.assertCandidate(winners[4], {
            'name': u'5, Candidate',
            'proportional_votes': Decimal('4'),
            'absolute_votes': 2,
            })
        self.assertCandidate(winners[5], {
            'name': u'0, Candidate',
            'proportional_votes': Decimal('4'),
            'absolute_votes': 2,
            })
        self.assertCandidate(winners[6], {
            'name': u'3, Candidate',
            'proportional_votes': Decimal('4'),
            'absolute_votes': 3,
            })
        self.assertCandidate(winners[7], {
            'name': u'4, Candidate',
            'proportional_votes': Decimal('8') / Decimal('3'),
            'absolute_votes': 2,
            })
    def test_candidates_less_than_quota(self):
        """Test for a case where a quota for a district is larger than the
        number of candidates.

        In reality this will most likely never happen.
        """
        from nuorisovaalit.results import dhont_selection

        district = self._populate(quota=20)
        self._add_votes(district.candidates[0], 2)
        self._add_votes(district.candidates[1], 3)

        self._add_votes(district.candidates[2], 2)
        self._add_votes(district.candidates[3], 4)

        self._add_votes(district.candidates[4], 1)
        self._add_votes(district.candidates[5], 2)
        self._add_votes(district.candidates[6], 4)

        self._add_votes(district.candidates[7], 3)

        winners = dhont_selection(district)

        # We can only get back the number of candidates there are, even if it
        # is less than the quota.
        self.assertEquals(8, len(winners))

        # Assert the secondary sorting key ordering. The expected ordering
        # was determined manually by evaluating the hashes in an interpreter.
        self.assertOrdering(
            district.candidates[2],
            district.candidates[6],
            district.candidates[5],
            district.candidates[7],
            district.candidates[0],
            district.candidates[4],
            district.candidates[3],
            district.candidates[1],
            )

        # First selected candidate
        self.assertCandidate(winners[0], {
            'name': u'6, Candidate',
            'proportional_votes': Decimal('7'),
            'absolute_votes': 4,
            })

        # Second selected candidate
        self.assertCandidate(winners[1], {
            'name': u'3, Candidate',
            'proportional_votes': Decimal('6'),
            'absolute_votes': 4,
            })

        # Third selected candidate
        self.assertCandidate(winners[2], {
            'name': u'1, Candidate',
            'proportional_votes': Decimal('5'),
            'absolute_votes': 3,
            })

        # Fourth selected candidate
        self.assertCandidate(winners[3], {
            'name': u'5, Candidate',
            'proportional_votes': Decimal('3.5'),
            'absolute_votes': 2,
            })

        # Fifth selected candidate
        self.assertCandidate(winners[4], {
            'name': u'2, Candidate',
            'proportional_votes': Decimal('3'),
            'absolute_votes': 2,
            })

        # Sixth selected candidate
        self.assertCandidate(winners[5], {
            'name': u'7, Candidate',
            'proportional_votes': Decimal('3'),
            'absolute_votes': 3,
            })

        # Seventh selected candidate
        self.assertCandidate(winners[6], {
            'name': u'0, Candidate',
            'proportional_votes': Decimal('2.5'),
            'absolute_votes': 2,
            })

        # Eighth selected candidate
        self.assertCandidate(winners[7], {
            'name': u'4, Candidate',
            'proportional_votes': Decimal('7') / Decimal('3'),
            'absolute_votes': 1,
            })
    def test_mixed_coalitions(self):
        """Tests when some parties are part of coalitions"""
        from nuorisovaalit.models import Coalition
        from nuorisovaalit.results import dhont_selection

        session = DBSession()
        district = self._populate(quota=3)

        # Create a coalition for Köyhien asialla and Piraattipuolue
        self._add_coalition(u'Red coalition', district, district.candidates[0].party, district.candidates[2].party)
        self.assertEquals(1, session.query(Coalition).count())

        # Create votes for red coalition
        self._add_votes(district.candidates[0], 2)
        self._add_votes(district.candidates[1], 3)
        self._add_votes(district.candidates[2], 2)
        self._add_votes(district.candidates[3], 4)
        # Create votes for Suomen työväenpuolue
        self._add_votes(district.candidates[4], 1)
        self._add_votes(district.candidates[5], 2)
        self._add_votes(district.candidates[6], 4)
        # Create votes for valitsijalista
        self._add_votes(district.candidates[7], 8)

        winners = dhont_selection(district)

        # Assert the secondary sorting key ordering. The expected ordering
        # was determined manually by evaluating the hashes in an interpreter.
        self.assertOrdering(
            district.candidates[2],
            district.candidates[6],
            district.candidates[5],
            district.candidates[7],
            district.candidates[0],
            district.candidates[4],
            district.candidates[3],
            district.candidates[1],
            )

        # First selected candidate
        self.assertCandidate(winners[0], {
            'name': u'3, Candidate',
            'proportional_votes': Decimal('11'),
            'absolute_votes': 4,
            })

        # Second selected candidate
        self.assertCandidate(winners[1], {
            'name': u'7, Candidate',
            'proportional_votes': Decimal('8'),
            'absolute_votes': 8,
            })

        # Third selected candidate
        self.assertCandidate(winners[2], {
            'name': u'6, Candidate',
            'proportional_votes': Decimal('7'),
            'absolute_votes': 4,
            })

        self.assertCandidate(winners[3], {
            'name': u'1, Candidate',
            'proportional_votes': Decimal('5.5'),
            'absolute_votes': 3,
            })
        self.assertCandidate(winners[4], {
            'name': u'2, Candidate',
            'proportional_votes': Decimal('11') / Decimal('3'),
            'absolute_votes': 2,
            })
        self.assertCandidate(winners[5], {
            'name': u'5, Candidate',
            'proportional_votes': Decimal('3.5'),
            'absolute_votes': 2,
            })
        self.assertCandidate(winners[6], {
            'name': u'0, Candidate',
            'proportional_votes': Decimal('2.75'),
            'absolute_votes': 2,
            })
        self.assertCandidate(winners[7], {
            'name': u'4, Candidate',
            'proportional_votes': Decimal('7') / Decimal('3'),
            'absolute_votes': 1,
            })
    def test_no_coalitions(self):
        """Test the results when there are no coalitions."""
        from nuorisovaalit.results import dhont_selection

        district = self._populate(quota=3)

        # group 1
        self._add_votes(district.candidates[0], 2)
        self._add_votes(district.candidates[1], 3)

        # group 2
        self._add_votes(district.candidates[2], 2)
        self._add_votes(district.candidates[3], 4)

        # group 3
        self._add_votes(district.candidates[4], 1)
        self._add_votes(district.candidates[5], 2)
        self._add_votes(district.candidates[6], 4)

        # group 4
        self._add_votes(district.candidates[7], 3)

        winners = dhont_selection(district)

        self.assertEquals(8, len(winners))

        # Assert the secondary sorting key ordering. The expected ordering
        # was determined manually by evaluating the hashes in an interpreter.
        self.assertOrdering(
            district.candidates[2],
            district.candidates[6],
            district.candidates[5],
            district.candidates[7],
            district.candidates[0],
            district.candidates[4],
            district.candidates[3],
            district.candidates[1],
            )

        # First selected candidate
        self.assertCandidate(winners[0], {
            'name': u'6, Candidate',
            'proportional_votes': Decimal('7'),
            'absolute_votes': 4,
            })

        # Second selected candidate
        self.assertCandidate(winners[1], {
            'name': u'3, Candidate',
            'proportional_votes': Decimal('6'),
            'absolute_votes': 4,
            })

        # Third selected candidate
        self.assertCandidate(winners[2], {
            'name': u'1, Candidate',
            'proportional_votes': Decimal('5'),
            'absolute_votes': 3,
            })

        self.assertCandidate(winners[3], {
            'name': u'5, Candidate',
            'proportional_votes': Decimal('3.5'),
            'absolute_votes': 2,
            })
        self.assertCandidate(winners[4], {
            'name': u'2, Candidate',
            'proportional_votes': Decimal('3'),
            'absolute_votes': 2,
            })
        self.assertCandidate(winners[5], {
            'name': u'7, Candidate',
            'proportional_votes': Decimal('3'),
            'absolute_votes': 3,
            })
        self.assertCandidate(winners[6], {
            'name': u'0, Candidate',
            'proportional_votes': Decimal('2.5'),
            'absolute_votes': 2,
            })
        self.assertCandidate(winners[7], {
            'name': u'4, Candidate',
            'proportional_votes': Decimal('7') / Decimal('3'),
            'absolute_votes': 1,
            })