Commit d3de2a35bfef47c5043dd088edba8cccd1c07a92
Exists in
master
and in
4 other branches
Merge pull request #123 from colab/change-passwd-signal
Send signal when password is changed
Showing
5 changed files
with
47 additions
and
3 deletions
Show diff stats
colab/accounts/forms.py
| ... | ... | @@ -9,7 +9,7 @@ from django.utils.functional import lazy |
| 9 | 9 | from django.utils.translation import ugettext_lazy as _ |
| 10 | 10 | from django.utils.safestring import mark_safe |
| 11 | 11 | |
| 12 | - | |
| 12 | +from .signals import user_created | |
| 13 | 13 | from .utils.validators import validate_social_account |
| 14 | 14 | from .utils import mailman |
| 15 | 15 | |
| ... | ... | @@ -235,9 +235,14 @@ class UserCreationForm(UserForm): |
| 235 | 235 | |
| 236 | 236 | def save(self, commit=True): |
| 237 | 237 | user = super(UserCreationForm, self).save(commit=False) |
| 238 | - user.set_password(self.cleaned_data["password1"]) | |
| 238 | + password = self.cleaned_data["password1"] | |
| 239 | + user.set_password(password) | |
| 240 | + | |
| 239 | 241 | if commit: |
| 240 | 242 | user.save() |
| 243 | + | |
| 244 | + user_created.send(user.__class__, user=user, password=password) | |
| 245 | + | |
| 241 | 246 | return user |
| 242 | 247 | |
| 243 | 248 | ... | ... |
colab/accounts/models.py
| ... | ... | @@ -8,11 +8,20 @@ from django.core.urlresolvers import reverse |
| 8 | 8 | from django.utils.crypto import get_random_string |
| 9 | 9 | from django.utils.translation import ugettext_lazy as _ |
| 10 | 10 | |
| 11 | +from .signals import user_created, user_password_changed | |
| 11 | 12 | from .utils import mailman |
| 12 | 13 | |
| 13 | 14 | |
| 14 | 15 | class ColabUserManager(UserManager): |
| 15 | 16 | |
| 17 | + def _create_user(self, username, email, password, | |
| 18 | + is_staff, is_superuser, **kwargs): | |
| 19 | + args = (username, email, password, is_staff, is_superuser) | |
| 20 | + user = super(ColabUserManager, self)._create_user(*args, **kwargs) | |
| 21 | + | |
| 22 | + user_created.send(user.__class__, user=user, password=password) | |
| 23 | + return user | |
| 24 | + | |
| 16 | 25 | def create_user(self, username, email=None, password=None, **extra_fields): |
| 17 | 26 | |
| 18 | 27 | # It creates a valid password for users |
| ... | ... | @@ -67,6 +76,11 @@ class User(AbstractUser): |
| 67 | 76 | self.username = self.username.lower() |
| 68 | 77 | super(User, self).save(*args, **kwargs) |
| 69 | 78 | |
| 79 | + def set_password(self, raw_password): | |
| 80 | + super(User, self).set_password(raw_password) | |
| 81 | + if self.pk: | |
| 82 | + user_password_changed.send(User, user=self, password=raw_password) | |
| 83 | + | |
| 70 | 84 | |
| 71 | 85 | # We need to have `email` field set as unique but Django does not |
| 72 | 86 | # support field overriding (at least not until 1.6). | ... | ... |
colab/accounts/tests/test_user.py
| ... | ... | @@ -426,3 +426,22 @@ class UserTest(TestCase): |
| 426 | 426 | self.authenticate_user() |
| 427 | 427 | response = self.client.get(url, follow=True) |
| 428 | 428 | self.assertIn(message, response.content) |
| 429 | + | |
| 430 | + @mock.patch('colab.accounts.signals.user_password_changed.send') | |
| 431 | + @mock.patch('colab.accounts.signals.user_created.send') | |
| 432 | + def test_user_created_signal(self, user_created_send, | |
| 433 | + user_password_changed_send): | |
| 434 | + user = User.objects.create_user( | |
| 435 | + username='test_user', | |
| 436 | + password='12345', | |
| 437 | + email='test@example.com', | |
| 438 | + ) | |
| 439 | + user_created_send.assert_called_with(User, user=user, password='12345') | |
| 440 | + user_password_changed_send.assert_not_called() | |
| 441 | + | |
| 442 | + @mock.patch('colab.accounts.signals.user_password_changed.send') | |
| 443 | + def test_user_password_changed_signal(self, user_password_changed_send): | |
| 444 | + user = User.objects.first() | |
| 445 | + user.set_password('54321') | |
| 446 | + user_password_changed_send.assert_called_with(User, user=user, | |
| 447 | + password='54321') | ... | ... |
colab/accounts/urls.py
| ... | ... | @@ -29,7 +29,7 @@ urlpatterns = patterns('', |
| 29 | 29 | {'template_name':'registration/password_reset_form_custom.html'}, |
| 30 | 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 | 33 | {'template_name':'registration/password_change_form_custom.html'}, |
| 34 | 34 | name='password_change'), |
| 35 | 35 | ... | ... |