Skip to content

Commit

Permalink
docs: add acount to annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanHHaeng committed Jun 3, 2024
1 parent b33b55c commit 722b51c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
25 changes: 25 additions & 0 deletions pure_plate/account/backend.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
from django.contrib.auth.backends import BaseBackend
from django.contrib.auth import get_user_model

# Retrieve the User model
User = get_user_model()

class EmailBackend(BaseBackend):
def authenticate(self, request, email=None, password=None, **kwargs):
"""
Authenticate a user based on email and password.
Args:
request: The HttpRequest object.
email: The email of the user trying to authenticate.
password: The password of the user trying to authenticate.
kwargs: Additional keyword arguments.
Returns:
The authenticated user object if authentication is successful, otherwise None.
"""
if email is None or password is None:
return None
try:
# Try to retrieve the user by email
user = User.objects.get(email=email)
# Check the password
if user.check_password(password):
return user
except User.DoesNotExist:
# Return None if the user does not exist
return None

def get_user(self, user_id):
"""
Retrieve a user by their ID.
Args:
user_id: The ID of the user to retrieve.
Returns:
The User object if found, otherwise None.
"""
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
Expand Down
6 changes: 6 additions & 0 deletions pure_plate/account/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

class UserManager(BaseUserManager):
def create_user(self, username, nickname, password=None):
"""
Create and return a regular user.
"""
if not username:
raise ValueError('Users must have a username')
user = self.model(username=username, nickname=nickname)
Expand All @@ -11,6 +14,9 @@ def create_user(self, username, nickname, password=None):
return user

def create_superuser(self, username, nickname, password):
"""
Create and return a superuser.
"""
user = self.create_user(username, nickname, password)
user.is_admin = True
user.save(using=self._db)
Expand Down
13 changes: 11 additions & 2 deletions pure_plate/account/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
User = get_user_model()

class UserSerializer(serializers.ModelSerializer):
"""
Serializer for the User model.
"""
class Meta:
model = User
fields = ('id', 'username', 'nickname', 'password')
extra_kwargs = {'password': {'write_only': True}}
extra_kwargs = {'password': {'write_only': True}} # Ensure password is write-only

def create(self, validated_data):
"""
Create and return a new user instance, given the validated data.
"""
user = User.objects.create_user(
username=validated_data['username'],
nickname=validated_data['nickname'],
Expand All @@ -18,5 +24,8 @@ def create(self, validated_data):
return user

class LoginSerializer(serializers.Serializer):
"""
Serializer for user login.
"""
username = serializers.CharField()
password = serializers.CharField(write_only=True)
password = serializers.CharField(write_only=True) # Ensure password is write-only
13 changes: 10 additions & 3 deletions pure_plate/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@
from rest_framework.permissions import AllowAny

class RegisterView(CreateAPIView):
"""
API view to handle user registration.
"""
queryset = User.objects.all()
serializer_class = UserSerializer


class CustomAuthToken(ObtainAuthToken):
"""
API view to handle user login and token generation.
"""
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
Expand All @@ -33,9 +38,11 @@ def post(self, request, *args, **kwargs):
})
return Response({'error': 'Invalid Credentials'}, status=status.HTTP_401_UNAUTHORIZED)


@permission_classes([AllowAny])
class LogoutView(APIView):
"""
API view to handle user logout by deleting the authentication token.
"""
permission_classes = [IsAuthenticated]

def post(self, request):
Expand All @@ -44,4 +51,4 @@ def post(self, request):
token.delete()
return Response(status=status.HTTP_200_OK)
except Token.DoesNotExist:
return Response(status=status.HTTP_400_BAD_REQUEST)
return Response(status=status.HTTP_400_BAD_REQUEST)

0 comments on commit 722b51c

Please sign in to comment.