Debian Codegraph @

docs/account/advanced.rst

raw
1 Advanced Usage
2 ==============
3
4 Custom User Models
5 ------------------
6
7 If you use a custom user model you need to specify what field
8 represents the ``username``, if any. Here, ``username`` really refers to
9 the field representing the nickname that the user uses to login, and not to
10 some unique identifier (possibly including an email address) as is
11 the case for Django's ``AbstractBaseUser.USERNAME_FIELD``.
12
13 Therefore, if your custom user model does not have a ``username`` field (again,
14 not to be mistaken with an email address or user id), you will need to set
15 ``ACCOUNT_USER_MODEL_USERNAME_FIELD`` to ``None``. This will disable username
16 related functionality in ``allauth``. Remember to also remove username from
17 ``ACCOUNT_SIGNUP_FIELDS`` as by default that is present.
18
19 Similarly, you will need to set ``ACCOUNT_USER_MODEL_EMAIL_FIELD`` to
20 ``None`` or to the proper field (if other than ``email``).
21
22 For example, if you want to use a custom user model that has ``email``
23 as the identifying field, and you don't want to collect usernames, you
24 need the following in your settings.py::
25
26 ACCOUNT_USER_MODEL_USERNAME_FIELD = None
27 ACCOUNT_SIGNUP_FIELDS = ['email*', 'password1*', 'password2*']
28 ACCOUNT_LOGIN_METHODS = {'email'}
29
30
31 Creating and Populating User instances
32 --------------------------------------
33
34 The following adapter methods can be used to intervene in how User
35 instances are created and populated with data
36
37 - ``allauth.account.adapter.DefaultAccountAdapter``:
38
39 - ``is_open_for_signup(self, request)``: The default function
40 returns ``True``. You can override this method by returning ``False``
41 if you want to disable account signup.
42
43 - ``new_user(self, request)``: Instantiates a new, empty ``User``.
44
45 - ``save_user(self, request, user, form)``: Populates and saves the
46 ``User`` instance using information provided in the signup form.
47
48 - ``populate_username(self, request, user)``:
49 Fills in a valid username, if required and missing. If the
50 username is already present, then it is assumed to be valid (unique).
51
52 - ``confirm_email(self, request, email_address)``: Marks the email address as
53 confirmed and saves to the db.
54
55 - ``generate_unique_username(self, txts, regex=None)``: Returns a unique username
56 from the combination of strings present in txts iterable. A regex pattern
57 can be passed to the method to make sure the generated username matches it.
58
59
60 Invitations
61 -----------
62
63 Invitation handling is not supported, and most likely will not be any
64 time soon. An invitation app could cover anything ranging from
65 invitations of new users, to invitations of existing users to
66 participate in restricted parts of the site. All in all, the scope of
67 invitation handling is large enough to warrant being addressed in an
68 app of its own.
69
70 Still, everything is in place to easily hook up any third party
71 invitation app. The account adapter
72 (``allauth.account.adapter.DefaultAccountAdapter``) offers the following
73 methods:
74
75 - ``is_open_for_signup(self, request)``. You can override this method to, for
76 example, inspect the session to check if an invitation was accepted.
77
78 - ``stash_verified_email(self, request, email)``. If an invitation was
79 accepted by following a link in an email, then there is no need to
80 send email verification mails after the signup is completed. Use
81 this method to record the fact that an email address was verified.
82
83
84 Custom Redirects
85 ----------------
86
87 If redirecting to statically configurable URLs (as specified in your
88 project settings) is not flexible enough, then you can override the
89 following adapter methods:
90
91 - ``allauth.account.adapter.DefaultAccountAdapter``:
92
93 - ``get_login_redirect_url(self, request)``
94
95 - ``get_logout_redirect_url(self, request)``
96
97 - ``get_email_verification_redirect_url(self, email_address)``
98
99 - ``get_signup_redirect_url(self, request)``
100
101 For example, redirecting to ``/accounts/<username>/`` can be implemented as
102 follows::
103
104 # project/settings.py:
105 ACCOUNT_ADAPTER = 'project.users.adapter.MyAccountAdapter'
106
107 # project/users/adapter.py:
108 from django.conf import settings
109 from allauth.account.adapter import DefaultAccountAdapter
110
111 class MyAccountAdapter(DefaultAccountAdapter):
112
113 def get_login_redirect_url(self, request):
114 path = "/accounts/{username}/"
115 return path.format(username=request.user.username)