forms.py 2.29 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"""
12
from .default_settings import settings
Valentin Samir's avatar
Valentin Samir committed
13

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

17 18
import utils
import models
Valentin Samir's avatar
Valentin Samir committed
19

Valentin Samir's avatar
PEP8  
Valentin Samir committed
20

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

Valentin Samir's avatar
Valentin Samir committed
30 31
    def __init__(self, request, *args, **kwargs):
        self.request = request
Valentin Samir's avatar
Valentin Samir committed
32 33 34 35
        super(UserCredential, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(UserCredential, self).clean()
36
        auth = utils.import_attr(settings.CAS_AUTH_CLASS)(cleaned_data.get("username"))
Valentin Samir's avatar
Valentin Samir committed
37 38
        if auth.test_password(cleaned_data.get("password")):
            try:
39 40
                user = models.User.objects.get(
                    username=auth.username,
Valentin Samir's avatar
oops  
Valentin Samir committed
41
                    session_key=self.request.session.session_key
42
                )
Valentin Samir's avatar
Valentin Samir committed
43 44
                user.save()
            except models.User.DoesNotExist:
45 46
                user = models.User.objects.create(
                    username=auth.username,
Valentin Samir's avatar
oops  
Valentin Samir committed
47
                    session_key=self.request.session.session_key
48
                )
Valentin Samir's avatar
Valentin Samir committed
49 50
                user.save()
        else:
Valentin Samir's avatar
Valentin Samir committed
51
            raise forms.ValidationError(_(u"Bad user"))
Valentin Samir's avatar
Valentin Samir committed
52 53 54


class TicketForm(forms.ModelForm):
Valentin Samir's avatar
Valentin Samir committed
55
    """Form for Tickets in the admin interface"""
Valentin Samir's avatar
Valentin Samir committed
56 57 58
    class Meta:
        model = models.Ticket
        exclude = []
59
    service = forms.CharField(label=_('service'), widget=forms.TextInput)