50 lines
2.0 KiB
Python
50 lines
2.0 KiB
Python
from django import forms
|
|
|
|
from .models import Event
|
|
|
|
|
|
class EventForm(forms.ModelForm):
|
|
start = forms.DateTimeField(
|
|
input_formats=['%Y-%m-%dT%H:%M'],
|
|
widget=forms.DateTimeInput(
|
|
attrs={'type': 'datetime-local', 'class': 'form-control'},
|
|
format='%Y-%m-%dT%H:%M',
|
|
),
|
|
)
|
|
end = forms.DateTimeField(
|
|
input_formats=['%Y-%m-%dT%H:%M'],
|
|
widget=forms.DateTimeInput(
|
|
attrs={'type': 'datetime-local', 'class': 'form-control'},
|
|
format='%Y-%m-%dT%H:%M',
|
|
),
|
|
)
|
|
|
|
class Meta:
|
|
model = Event
|
|
fields = ['name', 'location', 'start', 'end', 'event_url', 'summary', 'is_published']
|
|
widgets = {
|
|
'name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Downtown Spring Market'}),
|
|
'location': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Riverfront Pavilion'}),
|
|
'event_url': forms.URLInput(attrs={'class': 'form-control', 'placeholder': 'https://event-page.example.com'}),
|
|
'summary': forms.Textarea(attrs={'class': 'form-control', 'rows': 4, 'placeholder': 'Anything visitors should know before attending.'}),
|
|
'is_published': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
|
|
}
|
|
help_texts = {
|
|
'is_published': 'Only published events appear in the public calendar and embed widget.',
|
|
}
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
for field_name in ['start', 'end']:
|
|
value = self.initial.get(field_name) or getattr(self.instance, field_name, None)
|
|
if value:
|
|
self.initial[field_name] = value.strftime('%Y-%m-%dT%H:%M')
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
start = cleaned_data.get('start')
|
|
end = cleaned_data.get('end')
|
|
if start and end and end <= start:
|
|
self.add_error('end', 'End time must be after the start time.')
|
|
return cleaned_data
|