Commit cb89a2f3e8e8e9e9ad643da89e3b3b25bca0e1c1
1 parent
688b8c11
Exists in
master
and in
4 other branches
New signal for user created
Showing
5 changed files
with
26 additions
and
18 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,19 @@ from django.core.urlresolvers import reverse | @@ -9,11 +9,19 @@ 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, *args, **kwargs): | ||
| 19 | + user = super(ColabUserManager, self)._create_user(*args, **kwargs) | ||
| 20 | + password = kwargs.get('password') | ||
| 21 | + | ||
| 22 | + user_created.send(user.__class__, user=user, password=password) | ||
| 23 | + return user | ||
| 24 | + | ||
| 17 | def create_user(self, username, email=None, password=None, **extra_fields): | 25 | def create_user(self, username, email=None, password=None, **extra_fields): |
| 18 | 26 | ||
| 19 | # It creates a valid password for users | 27 | # It creates a valid password for users |
| @@ -68,6 +76,11 @@ class User(AbstractUser): | @@ -68,6 +76,11 @@ class User(AbstractUser): | ||
| 68 | self.username = self.username.lower() | 76 | self.username = self.username.lower() |
| 69 | super(User, self).save(*args, **kwargs) | 77 | super(User, self).save(*args, **kwargs) |
| 70 | 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 | + | ||
| 71 | 84 | ||
| 72 | # We need to have `email` field set as unique but Django does not | 85 | # We need to have `email` field set as unique but Django does not |
| 73 | # support field overriding (at least not until 1.6). | 86 | # support field overriding (at least not until 1.6). |
colab/accounts/signals.py
| 1 | 1 | ||
| 2 | -from django.dispatch import Signal | ||
| 3 | - | 2 | +from django.dispatch import receiver, Signal |
| 3 | +from django.db.models.signals import post_save | ||
| 4 | 4 | ||
| 5 | +user_created = Signal(providing_args=['user', 'password']) | ||
| 5 | user_password_changed = Signal(providing_args=['user', 'password']) | 6 | user_password_changed = Signal(providing_args=['user', 'password']) |
colab/accounts/urls.py
| @@ -3,7 +3,7 @@ from django.conf import settings | @@ -3,7 +3,7 @@ from django.conf import settings | ||
| 3 | from django.conf.urls import patterns, url | 3 | from django.conf.urls import patterns, url |
| 4 | 4 | ||
| 5 | from .views import (UserProfileDetailView, UserProfileUpdateView, | 5 | from .views import (UserProfileDetailView, UserProfileUpdateView, |
| 6 | - ManageUserSubscriptionsView, colab_password_change) | 6 | + ManageUserSubscriptionsView) |
| 7 | 7 | ||
| 8 | from colab.accounts import views | 8 | from colab.accounts import views |
| 9 | from django.contrib.auth import views as auth_views | 9 | from django.contrib.auth import views as auth_views |
| @@ -29,7 +29,8 @@ urlpatterns = patterns('', | @@ -29,7 +29,8 @@ 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/?$', colab_password_change, | 32 | + url(r'^change-password/?$', auth_views.password_change, |
| 33 | + {'template_name':'registration/password_change_form_custom.html'}, | ||
| 33 | name='password_change'), | 34 | name='password_change'), |
| 34 | 35 | ||
| 35 | url(r'^change-password-done/?$', | 36 | url(r'^change-password-done/?$', |
colab/accounts/views.py
| @@ -2,7 +2,6 @@ | @@ -2,7 +2,6 @@ | ||
| 2 | from collections import OrderedDict | 2 | from collections import OrderedDict |
| 3 | 3 | ||
| 4 | from django.contrib import messages | 4 | from django.contrib import messages |
| 5 | -from django.contrib.auth.views import password_change | ||
| 6 | from django.db.models import Count | 5 | from django.db.models import Count |
| 7 | from django.contrib.auth import get_user_model | 6 | from django.contrib.auth import get_user_model |
| 8 | from django.utils.translation import ugettext as _ | 7 | from django.utils.translation import ugettext as _ |
| @@ -18,7 +17,6 @@ from colab.search.utils import get_collaboration_data, get_visible_threads | @@ -18,7 +17,6 @@ from colab.search.utils import get_collaboration_data, get_visible_threads | ||
| 18 | from colab.accounts.models import User | 17 | from colab.accounts.models import User |
| 19 | 18 | ||
| 20 | from .forms import (UserCreationForm, ListsForm, UserUpdateForm) | 19 | from .forms import (UserCreationForm, ListsForm, UserUpdateForm) |
| 21 | -from .signals import user_password_changed | ||
| 22 | from .utils import mailman | 20 | from .utils import mailman |
| 23 | 21 | ||
| 24 | 22 | ||
| @@ -214,13 +212,3 @@ def myaccount_redirect(request, route): | @@ -214,13 +212,3 @@ def myaccount_redirect(request, route): | ||
| 214 | url = '/'.join(('/account', request.user.username, route)) | 212 | url = '/'.join(('/account', request.user.username, route)) |
| 215 | 213 | ||
| 216 | return redirect(url) | 214 | return redirect(url) |
| 217 | - | ||
| 218 | - | ||
| 219 | -def colab_password_change(request): | ||
| 220 | - template_name = 'registration/password_change_form_custom.html' | ||
| 221 | - response = password_change(request, template_name) | ||
| 222 | - if response.status_code == 302: | ||
| 223 | - user_password_changed.send(sender=colab_password_change.__name__, | ||
| 224 | - user=request.user, | ||
| 225 | - password=request.POST.get('new_password1')) | ||
| 226 | - return response |