From 68b70cf9b270dde093347384603a2e97b84f69e6 Mon Sep 17 00:00:00 2001 From: Dinuka De Silva Date: Thu, 14 Nov 2019 15:25:37 +0530 Subject: [PATCH 1/2] gh-236: Fix incomplete validation in reviced reports --- .../TallySheetVersion_PRE_34_AI_Api.py | 63 ++++++++++++++---- .../TallySheetVersion_PRE_34_ED_Api.py | 64 +++++++++++++++---- .../TallySheetVersion_PRE_34_PD_Api.py | 57 +++++++++-------- 3 files changed, 135 insertions(+), 49 deletions(-) diff --git a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_AI_Api.py b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_AI_Api.py index 19cc8706..c86cbfdc 100644 --- a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_AI_Api.py +++ b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_AI_Api.py @@ -46,9 +46,35 @@ def create(tallySheetId): query = db.session.query( func.count(Area.Model.areaId).label("areaCount"), ElectionCandidate.Model.candidateId, - TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber, - func.sum(TallySheetVersionRow_PRE_34_preference.Model.preferenceCount).label("preferenceCount"), - Submission.Model.electionId + ElectionCandidate.Model.qualifiedForPreferences, + Submission.Model.electionId, + func.sum( + func.IF( + and_( + TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber == 1 + ), + TallySheetVersionRow_PRE_34_preference.Model.preferenceCount, + None + ) + ).label("firstPreferenceCount"), + func.sum( + func.IF( + and_( + TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber == 2 + ), + TallySheetVersionRow_PRE_34_preference.Model.preferenceCount, + None + ) + ).label("secondPreferenceCount"), + func.sum( + func.IF( + and_( + TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber == 3 + ), + TallySheetVersionRow_PRE_34_preference.Model.preferenceCount, + None + ) + ).label("thirdPreferenceCount"), ).join( Submission.Model, Submission.Model.areaId == Area.Model.areaId @@ -73,26 +99,41 @@ def create(tallySheetId): TallySheetVersionRow_PRE_34_preference.Model.tallySheetVersionId == Submission.Model.lockedVersionId, TallySheetVersionRow_PRE_34_preference.Model.candidateId == ElectionCandidate.Model.candidateId ), + isouter=True ).filter( Submission.Model.areaId.in_(electoral_district_ids) ).group_by( - ElectionCandidate.Model.candidateId, - TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber + ElectionCandidate.Model.candidateId ).order_by( - TallySheetVersionRow_PRE_34_preference.Model.candidateId, - TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber + ElectionCandidate.Model.candidateId ).all() is_complete = True for row in query: - if (row.candidateId and row.preferenceNumber and row.preferenceCount) is not None and ( - row.preferenceNumber != 1 or row.preferenceCount is not None): + if (row.candidateId and row.firstPreferenceCount) is not None: tallySheetVersion.add_row( electionId=row.electionId, candidateId=row.candidateId, - preferenceNumber=row.preferenceNumber, - preferenceCount=row.preferenceCount + preferenceNumber=1, + preferenceCount=row.firstPreferenceCount ) + + if row.qualifiedForPreferences is True: + if (row.secondPreferenceCount and row.thirdPreferenceCount) is not None: + tallySheetVersion.add_row( + electionId=row.electionId, + candidateId=row.candidateId, + preferenceNumber=2, + preferenceCount=row.secondPreferenceCount + ) + tallySheetVersion.add_row( + electionId=row.electionId, + candidateId=row.candidateId, + preferenceNumber=3, + preferenceCount=row.thirdPreferenceCount + ) + else: + is_complete = False else: is_complete = False diff --git a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_ED_Api.py b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_ED_Api.py index 33c0facc..66687990 100644 --- a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_ED_Api.py +++ b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_ED_Api.py @@ -50,9 +50,35 @@ def create(tallySheetId): query = db.session.query( func.count(Area.Model.areaId).label("areaCount"), ElectionCandidate.Model.candidateId, - TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber, - func.sum(TallySheetVersionRow_PRE_34_preference.Model.preferenceCount).label("preferenceCount"), - Submission.Model.electionId + ElectionCandidate.Model.qualifiedForPreferences, + Submission.Model.electionId, + func.sum( + func.IF( + and_( + TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber == 1 + ), + TallySheetVersionRow_PRE_34_preference.Model.preferenceCount, + None + ) + ).label("firstPreferenceCount"), + func.sum( + func.IF( + and_( + TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber == 2 + ), + TallySheetVersionRow_PRE_34_preference.Model.preferenceCount, + None + ) + ).label("secondPreferenceCount"), + func.sum( + func.IF( + and_( + TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber == 3 + ), + TallySheetVersionRow_PRE_34_preference.Model.preferenceCount, + None + ) + ).label("thirdPreferenceCount"), ).join( Submission.Model, Submission.Model.areaId == Area.Model.areaId @@ -77,28 +103,42 @@ def create(tallySheetId): TallySheetVersionRow_PRE_34_preference.Model.tallySheetVersionId == Submission.Model.lockedVersionId, TallySheetVersionRow_PRE_34_preference.Model.candidateId == ElectionCandidate.Model.candidateId ), + isouter=True ).filter( Submission.Model.areaId.in_(electoral_district_and_polling_division_ids), # ElectionCandidate.Model.qualifiedForPreferences == True ).group_by( - ElectionCandidate.Model.candidateId, - TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber + ElectionCandidate.Model.candidateId ).order_by( - TallySheetVersionRow_PRE_34_preference.Model.candidateId, - TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber + ElectionCandidate.Model.candidateId ).all() is_complete = True for row in query: - if (row.candidateId and row.preferenceNumber) is not None and ( - row.preferenceNumber != 1 or row.preferenceCount is not None): - + if (row.candidateId and row.firstPreferenceCount) is not None: tallySheetVersion.add_row( electionId=row.electionId, candidateId=row.candidateId, - preferenceNumber=row.preferenceNumber, - preferenceCount=row.preferenceCount + preferenceNumber=1, + preferenceCount=row.firstPreferenceCount ) + + if row.qualifiedForPreferences is True: + if (row.secondPreferenceCount and row.thirdPreferenceCount) is not None: + tallySheetVersion.add_row( + electionId=row.electionId, + candidateId=row.candidateId, + preferenceNumber=2, + preferenceCount=row.secondPreferenceCount + ) + tallySheetVersion.add_row( + electionId=row.electionId, + candidateId=row.candidateId, + preferenceNumber=3, + preferenceCount=row.thirdPreferenceCount + ) + else: + is_complete = False else: is_complete = False diff --git a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_PD_Api.py b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_PD_Api.py index e735a757..703def1d 100644 --- a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_PD_Api.py +++ b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_PD_Api.py @@ -39,12 +39,9 @@ def create(tallySheetId): Election.Model.electionId, Area.Model.areaId, ElectionCandidate.Model.candidateId, + ElectionCandidate.Model.qualifiedForPreferences, func.sum( - func.IF( - TallySheetVersionRow_PRE_30_PD.Model.count == None, - 0, - TallySheetVersionRow_PRE_30_PD.Model.count - ) + TallySheetVersionRow_PRE_30_PD.Model.count ).label("firstPreferenceCount"), func.sum( func.IF( @@ -52,7 +49,7 @@ def create(tallySheetId): TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber == 2 ), TallySheetVersionRow_PRE_34_preference.Model.preferenceCount, - 0 + None ) ).label("secondPreferenceCount"), func.sum( @@ -61,7 +58,7 @@ def create(tallySheetId): TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber == 3 ), TallySheetVersionRow_PRE_34_preference.Model.preferenceCount, - 0 + None ) ).label("thirdPreferenceCount"), ).join( @@ -109,26 +106,34 @@ def create(tallySheetId): is_complete = True for row in query: + + print("###### ABCD #### ", + [row.candidateId, row.qualifiedForPreferences, row.firstPreferenceCount, row.secondPreferenceCount, + row.thirdPreferenceCount]) if (row.candidateId and row.firstPreferenceCount) is not None: - if (row.secondPreferenceCount and row.thirdPreferenceCount) is not None: - tallySheetVersion.add_row( - electionId=row.electionId, - candidateId=row.candidateId, - preferenceNumber=1, - preferenceCount=row.firstPreferenceCount - ) - tallySheetVersion.add_row( - electionId=row.electionId, - candidateId=row.candidateId, - preferenceNumber=2, - preferenceCount=row.secondPreferenceCount - ) - tallySheetVersion.add_row( - electionId=row.electionId, - candidateId=row.candidateId, - preferenceNumber=3, - preferenceCount=row.thirdPreferenceCount - ) + tallySheetVersion.add_row( + electionId=row.electionId, + candidateId=row.candidateId, + preferenceNumber=1, + preferenceCount=row.firstPreferenceCount + ) + + if row.qualifiedForPreferences is True: + if (row.secondPreferenceCount and row.thirdPreferenceCount) is not None: + tallySheetVersion.add_row( + electionId=row.electionId, + candidateId=row.candidateId, + preferenceNumber=2, + preferenceCount=row.secondPreferenceCount + ) + tallySheetVersion.add_row( + electionId=row.electionId, + candidateId=row.candidateId, + preferenceNumber=3, + preferenceCount=row.thirdPreferenceCount + ) + else: + is_complete = False else: is_complete = False From b8f7e95d518dc87397a234eabcaae4a484a6a784 Mon Sep 17 00:00:00 2001 From: Dinuka De Silva Date: Thu, 14 Nov 2019 18:32:07 +0530 Subject: [PATCH 2/2] gh-236: Map the preference result rows to areas --- .../TallySheetVersion_PRE_34_AI_Api.py | 19 +-- .../TallySheetVersion_PRE_34_ED_Api.py | 19 +-- .../TallySheetVersion_PRE_34_I_RO_Api.py | 6 +- .../TallySheetVersion_PRE_34_PD_Api.py | 13 +- migrations/versions/6a07b608d1fb_.py | 113 ++++++++++++++++++ .../TallySheetVersion_PRE_34_AI.py | 10 +- .../TallySheetVersion_PRE_34_CO.py | 3 +- .../TallySheetVersion_PRE_34_ED.py | 5 +- .../TallySheetVersion_PRE_34_I_RO.py | 5 +- .../TallySheetVersion_PRE_34_PD.py | 10 +- .../TallySheetVersionRow_PRE_34_preference.py | 17 ++- templates/PRE-34-CO.html | 10 +- 12 files changed, 190 insertions(+), 40 deletions(-) create mode 100644 migrations/versions/6a07b608d1fb_.py diff --git a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_AI_Api.py b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_AI_Api.py index c86cbfdc..c03627a3 100644 --- a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_AI_Api.py +++ b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_AI_Api.py @@ -44,7 +44,7 @@ def create(tallySheetId): electoral_district_ids.append(electoral_district.electoralDistrictId) query = db.session.query( - func.count(Area.Model.areaId).label("areaCount"), + Area.Model.areaId, ElectionCandidate.Model.candidateId, ElectionCandidate.Model.qualifiedForPreferences, Submission.Model.electionId, @@ -101,11 +101,13 @@ def create(tallySheetId): ), isouter=True ).filter( - Submission.Model.areaId.in_(electoral_district_ids) + Area.Model.areaId.in_(electoral_district_ids) ).group_by( - ElectionCandidate.Model.candidateId + ElectionCandidate.Model.candidateId, + Area.Model.areaId ).order_by( - ElectionCandidate.Model.candidateId + ElectionCandidate.Model.candidateId, + Area.Model.areaId ).all() is_complete = True @@ -115,7 +117,8 @@ def create(tallySheetId): electionId=row.electionId, candidateId=row.candidateId, preferenceNumber=1, - preferenceCount=row.firstPreferenceCount + preferenceCount=row.firstPreferenceCount, + areaId=row.areaId ) if row.qualifiedForPreferences is True: @@ -124,13 +127,15 @@ def create(tallySheetId): electionId=row.electionId, candidateId=row.candidateId, preferenceNumber=2, - preferenceCount=row.secondPreferenceCount + preferenceCount=row.secondPreferenceCount, + areaId=row.areaId ) tallySheetVersion.add_row( electionId=row.electionId, candidateId=row.candidateId, preferenceNumber=3, - preferenceCount=row.thirdPreferenceCount + preferenceCount=row.thirdPreferenceCount, + areaId=row.areaId ) else: is_complete = False diff --git a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_ED_Api.py b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_ED_Api.py index 66687990..2fd798f9 100644 --- a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_ED_Api.py +++ b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_ED_Api.py @@ -48,7 +48,7 @@ def create(tallySheetId): electoral_district_and_polling_division_ids.append(polling_division.pollingDivisionId) query = db.session.query( - func.count(Area.Model.areaId).label("areaCount"), + Area.Model.areaId, ElectionCandidate.Model.candidateId, ElectionCandidate.Model.qualifiedForPreferences, Submission.Model.electionId, @@ -105,12 +105,14 @@ def create(tallySheetId): ), isouter=True ).filter( - Submission.Model.areaId.in_(electoral_district_and_polling_division_ids), + Area.Model.areaId.in_(electoral_district_and_polling_division_ids), # ElectionCandidate.Model.qualifiedForPreferences == True ).group_by( - ElectionCandidate.Model.candidateId + ElectionCandidate.Model.candidateId, + Area.Model.areaId ).order_by( - ElectionCandidate.Model.candidateId + ElectionCandidate.Model.candidateId, + Area.Model.areaId ).all() is_complete = True @@ -120,7 +122,8 @@ def create(tallySheetId): electionId=row.electionId, candidateId=row.candidateId, preferenceNumber=1, - preferenceCount=row.firstPreferenceCount + preferenceCount=row.firstPreferenceCount, + areaId=row.areaId ) if row.qualifiedForPreferences is True: @@ -129,13 +132,15 @@ def create(tallySheetId): electionId=row.electionId, candidateId=row.candidateId, preferenceNumber=2, - preferenceCount=row.secondPreferenceCount + preferenceCount=row.secondPreferenceCount, + areaId=row.areaId ) tallySheetVersion.add_row( electionId=row.electionId, candidateId=row.candidateId, preferenceNumber=3, - preferenceCount=row.thirdPreferenceCount + preferenceCount=row.thirdPreferenceCount, + areaId=row.areaId ) else: is_complete = False diff --git a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_I_RO_Api.py b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_I_RO_Api.py index 5e90e682..aecee9f4 100644 --- a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_I_RO_Api.py +++ b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_I_RO_Api.py @@ -37,7 +37,7 @@ def create(tallySheetId): ) query = db.session.query( - func.count(Area.Model.areaId).label("areaCount"), + Area.Model.areaId, Election.Model.electionId, ElectionCandidate.Model.candidateId, TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber, @@ -69,6 +69,7 @@ def create(tallySheetId): Area.Model.areaId.in_([area.areaId for area in countingCentres]), ElectionCandidate.Model.qualifiedForPreferences == True ).group_by( + Area.Model.areaId, ElectionCandidate.Model.candidateId, TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber, ).order_by( @@ -107,7 +108,8 @@ def create(tallySheetId): electionId=row.electionId, candidateId=row.candidateId, preferenceNumber=row.preferenceNumber, - preferenceCount=row.preferenceCount + preferenceCount=row.preferenceCount, + areaId=row.areaId ) else: is_complete = False diff --git a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_PD_Api.py b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_PD_Api.py index 703def1d..6fd92a89 100644 --- a/api/TallySheetVersionApi/TallySheetVersion_PRE_34_PD_Api.py +++ b/api/TallySheetVersionApi/TallySheetVersion_PRE_34_PD_Api.py @@ -35,6 +35,8 @@ def create(tallySheetId): tallySheetCode=TallySheetCodeEnum.PRE_34_PD ) + polling_division_id = tallySheet.submission.areaId + query = db.session.query( Election.Model.electionId, Area.Model.areaId, @@ -94,7 +96,7 @@ def create(tallySheetId): ), isouter=True ).filter( - Area.Model.areaId == tallySheet.submission.areaId, + Area.Model.areaId == polling_division_id, # ElectionCandidate.Model.qualifiedForPreferences == True ).group_by( ElectionCandidate.Model.candidateId, @@ -115,7 +117,8 @@ def create(tallySheetId): electionId=row.electionId, candidateId=row.candidateId, preferenceNumber=1, - preferenceCount=row.firstPreferenceCount + preferenceCount=row.firstPreferenceCount, + areaId=polling_division_id ) if row.qualifiedForPreferences is True: @@ -124,13 +127,15 @@ def create(tallySheetId): electionId=row.electionId, candidateId=row.candidateId, preferenceNumber=2, - preferenceCount=row.secondPreferenceCount + preferenceCount=row.secondPreferenceCount, + areaId=polling_division_id ) tallySheetVersion.add_row( electionId=row.electionId, candidateId=row.candidateId, preferenceNumber=3, - preferenceCount=row.thirdPreferenceCount + preferenceCount=row.thirdPreferenceCount, + areaId=polling_division_id ) else: is_complete = False diff --git a/migrations/versions/6a07b608d1fb_.py b/migrations/versions/6a07b608d1fb_.py new file mode 100644 index 00000000..e1d9e7cb --- /dev/null +++ b/migrations/versions/6a07b608d1fb_.py @@ -0,0 +1,113 @@ +"""empty message + +Revision ID: 6a07b608d1fb +Revises: 8da6a3e78339 +Create Date: 2019-11-14 16:35:46.385240 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql +from sqlalchemy.orm import Session +from sqlalchemy.ext.declarative import declarative_base + +# revision identifiers, used by Alembic. +revision = '6a07b608d1fb' +down_revision = '8da6a3e78339' +branch_labels = None +depends_on = None + +Base = declarative_base() +bind = op.get_bind() +session = Session(bind=bind) + + +class _NewTallySheetVersionRow_PRE_34_preference_Model(Base): + __tablename__ = 'tallySheetVersionRow_PRE_34_Preference' + tallySheetVersionRowId = sa.Column(sa.Integer, primary_key=True, autoincrement=True) + tallySheetVersionId = sa.Column(sa.Integer, nullable=False) + electionId = sa.Column(sa.Integer, nullable=False) + candidateId = sa.Column(sa.Integer, nullable=True) + areaId = sa.Column(sa.Integer, nullable=True) + preferenceNumber = sa.Column(sa.Integer, nullable=False) + preferenceCount = sa.Column(sa.Integer, nullable=False) + + +def upgrade(): + existing_records = session.query( + _NewTallySheetVersionRow_PRE_34_preference_Model.tallySheetVersionRowId, + _NewTallySheetVersionRow_PRE_34_preference_Model.tallySheetVersionId, + _NewTallySheetVersionRow_PRE_34_preference_Model.electionId, + _NewTallySheetVersionRow_PRE_34_preference_Model.candidateId, + _NewTallySheetVersionRow_PRE_34_preference_Model.preferenceNumber, + _NewTallySheetVersionRow_PRE_34_preference_Model.preferenceCount + ).all() + + op.drop_table('tallySheetVersionRow_PRE_34_Preference') + op.create_table( + 'tallySheetVersionRow_PRE_34_Preference', + sa.Column('tallySheetVersionRowId', sa.Integer(), nullable=False), + sa.Column('tallySheetVersionId', sa.Integer(), nullable=False), + sa.Column('electionId', sa.Integer(), nullable=False), + sa.Column('candidateId', sa.Integer(), nullable=True), + sa.Column('areaId', sa.Integer(), nullable=True), + sa.Column('preferenceNumber', sa.Integer(), nullable=False), + sa.Column('preferenceCount', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['areaId'], ['area.areaId'], ), + sa.ForeignKeyConstraint(['candidateId'], ['candidate.candidateId'], ), + sa.ForeignKeyConstraint(['electionId'], ['election.electionId'], ), + sa.ForeignKeyConstraint(['tallySheetVersionId'], ['tallySheetVersion.tallySheetVersionId'], ), + sa.PrimaryKeyConstraint('tallySheetVersionRowId'), + sa.UniqueConstraint('tallySheetVersionId', 'preferenceNumber', 'candidateId', 'areaId', + name='TallySheetVersionRow_PRE_34_preference_Model_uk') + ) + + for existing_record in existing_records: + session.add(_NewTallySheetVersionRow_PRE_34_preference_Model( + electionId=existing_record.electionId, + tallySheetVersionId=existing_record.tallySheetVersionId, + preferenceNumber=existing_record.preferenceNumber, + preferenceCount=existing_record.preferenceCount, + candidateId=existing_record.candidateId + )) + + session.commit() + + +def downgrade(): + existing_records = session.query( + _NewTallySheetVersionRow_PRE_34_preference_Model.tallySheetVersionRowId, + _NewTallySheetVersionRow_PRE_34_preference_Model.tallySheetVersionId, + _NewTallySheetVersionRow_PRE_34_preference_Model.electionId, + _NewTallySheetVersionRow_PRE_34_preference_Model.candidateId, + _NewTallySheetVersionRow_PRE_34_preference_Model.preferenceNumber, + _NewTallySheetVersionRow_PRE_34_preference_Model.preferenceCount + ).all() + + op.drop_table('tallySheetVersionRow_PRE_34_Preference') + op.create_table( + 'tallySheetVersionRow_PRE_34_Preference', + sa.Column('tallySheetVersionRowId', sa.Integer(), nullable=False), + sa.Column('tallySheetVersionId', sa.Integer(), nullable=False), + sa.Column('electionId', sa.Integer(), nullable=False), + sa.Column('candidateId', sa.Integer(), nullable=True), + sa.Column('preferenceNumber', sa.Integer(), nullable=False), + sa.Column('preferenceCount', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['candidateId'], ['candidate.candidateId'], ), + sa.ForeignKeyConstraint(['electionId'], ['election.electionId'], ), + sa.ForeignKeyConstraint(['tallySheetVersionId'], ['tallySheetVersion.tallySheetVersionId'], ), + sa.PrimaryKeyConstraint('tallySheetVersionRowId'), + sa.UniqueConstraint('tallySheetVersionId', 'preferenceNumber', 'candidateId', + name='TallySheetVersionRow_PRE_34_preference_Model_uk') + ) + + for existing_record in existing_records: + session.add(_NewTallySheetVersionRow_PRE_34_preference_Model( + electionId=existing_record.electionId, + tallySheetVersionId=existing_record.tallySheetVersionId, + preferenceNumber=existing_record.preferenceNumber, + preferenceCount=existing_record.preferenceCount, + candidateId=existing_record.candidateId + )) + + session.commit() diff --git a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_AI.py b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_AI.py index b57410c6..29a495bd 100644 --- a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_AI.py +++ b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_AI.py @@ -22,7 +22,7 @@ def __init__(self, tallySheetId): 'polymorphic_identity': TallySheetCodeEnum.PRE_34_AI } - def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId): + def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId, areaId): from orm.entities.TallySheetVersionRow import TallySheetVersionRow_PRE_34_preference TallySheetVersionRow_PRE_34_preference.create( @@ -30,7 +30,8 @@ def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId): electionId=electionId, preferenceNumber=preferenceNumber, preferenceCount=preferenceCount, - candidateId=candidateId + candidateId=candidateId, + areaId=areaId ) @hybrid_property @@ -130,7 +131,8 @@ def html(self): return html def json_data(self): - candidate_wise_vote_count_result, total_valid_votes = TallySheetVersion.create_candidate_preference_struct(self.content) + candidate_wise_vote_count_result, total_valid_votes = TallySheetVersion.create_candidate_preference_struct( + self.content) candidates = [] for candidate_wise_valid_vote_count_result_item in candidate_wise_vote_count_result: @@ -141,7 +143,7 @@ def json_data(self): "votes1st": str(candidate_wise_valid_vote_count_result_item['firstPreferenceCount']), "votes2nd": str(candidate_wise_valid_vote_count_result_item['secondPreferenceCount']), "votes3rd": str(candidate_wise_valid_vote_count_result_item['thirdPreferenceCount']), - "percentage": f'{round(total_vote_count*100/total_valid_votes,2)}', + "percentage": f'{round(total_vote_count * 100 / total_valid_votes, 2)}', "party_name": candidate_wise_valid_vote_count_result_item['partyName'], "candidate": candidate_wise_valid_vote_count_result_item['name'] }) diff --git a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_CO.py b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_CO.py index 6d5abd4e..1f279e5f 100644 --- a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_CO.py +++ b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_CO.py @@ -30,7 +30,8 @@ def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId): electionId=electionId, preferenceNumber=preferenceNumber, preferenceCount=preferenceCount, - candidateId=candidateId + candidateId=candidateId, + areaId=self.submission.areaId ) @hybrid_property diff --git a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_ED.py b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_ED.py index c4e4242f..61292bf6 100644 --- a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_ED.py +++ b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_ED.py @@ -22,7 +22,7 @@ def __init__(self, tallySheetId): 'polymorphic_identity': TallySheetCodeEnum.PRE_34_ED } - def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId): + def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId, areaId): from orm.entities.TallySheetVersionRow import TallySheetVersionRow_PRE_34_preference TallySheetVersionRow_PRE_34_preference.create( @@ -30,7 +30,8 @@ def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId): electionId=electionId, preferenceNumber=preferenceNumber, preferenceCount=preferenceCount, - candidateId=candidateId + candidateId=candidateId, + areaId=areaId ) @hybrid_property diff --git a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_I_RO.py b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_I_RO.py index 0429fb9c..e309a5b8 100644 --- a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_I_RO.py +++ b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_I_RO.py @@ -22,7 +22,7 @@ def __init__(self, tallySheetId): 'polymorphic_identity': TallySheetCodeEnum.PRE_34_I_RO } - def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId): + def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId, areaId): from orm.entities.TallySheetVersionRow import TallySheetVersionRow_PRE_34_preference TallySheetVersionRow_PRE_34_preference.create( @@ -30,7 +30,8 @@ def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId): electionId=electionId, preferenceNumber=preferenceNumber, preferenceCount=preferenceCount, - candidateId=candidateId + candidateId=candidateId, + areaId=areaId ) @hybrid_property diff --git a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_PD.py b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_PD.py index cadd543e..80554692 100644 --- a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_PD.py +++ b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersion_PRE_34_PD.py @@ -22,7 +22,7 @@ def __init__(self, tallySheetId): 'polymorphic_identity': TallySheetCodeEnum.PRE_34_PD } - def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId): + def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId, areaId): from orm.entities.TallySheetVersionRow import TallySheetVersionRow_PRE_34_preference TallySheetVersionRow_PRE_34_preference.create( @@ -30,7 +30,8 @@ def add_row(self, preferenceNumber, preferenceCount, candidateId, electionId): electionId=electionId, preferenceNumber=preferenceNumber, preferenceCount=preferenceCount, - candidateId=candidateId + candidateId=candidateId, + areaId=areaId ) @hybrid_property @@ -145,7 +146,8 @@ def json_data(self): electoral_district = Area.get_associated_areas(self.submission.area, AreaTypeEnum.ElectoralDistrict)[0].areaName polling_division = self.submission.area.areaName - candidate_wise_vote_count_result, total_valid_votes = TallySheetVersion.create_candidate_preference_struct(self.content) + candidate_wise_vote_count_result, total_valid_votes = TallySheetVersion.create_candidate_preference_struct( + self.content) candidates = [] for candidate_wise_valid_vote_count_result_item in candidate_wise_vote_count_result: total_vote_count = candidate_wise_valid_vote_count_result_item['total'] @@ -155,7 +157,7 @@ def json_data(self): "votes1st": str(candidate_wise_valid_vote_count_result_item['firstPreferenceCount']), "votes2nd": str(candidate_wise_valid_vote_count_result_item['secondPreferenceCount']), "votes3rd": str(candidate_wise_valid_vote_count_result_item['thirdPreferenceCount']), - "percentage": f'{round(total_vote_count*100/total_valid_votes,2)}', + "percentage": f'{round(total_vote_count * 100 / total_valid_votes, 2)}', "party_name": candidate_wise_valid_vote_count_result_item['partyName'], "candidate": candidate_wise_valid_vote_count_result_item['name'] }) diff --git a/orm/entities/TallySheetVersionRow/TallySheetVersionRow_PRE_34_preference.py b/orm/entities/TallySheetVersionRow/TallySheetVersionRow_PRE_34_preference.py index 1d6a0872..4954bfa7 100644 --- a/orm/entities/TallySheetVersionRow/TallySheetVersionRow_PRE_34_preference.py +++ b/orm/entities/TallySheetVersionRow/TallySheetVersionRow_PRE_34_preference.py @@ -13,26 +13,30 @@ class TallySheetVersionRow_PRE_34_preference_Model(db.Model): nullable=False) electionId = db.Column(db.Integer, db.ForeignKey(Election.Model.__table__.c.electionId), nullable=False) candidateId = db.Column(db.Integer, db.ForeignKey(Candidate.Model.__table__.c.candidateId), nullable=True) + areaId = db.Column(db.Integer, db.ForeignKey(Area.Model.__table__.c.areaId), nullable=True) preferenceNumber = db.Column(db.Integer, nullable=False) preferenceCount = db.Column(db.Integer, nullable=False) candidate = relationship(Candidate.Model, foreign_keys=[candidateId]) + area = relationship(Area.Model, foreign_keys=[areaId]) election = relationship(Election.Model, foreign_keys=[electionId]) areaName = association_proxy("area", "areaName") __table_args__ = ( - db.UniqueConstraint('tallySheetVersionId', 'preferenceNumber', 'candidateId', - name='TallySheetVersionRow_RejectedVoteCount_Model'), + db.UniqueConstraint('tallySheetVersionId', 'preferenceNumber', 'candidateId', 'areaId', + name='TallySheetVersionRow_PRE_34_preference_Model_uk'), ) - def __init__(self, electionId, tallySheetVersionId, preferenceNumber, preferenceCount, candidateId): + def __init__(self, electionId, tallySheetVersionId, preferenceNumber, preferenceCount, candidateId=None, + areaId=None): super(TallySheetVersionRow_PRE_34_preference_Model, self).__init__( electionId=electionId, tallySheetVersionId=tallySheetVersionId, preferenceNumber=preferenceNumber, preferenceCount=preferenceCount, - candidateId=candidateId + candidateId=candidateId, + areaId=areaId ) db.session.add(self) db.session.flush() @@ -41,13 +45,14 @@ def __init__(self, electionId, tallySheetVersionId, preferenceNumber, preference Model = TallySheetVersionRow_PRE_34_preference_Model -def create(electionId, tallySheetVersionId, preferenceNumber, preferenceCount, candidateId): +def create(electionId, tallySheetVersionId, preferenceNumber, preferenceCount, candidateId=None, areaId=None): result = Model( electionId=electionId, tallySheetVersionId=tallySheetVersionId, preferenceNumber=preferenceNumber, preferenceCount=preferenceCount, - candidateId=candidateId + candidateId=candidateId, + areaId=areaId ) return result diff --git a/templates/PRE-34-CO.html b/templates/PRE-34-CO.html index 21a70ab9..84c41cd9 100644 --- a/templates/PRE-34-CO.html +++ b/templates/PRE-34-CO.html @@ -104,9 +104,17 @@ {% endfor %} + {% if content.summary is not none %} {{ content.summary.ballotPapersNotCounted }} {{ content.summary.remainingBallotPapers }} - {{ content.summary.remainingBallotPapers + content.summary.ballotPapersNotCounted }} + {{ content.summary.remainingBallotPapers + + content.summary.ballotPapersNotCounted }} + + {% else %} + + + + {% endif %}