website/www/conservancy/apps/assignment/forms.py
Ben Sturmfels 05c7ed6647
assignment: Prevent unhandled error when fields not provided
This typically happens when a bot is submitting the form. This should be a
validation error rather than an unhandled exception.
2023-10-19 11:52:04 +11:00

73 lines
2.5 KiB
Python

import datetime
from django import forms
from django.core.validators import ValidationError
from django.utils import timezone
from .models import Assignment
from .terms import TERMS
def validate_in_past(value):
# Adding a day to allow the current date anywhere on earth, regardless of
# the server timezone.
if value > timezone.now().date() + datetime.timedelta(days=1):
raise ValidationError('Enter a date in the past')
class AssignmentForm(forms.ModelForm):
period_begins = forms.DateField(
label='Assign the copyright in my above contributions starting on',
help_text='You can use the day you first started contributing (or, equivalently, your date of birth), or any later date.',
required=True,
widget=forms.DateInput(attrs={'type': 'date'}),
validators=[validate_in_past],
)
period_end_type = forms.ChoiceField(
label='and ending on',
choices=[
('all future contributions', 'all future contributions (no end date)'),
('a specific past date', 'a specific past date (specify below)'),
],
widget=forms.RadioSelect(),
initial='all future contributions',
)
period_ends = forms.DateField(
label='Specific past date (if applicable)',
required=False,
widget=forms.DateInput(attrs={'type': 'date'}),
validators=[validate_in_past],
)
agreement_terms = forms.CharField(
widget=forms.Textarea(attrs={'readonly': 'readonly'}),
initial=TERMS,
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['attestation_of_copyright'].required = True
class Meta:
model = Assignment
fields = [
'full_name',
'email',
'country_of_residence',
'repositories',
'all_emails',
'period_begins',
'period_end_type',
'period_ends',
'agreement_terms',
'attestation_of_copyright',
]
def clean_period_ends(self):
period_begins = self.cleaned_data.get('period_begins')
period_ends = self.cleaned_data.get('period_ends')
period_end_type = self.cleaned_data.get('period_end_type')
if period_begins and period_ends and period_begins > period_ends:
raise ValidationError('End of period is before start')
if period_end_type == 'a specific past date' and not period_ends:
raise ValidationError('This field is required')
return period_ends