Skip to content

Commit e227437

Browse files
committed
clean final endpoint, PATCH /term
1 parent 2e28910 commit e227437

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

src/officers/crud.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,10 @@ async def create_new_officer_term(
184184
)
185185
db_session.add(new_officer_term)
186186

187-
async def update_officer_info(db_session: database.DBSession, new_officer_info: OfficerInfo) -> bool:
187+
async def update_officer_info(
188+
db_session: database.DBSession,
189+
new_officer_info: OfficerInfo
190+
) -> bool:
188191
"""
189192
Return False if the officer doesn't exist yet
190193
"""
@@ -209,28 +212,25 @@ async def update_officer_info(db_session: database.DBSession, new_officer_info:
209212
async def update_officer_term(
210213
db_session: database.DBSession,
211214
new_officer_term: OfficerTerm,
212-
):
215+
) -> bool:
213216
"""
214217
Update based on the term id.
215-
216218
Returns false if the above entry does not exist.
217219
"""
218-
query = (
220+
officer_term = await db_session.scalar(
219221
sqlalchemy
220222
.select(OfficerTerm)
221223
.where(OfficerTerm.id == new_officer_term.id)
222224
)
223-
officer_term = await db_session.scalar(query)
224225
if officer_term is None:
225226
return False
226227

227-
query = (
228+
await db_session.execute(
228229
sqlalchemy
229230
.update(OfficerTerm)
230231
.where(OfficerTerm.id == new_officer_term.id)
231232
.values(new_officer_term.to_update_dict())
232233
)
233-
await db_session.execute(query)
234234
return True
235235

236236
async def remove_officer_term():

src/officers/tables.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class OfficerTerm(Base):
2828
__tablename__ = "officer_term"
2929

3030
# TODO: change primary key to computing_id, position, start_date?
31+
# nah, I like having a term-id -> just do a check when inserting?
3132
id = Column(Integer, primary_key=True, autoincrement=True)
3233

3334
computing_id = Column(

src/officers/types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class OfficerInfoUpload:
2222
github_username: None | str = None
2323
google_drive_email: None | str = None
2424

25+
# TODO: why are valid_or_raise and validate separate?
2526
def valid_or_raise(self):
2627
# TODO: more checks
2728
if self.legal_name is not None and self.legal_name == "":
@@ -119,8 +120,6 @@ def valid_or_raise(self):
119120
raise HTTPException(status_code=400, detail="end_date must be after start_date")
120121

121122
def to_officer_term(self, term_id: str, computing_id:str) -> OfficerTerm:
122-
# TODO: many positions have a length; if the length is defined, fill it in right here
123-
# (end date is 1st of month, 12 months after start date's month).
124123
return OfficerTerm(
125124
id = term_id,
126125
computing_id = computing_id,

src/officers/urls.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -276,38 +276,33 @@ async def update_term(
276276
officer_term_upload: OfficerTermUpload = Body(), # noqa: B008
277277
):
278278
officer_term_upload.valid_or_raise()
279-
280-
# Refactor all of these gets & raises into small functions
281-
session_id = request.cookies.get("session_id", None)
282-
if session_id is None:
283-
raise HTTPException(status_code=401, detail="must be logged in")
284-
285-
session_computing_id = await auth.crud.get_computing_id(db_session, session_id)
286-
if session_computing_id is None:
287-
raise HTTPException(status_code=401)
279+
_, session_computing_id = logged_in_or_raise(request, db_session)
288280

289281
old_officer_term = await officers.crud.get_officer_term_by_id(db_session, term_id)
282+
if old_officer_term.computing_id != session_computing_id:
283+
await WebsiteAdmin.has_permission_or_raise(
284+
db_session, session_computing_id,
285+
errmsg="must have website admin permissions to update another user"
286+
)
287+
elif utils.is_past_term(old_officer_term):
288+
await WebsiteAdmin.has_permission_or_raise(
289+
db_session, session_computing_id,
290+
errmsg="only website admins can update past terms"
291+
)
290292

291-
if (
292-
old_officer_term.computing_id != session_computing_id
293-
and not await WebsiteAdmin.has_permission(db_session, session_computing_id)
294-
):
295-
# the current user can only input the info for another user if they have permissions
296-
raise HTTPException(status_code=401, detail="must have website admin permissions to update another user")
297-
298-
if (
299-
utils.is_past_term(old_officer_term)
300-
and not await WebsiteAdmin.has_permission(db_session, session_computing_id)
301-
):
302-
raise HTTPException(status_code=401, detail="only website admins can update past terms")
303-
304-
# NOTE: Only admins can write new versions of position, start_date, and end_date.
305293
if (
306294
officer_term_upload.position != old_officer_term.position
307295
or officer_term_upload.start_date != old_officer_term.start_date.date()
308296
or officer_term_upload.end_date != old_officer_term.end_date.date()
309-
) and not await WebsiteAdmin.has_permission(db_session, session_computing_id):
310-
raise HTTPException(status_code=401, detail="Non-admins cannot modify position, start_date, or end_date.")
297+
):
298+
await WebsiteAdmin.has_permission_or_raise(
299+
db_session, session_computing_id,
300+
errmsg="only admins can write new versions of position, start_date, and end_date"
301+
)
302+
303+
if officer_term_upload.position != old_officer_term.position:
304+
# TODO: update the end_date here
305+
pass
311306

312307
# TODO: log all important changes to a .log file
313308
success = await officers.crud.update_officer_term(

0 commit comments

Comments
 (0)