Upgrade help
[django-cas-provider.git] / cas_provider / forms.py
1 from django import forms
2 from django.conf import settings
3 from django.contrib.auth import authenticate
4 from django.contrib.auth.forms import AuthenticationForm
5 from django.forms import ValidationError
6 from django.utils.translation import ugettext_lazy as _
7 from models import LoginTicket
8 import datetime
9
10
11 __all__ = ['LoginForm', ]
12
13
14 class LoginForm(AuthenticationForm):
15     lt = forms.CharField(widget=forms.HiddenInput)
16     service = forms.CharField(widget=forms.HiddenInput, required=False)
17
18     def clean_lt(self):
19         ticket = self.cleaned_data['lt']
20         timeframe = datetime.datetime.now() - \
21                     datetime.timedelta(minutes=settings.CAS_TICKET_EXPIRATION)
22         try:
23             return LoginTicket.objects.get(ticket=ticket, created__gte=timeframe)
24         except LoginTicket.DoesNotExist:
25             raise ValidationError(_('Login ticket expired. Please try again.'))
26         return ticket
27
28     def clean(self):
29         AuthenticationForm.clean(self)
30         self.cleaned_data.get('lt').delete()
31         return self.cleaned_data
32
33     def get_errors(self):
34         errors = []
35         for k, error in self.errors.items():
36             errors += [e for e in error]
37         return errors