Commit c449fb6b8884449367c06a8dca539939a9385be4
Exists in
master
and in
3 other branches
Merge pull request #119 from colab/change-passwd-signal
Send Django signals when user is created and password is changed
Showing
5 changed files
with
47 additions
and
3 deletions
Show diff stats
colab/accounts/forms.py
| @@ -17,7 +17,7 @@ from django.utils.text import capfirst | @@ -17,7 +17,7 @@ from django.utils.text import capfirst | ||
| 17 | from django.utils.translation import ugettext_lazy as _ | 17 | from django.utils.translation import ugettext_lazy as _ |
| 18 | from django.utils.safestring import mark_safe | 18 | from django.utils.safestring import mark_safe |
| 19 | 19 | ||
| 20 | - | 20 | +from .signals import user_created |
| 21 | from .utils.validators import validate_social_account | 21 | from .utils.validators import validate_social_account |
| 22 | from .utils import mailman | 22 | from .utils import mailman |
| 23 | 23 | ||
| @@ -242,9 +242,14 @@ class UserCreationForm(UserForm): | @@ -242,9 +242,14 @@ class UserCreationForm(UserForm): | ||
| 242 | 242 | ||
| 243 | def save(self, commit=True): | 243 | def save(self, commit=True): |
| 244 | user = super(UserCreationForm, self).save(commit=False) | 244 | user = super(UserCreationForm, self).save(commit=False) |
| 245 | - user.set_password(self.cleaned_data["password1"]) | 245 | + password = self.cleaned_data["password1"] |
| 246 | + user.set_password(password) | ||
| 247 | + | ||
| 246 | if commit: | 248 | if commit: |
| 247 | user.save() | 249 | user.save() |
| 250 | + | ||
| 251 | + user_created.send(user.__class__, user=user, password=password) | ||
| 252 | + | ||
| 248 | return user | 253 | return user |
| 249 | 254 | ||
| 250 | 255 |
colab/accounts/models.py
| @@ -9,11 +9,20 @@ from django.core.urlresolvers import reverse | @@ -9,11 +9,20 @@ from django.core.urlresolvers import reverse | ||
| 9 | from django.utils.crypto import get_random_string | 9 | from django.utils.crypto import get_random_string |
| 10 | from django.utils.translation import ugettext_lazy as _ | 10 | from django.utils.translation import ugettext_lazy as _ |
| 11 | 11 | ||
| 12 | +from .signals import user_created, user_password_changed | ||
| 12 | from .utils import mailman | 13 | from .utils import mailman |
| 13 | 14 | ||
| 14 | 15 | ||
| 15 | class ColabUserManager(UserManager): | 16 | class ColabUserManager(UserManager): |
| 16 | 17 | ||
| 18 | + def _create_user(self, username, email, password, | ||
| 19 | + is_staff, is_superuser, **kwargs): | ||
| 20 | + args = (username, email, password, is_staff, is_superuser) | ||
| 21 | + user = super(ColabUserManager, self)._create_user(*args, **kwargs) | ||
| 22 | + | ||
| 23 | + user_created.send(user.__class__, user=user, password=password) | ||
| 24 | + return user | ||
| 25 | + | ||
| 17 | def create_user(self, username, email=None, password=None, **extra_fields): | 26 | def create_user(self, username, email=None, password=None, **extra_fields): |
| 18 | 27 | ||
| 19 | # It creates a valid password for users | 28 | # It creates a valid password for users |
| @@ -68,6 +77,11 @@ class User(AbstractUser): | @@ -68,6 +77,11 @@ class User(AbstractUser): | ||
| 68 | self.username = self.username.lower() | 77 | self.username = self.username.lower() |
| 69 | super(User, self).save(*args, **kwargs) | 78 | super(User, self).save(*args, **kwargs) |
| 70 | 79 | ||
| 80 | + def set_password(self, raw_password): | ||
| 81 | + super(User, self).set_password(raw_password) | ||
| 82 | + if self.pk: | ||
| 83 | + user_password_changed.send(User, user=self, password=raw_password) | ||
| 84 | + | ||
| 71 | 85 | ||
| 72 | # We need to have `email` field set as unique but Django does not | 86 | # We need to have `email` field set as unique but Django does not |
| 73 | # support field overriding (at least not until 1.6). | 87 | # support field overriding (at least not until 1.6). |
colab/accounts/tests/test_user.py
| @@ -374,3 +374,22 @@ class UserTest(TestCase): | @@ -374,3 +374,22 @@ class UserTest(TestCase): | ||
| 374 | self.authenticate_user() | 374 | self.authenticate_user() |
| 375 | self.validate_non_mandatory_fields('bio', '', ' ') | 375 | self.validate_non_mandatory_fields('bio', '', ' ') |
| 376 | self.user.delete() | 376 | self.user.delete() |
| 377 | + | ||
| 378 | + @mock.patch('colab.accounts.signals.user_password_changed.send') | ||
| 379 | + @mock.patch('colab.accounts.signals.user_created.send') | ||
| 380 | + def test_user_created_signal(self, user_created_send, | ||
| 381 | + user_password_changed_send): | ||
| 382 | + user = User.objects.create_user( | ||
| 383 | + username='test_user', | ||
| 384 | + password='12345', | ||
| 385 | + email='test@example.com', | ||
| 386 | + ) | ||
| 387 | + user_created_send.assert_called_with(User, user=user, password='12345') | ||
| 388 | + user_password_changed_send.assert_not_called() | ||
| 389 | + | ||
| 390 | + @mock.patch('colab.accounts.signals.user_password_changed.send') | ||
| 391 | + def test_user_password_changed_signal(self, user_password_changed_send): | ||
| 392 | + user = User.objects.first() | ||
| 393 | + user.set_password('54321') | ||
| 394 | + user_password_changed_send.assert_called_with(User, user=user, | ||
| 395 | + password='54321') |
colab/accounts/urls.py
| @@ -29,7 +29,7 @@ urlpatterns = patterns('', | @@ -29,7 +29,7 @@ urlpatterns = patterns('', | ||
| 29 | {'template_name':'registration/password_reset_form_custom.html'}, | 29 | {'template_name':'registration/password_reset_form_custom.html'}, |
| 30 | name="password_reset"), | 30 | name="password_reset"), |
| 31 | 31 | ||
| 32 | - url(r'^change-password/?$',auth_views.password_change, | 32 | + url(r'^change-password/?$', auth_views.password_change, |
| 33 | {'template_name':'registration/password_change_form_custom.html'}, | 33 | {'template_name':'registration/password_change_form_custom.html'}, |
| 34 | name='password_change'), | 34 | name='password_change'), |
| 35 | 35 |