Improve Django-admin interface, inlines and filters

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello 2024-02-23 21:43:44 +01:00
parent cae1c6fdb8
commit de504398d2
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
6 changed files with 372 additions and 185 deletions

View File

@ -7,11 +7,34 @@ from django.utils.translation import gettext_lazy as _
from .models import Draw, Pool, Round, TeamDraw
class RoundInline(admin.TabularInline):
model = Round
extra = 0
autocomplete_fields = ('draw', 'current_pool',)
show_change_link = True
class PoolInline(admin.TabularInline):
model = Pool
extra = 0
autocomplete_fields = ('round', 'current_team', 'associated_pool',)
show_change_link = True
class TeamDrawInline(admin.TabularInline):
model = TeamDraw
extra = 0
autocomplete_fields = ('participation', 'round', 'pool',)
show_change_link = True
@admin.register(Draw)
class DrawAdmin(admin.ModelAdmin):
list_display = ('tournament', 'teams', 'current_round', 'get_state',)
list_filter = ('tournament', 'current_round',)
list_filter = ('tournament', 'current_round__number',)
search_fields = ('tournament__name', 'tournament__participation__team__trigram',)
autocomplete_fields = ('tournament',)
inlines = (RoundInline,)
@admin.display(description=_("teams"))
def teams(self, record: Draw):
@ -20,10 +43,16 @@ class DrawAdmin(admin.ModelAdmin):
@admin.register(Round)
class RoundAdmin(admin.ModelAdmin):
list_display = ('draw', 'number', 'teams',)
list_display = ('draw', 'tournament', 'number', 'teams',)
list_filter = ('draw__tournament', 'number',)
search_fields = ('draw__tournament__name', 'pool__teamdraw__participation__team__trigram')
ordering = ('draw__tournament__name', 'number')
autocomplete_fields = ('draw', 'current_pool',)
inlines = (PoolInline,)
@admin.display(description=_("tournament"), ordering='draw__tournament__name')
def tournament(self, record):
return record.draw.tournament
@admin.display(description=_("teams"))
def teams(self, record: Round):
@ -36,6 +65,8 @@ class PoolAdmin(admin.ModelAdmin):
list_filter = ('round__draw__tournament', 'round__number', 'letter')
ordering = ('round__draw__tournament__name', 'round', 'letter')
search_fields = ('round__draw__tournament__name', 'teamdraw__participation__team__trigram',)
autocomplete_fields = ('round', 'current_team', 'associated_pool',)
inlines = (TeamDrawInline,)
@admin.display(ordering='round__draw__tournament__name', description=_("tournament"))
def tournament(self, record):
@ -52,6 +83,7 @@ class TeamDrawAdmin(admin.ModelAdmin):
'passage_index', 'choose_index', 'passage_dice', 'choice_dice',)
list_filter = ('round__draw__tournament', 'round__number', 'pool__letter',)
search_fields = ('round__draw__tournament__name', 'participation__team__trigram',)
autocomplete_fields = ('participation', 'round', 'pool',)
@admin.display(ordering='round__draw__tournament__name', description=_("tournament"))
def tournament(self, record):

View File

@ -89,6 +89,7 @@ class Draw(models.Model):
return 'WAITING_DRAW_PROBLEM'
else:
return 'WAITING_CHOOSE_PROBLEM'
get_state.short_description = _('State')
@property
def information(self):

File diff suppressed because it is too large Load Diff

View File

@ -7,11 +7,74 @@ from django.utils.translation import gettext_lazy as _
from .models import Note, Participation, Passage, Pool, Solution, Synthesis, Team, Tournament, Tweak
class ParticipationInline(admin.StackedInline):
model = Participation
extra = 0
autocomplete_fields = ('team', 'tournament',)
show_change_link = True
class ParticipationTabularInline(admin.TabularInline):
model = Participation
extra = 0
fields = ('team', 'valid', 'final',)
readonly_fields = ('team',)
ordering = ('final', 'valid', 'team__trigram',)
autocomplete_fields = ('tournament',)
show_change_link = True
class SolutionInline(admin.TabularInline):
model = Solution
extra = 0
ordering = ('problem',)
autocomplete_fields = ('participation',)
show_change_link = True
class SynthesisInline(admin.TabularInline):
model = Synthesis
extra = 0
ordering = ('passage__solution_number', 'type',)
autocomplete_fields = ('passage',)
show_change_link = True
class PoolInline(admin.TabularInline):
model = Pool
extra = 0
autocomplete_fields = ('tournament', 'participations', 'juries',)
show_change_link = True
class PassageInline(admin.TabularInline):
model = Passage
extra = 0
ordering = ('position',)
autocomplete_fields = ('defender', 'opponent', 'reporter', 'observer',)
show_change_link = True
class NoteInline(admin.TabularInline):
model = Note
extra = 0
autocomplete_fields = ('jury',)
show_change_link = True
class TweakInline(admin.TabularInline):
model = Tweak
extra = 0
autocomplete_fields = ('participation', 'pool',)
show_change_link = True
@admin.register(Team)
class TeamAdmin(admin.ModelAdmin):
list_display = ('name', 'trigram', 'tournament', 'valid', 'final',)
search_fields = ('name', 'trigram',)
list_filter = ('participation__valid', 'participation__tournament', 'participation__final',)
inlines = (ParticipationInline,)
@admin.display(description=_("tournament"))
def tournament(self, record):
@ -32,6 +95,7 @@ class ParticipationAdmin(admin.ModelAdmin):
search_fields = ('team__name', 'team__trigram',)
list_filter = ('valid',)
autocomplete_fields = ('team', 'tournament',)
inlines = (SolutionInline, SynthesisInline,)
@admin.register(Pool)
@ -40,6 +104,7 @@ class PoolAdmin(admin.ModelAdmin):
list_filter = ('tournament', 'round', 'letter',)
search_fields = ('participations__team__name', 'participations__team__trigram',)
autocomplete_fields = ('tournament', 'participations', 'juries',)
inlines = (PassageInline, TweakInline,)
@admin.display(description=_("teams"))
def teams(self, record: Pool):
@ -49,28 +114,30 @@ class PoolAdmin(admin.ModelAdmin):
@admin.register(Passage)
class PassageAdmin(admin.ModelAdmin):
list_display = ('__str__', 'defender_trigram', 'solution_number', 'opponent_trigram', 'reporter_trigram',
'pool_abbr', 'tournament')
'pool_abbr', 'position', 'tournament')
list_filter = ('pool__tournament', 'pool__round', 'pool__letter', 'solution_number',)
search_fields = ('pool__participations__team__name', 'pool__participations__team__trigram',)
ordering = ('pool__tournament', 'pool__round', 'pool__letter', 'position',)
autocomplete_fields = ('pool', 'defender', 'opponent', 'reporter', 'observer',)
inlines = (NoteInline,)
@admin.display(description=_("defender"))
@admin.display(description=_("defender"), ordering='defender__team__trigram')
def defender_trigram(self, record: Passage):
return record.defender.team.trigram
@admin.display(description=_("opponent"))
@admin.display(description=_("opponent"), ordering='opponent__team__trigram')
def opponent_trigram(self, record: Passage):
return record.opponent.team.trigram
@admin.display(description=_("reporter"))
@admin.display(description=_("reporter"), ordering='reporter__team__trigram')
def reporter_trigram(self, record: Passage):
return record.reporter.team.trigram
@admin.display(description=_("pool"))
@admin.display(description=_("pool"), ordering='pool__letter')
def pool_abbr(self, record):
return f"{record.pool.get_letter_display()}{record.pool.round}"
@admin.display(description=_("tournament"))
@admin.display(description=_("tournament"), ordering='pool__tournament__name')
def tournament(self, record: Passage):
return record.pool.tournament
@ -124,9 +191,11 @@ class SynthesisAdmin(admin.ModelAdmin):
@admin.register(Tournament)
class TournamentAdmin(admin.ModelAdmin):
list_display = ('name',)
list_display = ('name', 'date_start', 'date_end',)
search_fields = ('name',)
ordering = ('date_start', 'name',)
autocomplete_fields = ('organizers',)
inlines = (ParticipationTabularInline, PoolInline,)
@admin.register(Tweak)

View File

@ -3,13 +3,45 @@
from django.contrib import admin
from django.contrib.admin import ModelAdmin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
from polymorphic.admin import PolymorphicChildModelAdmin, PolymorphicChildModelFilter, PolymorphicParentModelAdmin
from polymorphic.admin import PolymorphicChildModelAdmin, PolymorphicChildModelFilter, PolymorphicParentModelAdmin, \
PolymorphicInlineSupportMixin, StackedPolymorphicInline
from .models import CoachRegistration, ParticipantRegistration, Payment, Registration, \
StudentRegistration, VolunteerRegistration
class RegistrationInline(StackedPolymorphicInline):
class StudentRegistrationInline(StackedPolymorphicInline.Child):
model = StudentRegistration
autocomplete_fields = ('team',)
show_change_link = True
class CoachRegistrationInline(StackedPolymorphicInline.Child):
model = CoachRegistration
autocomplete_fields = ('team',)
show_change_link = True
class VolunteerRegistrationInline(StackedPolymorphicInline.Child):
model = VolunteerRegistration
show_change_link = True
model = Registration
child_inlines = (
StudentRegistrationInline,
CoachRegistrationInline,
VolunteerRegistrationInline,
)
class PaymentInline(admin.TabularInline):
model = Payment
extra = 0
autocomplete_fields = ('registrations',)
@admin.register(Registration)
class RegistrationAdmin(PolymorphicParentModelAdmin):
child_models = (StudentRegistration, CoachRegistration, VolunteerRegistration,)
@ -97,12 +129,34 @@ class VolunteerRegistrationAdmin(PolymorphicChildModelAdmin):
@admin.register(Payment)
class PaymentAdmin(ModelAdmin):
list_display = ('id', 'concerned_people', 'grouped', 'type', 'valid', )
search_fields = ('registrations__user__last_name', 'registrations__user__first_name', 'registrations__user__email',)
list_display = ('concerned_people', 'tournament', 'team', 'grouped', 'type', 'amount', 'valid', )
search_fields = ('registrations__user__last_name', 'registrations__user__first_name', 'registrations__user__email',
'registrations__team__name', 'registrations__team__participation__team__trigram',)
list_filter = ('registrations__team__participation__valid', 'type',
'grouped', 'valid', 'registrations__polymorphic_ctype',)
'grouped', 'valid', 'registrations__team__participation__tournament', 'final',)
autocomplete_fields = ('registrations',)
actions = ('mark_as_valid', 'mark_as_pending', 'mark_as_invalid',)
@admin.display(description=_('concerned people'))
def concerned_people(self, record: Payment):
return ", ".join(f"{reg.user.first_name} {reg.user.last_name}" for reg in record.registrations.all())
@admin.action(description=_('Mark as valid'))
def mark_as_valid(self, request, queryset):
queryset.update(valid=True)
@admin.action(description=_('Mark as pending'))
def mark_as_pending(self, request, queryset):
queryset.update(valid=None)
@admin.action(description=_('Mark as invalid'))
def mark_as_invalid(self, request, queryset):
queryset.update(valid=False)
admin.site.unregister(User)
@admin.register(User)
class UserCustomAdmin(PolymorphicInlineSupportMixin, UserAdmin):
inlines = [RegistrationInline]

View File

@ -589,6 +589,8 @@ class Payment(models.Model):
@property
def team(self):
return self.registrations.first().team
team.fget.short_description = _("team")
team.fget.admin_order_field = 'registrations__team__trigram'
@property
def tournament(self):
@ -596,6 +598,8 @@ class Payment(models.Model):
from participation.models import Tournament
return Tournament.final_tournament()
return self.registrations.first().team.participation.tournament
tournament.fget.short_description = _("tournament")
tournament.fget.admin_order_field = 'registrations__team__participation__tournament'
def get_checkout_intent(self, none_if_link_disabled=False):
if self.checkout_intent_id is None: