Skip to content

Commit

Permalink
Merge pull request #365 from dinukadesilva/gh-236
Browse files Browse the repository at this point in the history
gh-236: Fix incomplete validation in revised reports
  • Loading branch information
dinukadesilva authored Nov 14, 2019
2 parents 3d440e8 + b8f7e95 commit dd80af1
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 73 deletions.
70 changes: 58 additions & 12 deletions api/TallySheetVersionApi/TallySheetVersion_PRE_34_AI_Api.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,37 @@ 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,
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
Expand All @@ -73,26 +99,46 @@ 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)
Area.Model.areaId.in_(electoral_district_ids)
).group_by(
ElectionCandidate.Model.candidateId,
TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber
Area.Model.areaId
).order_by(
TallySheetVersionRow_PRE_34_preference.Model.candidateId,
TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber
ElectionCandidate.Model.candidateId,
Area.Model.areaId
).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,
areaId=row.areaId
)

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,
areaId=row.areaId
)
tallySheetVersion.add_row(
electionId=row.electionId,
candidateId=row.candidateId,
preferenceNumber=3,
preferenceCount=row.thirdPreferenceCount,
areaId=row.areaId
)
else:
is_complete = False
else:
is_complete = False

Expand Down
71 changes: 58 additions & 13 deletions api/TallySheetVersionApi/TallySheetVersion_PRE_34_ED_Api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,37 @@ 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,
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
Expand All @@ -77,28 +103,47 @@ 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),
Area.Model.areaId.in_(electoral_district_and_polling_division_ids),
# ElectionCandidate.Model.qualifiedForPreferences == True
).group_by(
ElectionCandidate.Model.candidateId,
TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber
Area.Model.areaId
).order_by(
TallySheetVersionRow_PRE_34_preference.Model.candidateId,
TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber
ElectionCandidate.Model.candidateId,
Area.Model.areaId
).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,
areaId=row.areaId
)

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,
areaId=row.areaId
)
tallySheetVersion.add_row(
electionId=row.electionId,
candidateId=row.candidateId,
preferenceNumber=3,
preferenceCount=row.thirdPreferenceCount,
areaId=row.areaId
)
else:
is_complete = False
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
64 changes: 37 additions & 27 deletions api/TallySheetVersionApi/TallySheetVersion_PRE_34_PD_Api.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,23 @@ def create(tallySheetId):
tallySheetCode=TallySheetCodeEnum.PRE_34_PD
)

polling_division_id = tallySheet.submission.areaId

query = db.session.query(
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(
and_(
TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber == 2
),
TallySheetVersionRow_PRE_34_preference.Model.preferenceCount,
0
None
)
).label("secondPreferenceCount"),
func.sum(
Expand All @@ -61,7 +60,7 @@ def create(tallySheetId):
TallySheetVersionRow_PRE_34_preference.Model.preferenceNumber == 3
),
TallySheetVersionRow_PRE_34_preference.Model.preferenceCount,
0
None
)
).label("thirdPreferenceCount"),
).join(
Expand Down Expand Up @@ -97,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 @@ -109,26 +108,37 @@ 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,
areaId=polling_division_id
)

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,
areaId=polling_division_id
)
tallySheetVersion.add_row(
electionId=row.electionId,
candidateId=row.candidateId,
preferenceNumber=3,
preferenceCount=row.thirdPreferenceCount,
areaId=polling_division_id
)
else:
is_complete = False
else:
is_complete = False

Expand Down
Loading

0 comments on commit dd80af1

Please sign in to comment.