Skip to content

Commit

Permalink
Merge pull request #276 from pinwheeeel/develop
Browse files Browse the repository at this point in the history
fix bugs and other improvements in add_clubs
  • Loading branch information
pinwheeeel authored Sep 28, 2024
2 parents 9acbf41 + 697b523 commit 1d8689e
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions core/management/commands/add_clubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def success(self, *args, **kwargs):
self.style.SUCCESS(*args, **kwargs),
)

def warn(self, *args, **kwargs):
self.stdout.write(
self.style.WARNING(*args, **kwargs),
)

def add_arguments(self, parser):
parser.add_argument(
"sheets_link",
Expand Down Expand Up @@ -74,11 +79,11 @@ def handle(self, *args, **options):
for row in csv_reader:
organization_is_not_approved = row[1] != "TRUE"
has_duplicate_owner = len(row[0]) == 0
if organization_is_not_approved:
self.error(f"Skipping {row[0]} because it is not approved\n")
if has_duplicate_owner:
# self.error(f"Skipping a row because it is a duplicate owner of the previously added club\n") # logging this is probably not necessary
continue
elif has_duplicate_owner:
self.error(f"Skipping {row[0]} as it's a duplicate owner\n")
elif organization_is_not_approved:
self.error(f"Skipping {row[0]} because it is not approved\n")
continue

self.success(f"New organization: {row[0]}")
Expand Down Expand Up @@ -124,15 +129,20 @@ def handle(self, *args, **options):
"show_members": True,
"is_active": True,
"is_open": False,
},
} # this singular comma gave me a run for my money. i have lost my family, my wealth, my sanity, and my soul from the inclusion of this character.
# fmt: on

slug="".join(
c.casefold() if c.isalnum() else "-"
for c in organization_name
).lower()

if not Organization.objects.filter(slug=slug).exists():
slug = self.get_corrected_slug_or_not(organization_name, slug)

if not options["dry_run"]:
club, created = Organization.objects.update_or_create(
slug="".join(
c.casefold() if c.isalnum() else "-"
for c in organization_name
).lower(),
slug=slug,
defaults=defaults,
create_defaults={
**defaults,
Expand All @@ -146,7 +156,7 @@ def handle(self, *args, **options):
else:
status = "(dry-run | would have added)"
self.success(
f"\tSuccessfully {status} {organization_name} organization, owned by {owner_name}"
f"\tSuccessfully {status} '{organization_name}' organization (slug={slug}), owned by {owner_name}"
)
except IntegrityError as IE:
self.error(IE.__traceback__)
Expand Down Expand Up @@ -178,3 +188,22 @@ def get_user_by_email(self, name: str, email: str) -> User | Status:
)

self.stdout.write("\tPlease re-enter email:")

def get_corrected_slug_or_not(self, organization_name: str, slug: str) -> str:
self.warn(
f"\tCould not find '{organization_name}' with the slug '{slug}'. Please enter the correct slug if the organization exists or leave blank to create club"
)

while True:
print("\t", end="")
new_slug = input()

if new_slug == "":
return slug
elif Organization.objects.filter(slug=new_slug).exists():
return new_slug
else:
self.error(
f"\tCould not find an organization with the slug '{new_slug}'. Please try again or leave blank to create a club."
)

0 comments on commit 1d8689e

Please sign in to comment.