2021-12-07 05:55:45 +00:00
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 ) :
if value > = timezone . now ( ) . date ( ) :
raise ValidationError ( ' Enter a date in the past ' )
class AssignmentForm ( forms . ModelForm ) :
period_begins = forms . DateField (
2021-12-10 00:33:37 +00:00
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. ' ,
2021-12-07 05:55:45 +00:00
required = True ,
widget = forms . DateInput ( attrs = { ' type ' : ' date ' } ) ,
validators = [ validate_in_past ] ,
)
period_end_type = forms . ChoiceField (
2021-12-10 00:33:37 +00:00
label = ' and ending on ' ,
2021-12-07 05:55:45 +00:00
choices = [
2021-12-10 00:33:37 +00:00
( ' all future contributions ' , ' all future contributions (no end date) ' ) ,
2021-12-07 05:55:45 +00:00
( ' a specific past date ' , ' a specific past date (specify below) ' ) ,
] ,
widget = forms . RadioSelect ( ) ,
2021-12-10 00:33:37 +00:00
initial = ' all future contributions ' ,
2021-12-07 05:55:45 +00:00
)
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 ,
help_text = ' Please be aware that some employment agreements explicitly transfer copyright ownership to the employer. We recommend you review your recent employment agreements for such clauses. ' ,
)
2021-12-10 00:33:37 +00:00
def __init__ ( self , * args , * * kwargs ) :
super ( ) . __init__ ( * args , * * kwargs )
self . fields [ ' attestation_of_copyright ' ] . required = True
2021-12-07 05:55:45 +00:00
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 ) :
2021-12-07 06:49:57 +00:00
if ' period_begins ' in self . cleaned_data and ' period_ends ' in self . cleaned_data and self . cleaned_data [ ' period_begins ' ] and self . cleaned_data [ ' period_ends ' ] and self . cleaned_data [ ' period_begins ' ] > self . cleaned_data [ ' period_ends ' ] :
2021-12-07 05:55:45 +00:00
raise ValidationError ( ' End of period is before start ' )
2021-12-10 07:37:13 +00:00
if self . cleaned_data [ ' period_end_type ' ] == ' a specific past date ' and not self . cleaned_data [ ' period_ends ' ] :
raise ValidationError ( ' This field is required ' )
2021-12-10 23:25:57 +00:00
return self . cleaned_data [ ' period_ends ' ]