Skip to content

Commit

Permalink
Merge pull request #827 from umayangag/gh-826
Browse files Browse the repository at this point in the history
area map replacement endpoint #826
  • Loading branch information
dinukadesilva authored Sep 8, 2020
2 parents ba721f6 + 217c5e8 commit 0b41ef0
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
17 changes: 16 additions & 1 deletion results-tabulation-api/api/ElectionApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down
24 changes: 23 additions & 1 deletion results-tabulation-api/ext/ExtendedElection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions results-tabulation-api/schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
57 changes: 57 additions & 0 deletions results-tabulation-api/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 0b41ef0

Please sign in to comment.