Skip to content

Commit 3ef249f

Browse files
committed
clean up naming of functions
1 parent 4fc04da commit 3ef249f

File tree

7 files changed

+22
-32
lines changed

7 files changed

+22
-32
lines changed

src/cron/daily.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import google_api
88
import utils
99
from database import _db_session
10-
from officers.crud import all_officer_terms, get_user_by_username, officer_terms
10+
from officers.crud import all_officer_data, get_user_by_username
1111

1212
_logger = logging.getLogger(__name__)
1313

@@ -18,7 +18,7 @@ async def update_google_permissions(db_session):
1818

1919
# TODO: for performance, only include officers with recent end-date (1 yr)
2020
# but measure performance first
21-
for term in await all_officer_terms(db_session):
21+
for term in await all_officer_data(db_session):
2222
if utils.is_active(term):
2323
# TODO: if google drive permission is not active, update them
2424
pass
@@ -31,7 +31,7 @@ async def update_google_permissions(db_session):
3131
async def update_github_permissions(db_session):
3232
github_permissions, team_id_map = github.all_permissions()
3333

34-
for term in await all_officer_terms(db_session):
34+
for term in await all_officer_data(db_session):
3535
new_teams = (
3636
# move all active officers to their respective teams
3737
github.officer_teams(term.position)

src/officers/constants.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from typing import Self
2-
3-
41
class OfficerPosition:
52
PRESIDENT = "president"
63
VICE_PRESIDENT = "vice-president"

src/officers/crud.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import database
77
import utils
8-
from auth.tables import SiteUser
98
from data import semesters
109
from officers.constants import OfficerPosition
1110
from officers.tables import OfficerInfo, OfficerTerm
@@ -69,24 +68,23 @@ async def officer_term(db_session: database.DBSession, term_id: int) -> OfficerT
6968
raise HTTPException(status_code=400, detail=f"Could not find officer_term with id={term_id}")
7069
return officer_term
7170

72-
# TODO: change to "get_officer_terms" naming convention (& all functions in this module)
73-
async def officer_terms(
71+
async def get_officer_terms(
7472
db_session: database.DBSession,
7573
computing_id: str,
7674
max_terms: None | int,
77-
# will not include officer term info that has not been filled in yet.
78-
view_only_filled_in: bool,
75+
# will include term info for officers that are not active
76+
# or have not yet been filled out
77+
include_inactive: bool,
7978
) -> list[OfficerTerm]:
8079
query = (
8180
sqlalchemy
8281
.select(OfficerTerm)
8382
.where(OfficerTerm.computing_id == computing_id)
83+
.order_by(OfficerTerm.start_date.desc())
8484
)
85-
# TODO: does active == filled_in ?
86-
if view_only_filled_in:
85+
if not include_inactive:
8786
query = utils.is_active_officer(query)
8887

89-
query = query.order_by(OfficerTerm.start_date.desc())
9088
if max_terms is not None:
9189
query.limit(max_terms)
9290

@@ -156,21 +154,20 @@ async def current_executive_team(db_session: database.DBSession, include_private
156154

157155
return officer_data
158156

159-
async def all_officer_terms(
157+
async def all_officer_data(
160158
db_session: database.DBSession,
161159
include_private: bool,
162160
view_only_filled_in: bool,
163161
) -> list[OfficerData]:
164162
"""
165-
Orders officers recent first.
166-
167163
This could be a lot of data, so be careful.
168164
169165
TODO: optionally paginate data, so it's not so bad.
170166
"""
171167
query = sqlalchemy.select(OfficerTerm)
172168
if view_only_filled_in:
173169
query = OfficerTerm.sql_is_filled_in(query)
170+
# Ordered recent first
174171
query = query.order_by(OfficerTerm.start_date.desc())
175172
officer_terms = (await db_session.scalars(query)).all()
176173

src/officers/tables.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,3 @@ def to_update_dict(self) -> dict:
194194
"github_username": self.github_username,
195195
"google_drive_email": self.google_drive_email,
196196
}
197-

src/officers/urls.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ async def has_access(db_session: database.DBSession, request: Request) -> bool:
7878

7979
has_private_access = await has_access(db_session, request)
8080

81-
all_officer_data = await officers.crud.all_officer_terms(db_session, has_private_access, view_only_filled_in)
81+
all_officer_data = await officers.crud.all_officer_data(db_session, has_private_access, view_only_filled_in)
8282
all_officer_data = [officer_data.serializable_dict() for officer_data in all_officer_data]
8383
return JSONResponse(all_officer_data)
8484

@@ -92,7 +92,7 @@ async def get_officer_terms(
9292
computing_id: str,
9393
# the maximum number of terms to return, in chronological order
9494
max_terms: int | None = None,
95-
view_only_filled_in: bool = True,
95+
include_inactive: bool = False,
9696
):
9797
# TODO: put these into a function
9898
session_id = request.cookies.get("session_id", None)
@@ -105,18 +105,18 @@ async def get_officer_terms(
105105
raise HTTPException(status_code=401)
106106

107107
if (
108-
not view_only_filled_in
109-
and computing_id != session_computing_id
108+
computing_id != session_computing_id
109+
and include_inactive
110110
and not await WebsiteAdmin.has_permission(db_session, session_computing_id)
111111
):
112112
raise HTTPException(status_code=401)
113113

114114
# all term info is public, so anyone can get any of it
115-
officer_terms = await officers.crud.officer_terms(
115+
officer_terms = await officers.crud.get_officer_terms(
116116
db_session,
117117
computing_id,
118118
max_terms,
119-
view_only_filled_in=view_only_filled_in
119+
include_inactive=include_inactive
120120
)
121121
return JSONResponse([term.serializable_dict() for term in officer_terms])
122122

@@ -323,13 +323,11 @@ async def update_term(
323323
# the current user can only input the info for another user if they have permissions
324324
raise HTTPException(status_code=401, detail="must have website admin permissions to update another user")
325325

326-
# TODO: turn is_active into a utils function
327-
is_active = (old_officer_term.end_date is None) or (datetime.today() <= old_officer_term.end_date)
328326
if (
329-
not is_active
327+
not utils.is_active_term(old_officer_term)
330328
and not await WebsiteAdmin.has_permission(db_session, session_computing_id)
331329
):
332-
raise HTTPException(status_code=401, detail="only website admin can update a past (non-active) term")
330+
raise HTTPException(status_code=401, detail="only website admin can update a non-active term")
333331

334332
# NOTE: Only admins can write new versions of position, start_date, and end_date.
335333
if (

src/permission/types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ async def has_permission(db_session: database.DBSession, computing_id: str) -> b
4646
"""
4747
A website admin has to be an active officer who has one of the above positions
4848
"""
49-
position_list = await officers.crud.current_officer_positions(db_session, computing_id)
50-
for position in position_list:
49+
for position in await officers.crud.current_officer_positions(db_session, computing_id):
5150
if position in WebsiteAdmin.WEBSITE_ADMIN_POSITIONS:
5251
return True
5352
return False

tests/integration/test_officers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import load_test_db
66
from database import SQLALCHEMY_TEST_DATABASE_URL, DatabaseSessionManager
77
from officers.constants import OfficerPosition
8-
from officers.crud import all_officer_terms, current_executive_team, most_recent_officer_term
8+
from officers.crud import all_officer_data, current_executive_team, most_recent_officer_term
99

1010
# TODO: setup a database on the CI machine & run this as a unit test then (since
1111
# this isn't really an integration test)
@@ -56,7 +56,7 @@ async def test__read_execs(database_setup):
5656
assert next(iter(current_exec_team.values()))[0].private_data is not None
5757
assert next(iter(current_exec_team.values()))[0].private_data.computing_id == "abc33"
5858

59-
all_terms = await all_officer_terms(db_session, include_private=True)
59+
all_terms = await all_officer_data(db_session, include_private=True)
6060
assert len(all_terms) == 3
6161

6262

0 commit comments

Comments
 (0)