properly clean selfbuy data

This commit is contained in:
Oliver Zander 2025-11-10 17:54:03 +01:00 committed by Tobias Herre
parent 20e5be26c0
commit 73cd0ff906
3 changed files with 27 additions and 12 deletions

View File

@ -5,6 +5,7 @@ from django.forms import ModelForm
from django.forms.renderers import DjangoTemplates
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as trans
from .models import (
Project,
@ -285,6 +286,27 @@ class LiteratureForm(TermsForm):
class Media:
js = ('dropdown/js/literature.js',)
def clean(self):
cleaned_data = TermsForm.clean(self)
if self.errors:
return cleaned_data
return self.clean_selfbuy_fields(cleaned_data)
def clean_selfbuy_fields(self, cleaned_data):
if cleaned_data['selfbuy'] == 'TRUE':
cleaned_data['selfbuy_data'] = ''
cleaned_data['selfbuy_give_data'] = False
else:
if not cleaned_data.get('selfbuy_data'):
self.add_error('selfbuy_data', trans('This field is required.'))
if not cleaned_data.get('selfbuy_give_data'):
self.add_error('selfbuy_give_data', trans('This field is required.'))
return cleaned_data
ADULT_CHOICES = {
'TRUE': mark_safe('Ich bin volljährig.'),

View File

@ -99,8 +99,8 @@ class AnonymousViewTestCase(TestCase):
'info': 'Test',
'source': 'Test',
'notes': '',
'selfbuy': 'TRUE',
'selfbuy_data': 'NONE',
'selfbuy': 'FALSE',
'selfbuy_data': 'Test',
'selfbuy_give_data': True,
'check': True,
'terms_accepted': True,
@ -113,8 +113,8 @@ class AnonymousViewTestCase(TestCase):
'source': 'Test',
'notes': '',
'selfbuy': 'TRUE',
'selfbuy_data': 'NONE',
'selfbuy_give_data': True,
'selfbuy_data': '',
'selfbuy_give_data': False,
'check': False,
})

View File

@ -252,14 +252,7 @@ class ApplicationView(FormView):
def prepare_data(self, form):
# Collect cleaned data and mark the current type
data = {**form.cleaned_data, 'choice': self.type_code}
# Special rule for literature applications
if self.type_code == TYPE_LIT and data.get('selfbuy') == 'TRUE':
data['selfbuy_give_data'] = 'False'
return data
return {**form.cleaned_data, 'choice': self.type_code}
def save_obj(self, form, data):
# Save model instance