diff --git a/server/api/match/admin.py b/server/api/match/admin.py index d4b2599..17d961e 100644 --- a/server/api/match/admin.py +++ b/server/api/match/admin.py @@ -2,29 +2,14 @@ from django.contrib import admin from .models import Match, Team, MatchResult +import nested_admin -@admin.register(Match) -class MatchAdmin(admin.ModelAdmin): - list_display = ('match_name', 'match_date', 'group_id', 'stage_id') - search_fields = ('match_name',) - list_filter = ('group_id', 'stage_id') - - -@admin.register(Team) -class TeamAdmin(admin.ModelAdmin): - list_display = ('team_name', 'created_at') - search_fields = ('team_name',) - list_filter = ('created_at',) - - -@admin.register(MatchResult) -class MatchResultAdmin(admin.ModelAdmin): - list_display = ( - 'match', 'team', 'white_pins', 'penalty_pins', 'yellow_cards', 'red_cards', - 'p1_position', 'p2_position', 'honor_point', 'point', 'completed_time_second' - ) - exclude = ('honor_point', 'point') +class MatchResultInline(nested_admin.NestedTabularInline): + model = MatchResult + extra = 4 # 4 match results by default + max_num = 5 + readonly_fields = ('honor_point', 'point') list_filter = ('match', 'team') search_fields = ('match__match_name', 'team__team_name') @@ -44,3 +29,17 @@ def save_model(self, request, obj, form, change): obj.honor_point = self._get_honor_point(obj.p1_position, obj.p2_position) super().save_model(request, obj, form, change) # Call the parent's save_model + + +class MatchAdmin(nested_admin.NestedModelAdmin): + inlines = [MatchResultInline] + + +admin.site.register(Match, MatchAdmin) + + +@admin.register(Team) +class TeamAdmin(admin.ModelAdmin): + list_display = ('team_name', 'created_at') + search_fields = ('team_name',) + list_filter = ('created_at',) diff --git a/server/api/match/models.py b/server/api/match/models.py index f28fc5e..03ccd6d 100644 --- a/server/api/match/models.py +++ b/server/api/match/models.py @@ -11,6 +11,9 @@ class Match(models.Model): def __str__(self): return self.match_name + class Meta: + verbose_name_plural = "matches" + class Team(models.Model): team_id = models.AutoField(primary_key=True) diff --git a/server/api/settings.py b/server/api/settings.py index 789adcc..e9e5dcf 100644 --- a/server/api/settings.py +++ b/server/api/settings.py @@ -51,6 +51,7 @@ "django_extensions", "rest_framework", "corsheaders", + "nested_admin", "api.healthcheck", "api.match", "api.sponsor.apps.SponsorConfig", diff --git a/server/api/urls.py b/server/api/urls.py index f236185..e0cea68 100644 --- a/server/api/urls.py +++ b/server/api/urls.py @@ -16,12 +16,13 @@ """ from django.contrib import admin -from django.urls import path, include +from django.urls import path, include, re_path from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path("admin/", admin.site.urls), + re_path(r'^_nested_admin/', include('nested_admin.urls')), path("api/healthcheck/", include(("api.healthcheck.urls"))), path("api/match/", include(("api.match.urls"))), path("api/sponsor/", include(("api.sponsor.urls"))), diff --git a/server/poetry.lock b/server/poetry.lock index 33809e6..9058263 100644 --- a/server/poetry.lock +++ b/server/poetry.lock @@ -89,6 +89,24 @@ files = [ [package.dependencies] Django = ">=3.2" +[[package]] +name = "django-nested-admin" +version = "4.1.1" +description = "Django admin classes that allow for nested inlines" +optional = false +python-versions = ">=3.6" +files = [ + {file = "django-nested-admin-4.1.1.tar.gz", hash = "sha256:645d63b38d579b034a65e0983f1f8cbb8824c75b4232f8d62a1583fa7a9f568f"}, + {file = "django_nested_admin-4.1.1-py3-none-any.whl", hash = "sha256:7e72ab7a8ae4c91074f6f709ce38fdf54f9c5f78d19e68772e0f05b83090ec9f"}, +] + +[package.dependencies] +python-monkey-business = ">=1.0.0" + +[package.extras] +dev = ["Pillow", "black", "dj-database-url", "django-selenosis", "flake8", "pytest", "pytest-cov", "pytest-django", "pytest-xdist", "selenium"] +test = ["Pillow", "dj-database-url", "django-selenosis", "pytest", "pytest-cov", "pytest-django", "pytest-xdist", "selenium"] + [[package]] name = "djangorestframework" version = "3.16.0" @@ -561,6 +579,17 @@ files = [ [package.extras] cli = ["click (>=5.0)"] +[[package]] +name = "python-monkey-business" +version = "1.1.0" +description = "Utility functions for monkey-patching python code" +optional = false +python-versions = "*" +files = [ + {file = "python-monkey-business-1.1.0.tar.gz", hash = "sha256:8393839cc741415ed5ddc2bd58e2d4ce07f966a7d26b7aebff19dcec64818edc"}, + {file = "python_monkey_business-1.1.0-py2.py3-none-any.whl", hash = "sha256:15b4f603c749ba9a7b4f1acd36af023a6c5ba0f7e591c945f8253f0ef44bf389"}, +] + [[package]] name = "six" version = "1.17.0" @@ -700,4 +729,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "bbeb2de304857cb562d397e150394c9b1a85f970a4d576d5a933a4cf69fbb406" +content-hash = "e8f454b71fc79bae3f90e160e1e26e32a32b29edac512b5a96ee79de6181f470" diff --git a/server/pyproject.toml b/server/pyproject.toml index 7eca013..9664cd9 100644 --- a/server/pyproject.toml +++ b/server/pyproject.toml @@ -17,6 +17,7 @@ gunicorn = "^23.0.0" python-dotenv = "^1.0.1" django-extensions = "^3.2.3" Pillow = ">=10.0.0" +django-nested-admin = "^4.1.1" [tool.poetry.group.dev.dependencies]