implement osusers.forms.ChangeOsUserPasswordForm
- implement new form for password changes - use osusers.forms.PASSWORD_MISMATCH_ERROR in osusers.admin - add autogenerated documentation
This commit is contained in:
		
							parent
							
								
									3a9110dc30
								
							
						
					
					
						commit
						68c0bfbb4e
					
				
					 3 changed files with 81 additions and 5 deletions
				
			
		| 
						 | 
				
			
			@ -18,6 +18,13 @@
 | 
			
		|||
   :members:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
:py:mod:`forms <osusers.forms>`
 | 
			
		||||
-------------------------------
 | 
			
		||||
 | 
			
		||||
.. automodule:: osusers.forms
 | 
			
		||||
   :members:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
:py:mod:`models <osusers.models>`
 | 
			
		||||
---------------------------------
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,6 +6,9 @@ from django import forms
 | 
			
		|||
from django.utils.translation import ugettext as _
 | 
			
		||||
from django.contrib import admin
 | 
			
		||||
 | 
			
		||||
from .forms import (
 | 
			
		||||
    PASSWORD_MISMATCH_ERROR
 | 
			
		||||
)
 | 
			
		||||
from .models import (
 | 
			
		||||
    AdditionalGroup,
 | 
			
		||||
    Group,
 | 
			
		||||
| 
						 | 
				
			
			@ -13,11 +16,6 @@ from .models import (
 | 
			
		|||
    User,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
PASSWORD_MISMATCH_ERROR = _("Passwords don't match")
 | 
			
		||||
"""
 | 
			
		||||
Error message for non matching passwords.
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class AdditionalGroupInline(admin.TabularInline):
 | 
			
		||||
    """
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										71
									
								
								gnuviechadmin/osusers/forms.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								gnuviechadmin/osusers/forms.py
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,71 @@
 | 
			
		|||
"""
 | 
			
		||||
This module defines operating system user related forms.
 | 
			
		||||
 | 
			
		||||
"""
 | 
			
		||||
from __future__ import unicode_literals
 | 
			
		||||
 | 
			
		||||
from django import forms
 | 
			
		||||
from django.core.urlresolvers import reverse
 | 
			
		||||
from django.utils.translation import ugettext_lazy as _
 | 
			
		||||
 | 
			
		||||
from crispy_forms.helper import FormHelper
 | 
			
		||||
from crispy_forms.layout import Submit
 | 
			
		||||
 | 
			
		||||
from .models import User
 | 
			
		||||
 | 
			
		||||
PASSWORD_MISMATCH_ERROR = _("Passwords don't match")
 | 
			
		||||
"""
 | 
			
		||||
Error message for non matching passwords.
 | 
			
		||||
"""
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class ChangeOsUserPasswordForm(forms.ModelForm):
 | 
			
		||||
    """
 | 
			
		||||
    A form for setting an OS user's password.
 | 
			
		||||
 | 
			
		||||
    """
 | 
			
		||||
    password1 = forms.CharField(
 | 
			
		||||
        label=_('Password'), widget=forms.PasswordInput,
 | 
			
		||||
        required=False,
 | 
			
		||||
    )
 | 
			
		||||
    password2 = forms.CharField(
 | 
			
		||||
        label=_('Password (again)'), widget=forms.PasswordInput,
 | 
			
		||||
        required=False,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    class Meta:
 | 
			
		||||
        model = User
 | 
			
		||||
        fields = []
 | 
			
		||||
 | 
			
		||||
    def __init__(self, *args, **kwargs):
 | 
			
		||||
        self.helper = FormHelper()
 | 
			
		||||
        super(ChangeOsUserPasswordForm, self).__init__(*args, **kwargs)
 | 
			
		||||
        self.helper.form_action = reverse(
 | 
			
		||||
            'set_osuser_password', kwargs={'slug': self.instance.username})
 | 
			
		||||
        self.helper.add_input(Submit('submit', _('Set password')))
 | 
			
		||||
 | 
			
		||||
    def clean_password2(self):
 | 
			
		||||
        """
 | 
			
		||||
        Check that the two password entries match.
 | 
			
		||||
 | 
			
		||||
        :return: the validated password
 | 
			
		||||
        :rtype: str or None
 | 
			
		||||
 | 
			
		||||
        """
 | 
			
		||||
        password1 = self.cleaned_data.get('password1')
 | 
			
		||||
        password2 = self.cleaned_data.get('password2')
 | 
			
		||||
        if password1 and password2 and password1 != password2:
 | 
			
		||||
            raise forms.ValidationError(PASSWORD_MISMATCH_ERROR)
 | 
			
		||||
        return password2
 | 
			
		||||
 | 
			
		||||
    def save(self, commit=True):
 | 
			
		||||
        """
 | 
			
		||||
        Save the provided password in hashed format.
 | 
			
		||||
 | 
			
		||||
        :param boolean commit: whether to save the created user
 | 
			
		||||
        :return: user instance
 | 
			
		||||
        :rtype: :py:class:`osusers.models.User`
 | 
			
		||||
 | 
			
		||||
        """
 | 
			
		||||
        self.instance.set_password(self.cleaned_data['password1'])
 | 
			
		||||
        return super(ChangeOsUserPasswordForm, self).save(commit=commit)
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue