Display a form error rather than a page error if a guest is already invited

This commit is contained in:
Yohann D'ANELLO 2020-08-15 23:03:49 +02:00
parent 1186b0f9a9
commit b16871d925
3 changed files with 4 additions and 3 deletions

View File

@ -3,6 +3,7 @@
from django.contrib import admin from django.contrib import admin
from note_kfet.admin import admin_site from note_kfet.admin import admin_site
from .forms import GuestForm
from .models import Activity, ActivityType, Entry, Guest from .models import Activity, ActivityType, Entry, Guest
@ -35,6 +36,7 @@ class GuestAdmin(admin.ModelAdmin):
Admin customisation for Guest Admin customisation for Guest
""" """
list_display = ('last_name', 'first_name', 'activity', 'inviter') list_display = ('last_name', 'first_name', 'activity', 'inviter')
form = GuestForm
@admin.register(Entry, site=admin_site) @admin.register(Entry, site=admin_site)

View File

@ -55,9 +55,8 @@ class GuestForm(forms.ModelForm):
first_name__iexact=cleaned_data["first_name"], first_name__iexact=cleaned_data["first_name"],
last_name__iexact=cleaned_data["last_name"], last_name__iexact=cleaned_data["last_name"],
activity__date_start__gte=self.activity.date_start - one_year, activity__date_start__gte=self.activity.date_start - one_year,
entry__isnull=False,
) )
if qs.count() >= 5: if qs.filter(entry__isnull=False).count() >= 5:
self.add_error("last_name", _("This person has been already invited 5 times this year.")) self.add_error("last_name", _("This person has been already invited 5 times this year."))
qs = qs.filter(activity=self.activity) qs = qs.filter(activity=self.activity)

View File

@ -257,7 +257,7 @@ class Guest(models.Model):
last_name__iexact=self.last_name, last_name__iexact=self.last_name,
activity__date_start__gte=self.activity.date_start - one_year, activity__date_start__gte=self.activity.date_start - one_year,
) )
if qs.count() >= 5: if qs.filter(entry__isnull=False).count() >= 5:
raise ValidationError(_("This person has been already invited 5 times this year.")) raise ValidationError(_("This person has been already invited 5 times this year."))
qs = qs.filter(activity=self.activity) qs = qs.filter(activity=self.activity)