Skip to content

Commit

Permalink
Merge pull request #694 from dinukadesilva/gh-692-fix-tally-sheet-lis…
Browse files Browse the repository at this point in the history
…t-area-mapping

Fix tally sheet responses
  • Loading branch information
dinukadesilva authored Jul 15, 2020
2 parents e88a847 + 03fbaa9 commit 1fee7a0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 28 deletions.
29 changes: 22 additions & 7 deletions results-tabulation-api/api/TallySheetApi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from api import ProofApi, FileApi
from app import db
from auth import authorize
from auth import authorize, has_role_based_access
from constants.AUTH_CONSTANTS import ALL_ROLES
from exception import NotFoundException
from exception.messages import MESSAGE_CODE_TALLY_SHEET_NOT_FOUND
Expand All @@ -12,16 +12,16 @@

@authorize(required_roles=ALL_ROLES)
def getAll(electionId=None, areaId=None, tallySheetCode=None, voteType=None):
result = TallySheet.get_all(
tally_sheets = TallySheet.get_all(
electionId=electionId,
areaId=areaId,
tallySheetCode=tallySheetCode,
voteType=voteType
)

# result = get_paginated_query(result).all()
tally_sheets = [refactor_tally_sheet_response(tally_sheet) for tally_sheet in tally_sheets]

return TallySheetSchema(many=True).dump(result).data
return TallySheetSchema(many=True).dump(tally_sheets).data


@authorize(required_roles=ALL_ROLES)
Expand All @@ -37,7 +37,7 @@ def get_by_id(tallySheetId):
extended_tally_sheet: ExtendedTallySheet = tally_sheet.get_extended_tally_sheet()
extended_tally_sheet.execute_tally_sheet_get()

return TallySheetSchema_1().dump(tally_sheet).data
return TallySheetSchema_1().dump(refactor_tally_sheet_response(tally_sheet)).data


@authorize(required_roles=ALL_ROLES)
Expand All @@ -58,7 +58,7 @@ def workflow(tallySheetId, body):

db.session.commit()

return TallySheetSchema_1().dump(tally_sheet).data
return TallySheetSchema_1().dump(refactor_tally_sheet_response(tally_sheet)).data


@authorize(required_roles=ALL_ROLES)
Expand All @@ -78,7 +78,7 @@ def upload_workflow_proof_file(body):
extended_tally_sheet: ExtendedTallySheet = tally_sheet.get_extended_tally_sheet()
extended_tally_sheet.execute_tally_sheet_proof_upload()

return TallySheetSchema_1().dump(tally_sheet).data
return TallySheetSchema_1().dump(refactor_tally_sheet_response(tally_sheet)).data


@authorize(required_roles=ALL_ROLES)
Expand Down Expand Up @@ -131,3 +131,18 @@ def get_workflow_proof_download_file(tallySheetId, fileId):
# TODO validate fileId

return FileApi.get_download_file(fileId=fileId)


def refactor_tally_sheet_response(tally_sheet):
workflow_instance = tally_sheet.workflowInstance
workflow_actions = tally_sheet.workflowInstance.workflow.actions
for workflow_action in workflow_actions:
setattr(workflow_action, "allowed", workflow_action.fromStatus == workflow_instance.status)
setattr(workflow_action, "authorized", has_role_based_access(tally_sheet=tally_sheet,
access_type=workflow_action.actionType))
setattr(tally_sheet.workflowInstance, "actions", workflow_actions)

setattr(tally_sheet, "areaId", tally_sheet.submission.areaId)
setattr(tally_sheet, "area", tally_sheet.submission.area)

return tally_sheet
3 changes: 2 additions & 1 deletion results-tabulation-api/api/TallySheetVersionApi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Response

from api import FileApi
from api.TallySheetApi import refactor_tally_sheet_response
from app import db
from auth import authorize
from constants.AUTH_CONSTANTS import ALL_ROLES
Expand Down Expand Up @@ -175,4 +176,4 @@ def create(tallySheetId, body):

db.session.commit()

return TallySheetSchema_1().dump(tally_sheet).data
return TallySheetSchema_1().dump(refactor_tally_sheet_response(tally_sheet)).data
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from orm.entities.Dashboard import StatusReport
from orm.entities.SubmissionVersion import TallySheetVersion
from orm.entities.Template import TemplateRow_DerivativeTemplateRow_Model, TemplateRowModel
from orm.entities.Workflow import WorkflowInstance, WorkflowActionModel
from orm.entities.Workflow import WorkflowInstance
from orm.enums import SubmissionTypeEnum, AreaTypeEnum
from sqlalchemy import func, bindparam

Expand Down Expand Up @@ -382,21 +382,6 @@ def _get_electoral_district_name(polling_division):
return electoral_district_name


def refactor_tally_sheet_response(tally_sheet):
workflow_instance = tally_sheet.workflowInstance
workflow_actions = tally_sheet.workflowInstance.workflow.actions
for workflow_action in workflow_actions:
setattr(workflow_action, "allowed", workflow_action.fromStatus == workflow_instance.status)
setattr(workflow_action, "authorized", has_role_based_access(tally_sheet=tally_sheet,
access_type=workflow_action.actionType))
setattr(tally_sheet.workflowInstance, "actions", workflow_actions)

setattr(tally_sheet, "areaId", tally_sheet.submission.areaId)
setattr(tally_sheet, "area", tally_sheet.submission.area)

return tally_sheet


def get_by_id(tallySheetId, tallySheetCode=None):
# Filter by authorized areas
user_access_area_ids: Set[int] = get_user_access_area_ids()
Expand All @@ -422,7 +407,7 @@ def get_by_id(tallySheetId, tallySheetCode=None):
code=MESSAGE_CODE_TALLY_SHEET_NOT_AUTHORIZED_TO_VIEW
)

return refactor_tally_sheet_response(tally_sheet)
return tally_sheet


def get_all(electionId=None, areaId=None, tallySheetCode=None, voteType=None):
Expand Down Expand Up @@ -456,7 +441,6 @@ def get_all(electionId=None, areaId=None, tallySheetCode=None, voteType=None):
authorized_tally_sheet_list = []
for tally_sheet in tally_sheet_list:
if has_role_based_access(tally_sheet=tally_sheet, access_type=WORKFLOW_ACTION_TYPE_VIEW):
refactor_tally_sheet_response(tally_sheet)
authorized_tally_sheet_list.append(tally_sheet)

return authorized_tally_sheet_list
Expand Down
1 change: 0 additions & 1 deletion results-tabulation-api/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@ class Meta:
metaDataList = ma.Nested(MetaDataSchema, many=True)
areaMapList = ma.Nested('AreaMapSchema', many=True, partial=True)
workflowInstance = ma.Nested(WorkflowInstanceSchema, only=["workflowId", "actions", "status"])
workflowInstanceActions = ma.Nested(StatusActionSchema, many=True)


class TemplateRowSchema(ma.ModelSchema):
Expand Down
2 changes: 1 addition & 1 deletion results-tabulation-ui/src/services/tally-sheet.provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function TallySheetProvider(props) {
method: 'get',
params: {}
}).then(async (tallySheet) => {
await refactorTallySheetObject(tallySheet);
tallySheet = await refactorTallySheetObject(tallySheet);
_updateTallySheetState(tallySheet);
return tallySheet;
})
Expand Down

0 comments on commit 1fee7a0

Please sign in to comment.