django-swingcms / swingcms / custodian / forms.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django import forms
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned

import settings

from custodian.models import Group, User
from custodian.code import authenticate


class GroupForm(forms.ModelForm):

    required_css_class = 'required'

    class Meta:

        model = Group
        fields = ('name',)

    def clean_name(self):

        name = self.cleaned_data['name']
        if self.instance and name == self.instance.name:
            return name
        try:
            Group.objects.get(name=name)
        except ObjectDoesNotExist:
            return name
        raise forms.ValidationError(_("A group with that name already exists."))


class UserAddForm(forms.ModelForm):

    required_css_class = 'required'

    raw_email = forms.EmailField(label=_('email'))
    username = forms.RegexField(label=_('username'), max_length=30, regex=r'^[\w.@+-]+$',
                                help_text=_("32 characters or fewer. Permitted letters, numbers and @.+-_ characters."),
                                error_messages = {'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
    password1 = forms.CharField(label=_('insert password'), initial=None,
                                help_text=_("Permitted letters, numbers and @.+-_ characters."),
                                widget=forms.PasswordInput(render_value=False))
    password2 = forms.CharField(label=_('re-insert password'), initial=None,
                                widget=forms.PasswordInput(render_value=False),
                                help_text=_("Enter the same password as above, for verification."))

    class Meta:

        model = User
        fields = ('username', 'first_name', 'last_name', 'raw_email', 'password1', 'password2')

    def __init__(self,
                 require_user_email=settings.REQUIRE_USER_EMAIL or settings.CONFIRM_USER_EMAIL,
                 *args,
                 **kwargs):

        super(UserAddForm, self).__init__(*args, **kwargs)
        self.fields['raw_email'].required = require_user_email

    def save(self, commit=True):

        user = super(UserAddForm, self).save(commit=False)

        # optionally bypass encryption
        if settings.ENCRYPT_PASSWORD:
            user.set_password(self.cleaned_data['password1'])
        else:
            user.password = self.cleaned_data['password1']

        if commit:
            user.save()

        return user


class UserChangeForm(forms.ModelForm):

    required_css_class = 'required'

    raw_email = forms.EmailField(label=_('email'))
    username = forms.RegexField(label=_('username'), max_length=30, regex=r"^[\w.@+-]+$",
                                help_text=_("32 characters or fewer. Permitted letters, numbers and @.+-_ characters."),
                                error_messages = {'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
    password1 = forms.CharField(label=_('password'), required=False,
                                widget=forms.PasswordInput(render_value=False),
                                help_text = _("Insert a new password only if you want change it. Permitted letters, numbers and @.+-_ characters."))
    password2 = forms.CharField(label=_('re-type password'),
                                required=False, widget=forms.PasswordInput(render_value=False),
                                help_text = _("Enter the same password as above, for verification."))

    class Meta:

        model = User
        fields = ('username', 'first_name', 'last_name', 'raw_email', 'password1', 'password2')

    def __init__(self,
                 require_user_email=settings.REQUIRE_USER_EMAIL or settings.CONFIRM_USER_EMAIL,
                 *args,
                 **kwargs):

        super(UserChangeForm, self).__init__(*args, **kwargs)
        self.fields['raw_email'].required = require_user_email
        try:
            self.fields['raw_email'].initial = self.instance.email.email
        except AttributeError:
            pass

    def clean_username(self):

        username = self.cleaned_data['username']
        if username == self.instance.username:
            return username
        try:
            User.objects.get(username=username)
        except ObjectDoesNotExist:
            return username
        raise forms.ValidationError(_("A user with that username already exists."))

    def clean_password2(self):

        password1 = self.cleaned_data['password1']
        password2 = self.cleaned_data['password2']
        if (password1 or password2) and password1 != password2:
            raise forms.ValidationError(_("The two password fields didn't match."))
        return password2

    def save(self, commit=True):

        user = super(UserChangeForm, self).save(commit=False)

        # optionally bypass encryption
        if self.cleaned_data['password1']:
            if settings.ENCRYPT_PASSWORD:
                user.set_password(self.cleaned_data['password1'])
            else:
                user.password = self.cleaned_data['password1']

        if commit:
            user.save()

        return user


class PasswordFormChange(UserChangeForm):

    class Meta(UserChangeForm.Meta):

        fields = ('password1', 'password2')


class AuthenticationForm(forms.Form):
    """
    Base class for authenticating users. Extend this to get a form that accepts
    username/password logins.
    """
    required_css_class = 'required'

    username = forms.CharField(label=_('Username'), max_length=32)
    password = forms.CharField(label=_('Password'), widget=forms.PasswordInput)

    def __init__(self, request=None, *args, **kwargs):
        """
        If request is passed in, the form will validate that cookies are
        enabled. Note that the request (a HttpRequest object) must have set a
        cookie with the key TEST_COOKIE_NAME and value TEST_COOKIE_VALUE before
        running this validation.
        """
        self.request = request
        self.user_cache = None
        super(AuthenticationForm, self).__init__(*args, **kwargs)

    def clean(self):

        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')

        if username and password:
            self.user_cache = authenticate(username=username, password=password)
            if self.user_cache is None:
                raise forms.ValidationError(_("Please enter a correct username and password. Note that both fields are case-sensitive."))
            elif not self.user_cache.is_active:
                raise forms.ValidationError(_("This account is inactive."))
        self.check_for_test_cookie()

        return self.cleaned_data

    def check_for_test_cookie(self):

        if self.request and not self.request.session.test_cookie_worked():
            raise forms.ValidationError(_("Your Web browser must to have cookies enabled."))

    def get_user_id(self):

        if self.user_cache:
            return self.user_cache.id
        return None

    def get_user(self):

        return self.user_cache


class IncludedUsersForm(forms.Form):

    required_css_class = 'required'

    included = forms.MultipleChoiceField(choices=(), widget=forms.SelectMultiple)

    def __init__(self, group, *args, **kwargs):

        super(IncludedUsersForm, self).__init__(*args, **kwargs)
        users = User.objects.exclude(groups=group).order_by('username')
        self.fields['included'].choices = users.values_list('id','username')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.