From f97dbd79cc79e360906ad3e377a0675ddbd25f50 Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Mon, 3 Nov 2025 17:21:27 +0100 Subject: [PATCH] Enhance Club Selection Form in EventAdmin Action Refactors the set_club_action method to improve form handling: - Add validation for club selection - Improve form initialization with hidden fields - Update template to match Django admin form conventions - Add error handling and more robust user feedback --- tournaments/admin.py | 39 ++++++++-- .../admin/tournaments/set_club_action.html | 75 +++++++++++-------- 2 files changed, 78 insertions(+), 36 deletions(-) diff --git a/tournaments/admin.py b/tournaments/admin.py index 58849ac..c1fcd18 100644 --- a/tournaments/admin.py +++ b/tournaments/admin.py @@ -151,12 +151,15 @@ class EventAdmin(SyncedObjectAdmin): """Action to set club for selected events""" from django import forms from django.contrib.admin.widgets import ForeignKeyRawIdWidget + from django.contrib.admin import helpers + from django.core.exceptions import ValidationError class ClubSelectionForm(forms.Form): - club = forms.ModelChoiceField( - queryset=Club.objects.all(), - required=True, + _selected_action = forms.CharField(widget=forms.MultipleHiddenInput) + action = forms.CharField(widget=forms.HiddenInput) + club_id = forms.CharField( label='Club', + required=True, help_text='Enter Club ID or use the search icon to find a club', widget=ForeignKeyRawIdWidget( Event._meta.get_field('club').remote_field, @@ -164,10 +167,20 @@ class EventAdmin(SyncedObjectAdmin): ) ) + def clean_club_id(self): + club_id = self.cleaned_data['club_id'] + try: + club = Club.objects.get(pk=club_id) + return club + except Club.DoesNotExist: + raise ValidationError(f'Club with ID {club_id} does not exist.') + except (ValueError, TypeError) as e: + raise ValidationError(f'Invalid Club ID format: {club_id}') + if 'apply' in request.POST: form = ClubSelectionForm(request.POST) if form.is_valid(): - club = form.cleaned_data['club'] + club = form.cleaned_data['club_id'] # This is now a Club instance updated_count = queryset.update(club=club) self.message_user( request, @@ -175,14 +188,28 @@ class EventAdmin(SyncedObjectAdmin): messages.SUCCESS ) return None - else: - form = ClubSelectionForm() + else: + # Show form errors + self.message_user( + request, + f'Form validation failed. Errors: {form.errors}', + messages.ERROR + ) + + # Initial form display + form = ClubSelectionForm(initial={ + '_selected_action': request.POST.getlist(helpers.ACTION_CHECKBOX_NAME), + 'action': 'set_club_action', + }) context = { 'form': form, 'events': queryset, + 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME, 'action_name': 'set_club_action', 'title': 'Set Club for Events', + 'media': form.media, + 'has_change_permission': True, } return render(request, 'admin/tournaments/set_club_action.html', context) diff --git a/tournaments/templates/admin/tournaments/set_club_action.html b/tournaments/templates/admin/tournaments/set_club_action.html index a0e68dd..7a9d4f4 100644 --- a/tournaments/templates/admin/tournaments/set_club_action.html +++ b/tournaments/templates/admin/tournaments/set_club_action.html @@ -3,7 +3,21 @@ {% block extrahead %} {{ block.super }} - {{ form.media }} + {{ media }} + +{% endblock %} + +{% block extrastyle %} + {{ block.super }} + +{% endblock %} + +{% block breadcrumbs %} + {% endblock %} {% block content %} @@ -26,41 +40,42 @@ -
+ {% csrf_token %} + {# Hidden fields to preserve the selection #} + {% for hidden in form.hidden_fields %} + {{ hidden }} + {% endfor %} + + {% if form.non_field_errors %} +
+ {{ form.non_field_errors }} +
+ {% endif %} +
-
- {{ form.club.errors }} -
- -
{{ form.club.help_text }}
- {{ form.club }} +
+
+
+ + + {% if form.club_id.help_text %} +
{{ form.club_id.help_text }}
+ {% endif %} + {% if form.club_id.errors %} +
{{ form.club_id.errors }}
+ {% endif %} +
-
+
-
- - - Cancel +
+ + Cancel
{% endblock %}