fix: shortcut SubscriptionNewUserForm.clean if there are errors

This commit is contained in:
imperosol
2026-05-08 13:50:36 +02:00
parent 7bd2f1da96
commit 94cda48508
3 changed files with 14 additions and 11 deletions
+6
View File
@@ -120,6 +120,12 @@ class SubscriptionNewUserForm(SubscriptionForm):
email=self.cleaned_data.get("email"), email=self.cleaned_data.get("email"),
date_of_birth=self.cleaned_data.get("date_of_birth"), date_of_birth=self.cleaned_data.get("date_of_birth"),
) )
if self.errors:
# don't bother generating username, password and other data.
# The form validation failed anyway, so using a dummy User
# (just for Subscription.clean not to crash) is enough
self.instance.member = member
return super().clean()
if self.cleaned_data.get("subscription_type") in [ if self.cleaned_data.get("subscription_type") in [
"un-semestre", "un-semestre",
"deux-semestres", "deux-semestres",
@@ -1,5 +1,5 @@
<form <form
hx-post="{{ url("subscription:fragment-existing-user") }}" hx-post="{{ url("subscription:fragment-new-user") }}"
hx-target="this" hx-target="this"
hx-disabled-elt="find input[type='submit']" hx-disabled-elt="find input[type='submit']"
hx-swap="outerHTML" hx-swap="outerHTML"
+7 -10
View File
@@ -12,7 +12,6 @@ from django.urls import reverse
from django.utils.timezone import localdate from django.utils.timezone import localdate
from model_bakery import baker from model_bakery import baker
from pytest_django.asserts import assertRedirects from pytest_django.asserts import assertRedirects
from pytest_django.fixtures import SettingsWrapper
from core.baker_recipes import board_user, old_subscriber_user, subscriber_user from core.baker_recipes import board_user, old_subscriber_user, subscriber_user
from core.models import Group, User from core.models import Group, User
@@ -26,9 +25,7 @@ from subscription.models import Subscription
"user_factory", "user_factory",
[old_subscriber_user.make, lambda: baker.make(User)], [old_subscriber_user.make, lambda: baker.make(User)],
) )
def test_form_existing_user_valid( def test_form_existing_user_valid(user_factory: Callable[[], User]):
user_factory: Callable[[], User], settings: SettingsWrapper
):
"""Test `SubscriptionExistingUserForm`""" """Test `SubscriptionExistingUserForm`"""
user = user_factory() user = user_factory()
user.date_of_birth = date(year=1967, month=3, day=14) user.date_of_birth = date(year=1967, month=3, day=14)
@@ -48,7 +45,7 @@ def test_form_existing_user_valid(
@pytest.mark.django_db @pytest.mark.django_db
def test_form_existing_user_with_birthdate(settings: SettingsWrapper): def test_form_existing_user_with_birthdate():
"""Test `SubscriptionExistingUserForm`""" """Test `SubscriptionExistingUserForm`"""
user = baker.make(User, date_of_birth=None) user = baker.make(User, date_of_birth=None)
data = { data = {
@@ -70,7 +67,7 @@ def test_form_existing_user_with_birthdate(settings: SettingsWrapper):
@pytest.mark.django_db @pytest.mark.django_db
def test_form_existing_user_invalid(settings: SettingsWrapper): def test_form_existing_user_invalid():
"""Test `SubscriptionExistingUserForm`, with users that shouldn't subscribe.""" """Test `SubscriptionExistingUserForm`, with users that shouldn't subscribe."""
user = subscriber_user.make() user = subscriber_user.make()
# make sure the current subscription will end in a long time # make sure the current subscription will end in a long time
@@ -91,7 +88,7 @@ def test_form_existing_user_invalid(settings: SettingsWrapper):
@pytest.mark.django_db @pytest.mark.django_db
def test_form_new_user(settings: SettingsWrapper): def test_form_new_user():
data = { data = {
"first_name": "John", "first_name": "John",
"last_name": "Doe", "last_name": "Doe",
@@ -121,7 +118,7 @@ def test_form_new_user(settings: SettingsWrapper):
"subscription_type", "subscription_type",
["un-semestre", "deux-semestres", "cursus-tronc-commun", "cursus-branche"], ["un-semestre", "deux-semestres", "cursus-tronc-commun", "cursus-branche"],
) )
def test_form_set_new_user_as_student(settings: SettingsWrapper, subscription_type): def test_form_set_new_user_as_student(subscription_type):
"""Test that new users have the student role by default.""" """Test that new users have the student role by default."""
data = { data = {
"first_name": "John", "first_name": "John",
@@ -165,7 +162,7 @@ def test_page_access_with_get_data(client: Client):
@pytest.mark.django_db @pytest.mark.django_db
def test_submit_form_existing_user(client: Client, settings: SettingsWrapper): def test_submit_form_existing_user(client: Client):
client.force_login( client.force_login(
baker.make( baker.make(
User, User,
@@ -196,7 +193,7 @@ def test_submit_form_existing_user(client: Client, settings: SettingsWrapper):
@pytest.mark.django_db @pytest.mark.django_db
def test_submit_form_new_user(client: Client, settings: SettingsWrapper): def test_submit_form_new_user(client: Client):
client.force_login( client.force_login(
baker.make( baker.make(
User, User,