Skip to content

Commit ea287c0

Browse files
committed
add new Signal django example
1 parent 5e9164b commit ea287c0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

content/pages/examples/django/django-dispatch-dispatcher-signal.markdown

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,42 @@ The code for the project is available under the
125125
~~ "traceback"])
126126
~~task_finished = Signal(providing_args=["process", "task"])
127127
```
128+
129+
130+
## Example 5 from django-registration (redux)
131+
[django-registration (redux)](https://github.com/macropin/django-registration)
132+
([project documentation](https://django-registration-redux.readthedocs.io/en/latest/))
133+
is a [Django](/django.html) code library for one-phase, two-phase and
134+
three-phase registration flows. The code is available
135+
[open source](https://github.com/macropin/django-registration/blob/master/LICENSE).
136+
137+
[**django-registration / registrations / signals.py**](https://github.com/macropin/django-registration/blob/master/registration/signals.py)
138+
139+
```python
140+
from django.conf import settings
141+
from django.contrib.auth import get_backends
142+
from django.contrib.auth import login
143+
~~from django.dispatch import Signal
144+
145+
# An admin has approved a user's account
146+
~~user_approved = Signal(providing_args=["user", "request"])
147+
148+
# A new user has registered.
149+
~~user_registered = Signal(providing_args=["user", "request"])
150+
151+
# A user has activated his or her account.
152+
~~user_activated = Signal(providing_args=["user", "request"])
153+
154+
155+
def login_user(sender, user, request, **kwargs):
156+
""" Automatically authenticate the user when activated """
157+
backend = get_backends()[0] # Hack to bypass `authenticate()`.
158+
user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
159+
login(request, user)
160+
request.session['REGISTRATION_AUTO_LOGIN'] = True
161+
request.session.modified = True
162+
163+
164+
if getattr(settings, 'REGISTRATION_AUTO_LOGIN', False):
165+
user_activated.connect(login_user)
166+
```

theme/templates/table-of-contents.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ <h2>Example Python Code</h2>
228228
<div class="row">
229229
<div class="c6">
230230
<h3 style="margin-top:16px">Django examples</h3>
231+
<h4 class="bp"><a href="/django-conf-settings-examples.html">django.conf.settings</a></h4>
231232
<h4 class="bp"><a href="/django-conf-urls-url-examples.html">django.conf.urls.url</a></h4>
232233
<h4 class="bp"><a href="/django-contrib-admin-examples.html">django.contrib.admin</a></h4>
233234
<h4 class="bp"><a href="/django-core-mail-send-mail-examples.html">django.core.mail.send_mail</a></h4>

0 commit comments

Comments
 (0)