Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import scripts #384

Merged
merged 6 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ swap_size_mb = 512
PORT = "8000"
DJANGO_SETTINGS_MODULE = "hub.production"

[[vm]]
size = "shared-cpu-2x"

[[services]]
protocol = "tcp"
internal_port = 80
Expand Down
22 changes: 22 additions & 0 deletions server/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
from server.season.models import Season
from server.series.models import Series, SeriesRegistration, SeriesRosterInvitation
from server.tournament.models import (
Bracket,
Event,
Match,
Pool,
PositionPool,
Registration,
Tournament,
TournamentField,
Expand Down Expand Up @@ -137,6 +139,26 @@ def get_name(self, obj: Pool) -> str:
return obj.tournament.event.title


@admin.register(Bracket)
class BracketAdmin(admin.ModelAdmin[Bracket]):
search_fields = ["tournament__event__title"]
list_display = ["get_name", "name"]

@admin.display(description="Tournament Name", ordering="tournament__event__title")
def get_name(self, obj: Pool) -> str:
return obj.tournament.event.title


@admin.register(PositionPool)
class PositionPoolAdmin(admin.ModelAdmin[PositionPool]):
search_fields = ["tournament__event__title"]
list_display = ["get_name", "name"]

@admin.display(description="Tournament Name", ordering="tournament__event__title")
def get_name(self, obj: Pool) -> str:
return obj.tournament.event.title


@admin.register(Match)
class MatchAdmin(admin.ModelAdmin[Match]):
search_fields = ["tournament__event__title"]
Expand Down
56 changes: 56 additions & 0 deletions server/management/commands/activate_memberships.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import csv
from pathlib import Path
from typing import Any

from django.core.management.base import BaseCommand, CommandParser

from server.core.models import Player, User
from server.membership.models import Membership
from server.season.models import Season
from server.utils import today


class Command(BaseCommand):
help = "Activate memberships from CSV file with player details"

def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("csv_file", type=Path, help="Path to the CSV file")

def handle(self, *args: Any, **options: Any) -> None:
season = Season.objects.filter(end_date__gt=today()).first()

if season is None:
self.stderr.write(self.style.ERROR("No Season found"))
return

membership_defaults = {
"start_date": season.start_date,
"end_date": season.end_date,
"event": None,
"season": season,
"is_active": True,
"waiver_valid": True,
}

csv_file = options["csv_file"]

with open(csv_file) as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
# Process each row of the CSV file
email = row["email"].strip().lower()

try:
user = User.objects.get(username=email)
player = Player.objects.get(user=user)
except (User.DoesNotExist, Player.DoesNotExist):
self.stderr.write(self.style.ERROR(f"Player not found: {email}"))
continue

membership, created = Membership.objects.get_or_create(
player=player, defaults=membership_defaults
)
if not created:
for key, value in membership_defaults.items():
setattr(membership, key, value)
membership.save()
72 changes: 72 additions & 0 deletions server/management/commands/add_to_event_roster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import csv
from pathlib import Path
from typing import Any

from django.core.management.base import BaseCommand, CommandParser
from django.db import IntegrityError

from server.core.models import Player, Team, User
from server.tournament.models import Event, Registration, Tournament
from server.tournament.utils import can_register_player_to_series_event


class Command(BaseCommand):
help = "Add to event roster from CSV file with player details"

def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("csv_file", type=Path, help="Path to the CSV file")
parser.add_argument("--team-id", "-t", help="Team ID")
parser.add_argument("--event-id", "-s", help="Event ID")

def handle(self, *args: Any, **options: Any) -> None:
try:
event = Event.objects.get(id=options["event_id"])
team = Team.objects.get(id=options["team_id"])
tournament = Tournament.objects.get(event=event)
except (Event.DoesNotExist, Team.DoesNotExist, Tournament.DoesNotExist):
self.stderr.write(self.style.ERROR("Event / Team / Tournament does not exist"))
return

if team not in tournament.teams.all():
self.stderr.write(
self.style.ERROR(f"{team.name} is not registered for ${event.title} !")
)
return

csv_file = options["csv_file"]

with open(csv_file) as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
# Process each row of the CSV file
email = row["email"].strip().lower()

try:
user = User.objects.get(username=email)
player = Player.objects.get(user=user)
except (User.DoesNotExist, Player.DoesNotExist):
self.stderr.write(self.style.ERROR(f"Player not found: {email}"))
continue

if event.series:
can_register, error = can_register_player_to_series_event(
event=event, team=team, player=player
)
if not can_register and error:
self.stderr.write(self.style.ERROR(f"Error: {error}, player: {email}"))
continue

registration = Registration(
event=event,
team=team,
player=player,
)
try:
registration.save()
except IntegrityError:
self.stderr.write(
self.style.ERROR(
f"Error: Player already added to another team for this event, player: {email}"
)
)
continue
54 changes: 54 additions & 0 deletions server/management/commands/add_to_series_roster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import csv
from pathlib import Path
from typing import Any

from django.core.management.base import BaseCommand, CommandParser

from server.core.models import Player, Team, User
from server.series.models import Series
from server.series.utils import register_player


class Command(BaseCommand):
help = "Add to series roster from CSV file with player details"

def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument("csv_file", type=Path, help="Path to the CSV file")
parser.add_argument("--team-id", "-t", help="Team ID")
parser.add_argument("--series-id", "-s", help="Series ID")

def handle(self, *args: Any, **options: Any) -> None:
try:
series = Series.objects.get(id=options["series_id"])
team = Team.objects.get(id=options["team_id"])
except (Series.DoesNotExist, Team.DoesNotExist):
self.stderr.write(self.style.ERROR("Series / Team does not exist"))
return

csv_file = options["csv_file"]

with open(csv_file) as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
# Process each row of the CSV file
email = row["email"].strip().lower()

try:
user = User.objects.get(username=email)
player = Player.objects.get(user=user)
except (User.DoesNotExist, Player.DoesNotExist):
self.stderr.write(self.style.ERROR(f"Player not found: {email}"))
continue

series_registration, error = register_player(
series=series, team=team, player=player
)
if error:
self.stderr.write(self.style.ERROR(f"Error: {error}, player: {email}"))
continue

if series_registration is None:
self.stderr.write(
self.style.ERROR(f"Error: Couldn't register player, player: {email}")
)
continue
1 change: 1 addition & 0 deletions server/management/commands/import_players.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def handle(self, *args: Any, **options: Any) -> None:
"not_in_india": row["not_in_india"].upper() == "Y",
"occupation": OCCUPATIONS.get(row["occupation"], None),
"educational_institution": row["educational_institution"],
"sponsored": row["sponsored"].upper() == "Y" if "sponsored" in row else False,
}
if player_data["gender"] != Player.GenderTypes.OTHER:
player_data["match_up"] = player_data["gender"]
Expand Down
Loading
Loading