forked from DGNum/gestioCOF
Better uniqueness checks
This commit is contained in:
parent
2bc97a115c
commit
1ffda1a5c4
3 changed files with 21 additions and 25 deletions
19
gestioncof/migrations/0017_petitscours_uniqueness.py
Normal file
19
gestioncof/migrations/0017_petitscours_uniqueness.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
# Generated by Django 2.2.12 on 2020-09-14 09:39
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
("gestioncof", "0016_unique_clippers"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterUniqueTogether(
|
||||
name="petitcoursability",
|
||||
unique_together={("user", "niveau", "matiere")},
|
||||
),
|
||||
]
|
|
@ -2,34 +2,11 @@ from captcha.fields import ReCaptchaField
|
|||
from django import forms
|
||||
from django.contrib.auth.models import User
|
||||
from django.forms import ModelForm
|
||||
from django.forms.models import BaseInlineFormSet, inlineformset_factory
|
||||
from django.forms.models import inlineformset_factory
|
||||
|
||||
from petitscours.models import PetitCoursAbility, PetitCoursDemande
|
||||
|
||||
|
||||
class BaseMatieresFormSet(BaseInlineFormSet):
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if any(self.errors):
|
||||
# Don't bother validating the formset unless each form is
|
||||
# valid on its own
|
||||
return
|
||||
matieres = []
|
||||
for i in range(0, self.total_form_count()):
|
||||
form = self.forms[i]
|
||||
if not form.cleaned_data:
|
||||
continue
|
||||
matiere = form.cleaned_data["matiere"]
|
||||
niveau = form.cleaned_data["niveau"]
|
||||
delete = form.cleaned_data["DELETE"]
|
||||
if not delete and (matiere, niveau) in matieres:
|
||||
raise forms.ValidationError(
|
||||
"Vous ne pouvez pas vous inscrire deux fois pour la "
|
||||
"même matiere avec le même niveau."
|
||||
)
|
||||
matieres.append((matiere, niveau))
|
||||
|
||||
|
||||
class DemandeForm(ModelForm):
|
||||
captcha = ReCaptchaField(attrs={"theme": "clean", "lang": "fr"})
|
||||
|
||||
|
@ -58,5 +35,4 @@ MatieresFormSet = inlineformset_factory(
|
|||
User,
|
||||
PetitCoursAbility,
|
||||
fields=("matiere", "niveau", "agrege"),
|
||||
formset=BaseMatieresFormSet,
|
||||
)
|
||||
|
|
|
@ -46,6 +46,7 @@ class PetitCoursAbility(models.Model):
|
|||
app_label = "gestioncof"
|
||||
verbose_name = "Compétence petits cours"
|
||||
verbose_name_plural = "Compétences des petits cours"
|
||||
unique_together = ("user", "niveau", "matiere")
|
||||
|
||||
def __str__(self):
|
||||
return "{:s} - {!s} - {:s}".format(
|
||||
|
|
Loading…
Reference in a new issue