forms.py 2.08 KB
Newer Older
Valentin Samir's avatar
Valentin Samir committed
1 2 3 4 5 6 7 8 9 10
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
# more details.
#
# You should have received a copy of the GNU General Public License version 3
# along with this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# (c) 2015 Valentin Samir
Valentin Samir's avatar
Valentin Samir committed
11
"""forms for the app"""
Valentin Samir's avatar
Valentin Samir committed
12
from . import default_settings
Valentin Samir's avatar
Valentin Samir committed
13

Valentin Samir's avatar
Valentin Samir committed
14 15
from django import forms
from django.conf import settings
Valentin Samir's avatar
Valentin Samir committed
16
from django.utils.translation import ugettext_lazy as _
Valentin Samir's avatar
Valentin Samir committed
17

Valentin Samir's avatar
Valentin Samir committed
18
from . import models
Valentin Samir's avatar
Valentin Samir committed
19 20

class UserCredential(forms.Form):
Valentin Samir's avatar
Valentin Samir committed
21
    """Form used on the login page to retrive user credentials"""
Valentin Samir's avatar
Valentin Samir committed
22
    username = forms.CharField(label=_('login'))
Valentin Samir's avatar
Valentin Samir committed
23
    service = forms.CharField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
24
    password = forms.CharField(label=_('password'), widget=forms.PasswordInput)
Valentin Samir's avatar
Valentin Samir committed
25
    method = forms.CharField(widget=forms.HiddenInput(), required=False)
Valentin Samir's avatar
Valentin Samir committed
26
    warn = forms.BooleanField(label=_('warn'), required=False)
Valentin Samir's avatar
Valentin Samir committed
27 28 29 30 31 32 33 34 35 36

    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
37
                user.attributs = auth.attributs()
Valentin Samir's avatar
Valentin Samir committed
38 39
                user.save()
            except models.User.DoesNotExist:
Valentin Samir's avatar
Valentin Samir committed
40 41 42 43
                user = models.User.objects.create(
                    username=auth.username,
                    attributs=auth.attributs()
                )
Valentin Samir's avatar
Valentin Samir committed
44 45
                user.save()
        else:
Valentin Samir's avatar
Valentin Samir committed
46
            raise forms.ValidationError(_(u"Bad user"))
Valentin Samir's avatar
Valentin Samir committed
47 48 49


class TicketForm(forms.ModelForm):
Valentin Samir's avatar
Valentin Samir committed
50
    """Form for Tickets in the admin interface"""
Valentin Samir's avatar
Valentin Samir committed
51 52 53 54
    class Meta:
        model = models.Ticket
        exclude = []
    service = forms.CharField(widget=forms.TextInput)