forms.py 1.4 KB
Newer Older
Valentin Samir's avatar
Valentin Samir committed
1 2
import default_settings

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

import models

class UserCredential(forms.Form):
Valentin Samir's avatar
Valentin Samir committed
10
    username = forms.CharField(label=_('login'))
Valentin Samir's avatar
Valentin Samir committed
11
    service = forms.CharField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
12
    password = forms.CharField(label=_('password'), widget=forms.PasswordInput)
Valentin Samir's avatar
Valentin Samir committed
13
    method = forms.CharField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
14
    warn = forms.BooleanField(label=_('warn'), required=False)
Valentin Samir's avatar
Valentin Samir committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

    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)
                user.attributs=auth.attributs()
                user.save()
            except models.User.DoesNotExist:
                user = models.User.objects.create(username=auth.username, attributs=auth.attributs())
                user.save()
            self.user = user
        else:
            raise forms.ValidationError("Bad user")


class TicketForm(forms.ModelForm):
    class Meta:
        model = models.Ticket
        exclude = []
    service = forms.CharField(widget=forms.TextInput)