Skip to content

Commit

Permalink
Added API View to check availability of username (#1043)
Browse files Browse the repository at this point in the history
* Added checkusername api view

* Added changes as requested

* Added changes as requested
  • Loading branch information
siddnikh authored Oct 19, 2022
1 parent 2c7d785 commit fa7b33e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 16 additions & 2 deletions care/users/api/viewsets/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from rest_framework import mixins, status
from rest_framework.authtoken.models import Token
from rest_framework.decorators import action
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated
from rest_framework.generics import get_object_or_404, GenericAPIView
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.response import Response
from rest_framework.serializers import ValidationError
from rest_framework.viewsets import GenericViewSet
Expand Down Expand Up @@ -286,3 +286,17 @@ def pnconfig(self, request, *args, **kwargs):
setattr(user, field, request.data[field])
user.save()
return Response(status=status.HTTP_200_OK)

class CheckUsernameAPIView(GenericAPIView):
"""
Checks availability of username by getting as query, returns 200 if available, and 409 otherwise.
"""

permission_classes = (IsAuthenticated, )

def get(self, request):
username = request.query_params.get('username')
user = User.objects.filter(username=username)
if user.exists():
return Response(status=status.HTTP_409_CONFLICT)
return Response(status=status.HTTP_200_OK)
3 changes: 2 additions & 1 deletion config/api_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
WardViewSet,
)
from care.users.api.viewsets.skill import SkillViewSet
from care.users.api.viewsets.users import UserViewSet
from care.users.api.viewsets.users import UserViewSet, CheckUsernameAPIView
from care.users.api.viewsets.userskill import UserSkillViewSet

if settings.DEBUG:
Expand All @@ -88,6 +88,7 @@
user_nested_rotuer = NestedSimpleRouter(router, r"users", lookup="users")
user_nested_rotuer.register("skill", UserSkillViewSet)

router.register("checkusername", CheckUsernameAPIView)
router.register("skill", SkillViewSet)

router.register("facility", FacilityViewSet)
Expand Down

0 comments on commit fa7b33e

Please sign in to comment.