from django.contrib.auth.models import User

from onetime.models import Key

class OneTimeBackend:
    def authenticate(self, key):
        data = Key.objects.get(key=key)
        if data is None:
            None

        if not data.is_valid():
            return None

        return data.user

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None
        

from django.contrib.auth.models import User

from onetime.models import Key

class OneTimeBackend:
    def authenticate(self, key):
        data = Key.objects.filter(key=key)
        if len(data) == 0:
            return None

        data = data[0]
        if not data.is_valid():
            return None

        return data.user

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None
        

Use filter() instead of get() since the key might be invalid
Use filter() instead of get() since the key might be invalid

  from django.contrib.auth.models import User
  
  from onetime.models import Key
  
  class OneTimeBackend:
      def authenticate(self, key):
-         data = Key.objects.get(key=key)
+         data = Key.objects.filter(key=key)
-         if data is None:
+         if len(data) == 0:
-             None
+             return None
  
+         data = data[0]
          if not data.is_valid():
              return None
  
          return data.user
  
      def get_user(self, user_id):
          try:
              return User.objects.get(pk=user_id)
          except User.DoesNotExist:
              return None
          
  
Use filter() instead of get() since the key might be invalid
## Code Before:
from django.contrib.auth.models import User

from onetime.models import Key

class OneTimeBackend:
    def authenticate(self, key):
        data = Key.objects.get(key=key)
        if data is None:
            None

        if not data.is_valid():
            return None

        return data.user

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None
        

## Instruction:
Use filter() instead of get() since the key might be invalid
## Code After:
from django.contrib.auth.models import User

from onetime.models import Key

class OneTimeBackend:
    def authenticate(self, key):
        data = Key.objects.filter(key=key)
        if len(data) == 0:
            return None

        data = data[0]
        if not data.is_valid():
            return None

        return data.user

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None
        
