Feat 144 grouped election endpoints#148
Conversation
jbriones1
left a comment
There was a problem hiding this comment.
Thank you for the PR and for writing tests! Lmk if you have any questions about the changes requests.
| election_list = await elections.crud.get_all_elections(db_session) | ||
| if election_list is None or len(election_list) == 0: | ||
| raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="no election found") | ||
|
|
||
| current_time = datetime.datetime.now(datetime.UTC) | ||
| if await is_user_election_admin(computing_id, db_session): | ||
| election_metadata_list = [election.private_details(current_time) for election in election_list] | ||
| election_metadata_list = [] | ||
| has_permission = await is_user_election_admin(computing_id, db_session) | ||
|
|
||
| if has_permission: | ||
| for election in election_list: | ||
| election_data = election.private_details(current_time) | ||
| # Get the nominees for the election | ||
| if with_nominees: | ||
| election_data["candidates"] = await _get_election_nominees(db_session, election, has_permission) | ||
| election_metadata_list.append(election_data) | ||
| else: | ||
| election_metadata_list = [election.public_details(current_time) for election in election_list] | ||
| for election in election_list: | ||
| election_data = election.public_details(current_time) | ||
| # Get the nominees for the election | ||
| if with_nominees: | ||
| election_data["candidates"] = await _get_election_nominees(db_session, election, has_permission) | ||
| election_metadata_list.append(election_data) |
There was a problem hiding this comment.
This will query the database once for each election, which is expensive. It'd be better to create a new CRUD operation that gets all the election and nominee information in one query (if it's requested). Something like:
SELECT *
FROM election
LEFT JOIN election_nominee_application
ON election.slug=election_nominee_info.nominee_election
So either the query on line 123 is executed or the query I suggested is executed. Make sure to use LEFT JOIN so elections with no candidates still appear.
You could even go one step further and define the fields to get from the database in the query i.e. don't fetch private details if the user does not have permission, but you don't have to.
If you do decide to remove the private details at the query level you can add response_model_exclude_none=True in the decorator, under response_model on line 114 and that will automatically strip away all fields that have None as their value.
There was a problem hiding this comment.
Added get_all_nominees_by_election helper function in elections/crud.py and used on GET /election
Feature: Grouped Election Endpoints
closes #144
Description:
Adds optional nominees to
GET /electionandGET /election/{name}via with_nominees=true.Guests sees:
Admins also get:
Features:
with_nomineesquery param on list and single election GET endpoints (default false)