| 1 | """ |
|---|
| 2 | Views which allow users to create and activate accounts. |
|---|
| 3 | |
|---|
| 4 | """ |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | from django.shortcuts import redirect |
|---|
| 8 | from django.shortcuts import render_to_response |
|---|
| 9 | from django.template import RequestContext |
|---|
| 10 | |
|---|
| 11 | from registration.backends import get_backend |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | def activate(request, backend, |
|---|
| 15 | template_name='registration/activate.html', |
|---|
| 16 | success_url=None, extra_context=None, **kwargs): |
|---|
| 17 | """ |
|---|
| 18 | Activate a user's account. |
|---|
| 19 | |
|---|
| 20 | The actual activation of the account will be delegated to the |
|---|
| 21 | backend specified by the ``backend`` keyword argument (see below); |
|---|
| 22 | the backend's ``activate()`` method will be called, passing any |
|---|
| 23 | keyword arguments captured from the URL, and will be assumed to |
|---|
| 24 | return a ``User`` if activation was successful, or a value which |
|---|
| 25 | evaluates to ``False`` in boolean context if not. |
|---|
| 26 | |
|---|
| 27 | Upon successful activation, the backend's |
|---|
| 28 | ``post_activation_redirect()`` method will be called, passing the |
|---|
| 29 | ``HttpRequest`` and the activated ``User`` to determine the URL to |
|---|
| 30 | redirect the user to. To override this, pass the argument |
|---|
| 31 | ``success_url`` (see below). |
|---|
| 32 | |
|---|
| 33 | On unsuccessful activation, will render the template |
|---|
| 34 | ``registration/activate.html`` to display an error message; to |
|---|
| 35 | override thise, pass the argument ``template_name`` (see below). |
|---|
| 36 | |
|---|
| 37 | **Arguments** |
|---|
| 38 | |
|---|
| 39 | ``backend`` |
|---|
| 40 | The dotted Python import path to the backend class to |
|---|
| 41 | use. Required. |
|---|
| 42 | |
|---|
| 43 | ``extra_context`` |
|---|
| 44 | A dictionary of variables to add to the template context. Any |
|---|
| 45 | callable object in this dictionary will be called to produce |
|---|
| 46 | the end result which appears in the context. Optional. |
|---|
| 47 | |
|---|
| 48 | ``success_url`` |
|---|
| 49 | The name of a URL pattern to redirect to on successful |
|---|
| 50 | acivation. This is optional; if not specified, this will be |
|---|
| 51 | obtained by calling the backend's |
|---|
| 52 | ``post_activation_redirect()`` method. |
|---|
| 53 | |
|---|
| 54 | ``template_name`` |
|---|
| 55 | A custom template to use. This is optional; if not specified, |
|---|
| 56 | this will default to ``registration/activate.html``. |
|---|
| 57 | |
|---|
| 58 | ``\*\*kwargs`` |
|---|
| 59 | Any keyword arguments captured from the URL, such as an |
|---|
| 60 | activation key, which will be passed to the backend's |
|---|
| 61 | ``activate()`` method. |
|---|
| 62 | |
|---|
| 63 | **Context:** |
|---|
| 64 | |
|---|
| 65 | The context will be populated from the keyword arguments captured |
|---|
| 66 | in the URL, and any extra variables supplied in the |
|---|
| 67 | ``extra_context`` argument (see above). |
|---|
| 68 | |
|---|
| 69 | **Template:** |
|---|
| 70 | |
|---|
| 71 | registration/activate.html or ``template_name`` keyword argument. |
|---|
| 72 | |
|---|
| 73 | """ |
|---|
| 74 | backend = get_backend(backend) |
|---|
| 75 | account = backend.activate(request, **kwargs) |
|---|
| 76 | |
|---|
| 77 | if account: |
|---|
| 78 | if success_url is None: |
|---|
| 79 | to, args, kwargs = backend.post_activation_redirect(request, account) |
|---|
| 80 | return redirect(to, *args, **kwargs) |
|---|
| 81 | else: |
|---|
| 82 | return redirect(success_url) |
|---|
| 83 | |
|---|
| 84 | if extra_context is None: |
|---|
| 85 | extra_context = {} |
|---|
| 86 | context = RequestContext(request) |
|---|
| 87 | for key, value in extra_context.items(): |
|---|
| 88 | context[key] = callable(value) and value() or value |
|---|
| 89 | |
|---|
| 90 | return render_to_response(template_name, |
|---|
| 91 | kwargs, |
|---|
| 92 | context_instance=context) |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | def register(request, backend, success_url=None, form_class=None, |
|---|
| 96 | disallowed_url='registration_disallowed', |
|---|
| 97 | template_name='registration/registration_form.html', |
|---|
| 98 | extra_context=None): |
|---|
| 99 | """ |
|---|
| 100 | Allow a new user to register an account. |
|---|
| 101 | |
|---|
| 102 | The actual registration of the account will be delegated to the |
|---|
| 103 | backend specified by the ``backend`` keyword argument (see below); |
|---|
| 104 | it will be used as follows: |
|---|
| 105 | |
|---|
| 106 | 1. The backend's ``registration_allowed()`` method will be called, |
|---|
| 107 | passing the ``HttpRequest``, to determine whether registration |
|---|
| 108 | of an account is to be allowed; if not, a redirect is issued to |
|---|
| 109 | the view corresponding to the named URL pattern |
|---|
| 110 | ``registration_disallowed``. To override this, see the list of |
|---|
| 111 | optional arguments for this view (below). |
|---|
| 112 | |
|---|
| 113 | 2. The form to use for account registration will be obtained by |
|---|
| 114 | calling the backend's ``get_form_class()`` method, passing the |
|---|
| 115 | ``HttpRequest``. To override this, see the list of optional |
|---|
| 116 | arguments for this view (below). |
|---|
| 117 | |
|---|
| 118 | 3. If valid, the form's ``cleaned_data`` will be passed (as |
|---|
| 119 | keyword arguments, and along with the ``HttpRequest``) to the |
|---|
| 120 | backend's ``register()`` method, which should return the new |
|---|
| 121 | ``User`` object. |
|---|
| 122 | |
|---|
| 123 | 4. Upon successful registration, the backend's |
|---|
| 124 | ``post_registration_redirect()`` method will be called, passing |
|---|
| 125 | the ``HttpRequest`` and the new ``User``, to determine the URL |
|---|
| 126 | to redirect the user to. To override this, see the list of |
|---|
| 127 | optional arguments for this view (below). |
|---|
| 128 | |
|---|
| 129 | **Required arguments** |
|---|
| 130 | |
|---|
| 131 | None. |
|---|
| 132 | |
|---|
| 133 | **Optional arguments** |
|---|
| 134 | |
|---|
| 135 | ``backend`` |
|---|
| 136 | The dotted Python import path to the backend class to use. |
|---|
| 137 | |
|---|
| 138 | ``disallowed_url`` |
|---|
| 139 | URL to redirect to if registration is not permitted for the |
|---|
| 140 | current ``HttpRequest``. Must be a value which can legally be |
|---|
| 141 | passed to ``django.shortcuts.redirect``. If not supplied, this |
|---|
| 142 | will be whatever URL corresponds to the named URL pattern |
|---|
| 143 | ``registration_disallowed``. |
|---|
| 144 | |
|---|
| 145 | ``form_class`` |
|---|
| 146 | The form class to use for registration. If not supplied, this |
|---|
| 147 | will be retrieved from the registration backend. |
|---|
| 148 | |
|---|
| 149 | ``extra_context`` |
|---|
| 150 | A dictionary of variables to add to the template context. Any |
|---|
| 151 | callable object in this dictionary will be called to produce |
|---|
| 152 | the end result which appears in the context. |
|---|
| 153 | |
|---|
| 154 | ``success_url`` |
|---|
| 155 | URL to redirect to after successful registration. Must be a |
|---|
| 156 | value which can legally be passed to |
|---|
| 157 | ``django.shortcuts.redirect``. If not supplied, this will be |
|---|
| 158 | retrieved from the registration backend. |
|---|
| 159 | |
|---|
| 160 | ``template_name`` |
|---|
| 161 | A custom template to use. If not supplied, this will default |
|---|
| 162 | to ``registration/registration_form.html``. |
|---|
| 163 | |
|---|
| 164 | **Context:** |
|---|
| 165 | |
|---|
| 166 | ``form`` |
|---|
| 167 | The registration form. |
|---|
| 168 | |
|---|
| 169 | Any extra variables supplied in the ``extra_context`` argument |
|---|
| 170 | (see above). |
|---|
| 171 | |
|---|
| 172 | **Template:** |
|---|
| 173 | |
|---|
| 174 | registration/registration_form.html or ``template_name`` keyword |
|---|
| 175 | argument. |
|---|
| 176 | |
|---|
| 177 | """ |
|---|
| 178 | backend = get_backend(backend) |
|---|
| 179 | if not backend.registration_allowed(request): |
|---|
| 180 | return redirect(disallowed_url) |
|---|
| 181 | if form_class is None: |
|---|
| 182 | form_class = backend.get_form_class(request) |
|---|
| 183 | |
|---|
| 184 | if request.method == 'POST': |
|---|
| 185 | form = form_class(data=request.POST, files=request.FILES) |
|---|
| 186 | if form.is_valid(): |
|---|
| 187 | new_user = backend.register(request, **form.cleaned_data) |
|---|
| 188 | if success_url is None: |
|---|
| 189 | to, args, kwargs = backend.post_registration_redirect(request, new_user) |
|---|
| 190 | return redirect(to, *args, **kwargs) |
|---|
| 191 | else: |
|---|
| 192 | return redirect(success_url) |
|---|
| 193 | else: |
|---|
| 194 | form = form_class() |
|---|
| 195 | |
|---|
| 196 | if extra_context is None: |
|---|
| 197 | extra_context = {} |
|---|
| 198 | context = RequestContext(request) |
|---|
| 199 | for key, value in extra_context.items(): |
|---|
| 200 | context[key] = callable(value) and value() or value |
|---|
| 201 | |
|---|
| 202 | return render_to_response(template_name, |
|---|
| 203 | {'form': form}, |
|---|
| 204 | context_instance=context) |
|---|