Add ClubMembership enhancements, CSV import command, and related tests

- Extend the ClubMembership model with `club`, `member`, and optional `license` fields, along with relevant constraints and ordering.
- Implement verbose names for Club and ClubMembership models and update admin configurations for better display and filtering.
- Add a `import_members_csv` management command for batch importing members, clubs, and memberships from a CSV file.
- Include extensive tests for the `import_members_csv` command, ClubMembership model, and Club model.
- Refactor related migrations, services, and test structure.
This commit is contained in:
2026-07-05 23:51:35 +02:00
parent 44fe658efa
commit 89c12c12b1
12 changed files with 753 additions and 2 deletions

View File

View File

@@ -0,0 +1,43 @@
from django.core.management.base import BaseCommand, CommandError
from authentication.services.member_csv_importer import MemberCsvImporter
class Command(BaseCommand):
help = "Import club members from a CSV file."
def add_arguments(self, parser) -> None:
parser.add_argument(
"csv_file",
type=str,
help="Path to the CSV file to import.",
)
parser.add_argument(
"--date-format",
default="%Y-%m-%d",
help="Date format for date_of_birth. Default: %%Y-%%m-%%d",
)
def handle(self, *args, **options) -> str | None:
importer = MemberCsvImporter(date_format=options["date_format"])
try:
result = importer.import_path(options["csv_file"])
except Exception as exc:
raise CommandError(str(exc)) from exc
for error in result.errors:
self.stderr.write(self.style.ERROR(f"Row {error.row_number} skipped: {error.message}"))
self.stdout.write(
self.style.SUCCESS(
"Import complete. "
f"Members created: {result.created_members}. "
f"Members updated: {result.updated_members}. "
f"Users created: {result.created_users}. "
f"Clubs created: {result.created_clubs}. "
f"Memberships created: {result.created_memberships}. "
f"Memberships updated: {result.updated_memberships}. "
f"Rows skipped: {result.skipped_rows}."
)
)