Skip to content

Commit 3ac7421

Browse files
authored
Upgrade django-allauth (#5012)
* Upgrade django-allauth * Update username signup
1 parent 3a32c3d commit 3ac7421

File tree

6 files changed

+45
-27
lines changed

6 files changed

+45
-27
lines changed

bims/forms/sign_up.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ class CustomSignupForm(SignupForm):
2323
queryset=Role.objects.all().order_by('order'),
2424
required=True
2525
)
26-
username = forms.CharField(
27-
widget=forms.HiddenInput(),
28-
required=False)
2926

3027
def __init__(self, *args, **kwargs):
3128
super(CustomSignupForm, self).__init__(*args, **kwargs)
@@ -35,35 +32,49 @@ def __init__(self, *args, **kwargs):
3532
self.fields['role'].initial = first_role
3633
except Role.DoesNotExist:
3734
pass
35+
# For django-allauth 65.x: properly configure username field
36+
if 'username' in self.fields:
37+
self.fields['username'].required = False
38+
self.fields['username'].widget = forms.HiddenInput()
3839

39-
def clean_username(self):
40-
username = self.cleaned_data.get('username')
41-
if not username:
42-
new_username = '{first_name}_{last_name}'.format(
43-
first_name=self.data.get('first_name').lower(),
44-
last_name=self.data.get('last_name').lower()
45-
)
46-
if Profile.objects.filter(user__username=new_username).exists():
40+
def clean(self):
41+
"""Override clean to generate username before form validation completes."""
42+
cleaned_data = super().clean()
43+
44+
# Generate username from first_name and last_name if not provided
45+
if not cleaned_data.get('username'):
46+
first_name = cleaned_data.get('first_name', '').lower()
47+
last_name = cleaned_data.get('last_name', '').lower()
48+
49+
if first_name and last_name:
50+
base_username = f'{first_name}_{last_name}'
51+
username = base_username
52+
53+
# Ensure uniqueness
54+
from django.contrib.auth import get_user_model
55+
User = get_user_model()
4756
counter = 1
48-
unique_username = new_username
49-
while Profile.objects.filter(user__username=unique_username).exists():
50-
unique_username = f'{new_username}_{counter}'
57+
while User.objects.filter(username=username).exists():
58+
username = f'{base_username}_{counter}'
5159
counter += 1
52-
return unique_username
53-
return new_username
54-
return username
60+
61+
cleaned_data['username'] = username
62+
63+
return cleaned_data
5564

5665
def custom_signup(self, request, user):
66+
"""Called after the user is created to set additional fields."""
5767
user.first_name = self.cleaned_data['first_name']
5868
user.last_name = self.cleaned_data['last_name']
5969
user.organization = self.cleaned_data['organization']
60-
user.username = self.cleaned_data.get('username', user.email)
70+
# Username is already set from clean() method
6171
user.save()
62-
bims_profile, created = Profile.objects.get_or_create(
63-
user=user
64-
)
72+
73+
# Create or get the BIMS profile
74+
bims_profile, created = Profile.objects.get_or_create(user=user)
6575
bims_profile.role = self.cleaned_data['role']
6676
bims_profile.save()
77+
6778
return user
6879

6980

bims/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ def login_redirect(request):
228228
name='add-source-reference'),
229229
re_path(r'^summary-report/$', SummaryReportView.as_view(),
230230
name='summary-report'),
231-
re_path(r'^profile/(?P<slug>\w+)/$', ProfileView.as_view(),
231+
# Updated pattern to accept email addresses as usernames (after allauth upgrade)
232+
re_path(r'^profile/(?P<slug>[^/]+)/$', ProfileView.as_view(),
232233
name='profile'),
233234
re_path(r'^download-request/$', DownloadRequestListView.as_view(),
234235
name='download-request'),

core/settings/contrib.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,11 @@
536536
'signup': 'bims.forms.CustomSignupForm',
537537
}
538538
ACCOUNT_UNIQUE_EMAIL = True
539-
ACCOUNT_USERNAME_REQUIRED = True
540-
ACCOUNT_EMAIL_REQUIRED = True
541-
ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
539+
# For django-allauth 65.x: Updated to new settings format
540+
# No username field in signup form - it's auto-generated from first_name and last_name
541+
ACCOUNT_SIGNUP_FIELDS = ['email*', 'password1*', 'password2*']
542+
# Replaced ACCOUNT_AUTHENTICATION_METHOD = 'username_email' with:
543+
ACCOUNT_LOGIN_METHODS = {'email', 'username'}
542544
ACCOUNT_AUTHENTICATED_LOGIN_REDIRECTS = False
543545
ACCOUNT_LOGOUT_REDIRECT_URL = '/'
544546

deployment/docker/REQUIREMENTS-dev.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ wrapt==1.16.0
77
# pdb plus plus
88
pdbpp
99
django-debug-toolbar==4.4.2
10+
debugpy>=1.8.0
11+
12+
# Auto-reload for celery worker
13+
watchdog>=3.0.0

deployment/docker/REQUIREMENTS.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ requests>=2.23.0
1010
pytz
1111
# support special characters
1212

13-
django-allauth==0.63.3
13+
django-allauth==65.13.0
1414

1515
# Django Easy Audit
1616
# https://github.com/soynatan/django-easy-audit

deployment/docker/temp/REQUIREMENTS.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ requests>=2.23.0
1010
pytz
1111
# support special characters
1212

13-
django-allauth==0.63.3
13+
django-allauth==65.13.0
1414

1515
# Django Easy Audit
1616
# https://github.com/soynatan/django-easy-audit

0 commit comments

Comments
 (0)