Skip to content

Commit

Permalink
pool-validation: Fix bug in validating new pool
Browse files Browse the repository at this point in the history
- The filter to check for invalid seeds was wrong, corrected this
- Lint files with new plugins
  • Loading branch information
SibiAkkash committed Nov 26, 2023
1 parent 024a214 commit a8d8ce8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
16 changes: 8 additions & 8 deletions frontend/src/components/TournamentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ import {
updateSeeding
} from "../queries";
import { useStore } from "../store";
import CreatePools from "./tournament/CreatePools";
import CreateTournamentForm from "./tournament/CreateTournamentForm";
import MatchCard from "./tournament/MatchCard";
import ReorderTeams from "./tournament/ReorderTeams";
import CreatePools from "./tournament/CreatePools";
import UpdateSpiritScoreForm from "./tournament/UpdateSpiritScoreForm";

const TournamentManager = () => {
Expand Down Expand Up @@ -497,12 +497,12 @@ const TournamentManager = () => {
</div>

<div class="my-5">
<div class="text-blue-500 text-xl font-bold mb-4">Pools</div>
<div class="mb-4 text-xl font-bold text-blue-500">Pools</div>
<Switch>
<Match when={selectedTournament()?.status === "DFT"}>
<Show when={isPoolsEdited()}>
<div
class="p-4 mb-4 text-sm text-red-800 rounded-lg bg-red-50 dark:bg-gray-800 dark:text-red-400"
class="mb-4 rounded-lg bg-red-50 p-4 text-sm text-red-800 dark:bg-gray-800 dark:text-red-400"
role="alert"
>
Changes are not saved. Please click on Submit Pools button.
Expand All @@ -518,7 +518,7 @@ const TournamentManager = () => {
/>
<button
type="button"
class="basis-1/3 px-4 py-2 my-4 text-sm font-normal rounded-lg text-white bg-blue-700 hover:bg-blue-800 dark:bg-blue-600 dark:hover:bg-blue-700 disabled:dark:bg-gray-400"
class="my-4 basis-1/3 rounded-lg bg-blue-700 px-4 py-2 text-sm font-normal text-white hover:bg-blue-800 dark:bg-blue-600 dark:hover:bg-blue-700 disabled:dark:bg-gray-400"
onClick={submitPools}
disabled={createPoolsMutation.isLoading}
>
Expand All @@ -542,8 +542,8 @@ const TournamentManager = () => {
<div>
<h3>Pool - {pool.name}</h3>{" "}
<div class="relative overflow-x-auto">
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<table class="w-full text-left text-sm text-gray-500 dark:text-gray-400">
<thead class="bg-gray-50 text-xs uppercase text-gray-700 dark:bg-gray-700 dark:text-gray-400">
<tr>
<th scope="col" class="px-6 py-3">
Seeding
Expand All @@ -559,10 +559,10 @@ const TournamentManager = () => {
<tbody>
<For each={Object.keys(pool.initial_seeding)}>
{seed => (
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
<tr class="border-b bg-white dark:border-gray-700 dark:bg-gray-800">
<th
scope="row"
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap dark:text-white"
class="whitespace-nowrap px-6 py-4 font-medium text-gray-900 dark:text-white"
>
{seed}
</th>
Expand Down
32 changes: 16 additions & 16 deletions frontend/src/components/tournament/CreatePools.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
DragDropProvider,
DragDropSensors,
closestCenter,
createDraggable,
createDroppable,
closestCenter
DragDropProvider,
DragDropSensors
} from "@thisbeyond/solid-dnd";
import { For, batch, createSignal, Show } from "solid-js";
import { batch, createSignal, For, Show } from "solid-js";

const remainingTeamsName = "Remaining";

Expand All @@ -17,8 +17,8 @@ const remainingTeamsName = "Remaining";
* @returns
*/
const TeamInfo = props => (
<div class="p-2 text-start flex gap-4 cursor-grab text-sm">
<div class="basis-1/6 text-center text-white dark:text-white-400 rounded-xl border-b bg-green-400 border-green-200 dark:bg-green-800 dark:border-green-700">
<div class="flex cursor-grab gap-4 p-2 text-start text-sm">
<div class="dark:text-white-400 basis-1/6 rounded-xl border-b border-green-200 bg-green-400 text-center text-white dark:border-green-700 dark:bg-green-800">
{props.team.seed}
</div>
<div class="basis-5/6">{props.team.name}</div>
Expand All @@ -34,7 +34,7 @@ const Draggable = props => {
return (
<div
use:draggable
class="text-gray-600 dark:text-gray-400 my-2 rounded-lg bg-gray-300 border-gray-400 border-b dark:bg-gray-800 dark:border-gray-700"
class="my-2 rounded-lg border-b border-gray-400 bg-gray-300 text-gray-600 dark:border-gray-700 dark:bg-gray-800 dark:text-gray-400"
>
<TeamInfo team={props.team} />
</div>
Expand All @@ -54,7 +54,7 @@ const Droppable = props => {
<>
<div
use:droppable
class="flex flex-col justify-items-center justify-start gap-y-2 content-center h-full p-4 rounded-lg dark:text-gray-400 bg-white text-center select-none border dark:border-gray-700"
class="flex h-full select-none flex-col content-center justify-start justify-items-center gap-y-2 rounded-lg border bg-white p-4 text-center dark:border-gray-700 dark:text-gray-400"
classList={{
"ring-4 ring-blue-500": droppable.isActiveDroppable,
"justify-between bg-gray-200/80 dark:bg-gray-700/80 text-black dark:text-white":
Expand All @@ -63,7 +63,7 @@ const Droppable = props => {
}}
>
{/* bg-gray-200 dark:bg-gray-700 border-b dark:border-gray-600 rounded-b-xl */}
<h4 class="text-center p-2 justify-self-start">
<h4 class="justify-self-start p-2 text-center">
<Show when={props.id !== remainingTeamsName} fallback={"All teams"}>
Pool - {props.name}
</Show>
Expand All @@ -73,7 +73,7 @@ const Droppable = props => {
</div>
<Show when={props.id !== remainingTeamsName}>
<button
class="justify-self-end p-2 text-sm font-normal text-white rounded-lg bg-red-700 hover:bg-red-800 dark:bg-red-500/75 dark:hover:bg-red-700/75"
class="justify-self-end rounded-lg bg-red-700 p-2 text-sm font-normal text-white hover:bg-red-800 dark:bg-red-500/75 dark:hover:bg-red-700/75"
onClick={() => props.removePool(props.id)}
>
Remove pool
Expand Down Expand Up @@ -142,22 +142,22 @@ const CreatePools = props => {

return (
<>
<div class="w-1/2 mb-4 flex gap-4 justify-start items-center rounded-md">
<div class="mb-4 flex w-1/2 items-center justify-start gap-4 rounded-md">
<input
type="text"
id="pool-name"
placeholder="Enter Pool Name"
value={enteredPoolName()}
onChange={e => setEnteredPoolName(e.target.value.trim())}
class="basis-2/3 p-2 placeholder:text-md placeholder:italic text-gray-900 rounded-lg bg-gray-50 border border-gray-300 dark:border-gray-600 sm:text-xs focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
class="placeholder:text-md basis-2/3 rounded-lg border border-gray-300 bg-gray-50 p-2 text-gray-900 placeholder:italic focus:border-blue-500 focus:ring-blue-500 dark:border-gray-600 dark:bg-gray-700 dark:text-white dark:placeholder-gray-400 dark:focus:border-blue-500 dark:focus:ring-blue-500 sm:text-xs"
/>
<button
type="button"
onClick={() => {
addPool(enteredPoolName());
setEnteredPoolName("");
}}
class="basis-1/3 p-2 font-normal text-sm rounded-lg text-white bg-blue-700 hover:bg-blue-800 dark:bg-blue-600 dark:hover:bg-blue-700 disabled:dark:bg-gray-400"
class="basis-1/3 rounded-lg bg-blue-700 p-2 text-sm font-normal text-white hover:bg-blue-800 dark:bg-blue-600 dark:hover:bg-blue-700 disabled:dark:bg-gray-400"
>
Add Pool
</button>
Expand All @@ -168,9 +168,9 @@ const CreatePools = props => {
collisionDetector={closestCenter}
>
<DragDropSensors>
<div class="w-full grid grid-rows-2 grid-cols-3 grid-flow-row gap-4">
<div class="grid w-full grid-flow-row grid-cols-3 grid-rows-2 gap-4">
<div
class="row-span-2 col-span-1 transition-opacity"
class="col-span-1 row-span-2 transition-opacity"
classList={{ "opacity-30": props.isUpdating }}
>
<Droppable
Expand All @@ -184,7 +184,7 @@ const CreatePools = props => {
{([poolName, teams]) => (
<Show when={poolName !== remainingTeamsName}>
<div
class="row-span-1 col-span-1 transition-opacity"
class="col-span-1 row-span-1 transition-opacity"
classList={{ "opacity-30": props.isUpdating }}
>
<Droppable
Expand Down
17 changes: 7 additions & 10 deletions server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,17 +1259,7 @@ def create_pools(
created_pools: list[Pool] = []

for pool in pools:

valid_pool, errors = validate_new_pool(
tournament=tournament, new_pool=set(pool.seeding)
)
if not valid_pool:
message = "Cannot create pools, due to following errors: \n"
message += "\n".join(f"{key}: {value}" for key, value in errors.items())
return 400, {"message": message}

existing_pool_names.discard(pool.name)

try:
# if the incoming pool's name and seeding matches an existing pool
# no need to recreate pool and matches, continue
Expand All @@ -1288,6 +1278,13 @@ def create_pools(
# we need to create pool and matches
print(f"pool {pool.name} not present in {tournament.event.title}")

valid_pool, errors = validate_new_pool(tournament=tournament, new_pool=set(pool.seeding))

if not valid_pool:
message = "Cannot create pools, due to following errors: \n"
message += "\n".join(f"{key}: {value}" for key, value in errors.items())
return 400, {"message": message}

created_pool = create_pool(tournament=tournament, pool=pool)
created_pools.append(created_pool)
create_pool_matches(tournament=tournament, pool=created_pool)
Expand Down
5 changes: 3 additions & 2 deletions server/tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -889,14 +889,15 @@ def validate_new_pool(
) # Pool.initial_seeding : dict[seed(str), team_id(int)]. Convert seeds from str to ints

repeated_seeds_in_new_pool = already_present_seeds.intersection(new_pool)
# the seed shouldn't negative, 0 or more than roster size

# the seed shouldn't be negative, 0 or more than roster size
tournament_roster_size = (
UCRegistration.objects.filter(event=tournament.event)
.values_list("team", flat=True)
.distinct()
.count()
)
invalid_seeds = list(filter(lambda seed: 1 <= seed <= tournament_roster_size, new_pool))
invalid_seeds = list(filter(lambda seed: not (1 <= seed <= tournament_roster_size), new_pool))
valid_pool = True
errors = {}

Expand Down

0 comments on commit a8d8ce8

Please sign in to comment.