forms.py 1.59 KB
Newer Older
Valentin Samir's avatar
Valentin Samir committed
1 2
"""forms for the app"""
import cas_server.default_settings
Valentin Samir's avatar
Valentin Samir committed
3

Valentin Samir's avatar
Valentin Samir committed
4 5
from django import forms
from django.conf import settings
Valentin Samir's avatar
Valentin Samir committed
6
from django.utils.translation import ugettext_lazy as _
Valentin Samir's avatar
Valentin Samir committed
7

Valentin Samir's avatar
Valentin Samir committed
8
from . import models
Valentin Samir's avatar
Valentin Samir committed
9 10

class UserCredential(forms.Form):
Valentin Samir's avatar
Valentin Samir committed
11
    """Form used on the login page to retrive user credentials"""
Valentin Samir's avatar
Valentin Samir committed
12
    username = forms.CharField(label=_('login'))
Valentin Samir's avatar
Valentin Samir committed
13
    service = forms.CharField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
14
    password = forms.CharField(label=_('password'), widget=forms.PasswordInput)
Valentin Samir's avatar
Valentin Samir committed
15
    method = forms.CharField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
16
    warn = forms.BooleanField(label=_('warn'), required=False)
Valentin Samir's avatar
Valentin Samir committed
17 18 19 20 21 22 23 24 25 26

    def __init__(self, *args, **kwargs):
        super(UserCredential, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(UserCredential, self).clean()
        auth = settings.CAS_AUTH_CLASS(cleaned_data.get("username"))
        if auth.test_password(cleaned_data.get("password")):
            try:
                user = models.User.objects.get(username=auth.username)
Valentin Samir's avatar
Valentin Samir committed
27
                user.attributs = auth.attributs()
Valentin Samir's avatar
Valentin Samir committed
28 29
                user.save()
            except models.User.DoesNotExist:
Valentin Samir's avatar
Valentin Samir committed
30 31 32 33
                user = models.User.objects.create(
                    username=auth.username,
                    attributs=auth.attributs()
                )
Valentin Samir's avatar
Valentin Samir committed
34 35
                user.save()
        else:
Valentin Samir's avatar
Valentin Samir committed
36
            raise forms.ValidationError(_(u"Bad user"))
Valentin Samir's avatar
Valentin Samir committed
37 38 39


class TicketForm(forms.ModelForm):
Valentin Samir's avatar
Valentin Samir committed
40
    """Form for Tickets in the admin interface"""
Valentin Samir's avatar
Valentin Samir committed
41 42 43 44
    class Meta:
        model = models.Ticket
        exclude = []
    service = forms.CharField(widget=forms.TextInput)