From 03fbaa95163c99629269c594f297cdbe363bd0d4 Mon Sep 17 00:00:00 2001 From: Dinuka De Silva Date: Wed, 15 Jul 2020 18:46:13 +0530 Subject: [PATCH] Fix tally sheet responses --- results-tabulation-api/api/TallySheetApi.py | 29 ++++++++++++++----- .../api/TallySheetVersionApi/__init__.py | 3 +- .../Submission/TallySheet/__init__.py | 20 ++----------- results-tabulation-api/schemas/__init__.py | 1 - .../src/services/tally-sheet.provider.js | 2 +- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/results-tabulation-api/api/TallySheetApi.py b/results-tabulation-api/api/TallySheetApi.py index 5e88da13..4650e8d2 100644 --- a/results-tabulation-api/api/TallySheetApi.py +++ b/results-tabulation-api/api/TallySheetApi.py @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 diff --git a/results-tabulation-api/api/TallySheetVersionApi/__init__.py b/results-tabulation-api/api/TallySheetVersionApi/__init__.py index f751a610..93f6481d 100644 --- a/results-tabulation-api/api/TallySheetVersionApi/__init__.py +++ b/results-tabulation-api/api/TallySheetVersionApi/__init__.py @@ -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 @@ -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 diff --git a/results-tabulation-api/orm/entities/Submission/TallySheet/__init__.py b/results-tabulation-api/orm/entities/Submission/TallySheet/__init__.py index 82faf346..2f915027 100644 --- a/results-tabulation-api/orm/entities/Submission/TallySheet/__init__.py +++ b/results-tabulation-api/orm/entities/Submission/TallySheet/__init__.py @@ -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 @@ -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() @@ -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): @@ -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 diff --git a/results-tabulation-api/schemas/__init__.py b/results-tabulation-api/schemas/__init__.py index a18165c5..d2255dd8 100644 --- a/results-tabulation-api/schemas/__init__.py +++ b/results-tabulation-api/schemas/__init__.py @@ -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): diff --git a/results-tabulation-ui/src/services/tally-sheet.provider.js b/results-tabulation-ui/src/services/tally-sheet.provider.js index c5b8b5ad..a693805e 100644 --- a/results-tabulation-ui/src/services/tally-sheet.provider.js +++ b/results-tabulation-ui/src/services/tally-sheet.provider.js @@ -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; })