Skip to content

Commit

Permalink
Refactor serializers to include read-only fields and remove unnecessa…
Browse files Browse the repository at this point in the history
…ry fields
  • Loading branch information
yunho7687 committed Jul 30, 2024
1 parent 38b7526 commit 8035565
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
8 changes: 1 addition & 7 deletions server/app/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ class Meta:
class RegistrationSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = (
"user_id",
"email",
"password",
"username",
"bio",
)
fields = ("user_id", "email", "password", "username", "bio", "avatar_url")
extra_kwargs = {
"password": {"write_only": True},
"user_id": {"read_only": True},
Expand Down
43 changes: 43 additions & 0 deletions server/app/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .serializers import BidsSerializer
from rest_framework import viewsets, status
from rest_framework.decorators import action

# from rest_framework import permissions

Expand All @@ -8,6 +9,9 @@
from rest_framework.permissions import AllowAny
from .models import Tasks, Bids, Users, TaskLocation

from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

# from rest_framework.parsers import MultiPartParser, FormParser
from .serializers import (
RegistrationSerializer,
Expand All @@ -24,9 +28,48 @@
class UserViewSet(viewsets.ModelViewSet):
queryset = Users.objects.all()
serializer_class = UsersSerializer

# permission_classes = [IsCurrentUser]
permission_classes = [AllowAny]

@swagger_auto_schema(
manual_parameters=[
openapi.Parameter(
"email",
openapi.IN_QUERY,
description="Email to check for uniqueness",
type=openapi.TYPE_STRING,
required=True,
),
],
responses={
200: openapi.Response(
description="Email uniqueness check",
examples={"application/json": {"is_unique": True}},
),
400: openapi.Response(
description="Invalid input",
examples={"application/json": {"error": "Email parameter is missing"}},
),
},
)
@action(
detail=False,
methods=["get"],
permission_classes=[AllowAny],
url_path="check-email",
)
def check_email_unique(self, request):
email = request.query_params.get("email", None)
if email is None:
return Response(
{"error": "Email parameter is missing"},
status=status.HTTP_400_BAD_REQUEST,
)

is_unique = not Users.objects.filter(email=email).exists()
return Response({"is_unique": is_unique})

# def get_queryset(self):
# if self.request.user.is_superuser:
# return Users.objects.all()
Expand Down

0 comments on commit 8035565

Please sign in to comment.