remove old & unused commands

This commit is contained in:
Corban-Lee Jones 2024-11-05 22:07:36 +00:00
parent 3d64b410d5
commit c37f69f64f
6 changed files with 0 additions and 258 deletions

View File

@ -1,117 +0,0 @@
import names
import json
import random
from typing import Optional, List, Set
from string import ascii_uppercase
from django.core.management.base import BaseCommand
from django.db.models import Q, Count
from mainapp.models import Member, Team, SectionValidator, Section
# TODO: refactor this file like create_teams_fixtures.py
class Command(BaseCommand):
help = "Creates a fixture with randomly generated Member objects"
def add_arguments(self, parser):
parser.add_argument("members_per_team", type=int)
def handle(self, *args, **options):
members_per_team = options["members_per_team"]
teams = Team.objects.all()
if not teams:
raise Exception("no teams")
new_members = []
for team in teams:
i = members_per_team
while i > 0:
team_member_ids = team.members.values_list('id', flat=True)
print(team_member_ids)
section = random.choice([
sec for sec in
Section.objects.exclude(members__id__in=team_member_ids)
])
member = Member.objects.create(
first_name=names.get_first_name(),
last_name=names.get_last_name(),
section=section,
team=team
)
# if not section.is_joinable(member):
# member.delete()
# i += 1
# continue
new_members.append(member)
print(member)
i -= 1
fixtures = []
for member in new_members:
member.delete()
fixture = {
"model": "mainapp.member",
"pk": member.pk,
"fields": {
"first_name": member.first_name,
"last_name": member.last_name,
"team": member.team_id,
"section": member.section_id,
"peg_number": member.peg_number
}
}
fixtures.append(fixture)
with open("src/mainapp/fixtures/members_fixture.json", "w") as f:
f.write(json.dumps(fixtures, indent=2))
self.stdout.write(self.style.SUCCESS("Created members_fixture.json."))
# def handle(self, *args, **options):
# num_members = options["num_members"]
# teams = Team.objects.all()
# if not teams:
# print("No teams found. Please create some teams first.")
# return
# members = []
# for team in teams:
# for _ in range(num_members):
# first_name = names.get_first_name()
# last_name = names.get_last_name()
# member = Member.objects.create(first_name=first_name, last_name=last_name, team=team)
# members.append(member)
# self.stdout.write(self.style.SUCCESS(f"Created {num_members} members."))
# # create a members fixture file
# members_fixture = []
# for member in members:
# member_fixture = {
# "model": "mainapp.member",
# "pk": member.pk,
# "fields": {
# "first_name": member.first_name,
# "last_name": member.last_name,
# "team": member.team_id,
# "peg_number": member.peg_number,
# }
# }
# members_fixture.append(member_fixture)
# with open("src/mainapp/fixtures/members_fixture.json", "w") as f:
# f.write(json.dumps(members_fixture, indent=2))
# self.stdout.write(self.style.SUCCESS("Created members_fixture.json."))

View File

@ -1,79 +0,0 @@
"""Command to create test data fixture for sections."""
import json
from string import ascii_uppercase
from django.core.management.base import BaseCommand
from mainapp.models import Section, SectionValidator
def name_sort_key(name):
if len(name) == 1:
return (0, name)
else:
return (1, name[:-1], name[-1])
class Command(BaseCommand):
help = "Creates a fixture file for Team objects"
def add_arguments(self, parser):
parser.add_argument("amount_of_sections", type=int, help="Number of sections to create")
def handle(self, *args, **options):
amount_of_sections = options["amount_of_sections"]
used_names = set()
last_name = ""
for i in range(amount_of_sections):
add_character = last_name.endswith("Z")
if add_character:
last_name = ""
new_name = last_name if add_character else ""
if not new_name.endswith("Z"):
new_name = new_name[:-1] if len(new_name) > 1 else new_name
try:
new_name += ascii_uppercase[i]
except IndexError:
quotient, remainder = divmod(i, 26)
new_name += ascii_uppercase[quotient - 1] + ascii_uppercase[remainder]
while new_name in used_names:
if new_name[-1] == "Z":
idx = len(new_name) - 2
while idx >= 0 and new_name[idx] == "Z":
idx -= 1
if idx < 0:
new_name = "A" * (len(new_name) + 1)
else:
new_name = new_name[:idx] + chr(ord(new_name[idx]) + 1) + "A" * (len(new_name) - idx - 1)
else:
new_name = new_name[:-1] + chr(ord(new_name[-1]) + 1)
used_names.add(new_name)
last_name = new_name
names = sorted([name for name in used_names], key=name_sort_key)
print([name for name in names])
sections = [Section(name=name) for name in names]
Section.objects.bulk_create(sections)
fixture = [{
"model": "mainapp.section",
"pk": section.pk,
"fields": {
"name": section.name
}
} for section in sections]
for section in sections:
section.delete()
with open("src/mainapp/fixtures/sections_fixture.json", "w") as file:
file.write(json.dumps(fixture, indent=4))
self.stdout.write(self.style.SUCCESS("Created sections_fixture.json."))

View File

@ -1,28 +0,0 @@
from django.contrib.auth.management.commands import createsuperuser
from django.core.management import CommandError
class Command(createsuperuser.Command):
help = 'Crate a superuser, and allow password to be provided'
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--password', dest='password', default=None,
help='Specifies the password for the superuser.',
)
def handle(self, *args, **options):
password = options.get('password')
username = options.get('username')
database = options.get('database')
if password and not username:
raise CommandError("--username is required if specifying --password")
super(Command, self).handle(*args, **options)
if password:
user = self.UserModel._default_manager.db_manager(database).get(username=username)
user.set_password(password)
user.save()

View File

@ -1,34 +0,0 @@
"""Command to create test data fixture for teams."""
import json
from django.core.management.base import BaseCommand
from mainapp.models import Team
class Command(BaseCommand):
help = "Creates a fixture file for Team objects"
def add_arguments(self, parser):
parser.add_argument("amount_of_teams", type=int, help="Number of teams to create")
def handle(self, *args, **options):
teams_amount = options["amount_of_teams"]
# Create the new teams (this will create them in the database)#
new_teams = [Team.objects.create() for i in range(teams_amount)]
teams_fixture = [{
"model": "mainapp.team",
"pk": team.pk,
"fields": {}
} for team in new_teams]
# Remove the teams from the database
for team in new_teams:
team.delete()
with open("src/mainapp/fixtures/teams_fixture.json", "w") as file:
file.write(json.dumps(teams_fixture, indent=4))
self.stdout.write(self.style.SUCCESS("Created teams_fixture.json."))