cradmin_activate_account — An activate account workflow¶
The purpose of the django_cradmin.apps.cradmin_activate_account app is to provide a
general purpose activate account workflow.
It is designed to work with any user model as long as it
has an email field or property and an is_active-object
like the Django User model or an activate_user()-method.
Install¶
Add the following to INSTALLED_APPS:
INSTALLED_APPS = (
# ...
'django_cradmin',
'django_cradmin.apps.cradmin_generic_token_with_metadata',
'django_cradmin.apps.cradmin_activate_account',
)
And add something like this to your root url config:
urlpatterns = patterns(
# ...
url(r'^activateaccount/', include('django_cradmin.apps.cradmin_activate_account.urls')),
# ...
)
Quick start¶
If you install and setup the app as explained above, you just need to set
the DJANGO_CRADMIN_SITENAME setting:
DJANGO_CRADMIN_SITENAME = 'Testsite'
Now you should be able to use the
ActivationEmail
class to initiate account activation.
The ActivationEmail class¶
-
class
ActivationEmail(request, user, next_url=None)¶ Bases:
django_cradmin.apps.cradmin_email.emailutils.AbstractEmailHandles account activation. Sends an email with a link that the user clicks to activate their account.
Example:
from django_cradmin.apps.cradmin_activate_account.utils import ActivationEmail def myview(request): someuser = get_some_user() # Insert your code to determine the user to activate here ActivationEmail(request=request, user=someuser).send()
Parameters: - request – A Django HttpRequest object.
- user – The user you want to activate. Must have an
emailattribute or property. - next_url – An optional URL to redirect to after the user has activated their account.
-
appname= 'cradmin_activate_account'¶ The name of the app. Used for
django_cradmin.apps.cradmin_generic_token_with_metadata.models.GenericTokenWithMetadata.app. If you override this, you also have to overrideget_activate_url()and return the URL to a view that pops a GenericTokenWithMetadata with the changed appname.
-
get_activate_url(token)¶ Get the activate account view URL.
-
get_context_data()¶ Get the context data of the email templates.
-
get_from_email()¶ Get the email sender address.
Defaults to the
DJANGO_CRADMIN_ACTIVATE_ACCOUNT_FROM_EMAILsetting falling back on theDEFAULT_FROM_EMAILsetting.
-
get_expiration_datetime()¶ Get the value to use for the
expiration_datetimeattribute ofGenericTokenWithMetadata.
Configure¶
- Required settings:
- DJANGO_CRADMIN_SITENAME
- The name of the site. You must set this setting unless you override the email subject and message templates as explained in Email templates and how to override them.
- Optional settings:
- DJANGO_CRADMIN_ACTIVATE_ACCOUNT_FROM_EMAIL
- Defaults to the
DEFAULT_FROM_EMAILsetting. - DJANGO_CRADMIN_ACTIVATE_ACCOUNT_DEFAULT_NEXT_URL
- The URL to redirect to when the account has been activated.
Defaults to the
LOGIN_URLsetting.
How it works¶
Step One — Send the activate account email¶
First we generate a token using cradmin_generic_token_with_metadata — Secure generic tokens with metadata. This is a single use token with the user and the next url as metadata. We send an email with a link containing this token.
Step two — Activate the account¶
When the user clicks on the email, we get the user and next url from the token (which is part of the URL). If the token validates, we activate the account, add a success message using the Django message framework, and redirect to the next url.
Email templates and how to override them¶
You can override the following templates:
- cradmin_activate_account/email/subject.django.txt
- Override this to set the email subject.
- cradmin_activate_account/email/html_message.django.txt
- Override this to change the email message.
All of the email templates get the following context variables:
DJANGO_CRADMIN_SITENAME: The value of the setting with the same name.activate_url: The URL that users should click to activate their account.user: The user that is activating their account.
UI message templates and how to override them¶
You do not have to override the entire templates to adjust the text in the UI. We provide the following templates for you to override:
- cradmin_activate_account/messages/success.django.html
- The success message added to
django.contrib.messages.successwhen the account is successfully activated. The activated user is available as theusertemplate context variable. - cradmin_activate_account/messages/activation-link-invalid.django.html
- The message shown in the UI when the activation link is invalid.
- cradmin_activate_account/messages/activation-link-expired.django.html
- The message shown in the UI when the activation link is expired.