1- import base64
21import logging
3- import os
42import re
5- import urllib .parse
63from datetime import datetime
74
8- import requests # TODO: make this async
9- import xmltodict
105from crud import ElectionParameters
11- from fastapi import APIRouter , BackgroundTasks , FastAPI , HTTPException , Request , status
6+ from fastapi import APIRouter , HTTPException , Request , status
127from fastapi .exceptions import RequestValidationError
138from tables import election_types
149
1712import database
1813import elections
1914from constants import root_ip_address
20- from permission import types
15+ from permission . types import ElectionOfficer
2116
2217_logger = logging .getLogger (__name__ )
2318
2722)
2823
2924def _slugify (
30- text : str
25+ text : str
3126) -> str :
3227 """
3328 Creates a unique slug based on text passed in. Assumes non-unicode text.
3429 """
3530 return re .sub (r"[\W_]+" , "-" , text )
3631
3732async def _validate_user (
38- db_session : database .DBSession ,
39- session_id : str
40- ) -> dict :
33+ request : Request ,
34+ db_session : database .DBSession ,
35+ ) -> tuple [bool , str , str ]:
36+ session_id = request .cookies .get ("session_id" , None )
37+ if session_id is None :
38+ return False , None , None
39+
4140 computing_id = await auth .crud .get_computing_id (db_session , session_id )
42- # Assuming now user is validated
43- result = await types .ElectionOfficer .has_permission (db_session , computing_id )
44- return result
41+ if computing_id is None :
42+ return False , None , None
43+
44+ has_permission = await ElectionOfficer .has_permission (db_session , computing_id )
45+ return has_permission , session_id , computing_id
4546
4647@router .get (
4748 "/create_election" ,
@@ -59,9 +60,11 @@ async def create_election(
5960 """
6061 aaa
6162 """
62- session_id = request .cookies .get ("session_id" , None )
63- user_auth = await _validate_user (db_session , session_id )
64- if user_auth is False :
63+ if election_type not in election_types :
64+ raise RequestValidationError ()
65+
66+ is_valid_user , session_id , _ = await _validate_user (request , db_session )
67+ if not is_valid_user :
6568 # let's workshop how we actually wanna handle this
6669 raise HTTPException (
6770 status_code = status .HTTP_401_UNAUTHORIZED ,
@@ -73,9 +76,6 @@ async def create_election(
7376 if start_datetime is None :
7477 start_datetime = datetime .now ()
7578
76- if election_type not in election_types :
77- raise RequestValidationError ()
78-
7979 params = ElectionParameters (
8080 _slugify (name ),
8181 name ,
@@ -93,17 +93,16 @@ async def create_election(
9393 return {}
9494
9595@router .get (
96- "/delete_election" ,
97- description = "Deletes an election from the database"
96+ "/delete_election" ,
97+ description = "Deletes an election from the database"
9898)
9999async def delete_election (
100100 request : Request ,
101101 db_session : database .DBSession ,
102102 slug : str
103103):
104- session_id = request .cookies .get ("session_id" , None )
105- user_auth = await _validate_user (db_session , session_id )
106- if user_auth is False :
104+ is_valid_user , _ , _ = await _validate_user (request , db_session )
105+ if not is_valid_user :
107106 # let's workshop how we actually wanna handle this
108107 raise HTTPException (
109108 status_code = status .HTTP_401_UNAUTHORIZED ,
@@ -130,16 +129,15 @@ async def update_election(
130129 end_datetime : datetime | None = None ,
131130 survey_link : str | None = None
132131):
133- session_id = request .cookies .get ("session_id" , None )
134- user_auth = await _validate_user (db_session , session_id )
135- if user_auth is False :
132+ is_valid_user , session_id , _ = await _validate_user (request , db_session )
133+ if not is_valid_user :
136134 # let's workshop how we actually wanna handle this
137135 raise HTTPException (
138136 status_code = status .HTTP_401_UNAUTHORIZED ,
139137 detail = "You do not have permission to access this resource" ,
140138 headers = {"WWW-Authenticate" : "Basic" },
141139 )
142- if slug is not None :
140+ elif slug is not None :
143141 params = ElectionParameters (
144142 _slugify (name ),
145143 name ,
@@ -152,7 +150,6 @@ async def update_election(
152150 await elections .crud .update_election (params , db_session )
153151 await db_session .commit ()
154152
155-
156153@router .get (
157154 "/test"
158155)
0 commit comments