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
main^2
Razmig Sarkissian 6 days ago
parent 240bb3fc25
commit f97dbd79cc
  1. 39
      tournaments/admin.py
  2. 75
      tournaments/templates/admin/tournaments/set_club_action.html

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

@ -3,7 +3,21 @@
{% block extrahead %}
{{ block.super }}
{{ form.media }}
{{ media }}
<script type="text/javascript" src="{% url 'admin:jsi18n' %}"></script>
{% endblock %}
{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/forms.css' %}">
{% endblock %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">Home</a>
&rsaquo; <a href="{% url 'admin:tournaments_event_changelist' %}">Events</a>
&rsaquo; Set Club
</div>
{% endblock %}
{% block content %}
@ -26,41 +40,42 @@
</ul>
</div>
<form method="post">
<form method="post" id="club-form" action="">
{% csrf_token %}
{# Hidden fields to preserve the selection #}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% if form.non_field_errors %}
<div class="errors">
{{ form.non_field_errors }}
</div>
{% endif %}
<div style="margin: 20px 0;">
<div class="form-row">
{{ form.club.errors }}
<div>
<label for="{{ form.club.id_for_label }}">{{ form.club.label }}:</label>
<div class="help">{{ form.club.help_text }}</div>
{{ form.club }}
<fieldset class="module aligned">
<div class="form-row field-club_id">
<div>
<label for="{{ form.club_id.id_for_label }}" class="required">{{ form.club_id.label }}:</label>
<div class="related-widget-wrapper">
{{ form.club_id }}
</div>
{% if form.club_id.help_text %}
<div class="help">{{ form.club_id.help_text }}</div>
{% endif %}
{% if form.club_id.errors %}
<div class="errors">{{ form.club_id.errors }}</div>
{% endif %}
</div>
</div>
</div>
</fieldset>
</div>
<div style="margin-top: 20px;">
<input type="submit" name="apply" value="Set Club" class="button default" style="
background-color: #417690;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
margin-right: 10px;
"/>
<a href="{% url 'admin:tournaments_event_changelist' %}" class="button" style="
display: inline-block;
padding: 10px 20px;
background-color: #6c757d;
color: white;
text-decoration: none;
border-radius: 4px;
font-size: 14px;
">Cancel</a>
<div class="submit-row" style="margin-top: 20px; padding: 10px; text-align: right;">
<input type="submit" name="apply" value="Set Club" class="default" style="margin-right: 10px;"/>
<a href="{% url 'admin:tournaments_event_changelist' %}" class="button cancel-link">Cancel</a>
</div>
</form>
{% endblock %}

Loading…
Cancel
Save