diff --git a/results-tabulation-api/api/ElectionApi.py b/results-tabulation-api/api/ElectionApi.py index c4ccbf01..ddc15bc4 100644 --- a/results-tabulation-api/api/ElectionApi.py +++ b/results-tabulation-api/api/ElectionApi.py @@ -6,7 +6,7 @@ from exception.messages import MESSAGE_CODE_ELECTION_NOT_FOUND from orm.entities.Election.election_helper import get_root_token from orm.entities import Election -from schemas import ElectionSchema as Schema, AreaMapSchema +from schemas import ElectionSchema as Schema, AreaMapSchema, MappedAreaSchema from util import RequestBody @@ -87,6 +87,21 @@ def get_area_map(electionId=None): return _cache_get_area_map(user_access_area_ids=user_access_area_ids, electionId=electionId) +@authorize(required_roles=ALL_ROLES) +def get_mapped_area(electionId=None, areaIds=None, requestedAreaType=None): + election = Election.get_by_id(electionId=electionId) + if election is None: + raise NotFoundException( + message="Election not found (electionId=%d)" % electionId, + code=MESSAGE_CODE_ELECTION_NOT_FOUND + ) + + extended_election = election.get_extended_election() + mapped_area = extended_election.get_mapped_area(areaIds, requestedAreaType) + + return MappedAreaSchema(many=True).dump(mapped_area).data + + @cache.memoize() def _cache_get_area_map(user_access_area_ids, electionId): election = Election.get_by_id(electionId=electionId) diff --git a/results-tabulation-api/ext/ExtendedElection/__init__.py b/results-tabulation-api/ext/ExtendedElection/__init__.py index 93b5b1a8..1db46816 100644 --- a/results-tabulation-api/ext/ExtendedElection/__init__.py +++ b/results-tabulation-api/ext/ExtendedElection/__init__.py @@ -6,7 +6,6 @@ WORKFLOW_STATUS_TYPE_RELEASE_NOTIFIED from ext.ExtendedTallySheet import ExtendedTallySheet - def get_extended_election(election): from constants.ELECTION_TEMPLATES import PRESIDENTIAL_ELECTION_2019, PARLIAMENT_ELECTION_2020 from ext.ExtendedElection.ExtendedElectionParliamentaryElection2020 import ExtendedElectionParliamentaryElection2020 @@ -62,6 +61,29 @@ def get_area_map_for_tally_sheet(self, tally_sheet): return self.get_area_map(area=area) + def get_mapped_area(self, area_ids=None, requested_area_type=None): + + from orm.entities import Area + from schemas import AreaMapSchema + filtered_area_map = [] + + for area_id in str(area_ids).split(","): + input_area = Area.Model.query.filter(Area.Model.areaId == area_id).one_or_none() + area_map = self.get_area_map(area=input_area) + area_map_data = AreaMapSchema(many=True).dump(area_map).data + + for area_data in area_map_data: + filtered_area_map.append({ + "areaId": input_area.areaId, + "areaName": input_area.areaName, + "areaType": input_area.areaType.name, + "mappedAreaId": area_data[requested_area_type+"Id"], + "mappedAreaName": area_data[requested_area_type+"Name"], + "mappedAreaType": requested_area_type + }) + + return filtered_area_map + def get_area_map(self, area=None, group_by=None, filter_by=None): from orm.enums import AreaTypeEnum diff --git a/results-tabulation-api/schemas/__init__.py b/results-tabulation-api/schemas/__init__.py index 5df49bc7..ab379e3b 100644 --- a/results-tabulation-api/schemas/__init__.py +++ b/results-tabulation-api/schemas/__init__.py @@ -288,6 +288,18 @@ class Meta: ) +class MappedAreaSchema(ma.ModelSchema): + class Meta: + fields = ( + "areaId", + "areaName", + "areaType", + "mappedAreaId", + "mappedAreaName", + "mappedAreaType", + ) + + class AreaAreaSchema(ma.ModelSchema): class Meta: fields = ( diff --git a/results-tabulation-api/swagger.yml b/results-tabulation-api/swagger.yml index cd34eb45..8c1c97f9 100644 --- a/results-tabulation-api/swagger.yml +++ b/results-tabulation-api/swagger.yml @@ -225,6 +225,63 @@ paths: schema: $ref: '#/components/schemas/ApiResponse' + /election/{electionId}/mapped-area: + get: + tags: + - Election + summary: Get Area Map + operationId: api.ElectionApi.get_mapped_area + parameters: + - name: electionId + in: path + required: true + schema: + type: integer + format: int64 + - name: areaIds + in: query + required: true + schema: + type: string + - name: requestedAreaType + in: query + required: true + schema: + type: string + responses: + '200': + description: Successful operation. + content: + application/json: + schema: + type: array + items: + type: object + '400': + description: Bad request. + content: + application/json: + schema: + $ref: '#/components/schemas/ApiResponse' + '401': + description: Unauthorized. + content: + application/json: + schema: + $ref: '#/components/schemas/ApiResponse' + '404': + description: Resource was not found. + content: + application/json: + schema: + $ref: '#/components/schemas/ApiResponse' + '500': + description: Unexpected error. + content: + application/json: + schema: + $ref: '#/components/schemas/ApiResponse' + /election: get: tags: