Skip to content

Commit f3f59f2

Browse files
committed
add DELETE /term endpoint
1 parent e227437 commit f3f59f2

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

src/officers/crud.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ async def update_officer_term(
233233
)
234234
return True
235235

236-
async def remove_officer_term():
237-
# TODO: implement this
238-
pass
236+
async def delete_officer_term_by_id(db_session: database.DBSession, term_id: int):
237+
await db_session.execute(
238+
sqlalchemy
239+
.delete(OfficerTerm)
240+
.where(OfficerTerm.id == term_id)
241+
)

src/officers/urls.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ async def update_info(
261261

262262
updated_officer_info = await officers.crud.get_officer_info(db_session, computing_id)
263263
return JSONResponse({
264-
"updated_officer_info": updated_officer_info.serializable_dict(),
264+
"officer_info": updated_officer_info.serializable_dict(),
265265
"validation_failures": validation_failures,
266266
})
267267

@@ -316,14 +316,31 @@ async def update_term(
316316

317317
new_officer_term = await officers.crud.get_officer_term_by_id(db_session, term_id)
318318
return JSONResponse({
319-
"updated_officer_term": new_officer_term.serializable_dict(),
319+
"officer_term": new_officer_term.serializable_dict(),
320320
"validation_failures": [], # none for now, but may be important later
321321
})
322322

323+
# TODO: test this endpoint
323324
@router.delete(
324325
"/term/{term_id}",
325326
description="Remove the specified officer term. Only website admins can run this endpoint. BE CAREFUL WITH THIS!",
326327
)
327-
async def remove_officer():
328-
# TODO: this
329-
return {}
328+
async def remove_officer(
329+
request: Request,
330+
db_session: database.DBSession,
331+
term_id: int,
332+
):
333+
_, session_computing_id = logged_in_or_raise(request, db_session)
334+
await WebsiteAdmin.has_permission_or_raise(
335+
db_session, session_computing_id,
336+
errmsg="must have website admin permissions to remove a term"
337+
)
338+
339+
deleted_officer_term = await officers.crud.get_officer_term_by_id(db_session, term_id)
340+
341+
# TODO: log all important changes to a .log file
342+
await officers.crud.delete_officer_term_by_id(db_session, term_id)
343+
344+
return JSONResponse({
345+
"officer_term": deleted_officer_term.serializable_dict(),
346+
})

0 commit comments

Comments
 (0)