diff --git a/input/forms.py b/input/forms.py index d7d2d18..4059d72 100755 --- a/input/forms.py +++ b/input/forms.py @@ -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.'), diff --git a/input/tests/views.py b/input/tests/views.py index aee69c9..dc19d14 100644 --- a/input/tests/views.py +++ b/input/tests/views.py @@ -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, }) diff --git a/input/views.py b/input/views.py index 561a2b7..1472fd0 100755 --- a/input/views.py +++ b/input/views.py @@ -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