Skip to content

Commit

Permalink
feat: also remove class
Browse files Browse the repository at this point in the history
  • Loading branch information
tiptenbrink committed Jan 4, 2024
1 parent 217f5be commit 6f13757
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 57 deletions.
8 changes: 4 additions & 4 deletions backend/actions/local_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ async def test_add_classification(local_dsrc):
@pytest.mark.asyncio
async def test_update_points(local_dsrc):
async with get_conn(local_dsrc) as conn:
training_class = await data.classifications.most_recent_class_of_type(
training_class = (await data.classifications.most_recent_class_of_type(
conn, "training"
)
points_class = await data.classifications.most_recent_class_of_type(
))[0]
points_class = (await data.classifications.most_recent_class_of_type(
conn, "points"
)
))[0]
await update_class_points(conn, training_class.classification_id)
await update_class_points(conn, points_class.classification_id)

Expand Down
8 changes: 6 additions & 2 deletions backend/src/apiserver/app/ops/startup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from apiserver.app.error import AppError, ErrorKeys
from apiserver.data.special import update_class_points
from loguru import logger
from asyncio import sleep
from datetime import date
Expand Down Expand Up @@ -199,8 +200,11 @@ async def initial_population(dsrc: Source, config: Config) -> None:
user_id = await data.user.insert_return_user_id(conn, fake_user)
assert user_id == "1_fakerecord"

await insert_classification(conn, "training")
await insert_classification(conn, "points")
new_training_id = await insert_classification(conn, "training")
new_points_id = await insert_classification(conn, "points")

await update_class_points(conn, new_training_id, False)
await update_class_points(conn, new_points_id, False)


async def get_keystate(dsrc: Source) -> KeyState:
Expand Down
31 changes: 25 additions & 6 deletions backend/src/apiserver/app/routers/ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
mod_user_events_in_class,
)
from apiserver.app.response import RawJSONResponse
from apiserver.data.api.classifications import get_event_user_points
from apiserver.data.api.classifications import (
get_event_user_points,
remove_classification,
)
from apiserver.data import Source
from apiserver.data.context.app_context import RankingContext, conn_wrap
from apiserver.data.context.ranking import (
Expand Down Expand Up @@ -108,8 +111,10 @@ async def get_classification_with_meta(
async def get_classifications(
recent_number: int, dsrc: SourceDep, app_context: AppContext
) -> RawJSONResponse:
recent_classes = await most_recent_classes(app_context.rank_ctx, dsrc, recent_number)

recent_classes = await most_recent_classes(
app_context.rank_ctx, dsrc, recent_number
)

return RawJSONResponse(ClassMetaList.dump_json(recent_classes))


Expand Down Expand Up @@ -206,13 +211,27 @@ async def get_event_users(

@ranking_admin_router.post("/new/")
async def new_classes(
dsrc: SourceDep, app_context: AppContext,
dsrc: SourceDep,
app_context: AppContext,
) -> None:
await context_new_classes(app_context.rank_ctx, dsrc)


@ranking_admin_router.post("/modify/")
async def modify_class(
updated_class: ClassUpdate, dsrc: SourceDep, app_context: AppContext,
updated_class: ClassUpdate,
dsrc: SourceDep,
app_context: AppContext,
) -> None:
await context_modify_class(app_context.rank_ctx, dsrc, updated_class)
await context_modify_class(app_context.rank_ctx, dsrc, updated_class)


@ranking_admin_router.post("/remove/{class_id}/")
async def remove_class(
class_id: int,
dsrc: SourceDep,
app_context: AppContext,
) -> None:
await ctxlize_wrap(remove_classification, conn_wrap)(
app_context.rank_ctx, dsrc, class_id
)
36 changes: 27 additions & 9 deletions backend/src/apiserver/data/api/classifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
ClassMetaList,
ClassUpdate,
Classification,
ClassView,
EventDate,
UserPoints,
UserPointsNames,
Expand Down Expand Up @@ -44,15 +43,16 @@
)
from store.db import (
LiteralDict,
delete_by_column,
get_largest_where,
insert,
insert_many,
insert_return_col,
lit_model,
select_some_join_where,
select_some_where,
update_by_unique,
update_column_by_unique,
upsert_by_unique,
)
from store.error import DataError, NoDataError, DbError, DbErrors

Expand All @@ -67,7 +67,7 @@ def parse_user_points(user_points: list[RowMapping]) -> list[UserPointsNames]:

async def insert_classification(
conn: AsyncConnection, class_type: str, start_date: date | None = None
) -> None:
) -> int:
if start_date is None:
start_date = date.today()
new_classification = Classification(
Expand All @@ -77,7 +77,10 @@ async def insert_classification(
end_date=start_date + timedelta(days=30 * 5),
hidden_date=start_date + timedelta(days=30 * 4),
)
await insert(conn, CLASSIFICATION_TABLE, lit_model(new_classification))
return_id: int = await insert_return_col(
conn, CLASSIFICATION_TABLE, lit_model(new_classification), CLASS_ID
)
return return_id


async def most_recent_class_of_type(
Expand All @@ -96,7 +99,14 @@ async def most_recent_class_of_type(
largest_class_list = await get_largest_where(
conn,
CLASSIFICATION_TABLE,
{CLASS_ID, CLASS_LAST_UPDATED, CLASS_START_DATE, CLASS_HIDDEN_DATE, CLASS_END_DATE},
{
CLASS_ID,
CLASS_TYPE,
CLASS_LAST_UPDATED,
CLASS_START_DATE,
CLASS_HIDDEN_DATE,
CLASS_END_DATE,
},
CLASS_TYPE,
query_class_type,
CLASS_START_DATE,
Expand Down Expand Up @@ -267,8 +277,16 @@ async def class_update_last_updated(
conn, CLASSIFICATION_TABLE, CLASS_LAST_UPDATED, date, CLASS_ID, class_id
)

async def update_classification(
conn: AsyncConnection, class_view: ClassUpdate
) -> None:

await update_by_unique(conn, CLASSIFICATION_TABLE, lit_model(class_view), "classification_id", class_view.classification_id)
async def update_classification(conn: AsyncConnection, class_view: ClassUpdate) -> None:
await update_by_unique(
conn,
CLASSIFICATION_TABLE,
lit_model(class_view),
"classification_id",
class_view.classification_id,
)


async def remove_classification(conn: AsyncConnection, class_id: int) -> None:
await delete_by_column(conn, CLASSIFICATION_TABLE, "classification_id", class_id)
15 changes: 6 additions & 9 deletions backend/src/apiserver/data/context/app_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
ClassEvent,
ClassMeta,
ClassUpdate,
ClassView,
NewEvent,
RankingInfo,
UserData,
Expand Down Expand Up @@ -104,23 +103,21 @@ async def context_get_event_users(
cls, dsrc: Source, event_id: str
) -> list[UserPointsNames]:
raise ContextNotImpl()

@classmethod
async def most_recent_classes(
cls, dsrc: Source, amount: int = 10
) -> list[ClassMeta]:
raise ContextNotImpl()

@classmethod
async def context_new_classes(
cls, dsrc: Source
) -> None:
async def context_new_classes(cls, dsrc: Source) -> None:
raise ContextNotImpl()

@classmethod
async def context_modify_class(
cls, dsrc: Source, class_update: ClassUpdate
) -> None:
cls, dsrc: Source, class_update: ClassUpdate
) -> None:
raise ContextNotImpl()


Expand Down
55 changes: 32 additions & 23 deletions backend/src/apiserver/data/context/ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ async def add_new_event(dsrc: Source, new_event: NewEvent) -> None:
date. Use the 'publish' function to force them to be equal."""
async with get_conn(dsrc) as conn:
try:
classification = (await most_recent_class_of_type(conn, new_event.class_type))[0]
classification = (
await most_recent_class_of_type(conn, new_event.class_type)
)[0]
except DataError as e:
if e.key != "incorrect_class_type":
raise e
Expand Down Expand Up @@ -139,14 +141,18 @@ async def context_most_recent_class_id_of_type(
dsrc: Source, rank_type: Literal["points", "training"]
) -> int:
async with get_conn(dsrc) as conn:
class_id = (await most_recent_class_of_type(conn, rank_type))[0].classification_id
class_id = (await most_recent_class_of_type(conn, rank_type))[
0
].classification_id

return class_id


@ctx_reg.register(RankingContext)
async def context_most_recent_class_points(
dsrc: Source, rank_type: Literal["points", "training"], is_admin: bool,
dsrc: Source,
rank_type: Literal["points", "training"],
is_admin: bool,
) -> RankingInfo:
async with get_conn(dsrc) as conn:
class_view = (await most_recent_class_of_type(conn, rank_type))[0]
Expand Down Expand Up @@ -198,36 +204,39 @@ async def context_get_event_users(dsrc: Source, event_id: str) -> list[UserPoint
return events_points


MIN_AMOUNT = 2


@ctx_reg.register(RankingContext)
async def most_recent_classes(
dsrc: Source, amount: int = 10
) -> list[ClassMeta]:
if amount < 2 or amount % 2 != 0:
async def most_recent_classes(dsrc: Source, amount: int = 10) -> list[ClassMeta]:
if amount < MIN_AMOUNT or amount % 2 != 0:
raise AppError(
ErrorKeys.DATA,
"Request at least 2 classes and make sure it is an even number!",
"most_recent_too_few",
)
ErrorKeys.DATA,
"Request at least 2 classes and make sure it is an even number!",
"most_recent_too_few",
)

async with get_conn(dsrc) as conn:
training_classes = (await most_recent_class_of_type(conn, "training", amount // 2))
points_classes = (await most_recent_class_of_type(conn, "points", amount // 2))
training_classes = await most_recent_class_of_type(
conn, "training", amount // 2
)
points_classes = await most_recent_class_of_type(conn, "points", amount // 2)

return training_classes + points_classes


@ctx_reg.register(RankingContext)
async def context_new_classes(
dsrc: Source
) -> None:
async def context_new_classes(dsrc: Source) -> None:
async with get_conn(dsrc) as conn:
await insert_classification(conn, "training")
await insert_classification(conn, "points")
new_training_id = await insert_classification(conn, "training")
new_points_id = await insert_classification(conn, "points")

await update_class_points(conn, new_training_id, False)
await update_class_points(conn, new_points_id, False)


@ctx_reg.register(RankingContext)
async def context_modify_class(
dsrc: Source, class_update: ClassUpdate
) -> None:
async def context_modify_class(dsrc: Source, class_update: ClassUpdate) -> None:
async with get_conn(dsrc) as conn:
await update_classification(conn, class_update)
await update_classification(conn, class_update)
await update_class_points(conn, class_update.classification_id, False)
4 changes: 3 additions & 1 deletion backend/src/apiserver/lib/model/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ class EventDate(BaseModel):


class ClassMeta(ClassView):
type: Literal["points", "training"]
end_date: date


Expand All @@ -294,4 +295,5 @@ class ClassUpdate(BaseModel):
hidden_date: date
end_date: date

ClassMetaList = TypeAdapter(List[ClassMeta])

ClassMetaList = TypeAdapter(List[ClassMeta])
4 changes: 1 addition & 3 deletions backend/src/store/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ async def update_by_unique(

_, _, row_keys_set = _row_keys_vars_set(set_dict)

query = text(
f"UPDATE {table} SET {row_keys_set} WHERE {unique_column} = :val;"
)
query = text(f"UPDATE {table} SET {row_keys_set} WHERE {unique_column} = :val;")
val_dict: LiteralDict = {"val": value}
params = set_dict | val_dict

Expand Down

0 comments on commit 6f13757

Please sign in to comment.