Skip to content

Commit

Permalink
Move allauth forms to adapter.py (#1145)
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenklar committed Sep 9, 2024
1 parent a9772a0 commit 8b8991d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 39 deletions.
37 changes: 37 additions & 0 deletions rdmo/accounts/adapter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from django.conf import settings
from django.contrib.auth.models import Group
from django.forms import BooleanField

from allauth.account.adapter import DefaultAccountAdapter
from allauth.account.forms import LoginForm as AllauthLoginForm
from allauth.account.forms import SignupForm as AllauthSignupForm
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter

from .forms import ProfileForm
from .models import ConsentFieldValue


class AccountAdapter(DefaultAccountAdapter):

Expand Down Expand Up @@ -33,3 +39,34 @@ def save_user(self, request, sociallogin, form=None):
user.groups.set(groups)

return user


class LoginForm(AllauthLoginForm):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# remove forget password link introduced with allauth 0.57.0
password_field = self.fields.get('password')
if password_field:
password_field.help_text = None


class SignupForm(AllauthSignupForm, ProfileForm):

use_required_attribute = False

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# add a consent field, the label is added in the template
if settings.ACCOUNT_TERMS_OF_USE:
self.fields['consent'] = BooleanField(required=True)

def signup(self, request, user):
self._save_additional_values(user)

# store the consent field
if settings.ACCOUNT_TERMS_OF_USE:
consent = ConsentFieldValue(user=user, consent=self.cleaned_data['consent'])
consent.save()
36 changes: 1 addition & 35 deletions rdmo/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
from django.contrib.auth import get_user_model
from django.utils.translation import gettext_lazy as _

from allauth.account.forms import LoginForm as AllauthLoginForm
from allauth.account.forms import SignupForm as AllauthSignupForm

from .models import AdditionalField, AdditionalFieldValue, ConsentFieldValue
from .models import AdditionalField, AdditionalFieldValue

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -71,37 +68,6 @@ def _save_additional_values(self, user=None):
additional_value.save()


class LoginForm(AllauthLoginForm):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# remove forget password link introduced with allauth 0.57.0
password_field = self.fields.get('password')
if password_field:
password_field.help_text = None


class SignupForm(AllauthSignupForm, ProfileForm):

use_required_attribute = False

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# add a consent field, the label is added in the template
if settings.ACCOUNT_TERMS_OF_USE:
self.fields['consent'] = forms.BooleanField(required=True)

def signup(self, request, user):
self._save_additional_values(user)

# store the consent field
if settings.ACCOUNT_TERMS_OF_USE:
consent = ConsentFieldValue(user=user, consent=self.cleaned_data['consent'])
consent.save()


class RemoveForm(forms.Form):

def __init__(self, *args, **kwargs):
Expand Down
5 changes: 2 additions & 3 deletions rdmo/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@
ACCOUNT_TERMS_OF_USE = False
ACCOUNT_ADAPTER = 'rdmo.accounts.adapter.AccountAdapter'
ACCOUNT_FORMS = {
'login': 'rdmo.accounts.forms.LoginForm',
'signup': 'rdmo.accounts.forms.SignupForm'

'login': 'rdmo.accounts.adapter.LoginForm',
'signup': 'rdmo.accounts.adapter.SignupForm'
}
ACCOUNT_USER_DISPLAY = 'rdmo.accounts.utils.get_full_name'
ACCOUNT_EMAIL_REQUIRED = True
Expand Down
2 changes: 1 addition & 1 deletion rdmo/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def home(request):
else:
if settings.LOGIN_FORM:
if settings.ACCOUNT or settings.SOCIALACCOUNT:
from rdmo.accounts.forms import LoginForm
from rdmo.accounts.adapter import LoginForm
return render(request, 'core/home.html', {
'form': LoginForm(),
'signup_url': reverse("account_signup")
Expand Down

0 comments on commit 8b8991d

Please sign in to comment.