reg/forms.py
changeset 8 e2699e042129
parent 7 af9ab5ad2786
child 9 e29ecb7819e7
equal deleted inserted replaced
7:af9ab5ad2786 8:e2699e042129
     1 import string
     1 import string
       
     2 from datetime import datetime
     2 
     3 
     3 from django import forms
     4 from django import forms
     4 from django.contrib.auth.models import User
     5 from django.contrib.auth.models import User
     5 from django.contrib.auth import authenticate
     6 from django.contrib.auth import authenticate
     6 
     7 
     7 from workshop.reg.models import Profile
     8 from workshop.reg.models import Profile, Event
     8 
     9 
     9 class LoginForm(forms.Form):
    10 class LoginForm(forms.Form):
    10     """ a form to handle login.
    11     """ a form to handle login.
    11     """
    12     """
    12 
    13 
    71         if not 8 <= len(password) <= 30:
    72         if not 8 <= len(password) <= 30:
    72             raise forms.ValidationError("Password must be atleast 8 characters and at most 30 characters")
    73             raise forms.ValidationError("Password must be atleast 8 characters and at most 30 characters")
    73 
    74 
    74         if not password == self.data['confirm_password']:
    75         if not password == self.data['confirm_password']:
    75             raise forms.ValidationError("Passwords do not match")
    76             raise forms.ValidationError("Passwords do not match")
       
    77 
       
    78 class EventCreateForm(forms.ModelForm):
       
    79     """ A form to create an event.
       
    80     """
       
    81 
       
    82     class Meta:
       
    83         model = Event
       
    84         fields = ['title', 'description', 'start_date', 'stop_date']
       
    85 
       
    86     def clean_start_date(self):
       
    87         """ see if the start date is greater than today or not.
       
    88         """
       
    89 
       
    90         start_date = self.cleaned_data['start_date']
       
    91         if start_date < datetime.now().date():
       
    92             raise forms.ValidationError("The event must start at the latest today.")
       
    93         return start_date
       
    94 
       
    95     def clean_stop_date(self):
       
    96         """ see that stop_date is not less than start_date.
       
    97         """
       
    98 
       
    99         stop_date = self.cleaned_data['stop_date']
       
   100         try:
       
   101             start_date = datetime.strptime(self.data['start_date'], "%Y-%m-%d").date()
       
   102         except ValueError:
       
   103             raise forms.ValidationError("Enter a valid date")
       
   104 
       
   105         if start_date > stop_date:
       
   106             raise forms.ValidationError('The event stops even before it starts. I am starting to wonder what kind of event is this.')
       
   107         return stop_date