Commit d2f0c7bb9c5878be8e21935a2fe40f6aeab690b5

Authored by Sergio Oliveira
Committed by Matheus de Sousa Faria
1 parent 78f2007b

New signal for user created

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,19 @@ 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, *args, **kwargs):
  18 + user = super(ColabUserManager, self)._create_user(*args, **kwargs)
  19 + password = kwargs.get('password')
  20 +
  21 + user_created.send(user.__class__, user=user, password=password)
  22 + return user
  23 +
16 24 def create_user(self, username, email=None, password=None, **extra_fields):
17 25  
18 26 # It creates a valid password for users
... ... @@ -67,6 +75,11 @@ class User(AbstractUser):
67 75 self.username = self.username.lower()
68 76 super(User, self).save(*args, **kwargs)
69 77  
  78 + def set_password(self, raw_password):
  79 + super(User, self).set_password(raw_password)
  80 + if self.pk:
  81 + user_password_changed.send(User, user=self, password=raw_password)
  82 +
70 83  
71 84 # We need to have `email` field set as unique but Django does not
72 85 # 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 6 user_password_changed = Signal(providing_args=['user', 'password'])
... ...
colab/accounts/urls.py
... ... @@ -3,7 +3,7 @@ from django.conf import settings
3 3 from django.conf.urls import patterns, url
4 4  
5 5 from .views import (UserProfileDetailView, UserProfileUpdateView,
6   - ManageUserSubscriptionsView, colab_password_change)
  6 + ManageUserSubscriptionsView)
7 7  
8 8 from colab.accounts import views
9 9 from django.contrib.auth import views as auth_views
... ... @@ -29,7 +29,8 @@ urlpatterns = patterns('',
29 29 {'template_name':'registration/password_reset_form_custom.html'},
30 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 34 name='password_change'),
34 35  
35 36 url(r'^change-password-done/?$',
... ...
colab/accounts/views.py
... ... @@ -2,7 +2,6 @@
2 2 from collections import OrderedDict
3 3  
4 4 from django.contrib import messages
5   -from django.contrib.auth.views import password_change
6 5 from django.db.models import Count
7 6 from django.contrib.auth import get_user_model
8 7 from django.utils.translation import ugettext as _
... ... @@ -18,7 +17,6 @@ from colab.search.utils import get_collaboration_data, get_visible_threads
18 17 from colab.accounts.models import User
19 18  
20 19 from .forms import (UserCreationForm, ListsForm, UserUpdateForm)
21   -from .signals import user_password_changed
22 20 from .utils import mailman
23 21  
24 22  
... ... @@ -214,13 +212,3 @@ def myaccount_redirect(request, route):
214 212 url = '/'.join(('/account', request.user.username, route))
215 213  
216 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
... ...