Exemple #1
0
    def __init__(self, *args, **kwargs):
        super(GroupEditForm, self).__init__(*args, **kwargs)

        if self.instance.id:
            self.fields['name'].widget.attrs['readonly'] = True
            initial_members = User.objects.filter(
                groups=self.instance).order_by(
                    'email' if ENABLE_ORGANIZATIONS.get() else 'username')
            initial_perms = HuePermission.objects.filter(
                grouppermission__group=self.instance).order_by(
                    'app', 'description')
        else:
            initial_members = []
            initial_perms = []

        self.fields["members"] = _make_model_field(
            _("members"), initial_members, User.objects.order_by('username'))
        self.fields["permissions"] = _make_model_field(
            _("permissions"), initial_perms,
            HuePermission.objects.order_by('app', 'description'))
        if 'organization' in self.fields:
            self.fields['organization'] = forms.ChoiceField(
                choices=((get_user_request_organization().id,
                          get_user_request_organization()), ),
                initial=get_user_request_organization())
Exemple #2
0
    def __init__(self, *args, **kwargs):
        super(GroupEditForm, self).__init__(*args, **kwargs)

        ordering_field = orm_user_lookup()

        if self.instance.id:
            self.fields['name'].widget.attrs['readonly'] = True
            initial_members = User.objects.filter(
                groups=self.instance).order_by(ordering_field)
            initial_perms = HuePermission.objects.filter(
                grouppermission__group=self.instance).order_by(
                    'app', 'description')
        else:
            initial_members = []
            initial_perms = []

        self.fields["members"] = _make_model_field(
            _("members"), initial_members,
            User.objects.order_by(ordering_field))
        self.fields["permissions"] = _make_model_field(
            _("permissions"), initial_perms,
            HuePermission.objects.order_by('app', 'description'))
        if 'organization' in self.fields:
            self.fields['organization'] = forms.ChoiceField(
                choices=((default_organization().id,
                          default_organization()), ),
                initial=default_organization())
Exemple #3
0
        def __init__(self, *args, **kwargs):
            super(OrganizationUserChangeForm, self).__init__(*args, **kwargs)

            if self.instance.id:
                self.fields['email'].widget.attrs['readonly'] = True

            self.fields['organization'] = forms.ChoiceField(
                choices=((get_user_request_organization().id,
                          get_user_request_organization()), ),
                initial=get_user_request_organization())
Exemple #4
0
class CurrentTermForm(forms.Form):
    """
    Form that allows user to select a current term.
    """
    current_term = forms.ChoiceField()

    def __init__(self, term_choices, *args, **kwargs):
        """
        HTML widget modification and initializes form with term-choices it
        receives from the associated view's form_kwargs.
        """
        super().__init__(*args, **kwargs)
        self.fields['current_term'].label = ''
        self.fields['current_term'].choices = term_choices
Exemple #5
0
class UserChangeForm(django.contrib.auth.forms.UserChangeForm):
    """
  This is similar, but not quite the same as djagno.contrib.auth.forms.UserChangeForm and UserCreationForm.
  """

    GENERIC_VALIDATION_ERROR = _("Username or password is invalid.")

    username = forms.RegexField(
        label=_t("Username"),
        max_length=30,
        regex='^%s$' %
        (get_username_re_rule(), ),  # Could use UnicodeUsernameValidator()
        help_text=_t(
            "Required. 30 characters or fewer. No whitespaces or colons."),
        error_messages={'invalid': _t("Whitespaces and ':' not allowed")})

    password1 = forms.CharField(label=_t("New Password"),
                                widget=forms.PasswordInput,
                                required=False,
                                validators=hue_get_password_validators())
    password2 = forms.CharField(label=_t("Password confirmation"),
                                widget=forms.PasswordInput,
                                required=False,
                                validators=hue_get_password_validators())
    password_old = forms.CharField(label=_t("Current password"),
                                   widget=forms.PasswordInput,
                                   required=False)
    ensure_home_directory = forms.BooleanField(
        label=_t("Create home directory"),
        help_text=_t("Create home directory if one doesn't already exist."),
        initial=True,
        required=False)
    language = forms.ChoiceField(label=_t("Language Preference"),
                                 choices=LANGUAGES,
                                 required=False)
    unlock_account = forms.BooleanField(
        label=_t("Unlock Account"),
        help_text=_t("Unlock user's account for login."),
        initial=False,
        required=False)

    class Meta(django.contrib.auth.forms.UserChangeForm.Meta):
        model = User
        fields = [
            "username", "first_name", "last_name", "email",
            "ensure_home_directory"
        ]

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

        if self.instance.id and 'username' in self.fields:
            self.fields['username'].widget.attrs['readonly'] = True

        if 'desktop.auth.backend.LdapBackend' in desktop_conf.AUTH.BACKEND.get(
        ):
            self.fields['password1'].widget.attrs['readonly'] = True
            self.fields['password2'].widget.attrs['readonly'] = True
            self.fields['password_old'].widget.attrs['readonly'] = True
            self.fields['first_name'].widget.attrs['readonly'] = True
            self.fields['last_name'].widget.attrs['readonly'] = True
            self.fields['email'].widget.attrs['readonly'] = True
            if 'is_active' in self.fields:
                self.fields['is_active'].widget.attrs['readonly'] = True
            if 'is_superuser' in self.fields:
                self.fields['is_superuser'].widget.attrs['readonly'] = True
            if 'unlock_account' in self.fields:
                self.fields['unlock_account'].widget.attrs['readonly'] = True
            if 'groups' in self.fields:
                self.fields['groups'].widget.attrs['readonly'] = True

        if ENABLE_ORGANIZATIONS.get():
            organization = self.instance.organization if self.instance.id else get_user_request_organization(
            )
            self.fields['groups'].choices = [
                (group.id, group.name)
                for group in organization.organizationgroup_set.all()
            ]

    def clean_username(self):
        username = self.cleaned_data["username"]
        if self.instance.username == username:
            return username
        try:
            User._default_manager.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError(_("Username already exists."),
                                    code='duplicate_username')

    def clean_password(self):
        return self.cleaned_data["password"]

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1", "")
        password2 = self.cleaned_data["password2"]
        if password1 != password2:
            raise forms.ValidationError(_t("Passwords do not match."))
        return password2

    def clean_password1(self):
        password = self.cleaned_data.get("password1", "")
        if self.instance.id is None and password == "":
            raise forms.ValidationError(
                _("You must specify a password when creating a new user."))
        return self.cleaned_data.get("password1", "")

    def clean_password_old(self):
        if self.instance.id is not None:
            password1 = self.cleaned_data.get("password1", "")
            password_old = self.cleaned_data.get("password_old", "")
            if password1 != '' and not self.instance.check_password(
                    password_old):
                raise forms.ValidationError(self.GENERIC_VALIDATION_ERROR)
        return self.cleaned_data.get("password_old", "")

    def save(self, commit=True):
        """
    Update password if it's set.
    """
        user = super(UserChangeForm, self).save(commit=False)
        if self.cleaned_data["password1"]:
            user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
            # groups must be saved after the user
            self.save_m2m()
        return user
Exemple #6
0
 def __init__(self, *args, **kwargs):
     super(SyncLdapUsersGroupsForm, self).__init__(*args, **kwargs)
     if get_server_choices():
         self.fields['server'] = forms.ChoiceField(
             choices=get_server_choices(), required=True)
Exemple #7
0
 def __init__(self, *args, **kwargs):
     super(AddLdapGroupsForm, self).__init__(*args, **kwargs)
     self.fields['server'] = forms.ChoiceField(choices=get_server_choices(),
                                               required=False)