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 @@ -