|
| 1 | +from django.conf import settings |
| 2 | +from django.contrib.sites.models import RequestSite |
| 3 | +from django.contrib.sites.models import Site |
| 4 | + |
| 5 | +from registration import signals |
| 6 | +from registration.forms import RegistrationForm |
| 7 | +from registration.models import RegistrationProfile |
| 8 | + |
| 9 | + |
| 10 | +class DefaultBackend(object): |
| 11 | + """ |
| 12 | + A registration backend which follows a simple workflow: |
| 13 | +
|
| 14 | + 1. User signs up, inactive account is created. |
| 15 | +
|
| 16 | + 2. Email is sent to user with activation link. |
| 17 | +
|
| 18 | + 3. User clicks activation link, account is now active. |
| 19 | +
|
| 20 | + Using this backend requires that |
| 21 | +
|
| 22 | + * ``registration`` be listed in the ``INSTALLED_APPS`` setting |
| 23 | + (since this backend makes use of models defined in this |
| 24 | + application). |
| 25 | +
|
| 26 | + * The setting ``ACCOUNT_ACTIVATION_DAYS`` be supplied, specifying |
| 27 | + (as an integer) the number of days from registration during |
| 28 | + which a user may activate their account (after that period |
| 29 | + expires, activation will be disallowed). |
| 30 | +
|
| 31 | + * The creation of the templates |
| 32 | + ``registration/activation_email_subject.txt`` and |
| 33 | + ``registration/activation_email.txt``, which will be used for |
| 34 | + the activation email. See the notes for this backends |
| 35 | + ``register`` method for details regarding these templates. |
| 36 | +
|
| 37 | + Additionally, registration can be temporarily closed by adding the |
| 38 | + setting ``REGISTRATION_OPEN`` and setting it to |
| 39 | + ``False``. Omitting this setting, or setting it to ``True``, will |
| 40 | + be interpreted as meaning that registration is currently open and |
| 41 | + permitted. |
| 42 | +
|
| 43 | + Internally, this is accomplished via storing an activation key in |
| 44 | + an instance of ``registration.models.RegistrationProfile``. See |
| 45 | + that model and its custom manager for full documentation of its |
| 46 | + fields and supported operations. |
| 47 | + |
| 48 | + """ |
| 49 | + def register(self, request, **kwargs): |
| 50 | + """ |
| 51 | + Given a username, email address and password, register a new |
| 52 | + user account, which will initially be inactive. |
| 53 | +
|
| 54 | + Along with the new ``User`` object, a new |
| 55 | + ``registration.models.RegistrationProfile`` will be created, |
| 56 | + tied to that ``User``, containing the activation key which |
| 57 | + will be used for this account. |
| 58 | +
|
| 59 | + An email will be sent to the supplied email address; this |
| 60 | + email should contain an activation link. The email will be |
| 61 | + rendered using two templates. See the documentation for |
| 62 | + ``RegistrationProfile.send_activation_email()`` for |
| 63 | + information about these templates and the contexts provided to |
| 64 | + them. |
| 65 | +
|
| 66 | + After the ``User`` and ``RegistrationProfile`` are created and |
| 67 | + the activation email is sent, the signal |
| 68 | + ``registration.signals.user_registered`` will be sent, with |
| 69 | + the new ``User`` as the keyword argument ``user`` and the |
| 70 | + class of this backend as the sender. |
| 71 | +
|
| 72 | + """ |
| 73 | + username, email, password = kwargs['username'], kwargs['email'], kwargs['password1'] |
| 74 | + if Site._meta.installed: |
| 75 | + site = Site.objects.get_current() |
| 76 | + else: |
| 77 | + site = RequestSite(request) |
| 78 | + new_user = RegistrationProfile.objects.create_inactive_user(username, email, |
| 79 | + password, site) |
| 80 | + signals.user_registered.send(sender=self.__class__, |
| 81 | + user=new_user, |
| 82 | + request=request) |
| 83 | + return new_user |
| 84 | + |
| 85 | + def activate(self, request, activation_key): |
| 86 | + """ |
| 87 | + Given an an activation key, look up and activate the user |
| 88 | + account corresponding to that key (if possible). |
| 89 | +
|
| 90 | + After successful activation, the signal |
| 91 | + ``registration.signals.user_activated`` will be sent, with the |
| 92 | + newly activated ``User`` as the keyword argument ``user`` and |
| 93 | + the class of this backend as the sender. |
| 94 | + |
| 95 | + """ |
| 96 | + activated = RegistrationProfile.objects.activate_user(activation_key) |
| 97 | + if activated: |
| 98 | + signals.user_activated.send(sender=self.__class__, |
| 99 | + user=activated, |
| 100 | + request=request) |
| 101 | + return activated |
| 102 | + |
| 103 | + def registration_allowed(self, request): |
| 104 | + """ |
| 105 | + Indicate whether account registration is currently permitted, |
| 106 | + based on the value of the setting ``REGISTRATION_OPEN``. This |
| 107 | + is determined as follows: |
| 108 | +
|
| 109 | + * If ``REGISTRATION_OPEN`` is not specified in settings, or is |
| 110 | + set to ``True``, registration is permitted. |
| 111 | +
|
| 112 | + * If ``REGISTRATION_OPEN`` is both specified and set to |
| 113 | + ``False``, registration is not permitted. |
| 114 | + |
| 115 | + """ |
| 116 | + return getattr(settings, 'REGISTRATION_OPEN', True) |
| 117 | + |
| 118 | + def get_form_class(self, request): |
| 119 | + """ |
| 120 | + Return the default form class used for user registration. |
| 121 | + |
| 122 | + """ |
| 123 | + return RegistrationForm |
| 124 | + |
| 125 | + def post_registration_redirect(self, request, user): |
| 126 | + """ |
| 127 | + Return the name of the URL to redirect to after successful |
| 128 | + user registration. |
| 129 | + |
| 130 | + """ |
| 131 | + return ('registration_complete', (), {}) |
| 132 | + |
| 133 | + def post_activation_redirect(self, request, user): |
| 134 | + """ |
| 135 | + Return the name of the URL to redirect to after successful |
| 136 | + account activation. |
| 137 | + |
| 138 | + """ |
| 139 | + return ('registration_activation_complete', (), {}) |
0 commit comments