def json(self):
            extended_tally_sheet = self.tallySheet.get_extended_tally_sheet()
            result_type, result_code, result_level, ed_code, ed_name, pd_code, pd_name = extended_tally_sheet.on_get_release_result_params(
            )

            party_wise_results = self.get_party_wise_valid_vote_count_result(
            ).sort_values(by=['numValue', "electionPartyId"],
                          ascending=[False, True]).reset_index()

            registered_voters_count = self.tallySheetVersion.tallySheet.area.get_registered_voters_count(
                vote_type=self.tallySheetVersion.tallySheet.election.voteType)
            total_valid_vote_count = 0
            total_rejected_vote_count = self.get_rejected_vote_count_result(
            )["numValue"].values[0]
            for party_wise_result in party_wise_results.itertuples():
                total_valid_vote_count += float(party_wise_result.numValue)
            total_vote_count = total_valid_vote_count + total_rejected_vote_count

            return {
                "type":
                result_type,
                "level":
                result_level,
                "by_party": [{
                    "party_code":
                    party_wise_result.partyAbbreviation,
                    "party_name":
                    party_wise_result.partyName,
                    "vote_count":
                    int(party_wise_result.numValue),
                    "vote_percentage":
                    to_percentage(
                        (party_wise_result.numValue / total_valid_vote_count) *
                        100),
                    "seat_count":
                    0,
                    "national_list_seat_count":
                    0
                } for party_wise_result in party_wise_results.itertuples()],
                "summary": {
                    "valid":
                    int(total_valid_vote_count),
                    "rejected":
                    int(total_rejected_vote_count),
                    "polled":
                    int(total_vote_count),
                    "electors":
                    int(registered_voters_count),
                    "percent_valid":
                    to_percentage(
                        (total_valid_vote_count / total_vote_count) * 100),
                    "percent_rejected":
                    to_percentage(
                        (total_rejected_vote_count / total_vote_count) * 100),
                    "percent_polled":
                    to_percentage(
                        (total_vote_count / registered_voters_count) * 100)
                }
            }
    def get_html_content_dict(self):
        tallySheetContent = self.content
        stamp = self.stamp

        content = {
            "resultTitle": "ALL ISLAND RESULT",
            "election": {
                "electionName": self.submission.election.get_official_name()
            },
            "stamp": {
                "createdAt": stamp.createdAt,
                "createdBy": stamp.createdBy,
                "barcodeString": stamp.barcodeString
            },
            "date": stamp.createdAt.strftime("%d/%m/%Y"),
            "time": stamp.createdAt.strftime("%H:%M:%S %p"),
            "data": [
            ],
            "validVoteCounts": [0, 0],
            "rejectedVoteCounts": [0, 0],
            "totalVoteCounts": [0, 0],
            "registeredVoters": [
                to_comma_seperated_num(self.submission.area.registeredVotersCount),
                100
            ]
        }

        candidate_wise_valid_vote_count_result = self.candidate_wise_valid_vote_count_query().all()
        vote_count_result = self.vote_count_result()

        for candidate_wise_valid_vote_count_result_item in candidate_wise_valid_vote_count_result:
            content["data"].append([
                candidate_wise_valid_vote_count_result_item.candidateName,
                candidate_wise_valid_vote_count_result_item.partyAbbreviation,
                to_comma_seperated_num(candidate_wise_valid_vote_count_result_item.validVoteCount),
                to_percentage(candidate_wise_valid_vote_count_result_item.validVotePercentage),
                candidate_wise_valid_vote_count_result_item.validVoteCount
            ])

        content['data'] = sorted(content['data'], key=operator.itemgetter(4), reverse=True)
        content["validVoteCounts"] = [
            to_comma_seperated_num(vote_count_result["validVoteCount"]),
            to_percentage(vote_count_result["validVoteCountPercentage"])
        ]

        content["rejectedVoteCounts"] = [
            to_comma_seperated_num(vote_count_result["rejectedVoteCount"]),
            to_percentage(vote_count_result["rejectedVoteCountPercentage"])
        ]

        content["totalVoteCounts"] = [
            to_comma_seperated_num(vote_count_result["totalVoteCount"]),
            to_percentage(vote_count_result["totalVoteCountPercentage"])
        ]

        content["logo"] = convert_image_to_data_uri("static/Emblem_of_Sri_Lanka.png")

        return content
예제 #3
0
    def html(self):
        tallySheetContent = self.content
        stamp = self.stamp

        content = {
            "election": {
                "electionName": self.submission.election.get_official_name()
            },
            "stamp": {
                "createdAt": stamp.createdAt,
                "createdBy": stamp.createdBy,
                "barcodeString": stamp.barcodeString
            },
            "date": stamp.createdAt.strftime("%d/%m/%Y"),
            "time": stamp.createdAt.strftime("%H:%M:%S %p"),
            "data": [
            ],
            "validVoteCounts": [0, 0],
            "rejectedVoteCounts": [0, 0],
            "totalVoteCounts": [0, 0],
            "registeredVoters": [
                to_comma_seperated_num(self.submission.area.registeredVotersCount),
                100
            ]
        }

        candidate_wise_valid_vote_count_result = self.candidate_wise_valid_vote_count_query().all()
        vote_count_result = self.vote_count_result()

        for candidate_wise_valid_vote_count_result_item in candidate_wise_valid_vote_count_result:
            content["data"].append([
                candidate_wise_valid_vote_count_result_item.candidateName,
                candidate_wise_valid_vote_count_result_item.partyAbbreviation,
                to_comma_seperated_num(candidate_wise_valid_vote_count_result_item.validVoteCount),
                to_percentage(candidate_wise_valid_vote_count_result_item.validVotePercentage)
            ])

        content["validVoteCounts"] = [
            to_comma_seperated_num(vote_count_result["validVoteCount"]),
            to_percentage(vote_count_result["validVoteCountPercentage"])
        ]

        content["rejectedVoteCounts"] = [
            to_comma_seperated_num(vote_count_result["rejectedVoteCount"]),
            to_percentage(vote_count_result["rejectedVoteCountPercentage"])
        ]

        content["totalVoteCounts"] = [
            to_comma_seperated_num(vote_count_result["totalVoteCount"]),
            to_percentage(vote_count_result["totalVoteCountPercentage"])
        ]

        html = render_template(
            'PRE_ALL_ISLAND_RESULTS.html',
            content=content
        )

        return html
예제 #4
0
        def html_letter(self, title="", total_registered_voters=None):
            tallySheetVersion = self.tallySheetVersion
            party_wise_valid_vote_count_result = self.get_party_wise_valid_vote_count_result(
            )
            area_wise_valid_vote_count_result = self.get_area_wise_valid_vote_count_result(
            )
            area_wise_rejected_vote_count_result = self.get_area_wise_rejected_vote_count_result(
            )
            area_wise_vote_count_result = self.get_area_wise_vote_count_result(
            )
            stamp = tallySheetVersion.stamp

            registered_voters_count = tallySheetVersion.submission.area.get_registered_voters_count(
                vote_type=tallySheetVersion.submission.election.voteType)

            content = {
                "election": {
                    "electionName":
                    tallySheetVersion.submission.election.get_official_name()
                },
                "stamp": {
                    "createdAt": stamp.createdAt,
                    "createdBy": stamp.createdBy,
                    "barcodeString": stamp.barcodeString
                },
                "electoralDistrict":
                tallySheetVersion.submission.area.areaName,
                "data": [],
                "validVoteCounts": [0, "0%"],
                "rejectedVoteCounts": [0, "0%"],
                "totalVoteCounts": [0, "0%"],
                "registeredVoters":
                [to_comma_seperated_num(registered_voters_count), 100],
                "logo":
                convert_image_to_data_uri("static/Emblem_of_Sri_Lanka.png"),
                "date":
                stamp.createdAt.strftime("%d/%m/%Y"),
                "time":
                stamp.createdAt.strftime("%H:%M:%S %p")
            }

            total_valid_vote_count = 0
            for area_wise_valid_vote_count_result_item in area_wise_valid_vote_count_result.itertuples(
            ):
                total_valid_vote_count += float(
                    area_wise_valid_vote_count_result_item.incompleteNumValue)
            content["validVoteCounts"][0] = to_comma_seperated_num(
                total_valid_vote_count)
            content["validVoteCounts"][1] = to_percentage(
                (total_valid_vote_count / registered_voters_count) * 100)

            total_rejected_vote_count = 0
            for area_wise_rejected_vote_count_result_item in area_wise_rejected_vote_count_result.itertuples(
            ):
                total_rejected_vote_count += float(
                    area_wise_rejected_vote_count_result_item.numValue)
            content["rejectedVoteCounts"][0] = to_comma_seperated_num(
                total_rejected_vote_count)
            content["rejectedVoteCounts"][1] = to_percentage(
                (total_rejected_vote_count / registered_voters_count) * 100)

            total_vote_count = 0
            for area_wise_vote_count_result_item in area_wise_vote_count_result.itertuples(
            ):
                total_vote_count += float(
                    area_wise_vote_count_result_item.incompleteNumValue)
            content["totalVoteCounts"][0] = to_comma_seperated_num(
                total_vote_count)
            content["totalVoteCounts"][1] = to_percentage(
                (total_vote_count / registered_voters_count) * 100)

            # sort by vote count descending
            party_wise_valid_vote_count_result = party_wise_valid_vote_count_result.sort_values(
                by=['numValue',
                    "electionPartyId"], ascending=[False, True]).reset_index()

            for party_wise_valid_vote_count_result_item_index, party_wise_valid_vote_count_result_item in party_wise_valid_vote_count_result.iterrows(
            ):
                data_row = [
                    party_wise_valid_vote_count_result_item.partyName,
                    party_wise_valid_vote_count_result_item.partyAbbreviation,
                    to_comma_seperated_num(
                        party_wise_valid_vote_count_result_item.numValue)
                ]

                if total_valid_vote_count > 0:
                    data_row.append(
                        to_percentage(
                            party_wise_valid_vote_count_result_item.numValue *
                            100 / total_valid_vote_count))
                else:
                    data_row.append('')

                content["data"].append(data_row)

            html = render_template(
                'ParliamentaryElection2020/PE-CE-RO-V2-LETTER.html',
                content=content)

            return html
        def html(self, title="", total_registered_voters=None):
            tallySheetVersion = self.tallySheetVersion

            party_wise_valid_vote_count_result = self.get_party_wise_valid_vote_count_result(
            )
            area_wise_rejected_vote_count_result = self.get_area_wise_rejected_vote_count_result(
            )

            stamp = tallySheetVersion.stamp

            registered_voters_count = tallySheetVersion.tallySheet.area.get_registered_voters_count(
                vote_type=tallySheetVersion.tallySheet.election.voteType)
            polling_division_name = tallySheetVersion.tallySheet.area.areaName
            if tallySheetVersion.tallySheet.election.voteType != NonPostal:
                polling_division_name = tallySheetVersion.tallySheet.election.voteType

            content = {
                "election": {
                    "electionName":
                    tallySheetVersion.tallySheet.election.get_official_name(),
                    "provinceName":
                    Area.get_associated_areas(
                        tallySheetVersion.tallySheet.area,
                        AreaTypeEnum.Province)[0].areaName
                },
                "stamp": {
                    "createdAt": stamp.createdAt,
                    "createdBy": stamp.createdBy,
                    "barcodeString": stamp.barcodeString
                },
                "tallySheetCode":
                "PCE-PD-V",
                "administrativeDistrict":
                Area.get_associated_areas(
                    tallySheetVersion.tallySheet.area,
                    AreaTypeEnum.AdministrativeDistrict)[0].areaName,
                "pollingDivision":
                polling_division_name,
                "data": [],
                "totalValidVoteCount":
                '',
                "totalValidVotePercentage":
                '',
                "rejectedVoteCount":
                '',
                "rejectedVotePercentage":
                '',
                "totalVoteCount":
                '',
                "totalVotePercentage":
                '',
                "numberOfElectors":
                to_comma_seperated_num(registered_voters_count),
                "logo":
                convert_image_to_data_uri("static/Emblem_of_Sri_Lanka.png")
            }

            total_valid_vote_count = 0
            total_rejected_vote_count = 0
            total_vote_count = 0
            party_wise_valid_total_votes = []

            for index, party_wise_valid_vote_count_result_item in party_wise_valid_vote_count_result.iterrows(
            ):
                data_row = [party_wise_valid_vote_count_result_item.partyName]
                vote_count = party_wise_valid_vote_count_result_item.numValue
                party_wise_valid_total_votes.append(
                    vote_count if not math.isnan(vote_count) else 0)
                data_row.append(to_comma_seperated_num(vote_count))
                total_valid_vote_count += vote_count if not math.isnan(
                    vote_count) else 0
                content["data"].append(data_row)

            total_valid_vote_count = total_valid_vote_count if not math.isnan(
                total_valid_vote_count) else 0
            content["totalValidVoteCount"] = to_comma_seperated_num(
                total_valid_vote_count)

            # party wise percentage calculation
            for index, party_vote_count in enumerate(
                    party_wise_valid_total_votes):
                percentage_value = to_percentage(0)
                if total_valid_vote_count > 0:
                    percentage_value = to_percentage(party_vote_count * 100 /
                                                     total_valid_vote_count)
                content["data"][index].append(percentage_value)

            for index, area_wise_rejected_vote_count_result_item in area_wise_rejected_vote_count_result.iterrows(
            ):
                rejected_vote_count = area_wise_rejected_vote_count_result_item.numValue
                total_rejected_vote_count += rejected_vote_count if not math.isnan(
                    rejected_vote_count) else 0
            content["rejectedVoteCount"] = to_comma_seperated_num(
                total_rejected_vote_count)

            total_vote_count = total_valid_vote_count + total_rejected_vote_count
            content["totalVoteCount"] = to_comma_seperated_num(
                total_vote_count)
            total_valid_vote_percentage = (
                total_valid_vote_count * 100 /
                total_vote_count) if total_vote_count > 0 else 0
            content["totalValidVotePercentage"] = to_percentage(
                total_valid_vote_percentage)
            rejected_vote_percentage = (
                total_rejected_vote_count * 100 /
                total_vote_count) if total_vote_count > 0 else 0
            content["rejectedVotePercentage"] = to_percentage(
                rejected_vote_percentage)
            total_vote_percentage = (
                total_vote_count * 100 /
                registered_voters_count) if registered_voters_count > 0 else 0
            content["totalVotePercentage"] = to_percentage(
                total_vote_percentage)

            html = render_template(
                'ProvincialCouncilElection2021/PCE-PD-V.html', content=content)

            return html
예제 #6
0
        def html_letter(self,
                        title="",
                        total_registered_voters=None,
                        signatures=[]):
            tallySheetVersion = self.tallySheetVersion
            party_wise_valid_vote_count_result = self.get_party_wise_seat_calculations(
            )
            area_wise_valid_vote_count_result = self.get_area_wise_valid_vote_count_result(
            )
            area_wise_rejected_vote_count_result = self.get_area_wise_rejected_vote_count_result(
            )
            area_wise_vote_count_result = self.get_area_wise_vote_count_result(
            )
            stamp = tallySheetVersion.stamp

            registered_voters_count = tallySheetVersion.tallySheet.area.get_registered_voters_count(
            )
            content = {
                "election": {
                    "electionName":
                    tallySheetVersion.tallySheet.election.get_official_name(),
                    "provinceName":
                    tallySheetVersion.tallySheet.area.areaName
                },
                "stamp": {
                    "createdAt": stamp.createdAt,
                    "createdBy": stamp.createdBy,
                    "barcodeString": stamp.barcodeString
                },
                "signatures":
                signatures,
                "data": [],
                "validVoteCounts": [0, "0%"],
                "rejectedVoteCounts": [0, "0%"],
                "totalVoteCounts": [0, "0%"],
                "registeredVoters":
                [to_comma_seperated_num(registered_voters_count), 100],
                "logo":
                convert_image_to_data_uri("static/Emblem_of_Sri_Lanka.png"),
                "date":
                stamp.createdAt.strftime("%d/%m/%Y"),
                "time":
                stamp.createdAt.strftime("%H:%M:%S %p")
            }

            total_vote_count = 0
            for area_wise_vote_count_result_item in area_wise_vote_count_result.itertuples(
            ):
                total_vote_count += float(
                    area_wise_vote_count_result_item.incompleteNumValue)
            content["totalVoteCounts"][0] = to_comma_seperated_num(
                total_vote_count)
            content["totalVoteCounts"][1] = to_percentage(
                (total_vote_count / registered_voters_count) * 100)

            total_valid_vote_count = 0
            for area_wise_valid_vote_count_result_item in area_wise_valid_vote_count_result.itertuples(
            ):
                total_valid_vote_count += float(
                    area_wise_valid_vote_count_result_item.incompleteNumValue)
            content["validVoteCounts"][0] = to_comma_seperated_num(
                total_valid_vote_count)
            content["validVoteCounts"][1] = to_percentage(
                (total_valid_vote_count / total_vote_count) * 100)

            total_rejected_vote_count = 0
            for area_wise_rejected_vote_count_result_item in area_wise_rejected_vote_count_result.itertuples(
            ):
                total_rejected_vote_count += float(
                    area_wise_rejected_vote_count_result_item.numValue)
            content["rejectedVoteCounts"][0] = to_comma_seperated_num(
                total_rejected_vote_count)
            content["rejectedVoteCounts"][1] = to_percentage(
                (total_rejected_vote_count / total_vote_count) * 100)

            for party_wise_valid_vote_count_result_item_index, party_wise_valid_vote_count_result_item in party_wise_valid_vote_count_result.iterrows(
            ):

                data_row = [
                    party_wise_valid_vote_count_result_item.partyName,
                    party_wise_valid_vote_count_result_item.partyAbbreviation,
                    to_comma_seperated_num(
                        party_wise_valid_vote_count_result_item.numValue)
                ]

                if total_valid_vote_count > 0:
                    data_row.append(
                        to_percentage(
                            party_wise_valid_vote_count_result_item.numValue *
                            100 / total_valid_vote_count))
                else:
                    data_row.append('')

                data_row.append(
                    to_comma_seperated_num(
                        party_wise_valid_vote_count_result_item.seatsAllocated)
                )

                content["data"].append(data_row)

            html = render_template(
                'ProvincialCouncilElection2021/PCE-PC-BS-1-LETTER.html',
                content=content)

            return html
예제 #7
0
    def get_html_content_dict(self):
        candidate_and_area_wise_valid_vote_count_result = self.candidate_and_area_wise_valid_vote_count_query(
        ).all()
        candidate_wise_vote_count_result = self.candidate_wise_vote_count(
        ).all()
        area_wise_vote_count_result = self.area_wise_vote_count_query().all()
        vote_count_result = self.vote_count_query().one_or_none()

        stamp = self.stamp

        content = {
            "election": {
                "electionName": self.submission.election.get_official_name()
            },
            "stamp": {
                "createdAt": stamp.createdAt,
                "createdBy": stamp.createdBy,
                "barcodeString": stamp.barcodeString
            },
            "electoralDistricts": [],
            "data": [],
            "validVoteCounts": [],
            "rejectedVoteCounts": [],
            "totalVoteCounts": [],
            "registeredVoterCounts": []
        }

        # Append the area wise column totals
        for area_wise_vote_count_result_item in area_wise_vote_count_result:
            content["electoralDistricts"].append(
                area_wise_vote_count_result_item.areaName)
            content["validVoteCounts"].append(
                to_comma_seperated_num(
                    area_wise_vote_count_result_item.validVoteCount))
            content["rejectedVoteCounts"].append(
                to_comma_seperated_num(
                    area_wise_vote_count_result_item.rejectedVoteCount))
            content["totalVoteCounts"].append(
                to_comma_seperated_num(
                    area_wise_vote_count_result_item.totalVoteCount))
            content["registeredVoterCounts"].append(
                to_comma_seperated_num(
                    area_wise_vote_count_result_item.registeredVotersCount))

        # Append the grand totals
        content["validVoteCounts"].append(
            to_comma_seperated_num(vote_count_result.validVoteCount))
        content["rejectedVoteCounts"].append(
            to_comma_seperated_num(vote_count_result.rejectedVoteCount))
        content["totalVoteCounts"].append(
            to_comma_seperated_num(vote_count_result.totalVoteCount))
        content["registeredVoterCounts"].append(
            to_comma_seperated_num(vote_count_result.registeredVotersCount))

        # Append the percentages.
        content["validVoteCounts"].append(
            to_percentage(vote_count_result.validVoteCount * 100 /
                          vote_count_result.totalVoteCount))
        content["rejectedVoteCounts"].append(
            to_percentage(vote_count_result.rejectedVoteCount * 100 /
                          vote_count_result.totalVoteCount))
        content["totalVoteCounts"].append(
            to_percentage(vote_count_result.totalVoteCount * 100 /
                          vote_count_result.registeredVotersCount))

        number_of_electoral_districts = len(area_wise_vote_count_result)
        number_of_candidates = len(candidate_wise_vote_count_result)

        for candidate_wise_vote_count_result_item_index in range(
                number_of_candidates):
            candidate_wise_vote_count_result_item = candidate_wise_vote_count_result[
                candidate_wise_vote_count_result_item_index]

            data_row = []

            # data_row_number = candidate_wise_vote_count_result_item_index + 1
            # data_row.append(data_row_number)

            data_row.append(
                candidate_wise_vote_count_result_item.candidateName)

            for electoral_district_index in range(
                    number_of_electoral_districts):
                candidate_and_area_wise_valid_vote_count_result_item_index = \
                    (
                            number_of_electoral_districts * candidate_wise_vote_count_result_item_index) + electoral_district_index

                candidate_and_area_wise_valid_vote_count_result_item = candidate_and_area_wise_valid_vote_count_result[
                    candidate_and_area_wise_valid_vote_count_result_item_index]

                data_row.append(
                    to_comma_seperated_num(
                        candidate_and_area_wise_valid_vote_count_result_item.
                        validVoteCount))

            data_row.append(
                to_comma_seperated_num(
                    candidate_wise_vote_count_result_item.validVoteCount))

            candidate_wise_vote_count_percentage = (
                candidate_wise_vote_count_result_item.validVoteCount /
                vote_count_result.validVoteCount) * 100
            data_row.append(
                to_percentage(candidate_wise_vote_count_percentage))

            content["data"].append(data_row)

        return content
예제 #8
0
    def html_letter(self, title="", total_registered_voters=None):
        tallySheetVersion = self.tallySheetVersion
        stamp = tallySheetVersion.stamp

        if total_registered_voters is None:
            total_registered_voters = float(
                tallySheetVersion.submission.area.registeredVotersCount)

        content = {
            "resultTitle":
            title,
            "election": {
                "electionName":
                tallySheetVersion.submission.election.get_official_name()
            },
            "stamp": {
                "createdAt": stamp.createdAt,
                "createdBy": stamp.createdBy,
                "barcodeString": stamp.barcodeString
            },
            "date":
            stamp.createdAt.strftime("%d/%m/%Y"),
            "time":
            stamp.createdAt.strftime("%H:%M:%S %p"),
            "data": [],
            "validVoteCounts": [0, 0],
            "rejectedVoteCounts": [0, 0],
            "totalVoteCounts": [0, 0],
            "registeredVoters":
            [to_comma_seperated_num(total_registered_voters), 100]
        }

        candidate_wise_valid_vote_count_result = self.get_candidate_wise_valid_vote_count_result(
        )
        vote_count_result = self.get_vote_count_result()
        valid_vote_count_result = self.get_valid_vote_count_result()
        rejected_vote_count_result = self.get_rejected_vote_count_result()

        for candidate_wise_valid_non_postal_vote_count_result_item in candidate_wise_valid_vote_count_result.itertuples(
        ):
            content["data"].append([
                candidate_wise_valid_non_postal_vote_count_result_item.
                candidateName,
                candidate_wise_valid_non_postal_vote_count_result_item.
                partyAbbreviation,
                to_comma_seperated_num(
                    candidate_wise_valid_non_postal_vote_count_result_item.
                    numValue),
                to_percentage(
                    candidate_wise_valid_non_postal_vote_count_result_item.
                    numValue * 100 /
                    valid_vote_count_result["numValue"].values[0])
                if valid_vote_count_result["numValue"].values[0] > 0 else ""
            ])

        content["validVoteCounts"] = [
            to_comma_seperated_num(
                valid_vote_count_result["numValue"].values[0]),
            to_percentage(valid_vote_count_result["numValue"].values[0] * 100 /
                          vote_count_result["numValue"].values[0])
            if vote_count_result["numValue"].values[0] > 0 else ""
        ]

        content["rejectedVoteCounts"] = [
            to_comma_seperated_num(
                rejected_vote_count_result["numValue"].values[0]),
            rejected_vote_count_result["numValue"].values[0] * 100 /
            vote_count_result["numValue"].values[0]
            if vote_count_result["numValue"].values[0] > 0 else ""
        ]

        content["totalVoteCounts"] = [
            to_comma_seperated_num(vote_count_result["numValue"].values[0]),
            to_percentage(vote_count_result["numValue"].values[0] * 100 /
                          total_registered_voters)
        ]

        content["logo"] = convert_image_to_data_uri(
            "static/Emblem_of_Sri_Lanka.png")

        html = render_template('PRE_ALL_ISLAND_RESULTS.html', content=content)

        return html
        def html(self, title="", total_registered_voters=None):
            tallySheetVersion = self.tallySheetVersion

            party_wise_valid_vote_count_result = self.get_party_wise_valid_vote_count_result(
            )
            area_wise_rejected_vote_count_result = self.get_area_wise_rejected_vote_count_result(
            )

            stamp = tallySheetVersion.stamp

            area: AreaModel = tallySheetVersion.submission.area
            pollingDivision = area.areaName
            number_of_electors = float(area.registeredVotersCount)
            if tallySheetVersion.submission.election.voteType == Postal:
                pollingDivision = 'Postal'
                number_of_electors = float(area.registeredPostalVotersCount)

            content = {
                "election": {
                    "electionName":
                    tallySheetVersion.submission.election.get_official_name()
                },
                "stamp": {
                    "createdAt": stamp.createdAt,
                    "createdBy": stamp.createdBy,
                    "barcodeString": stamp.barcodeString
                },
                "tallySheetCode":
                "POLLING DIVISION RESULTS",
                "electoralDistrict":
                Area.get_associated_areas(
                    tallySheetVersion.submission.area,
                    AreaTypeEnum.ElectoralDistrict)[0].areaName,
                "pollingDivision":
                pollingDivision,
                "data": [],
                "totalValidVoteCount":
                '',
                "totalValidVotePercentage":
                '',
                "rejectedVoteCount":
                '',
                "rejectedVotePercentage":
                '',
                "totalVoteCount":
                '',
                "totalVotePercentage":
                '',
                "numberOfElectors":
                to_comma_seperated_num(number_of_electors)
            }

            total_valid_vote_count = 0
            total_rejected_vote_count = 0
            total_vote_count = 0
            party_wise_valid_total_votes = []

            for index, party_wise_valid_vote_count_result_item in party_wise_valid_vote_count_result.iterrows(
            ):
                data_row = [party_wise_valid_vote_count_result_item.partyName]
                vote_count = party_wise_valid_vote_count_result_item.numValue
                party_wise_valid_total_votes.append(
                    vote_count if not math.isnan(vote_count) else 0)
                data_row.append(to_comma_seperated_num(vote_count))
                total_valid_vote_count += vote_count if not math.isnan(
                    vote_count) else 0
                content["data"].append(data_row)

            total_valid_vote_count = total_valid_vote_count if not math.isnan(
                total_valid_vote_count) else 0
            content["totalValidVoteCount"] = to_comma_seperated_num(
                total_valid_vote_count)

            # party wise percentage calculation
            for index, party_vote_count in enumerate(
                    party_wise_valid_total_votes):
                percentage_value = to_percentage(0)
                if total_valid_vote_count > 0:
                    percentage_value = to_percentage(party_vote_count * 100 /
                                                     total_valid_vote_count)
                content["data"][index].append(percentage_value)

            for index, area_wise_rejected_vote_count_result_item in area_wise_rejected_vote_count_result.iterrows(
            ):
                rejected_vote_count = area_wise_rejected_vote_count_result_item.numValue
                total_rejected_vote_count += rejected_vote_count if not math.isnan(
                    rejected_vote_count) else 0
            content["rejectedVoteCount"] = to_comma_seperated_num(
                total_rejected_vote_count)

            total_vote_count = total_valid_vote_count + total_rejected_vote_count
            content["totalVoteCount"] = to_comma_seperated_num(
                total_vote_count)
            total_valid_vote_percentage = (
                total_valid_vote_count * 100 /
                total_vote_count) if total_vote_count > 0 else 0
            content["totalValidVotePercentage"] = to_percentage(
                total_valid_vote_percentage)
            rejected_vote_percentage = (
                total_rejected_vote_count * 100 /
                total_vote_count) if total_vote_count > 0 else 0
            content["rejectedVotePercentage"] = to_percentage(
                rejected_vote_percentage)
            total_vote_percentage = (
                total_vote_count * 100 /
                number_of_electors) if number_of_electors > 0 else 0
            content["totalVotePercentage"] = to_percentage(
                total_vote_percentage)

            html = render_template('POLLING-DIVISION-RESULTS.html',
                                   content=content)

            return html
예제 #10
0
    def html_letter(self):

        stamp = self.stamp
        electoralDistrict = self.submission.area

        content = {
            "resultTitle":
            "Results of Electoral District %s" % electoralDistrict.areaName,
            "election": {
                "electionName": self.submission.election.get_official_name()
            },
            "stamp": {
                "createdAt": stamp.createdAt,
                "createdBy": stamp.createdBy,
                "barcodeString": stamp.barcodeString
            },
            "date":
            stamp.createdAt.strftime("%d/%m/%Y"),
            "time":
            stamp.createdAt.strftime("%H:%M:%S %p"),
            "data": [],
            "validVoteCounts": [0, 0],
            "rejectedVoteCounts": [0, 0],
            "totalVoteCounts": [0, 0],
            "registeredVoters": [
                to_comma_seperated_num(
                    self.submission.area.registeredVotersCount), 100
            ],
            "electoralDistrict":
            electoralDistrict.areaName
        }

        candidate_wise_vote_count_result = self.candidate_wise_vote_count(
        ).all()
        vote_count_result = self.vote_count_query().one_or_none()

        for candidate_wise_valid_vote_count_result_item in candidate_wise_vote_count_result:
            content["data"].append([
                candidate_wise_valid_vote_count_result_item.candidateName,
                candidate_wise_valid_vote_count_result_item.partyAbbreviation,
                to_comma_seperated_num(
                    candidate_wise_valid_vote_count_result_item.validVoteCount
                ),
                to_percentage(candidate_wise_valid_vote_count_result_item.
                              validVotePercentage)
            ])

        content["validVoteCounts"] = [
            to_comma_seperated_num(vote_count_result.validVoteCount),
            to_percentage(vote_count_result.validVoteCount * 100 /
                          self.submission.area.registeredVotersCount)
        ]

        content["rejectedVoteCounts"] = [
            to_comma_seperated_num(vote_count_result.rejectedVoteCount),
            to_percentage(vote_count_result.rejectedVoteCount * 100 /
                          self.submission.area.registeredVotersCount)
        ]

        content["totalVoteCounts"] = [
            to_comma_seperated_num(vote_count_result.totalVoteCount),
            to_percentage(vote_count_result.totalVoteCount * 100 /
                          self.submission.area.registeredVotersCount)
        ]

        content["logo"] = convert_image_to_data_uri(
            "static/Emblem_of_Sri_Lanka.png")

        html = render_template('PRE_ALL_ISLAND_RESULTS.html', content=content)

        return html
예제 #11
0
 def read(self):
     value = super().read()
     return to_percentage(value, min=self.dry, max=self.wet)
예제 #12
0
    def html_letter(self):

        stamp = self.stamp

        content = {
            "election": {
                "electionName": self.submission.election.get_official_name()
            },
            "stamp": {
                "createdAt": stamp.createdAt,
                "createdBy": stamp.createdBy,
                "barcodeString": stamp.barcodeString
            },
            "date":
            stamp.createdAt.strftime("%d/%m/%Y"),
            "time":
            stamp.createdAt.strftime("%H:%M:%S %p"),
            "data": [],
            "validVoteCounts": [0, 0],
            "rejectedVoteCounts": [0, 0],
            "totalVoteCounts": [0, 0],
            "registeredVoters": [
                to_comma_seperated_num(
                    self.submission.area.registeredVotersCount), 100
            ],
            "electoralDistrict":
            self.submission.area.areaName
        }

        candidate_wise_vote_count_result = self.candidate_wise_vote_count(
        ).all()
        vote_count_result = self.vote_count_query().one_or_none()

        for candidate_wise_valid_vote_count_result_item in candidate_wise_vote_count_result:
            content["data"].append([
                candidate_wise_valid_vote_count_result_item.candidateName,
                candidate_wise_valid_vote_count_result_item.partyAbbreviation,
                to_comma_seperated_num(
                    candidate_wise_valid_vote_count_result_item.validVoteCount
                ),
                to_percentage(candidate_wise_valid_vote_count_result_item.
                              validVotePercentage)
            ])

        content["validVoteCounts"] = [
            to_comma_seperated_num(vote_count_result.validVoteCount),
            to_percentage(vote_count_result.validVoteCount * 100 /
                          self.submission.area.registeredVotersCount)
        ]

        content["rejectedVoteCounts"] = [
            to_comma_seperated_num(vote_count_result.rejectedVoteCount),
            to_percentage(vote_count_result.rejectedVoteCount * 100 /
                          self.submission.area.registeredVotersCount)
        ]

        content["totalVoteCounts"] = [
            to_comma_seperated_num(vote_count_result.totalVoteCount),
            to_percentage(vote_count_result.totalVoteCount * 100 /
                          self.submission.area.registeredVotersCount)
        ]

        html = render_template('PRE-30-ED-LETTER.html', content=content)

        return html
        def html_letter(self, title="", total_registered_voters=None):
            tallySheetVersion = self.tallySheetVersion
            party_wise_results = self.get_party_wise_results()
            stamp = tallySheetVersion.stamp

            registered_voters_count = tallySheetVersion.submission.area.get_registered_voters_count(
            )
            content = {
                "election": {
                    "electionName":
                    tallySheetVersion.submission.election.get_official_name()
                },
                "stamp": {
                    "createdAt": stamp.createdAt,
                    "createdBy": stamp.createdBy,
                    "barcodeString": stamp.barcodeString
                },
                "data": [],
                "validVoteCounts": [0, "0%"],
                "rejectedVoteCounts": [0, "0%"],
                "totalVoteCounts": [0, "0%"],
                "registeredVoters":
                [to_comma_seperated_num(registered_voters_count), 100],
                "logo":
                convert_image_to_data_uri("static/Emblem_of_Sri_Lanka.png"),
                "date":
                stamp.createdAt.strftime("%d/%m/%Y"),
                "time":
                stamp.createdAt.strftime("%H:%M:%S %p")
            }

            total_valid_vote_count = 0
            total_rejected_vote_count = self.get_rejected_vote_count_result(
            )["numValue"].values[0]
            for party_wise_result in party_wise_results.itertuples():
                total_valid_vote_count += float(
                    party_wise_result.validVoteCount)
            total_vote_count = total_valid_vote_count + total_rejected_vote_count

            content["validVoteCounts"][0] = to_comma_seperated_num(
                total_valid_vote_count)
            content["validVoteCounts"][1] = to_percentage(
                (total_valid_vote_count / registered_voters_count) * 100)

            content["rejectedVoteCounts"][0] = to_comma_seperated_num(
                total_rejected_vote_count)
            content["rejectedVoteCounts"][1] = to_percentage(
                (total_rejected_vote_count / registered_voters_count) * 100)

            content["totalVoteCounts"][0] = to_comma_seperated_num(
                total_vote_count)
            content["totalVoteCounts"][1] = to_percentage(
                (total_vote_count / registered_voters_count) * 100)

            party_wise_results = self.get_party_wise_results().sort_values(
                by=["totalSeatsAllocated", "numValue", "electionPartyId"],
                ascending=[False, False, True]).reset_index()

            for party_wise_result in party_wise_results.itertuples():
                data_row = [
                    party_wise_result.partyName,
                    party_wise_result.partyAbbreviation,
                    to_comma_seperated_num(party_wise_result.validVoteCount)
                ]

                if total_valid_vote_count > 0:
                    data_row.append(
                        to_percentage(party_wise_result.validVoteCount * 100 /
                                      total_valid_vote_count))
                else:
                    data_row.append('')

                data_row.append(
                    to_comma_seperated_num(party_wise_result.seatsAllocated))
                data_row.append(
                    to_comma_seperated_num(
                        party_wise_result.nationalListSeatsAllocated))
                data_row.append(
                    to_comma_seperated_num(
                        party_wise_result.totalSeatsAllocated))

                content["data"].append(data_row)

            html = render_template(
                'ParliamentaryElection2020/PE-AI-1-LETTER.html',
                content=content)

            return html
예제 #14
0
    def html_letter(self):

        stamp = self.stamp
        total_registered_voters = get_polling_division_total_registered_voters(tallySheetVersion=self)
        electoral_district = Area.get_associated_areas(self.submission.area, AreaTypeEnum.ElectoralDistrict)[0]
        polling_division = self.submission.area

        content = {
            "resultTitle": "Results of PD",
            "election": {
                "electionName": self.submission.election.get_official_name(),
                "isPostal": self.submission.election.voteType == VoteTypeEnum.Postal
            },
            "stamp": {
                "createdAt": stamp.createdAt,
                "createdBy": stamp.createdBy,
                "barcodeString": stamp.barcodeString
            },
            "date": stamp.createdAt.strftime("%d/%m/%Y"),
            "time": stamp.createdAt.strftime("%H:%M:%S %p"),
            "data": [
            ],
            "validVoteCounts": [0, 0],
            "rejectedVoteCounts": [0, 0],
            "totalVoteCounts": [0, 0],
            "registeredVoters": [
                to_comma_seperated_num(total_registered_voters),
                100
            ],
            "electoralDistrict": electoral_district.areaName,
            "pollingDivision": polling_division.areaName
        }

        if self.submission.election.voteType == VoteTypeEnum.Postal:
            content["tallySheetCode"] = "PRE/30/PV"
            content["pollingDivision"] = "Postal"
            content["resultTitle"] = "Results of Electoral District %s (Postal)" % electoral_district.areaName
        else:
            content["pollingDivision"] = self.submission.area.areaName
            content["resultTitle"] = "Results of Electoral District %s - Polling Division %s" % (
                electoral_district.areaName,
                polling_division.areaName
            )

        candidate_wise_vote_count_result = self.candidate_wise_vote_count().all()
        vote_count_result = self.vote_count_query().one_or_none()

        for candidate_wise_valid_vote_count_result_item in candidate_wise_vote_count_result:
            content["data"].append([
                candidate_wise_valid_vote_count_result_item.candidateName,
                candidate_wise_valid_vote_count_result_item.partyAbbreviation,
                to_comma_seperated_num(candidate_wise_valid_vote_count_result_item.validVoteCount),
                to_percentage(candidate_wise_valid_vote_count_result_item.validVotePercentage)
            ])

        content["validVoteCounts"] = [
            to_comma_seperated_num(vote_count_result.validVoteCount),
            to_percentage(vote_count_result.validVoteCount * 100 / vote_count_result.totalVoteCount)
            if vote_count_result.totalVoteCount > 0 else ""
        ]

        content["rejectedVoteCounts"] = [
            to_comma_seperated_num(vote_count_result.rejectedVoteCount),
            to_percentage(vote_count_result.rejectedVoteCount * 100 / vote_count_result.totalVoteCount)
            if vote_count_result.totalVoteCount > 0 else ""
        ]

        content["totalVoteCounts"] = [
            to_comma_seperated_num(vote_count_result.totalVoteCount),
            to_percentage(vote_count_result.totalVoteCount * 100 / total_registered_voters)
        ]

        content["logo"] = convert_image_to_data_uri("static/Emblem_of_Sri_Lanka.png")

        html = render_template(
            'PRE_ALL_ISLAND_RESULTS.html',
            content=content
        )

        return html
        def html(self, title="", total_registered_voters=None):
            tallySheetVersion = self.tallySheetVersion

            party_wise_valid_vote_count_result = self.get_party_wise_valid_vote_count_result(
            ).sort_values(by=['incompleteNumValue'],
                          ascending=False).reset_index()
            area_wise_rejected_vote_count_result = self.get_area_wise_rejected_vote_count_result(
            )

            stamp = tallySheetVersion.stamp

            area: AreaModel = tallySheetVersion.submission.area
            number_of_electors = float(area.registeredVotersCount) + float(
                area.registeredPostalVotersCount)

            content = {
                "election": {
                    "electionName":
                    tallySheetVersion.submission.election.get_official_name()
                },
                "stamp": {
                    "createdAt": stamp.createdAt,
                    "createdBy": stamp.createdBy,
                    "barcodeString": stamp.barcodeString
                },
                "tallySheetCode": "ALL ISLAND RESULTS",
                "data": [],
                "totalValidVoteCount": '',
                "totalValidVotePercentage": '',
                "rejectedVoteCount": '',
                "rejectedVotePercentage": '',
                "totalVoteCount": '',
                "totalVotePercentage": '',
                "numberOfElectors": to_comma_seperated_num(number_of_electors)
            }

            total_valid_vote_count = 0
            total_rejected_vote_count = 0
            total_vote_count = 0
            party_wise_valid_total_votes = []

            for index, party_wise_valid_vote_count_result_item in party_wise_valid_vote_count_result.iterrows(
            ):
                data_row = [party_wise_valid_vote_count_result_item.partyName]
                vote_count = party_wise_valid_vote_count_result_item.incompleteNumValue
                party_wise_valid_total_votes.append(
                    vote_count if not math.isnan(vote_count) else 0)
                data_row.append(to_comma_seperated_num(vote_count))
                total_valid_vote_count += vote_count if not math.isnan(
                    vote_count) else 0
                content["data"].append(data_row)

            total_valid_vote_count = total_valid_vote_count if not math.isnan(
                total_valid_vote_count) else 0
            content["totalValidVoteCount"] = to_comma_seperated_num(
                total_valid_vote_count)

            # party wise percentage calculation
            for index, party_vote_count in enumerate(
                    party_wise_valid_total_votes):
                percentage_value = to_percentage(0)
                if total_valid_vote_count > 0:
                    percentage_value = to_percentage(party_vote_count * 100 /
                                                     total_valid_vote_count)
                content["data"][index].append(percentage_value)

            for index, area_wise_rejected_vote_count_result_item in area_wise_rejected_vote_count_result.iterrows(
            ):
                rejected_vote_count = area_wise_rejected_vote_count_result_item.numValue
                total_rejected_vote_count += rejected_vote_count if not math.isnan(
                    rejected_vote_count) else 0
            content["rejectedVoteCount"] = to_comma_seperated_num(
                total_rejected_vote_count)

            total_vote_count = total_valid_vote_count + total_rejected_vote_count
            content["totalVoteCount"] = to_comma_seperated_num(
                total_vote_count)
            total_valid_vote_percentage = (
                total_valid_vote_count * 100 /
                total_vote_count) if total_vote_count > 0 else 0
            content["totalValidVotePercentage"] = to_percentage(
                total_valid_vote_percentage)
            rejected_vote_percentage = (
                total_rejected_vote_count * 100 /
                total_vote_count) if total_vote_count > 0 else 0
            content["rejectedVotePercentage"] = to_percentage(
                rejected_vote_percentage)
            total_vote_percentage = (
                total_vote_count * 100 /
                number_of_electors) if number_of_electors > 0 else 0
            content["totalVotePercentage"] = to_percentage(
                total_vote_percentage)

            html = render_template('ALL_ISLAND_RESULT.html', content=content)

            return html