Skip to content

Commit

Permalink
gh-236: Map the preference result rows to areas
Browse files Browse the repository at this point in the history
  • Loading branch information
dinukadesilva committed Nov 14, 2019
1 parent 68b70cf commit b8f7e95
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 40 deletions.
19 changes: 12 additions & 7 deletions api/TallySheetVersionApi/TallySheetVersion_PRE_34_AI_Api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
19 changes: 12 additions & 7 deletions api/TallySheetVersionApi/TallySheetVersion_PRE_34_ED_Api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions api/TallySheetVersionApi/TallySheetVersion_PRE_34_I_RO_Api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions api/TallySheetVersionApi/TallySheetVersion_PRE_34_PD_Api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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
Expand Down
113 changes: 113 additions & 0 deletions migrations/versions/6a07b608d1fb_.py
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ 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(
tallySheetVersionId=self.tallySheetVersionId,
electionId=electionId,
preferenceNumber=preferenceNumber,
preferenceCount=preferenceCount,
candidateId=candidateId
candidateId=candidateId,
areaId=areaId
)

@hybrid_property
Expand Down Expand Up @@ -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:
Expand All @@ -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']
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ 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(
tallySheetVersionId=self.tallySheetVersionId,
electionId=electionId,
preferenceNumber=preferenceNumber,
preferenceCount=preferenceCount,
candidateId=candidateId
candidateId=candidateId,
areaId=areaId
)

@hybrid_property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ 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(
tallySheetVersionId=self.tallySheetVersionId,
electionId=electionId,
preferenceNumber=preferenceNumber,
preferenceCount=preferenceCount,
candidateId=candidateId
candidateId=candidateId,
areaId=areaId
)

@hybrid_property
Expand Down
Loading

0 comments on commit b8f7e95

Please sign in to comment.