Skip to content

Commit

Permalink
Merge pull request #289 from dinukadesilva/gh-284
Browse files Browse the repository at this point in the history
Gh 284: Update the exceptions and claim structure
  • Loading branch information
dinukadesilva authored Nov 2, 2019
2 parents fdecbda + 68300d5 commit 6cb4153
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 60 deletions.
2 changes: 1 addition & 1 deletion api/ElectionApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_by_id(electionId):
if result is None:
raise NotFoundException(
message="Election not found (electionId=%d)" % electionId,
const=MESSAGE_CODE_ELECTION_NOT_FOUND
code=MESSAGE_CODE_ELECTION_NOT_FOUND
)

return Schema().dump(result).data
Expand Down
5 changes: 3 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
cache = Cache(config={
"DEBUG": True, # some Flask specific configs
"CACHE_TYPE": "simple", # Flask-Caching related configs
"CACHE_DEFAULT_TIMEOUT": 18144000000 # One month
"CACHE_DEFAULT_TIMEOUT": 18144000000 # One month
})


Expand All @@ -44,7 +44,8 @@ def render_connexion_problem_exception(connexion_exception):
return json.dumps({
"detail": connexion_exception.detail,
"status": connexion_exception.status,
"title": connexion_exception.title
"title": connexion_exception.title,
"code": connexion_exception.args[4]
}, indent=2), connexion_exception.status


Expand Down
8 changes: 6 additions & 2 deletions auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def get_user_name() -> str:
"""
return connexion.context[USER_NAME]


def get_user_roles():
return connexion.context[USER_ROLES]

Expand Down Expand Up @@ -229,17 +230,20 @@ def authorize(func, required_roles=None, *args, **kwargs):
user_access_area_ids.extend([x.get(AREA_ID) for x in claims.get(claim)])

if role is DATA_EDITOR_ROLE:
counting_centre_ids = []
global_area_map = init_global_area_map()
for electoral_district_id in user_access_area_ids:
if electoral_district_id in global_area_map["electoral_district_counting_centre"]:
user_access_area_ids.extend(
counting_centre_ids.extend(
global_area_map["electoral_district_counting_centre"][electoral_district_id]
)

elif role is POLLING_DIVISION_REPORT_VIEWER_ROLE or role is POLLING_DIVISION_REPORT_VERIFIER_ROLE:
counting_centre_ids = []
global_area_map = init_global_area_map()
for electoral_district_id in user_access_area_ids:
if electoral_district_id in global_area_map["electoral_district_polling_division"]:
user_access_area_ids.extend(
counting_centre_ids.extend(
global_area_map["electoral_district_polling_division"][electoral_district_id]
)

Expand Down
12 changes: 6 additions & 6 deletions exception/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@


def UnauthorizedException(message="", code=None):
raise ProblemException(401, "Unauthorized", message, "Unauthorized")
raise ProblemException(401, "Unauthorized", message, "Unauthorized", code)


def ForbiddenException(message="", code=None):
raise ProblemException(403, "Forbidden", message, "Forbidden")
raise ProblemException(403, "Forbidden", message, "Forbidden", code)


def NotFoundException(message="", code=None):
raise ProblemException(404, "Not Found", message, "NotFound")
raise ProblemException(404, "Not Found", message, "NotFound", code)


def MethodNotAllowedException(message="", code=None):
raise ProblemException(405, "Method Not Allowed", message, "MethodNotAllowed")
raise ProblemException(405, "Method Not Allowed", message, "MethodNotAllowed", code)


def InternalServerErrorException(message="", code=None):
raise ProblemException(500, "Internal Server Error", message, "InternalServerError")
raise ProblemException(500, "Internal Server Error", message, "InternalServerError", code)


def NotImplementedException(message="", code=None):
raise ProblemException(501, "Not Implemented", message, "NotImplemented")
raise ProblemException(501, "Not Implemented", message, "NotImplemented", code)
1 change: 1 addition & 0 deletions exception/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
MESSAGE_CODE_SUBMISSION_IRRELEVANT_VERSION_CANNOT_BE_MAPPED = 19
MESSAGE_CODE_SUBMISSION_NOT_FOUND = 21
# next: 23
# Do not change the numbers and new always. These are linked to client applications.
7 changes: 2 additions & 5 deletions orm/entities/Area/Electorate/AdministrativeDistrict.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from app import db
from orm.entities.Area import Electorate
from orm.entities.Area.Electorate import Province
from orm.enums import ElectorateTypeEnum, AreaTypeEnum
from exception import NotFoundException
from sqlalchemy.orm import relationship, synonym
from orm.enums import AreaTypeEnum
from sqlalchemy.orm import synonym
from sqlalchemy.ext.associationproxy import association_proxy


Expand Down
6 changes: 1 addition & 5 deletions orm/entities/Area/Electorate/ElectoralDistrict.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from app import db
from orm.entities.Area import Electorate
from orm.entities.Area.Electorate import Country
from orm.enums import ElectorateTypeEnum, AreaTypeEnum
from exception import NotFoundException
from orm.enums import AreaTypeEnum
from sqlalchemy.orm import synonym
from sqlalchemy.ext.associationproxy import association_proxy


class ElectoralDistrictModel(Electorate.Model):
Expand Down
3 changes: 0 additions & 3 deletions orm/entities/Area/Electorate/PollingDistrict.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from app import db
from orm.entities.Area import Electorate
from orm.entities.Area.Electorate import PollingDivision
from orm.enums import AreaTypeEnum
from exception import NotFoundException
from sqlalchemy.orm import synonym
from sqlalchemy.ext.associationproxy import association_proxy

Expand Down
7 changes: 2 additions & 5 deletions orm/entities/Area/Electorate/PollingDivision.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from app import db
from orm.entities import Electorate
from orm.entities.Area.Electorate import ElectoralDistrict
from orm.enums import ElectorateTypeEnum, AreaTypeEnum
from exception import NotFoundException
from sqlalchemy.orm import relationship, synonym
from orm.enums import AreaTypeEnum
from sqlalchemy.orm import synonym
from sqlalchemy.ext.associationproxy import association_proxy


Expand Down
9 changes: 2 additions & 7 deletions orm/entities/Area/Electorate/Province.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from app import db
from orm.entities import Electorate
from orm.entities.Area.Electorate import Country
from orm.enums import ElectorateTypeEnum, AreaTypeEnum
from exception import NotFoundException
from sqlalchemy.orm import relationship, synonym
from orm.enums import AreaTypeEnum
from sqlalchemy.orm import synonym


class ProvinceModel(Electorate.Model):
# parentElectorateId = db.Column(db.Integer, db.ForeignKey("electorateId"), nullable=True)

country = synonym("parentElectorate")

__mapper_args__ = {
Expand Down
42 changes: 21 additions & 21 deletions orm/entities/Election/election_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@ def get_root_token(electionId):
AREA_CLAIM_PREFIX + DATA_EDITOR_ROLE: str([{
"areaId": electoral_district.areaId
} for electoral_district in electoral_districts]),
AREA_CLAIM_PREFIX + POLLING_DIVISION_REPORT_VIEWER_ROLE: str([{
"areaId": electoral_district.areaId
} for electoral_district in electoral_districts]),
AREA_CLAIM_PREFIX + POLLING_DIVISION_REPORT_VERIFIER_ROLE: str([{
"areaId": electoral_district.areaId
} for electoral_district in electoral_districts]),
AREA_CLAIM_PREFIX + ELECTORAL_DISTRICT_REPORT_VIEWER_ROLE: str([{
"areaId": electoral_district.areaId
} for electoral_district in electoral_districts]),
AREA_CLAIM_PREFIX + ELECTORAL_DISTRICT_REPORT_VERIFIER_ROLE: str([{
"areaId": electoral_district.areaId
} for electoral_district in electoral_districts]),
AREA_CLAIM_PREFIX + NATIONAL_REPORT_VIEWER_ROLE: str([{
"areaId": country.areaId
} for country in countries]),
AREA_CLAIM_PREFIX + NATIONAL_REPORT_VERIFIER_ROLE: str([{
"areaId": country.areaId
} for country in countries]),
AREA_CLAIM_PREFIX + EC_LEADERSHIP_ROLE: str([{
"areaId": country.areaId
} for country in countries])
# AREA_CLAIM_PREFIX + POLLING_DIVISION_REPORT_VIEWER_ROLE: str([{
# "areaId": electoral_district.areaId
# } for electoral_district in electoral_districts]),
# AREA_CLAIM_PREFIX + POLLING_DIVISION_REPORT_VERIFIER_ROLE: str([{
# "areaId": electoral_district.areaId
# } for electoral_district in electoral_districts]),
# AREA_CLAIM_PREFIX + ELECTORAL_DISTRICT_REPORT_VIEWER_ROLE: str([{
# "areaId": electoral_district.areaId
# } for electoral_district in electoral_districts]),
# AREA_CLAIM_PREFIX + ELECTORAL_DISTRICT_REPORT_VERIFIER_ROLE: str([{
# "areaId": electoral_district.areaId
# } for electoral_district in electoral_districts]),
# AREA_CLAIM_PREFIX + NATIONAL_REPORT_VIEWER_ROLE: str([{
# "areaId": country.areaId
# } for country in countries]),
# AREA_CLAIM_PREFIX + NATIONAL_REPORT_VERIFIER_ROLE: str([{
# "areaId": country.areaId
# } for country in countries]),
# AREA_CLAIM_PREFIX + EC_LEADERSHIP_ROLE: str([{
# "areaId": country.areaId
# } for country in countries])
}

# Generate a token with claims for everything.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

from app import db

from orm.entities import Candidate
from orm.entities.Election import ElectionCandidate, InvalidVoteCategory
from exception import NotFoundException
from orm.entities.Election import InvalidVoteCategory
from orm.entities.SubmissionVersion import TallySheetVersion


Expand Down

0 comments on commit 6cb4153

Please sign in to comment.