--- a/reg/forms.py Fri Apr 09 15:48:47 2010 +0530
+++ b/reg/forms.py Fri Apr 09 16:51:56 2010 +0530
@@ -1,10 +1,11 @@
import string
+from datetime import datetime
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
-from workshop.reg.models import Profile
+from workshop.reg.models import Profile, Event
class LoginForm(forms.Form):
""" a form to handle login.
@@ -73,3 +74,34 @@
if not password == self.data['confirm_password']:
raise forms.ValidationError("Passwords do not match")
+
+class EventCreateForm(forms.ModelForm):
+ """ A form to create an event.
+ """
+
+ class Meta:
+ model = Event
+ fields = ['title', 'description', 'start_date', 'stop_date']
+
+ def clean_start_date(self):
+ """ see if the start date is greater than today or not.
+ """
+
+ start_date = self.cleaned_data['start_date']
+ if start_date < datetime.now().date():
+ raise forms.ValidationError("The event must start at the latest today.")
+ return start_date
+
+ def clean_stop_date(self):
+ """ see that stop_date is not less than start_date.
+ """
+
+ stop_date = self.cleaned_data['stop_date']
+ try:
+ start_date = datetime.strptime(self.data['start_date'], "%Y-%m-%d").date()
+ except ValueError:
+ raise forms.ValidationError("Enter a valid date")
+
+ if start_date > stop_date:
+ raise forms.ValidationError('The event stops even before it starts. I am starting to wonder what kind of event is this.')
+ return stop_date