from django import forms
from django.contrib.auth import get_user_model
from django.core.mail import EmailMultiAlternatives
from django.contrib.sites.shortcuts import get_current_site
from django.urls import reverse_lazy

from logintokens.tokens import default_token_generator


USER = get_user_model()


class TokenLoginForm(forms.Form):
    email = forms.EmailField(label="Email", max_length=254)

    def generate_login_link(self, email, request):
        protocol = 'https' if request.is_secure() else 'http'
        domain = get_current_site(request).domain
        url = reverse_lazy('token_login')
        token = default_token_generator.make_token(email)
        return '{}://{}{}?token={}'.format(protocol, domain, url, token)

    def save(self, request):
        """Generate a login token and send it to the email from the form.

        """
        email = self.cleaned_data['email']
        body = 'To complete the login process, simply click on this link: {}'
        login_link = self.generate_login_link(email, request)

        email_message = EmailMultiAlternatives(
            'Your login link for ANIAuth',
            body.format(login_link),
            to=[email]
        )
        email_message.send()

from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UsernameField
from django.core.mail import EmailMultiAlternatives
from django.contrib.sites.shortcuts import get_current_site
from django.urls import reverse_lazy

from logintokens.tokens import default_token_generator


USER = get_user_model()


class TokenLoginForm(forms.Form):
    email = UsernameField(
        max_length=254,
        widget=forms.TextInput(attrs={'autofocus': True}),
    )

    def generate_login_link(self, username, request):
        protocol = 'https' if request.is_secure() else 'http'
        domain = get_current_site(request).domain
        url = reverse_lazy('token_login')
        token = default_token_generator.make_token(username)
        return '{}://{}{}?token={}'.format(protocol, domain, url, token)

    def save(self, request):
        """Generate a login token and send it to the email from the form.

        """
        username = self.cleaned_data['email']
        try:
            user = USER._default_manager.get_by_natural_key(username)
            email = getattr(user, USER.EMAIL_FIELD)
        except USER.DoesNotExist:
            email = username
        body = 'To complete the login process, simply click on this link: {}'
        login_link = self.generate_login_link(username, request)

        email_message = EmailMultiAlternatives(
            'Your login link for ANIAuth',
            body.format(login_link),
            to=[email]
        )
        email_message.send()

Update form to pass new test
Update form to pass new test

  from django import forms
  from django.contrib.auth import get_user_model
+ from django.contrib.auth.forms import UsernameField
  from django.core.mail import EmailMultiAlternatives
  from django.contrib.sites.shortcuts import get_current_site
  from django.urls import reverse_lazy
  
  from logintokens.tokens import default_token_generator
  
  
  USER = get_user_model()
  
  
  class TokenLoginForm(forms.Form):
-     email = forms.EmailField(label="Email", max_length=254)
+     email = UsernameField(
+         max_length=254,
+         widget=forms.TextInput(attrs={'autofocus': True}),
+     )
  
-     def generate_login_link(self, email, request):
+     def generate_login_link(self, username, request):
          protocol = 'https' if request.is_secure() else 'http'
          domain = get_current_site(request).domain
          url = reverse_lazy('token_login')
-         token = default_token_generator.make_token(email)
+         token = default_token_generator.make_token(username)
          return '{}://{}{}?token={}'.format(protocol, domain, url, token)
  
      def save(self, request):
          """Generate a login token and send it to the email from the form.
  
          """
-         email = self.cleaned_data['email']
+         username = self.cleaned_data['email']
+         try:
+             user = USER._default_manager.get_by_natural_key(username)
+             email = getattr(user, USER.EMAIL_FIELD)
+         except USER.DoesNotExist:
+             email = username
          body = 'To complete the login process, simply click on this link: {}'
-         login_link = self.generate_login_link(email, request)
+         login_link = self.generate_login_link(username, request)
  
          email_message = EmailMultiAlternatives(
              'Your login link for ANIAuth',
              body.format(login_link),
              to=[email]
          )
          email_message.send()
  
Update form to pass new test
## Code Before:
from django import forms
from django.contrib.auth import get_user_model
from django.core.mail import EmailMultiAlternatives
from django.contrib.sites.shortcuts import get_current_site
from django.urls import reverse_lazy

from logintokens.tokens import default_token_generator


USER = get_user_model()


class TokenLoginForm(forms.Form):
    email = forms.EmailField(label="Email", max_length=254)

    def generate_login_link(self, email, request):
        protocol = 'https' if request.is_secure() else 'http'
        domain = get_current_site(request).domain
        url = reverse_lazy('token_login')
        token = default_token_generator.make_token(email)
        return '{}://{}{}?token={}'.format(protocol, domain, url, token)

    def save(self, request):
        """Generate a login token and send it to the email from the form.

        """
        email = self.cleaned_data['email']
        body = 'To complete the login process, simply click on this link: {}'
        login_link = self.generate_login_link(email, request)

        email_message = EmailMultiAlternatives(
            'Your login link for ANIAuth',
            body.format(login_link),
            to=[email]
        )
        email_message.send()

## Instruction:
Update form to pass new test
## Code After:
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UsernameField
from django.core.mail import EmailMultiAlternatives
from django.contrib.sites.shortcuts import get_current_site
from django.urls import reverse_lazy

from logintokens.tokens import default_token_generator


USER = get_user_model()


class TokenLoginForm(forms.Form):
    email = UsernameField(
        max_length=254,
        widget=forms.TextInput(attrs={'autofocus': True}),
    )

    def generate_login_link(self, username, request):
        protocol = 'https' if request.is_secure() else 'http'
        domain = get_current_site(request).domain
        url = reverse_lazy('token_login')
        token = default_token_generator.make_token(username)
        return '{}://{}{}?token={}'.format(protocol, domain, url, token)

    def save(self, request):
        """Generate a login token and send it to the email from the form.

        """
        username = self.cleaned_data['email']
        try:
            user = USER._default_manager.get_by_natural_key(username)
            email = getattr(user, USER.EMAIL_FIELD)
        except USER.DoesNotExist:
            email = username
        body = 'To complete the login process, simply click on this link: {}'
        login_link = self.generate_login_link(username, request)

        email_message = EmailMultiAlternatives(
            'Your login link for ANIAuth',
            body.format(login_link),
            to=[email]
        )
        email_message.send()
