Esempio n. 1
0
def custom_exception_handler(exc, context):  # type: ignore
    """
    Custom exception handler for rest api views
    """

    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc, context)

    if isinstance(exc, ValidationError):
        return Response(
            response_wrapper(data=response.data, success=False),
            status=response.status_code,
        )

    if isinstance(exc, NotAuthenticated):
        return Response(
            response_wrapper(data=response.data, success=False),
            status=response.status_code,
        )

    if (isinstance(exc, ObjectDoesNotExist) or isinstance(exc, Http404)
            or isinstance(exc, NotFound)):
        return Response(
            response_wrapper(data={"error": "Not found."}, success=False),
            status=status.HTTP_404_NOT_FOUND,
        )

    if response is not None:
        return response
Esempio n. 2
0
    def post(self, request, friend_id):
        friend = Friend.objects.filter(user=request.user, friend_id=friend_id)
        if friend.exists() or request.user.id == friend_id:
            return Response(
                response_wrapper(data={"detail": "Friend already exists"},
                                 success=False),
                status=status.HTTP_400_BAD_REQUEST,
            )

        try:
            friend = Friend.objects.get(user=friend_id, friend_id=request.user)
        except ObjectDoesNotExist:
            friend = Friend.objects.create(user=request.user,
                                           friend_id=friend_id)
        else:
            friend.accepted = True
            friend.accepted_date = timezone.now()
            friend.save()

        serialiser = FriendModelSerializer(friend)

        return Response(
            response_wrapper(data=serialiser.data, success=True),
            status=status.HTTP_201_CREATED,
        )
Esempio n. 3
0
    def token_verify(self, request):
        token_refresh = TokenSerialiser(data=request.data)
        token_refresh.is_valid(raise_exception=True)

        token = token_refresh.data["token"]

        return Response(response_wrapper(data={"token": token}, success=True))
Esempio n. 4
0
 def signup(self, request):
     serializer = UserSignUpSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user = serializer.save()
     data = UserModelSerializer(user).data
     return Response(
         response_wrapper(data=data, success=True),
         status=status.HTTP_201_CREATED,
     )
Esempio n. 5
0
    def post(self, request):
        data = request.data
        data["user"] = request.user.id

        serialiser = TasksModelSerializer(data=data)
        serialiser.is_valid(raise_exception=True)
        serialiser.save()
        return Response(
            response_wrapper(data=serialiser.data, success=True),
            status=status.HTTP_201_CREATED,
        )
Esempio n. 6
0
    def delete(self, request, friend_id):
        try:
            friend = Friend.objects.get(user=request.user, friend_id=friend_id)
            friend.delete()
        except ObjectDoesNotExist:
            friend = Friend.objects.get(user=friend_id, friend_id=request.user)
            friend.delete()

        return Response(
            response_wrapper(data=None, success=True),
            status=status.HTTP_200_OK,
        )
Esempio n. 7
0
    def profile(self, request, *args, **kwargs):
        user = self.get_object()

        profile = user.profile
        serializer = ProfileModelSerializer(
            profile, data=request.data, partial=True
        )
        serializer.is_valid(raise_exception=True)
        serializer.save()

        data = UserModelSerializer(user).data
        return Response(response_wrapper(data=data, success=True))
Esempio n. 8
0
 def get(self, request):
     tasks = Task.objects.filter(user=request.user)
     serialiser = TasksModelSerializer(tasks, many=True)
     return Response(response_wrapper(data=serialiser.data, success=True))
Esempio n. 9
0
 def get(self, request):
     friends = Friend.objects.filter(
         Q(user=request.user) | Q(friend=request.user)).distinct()
     serialiser = FriendModelSerializer(friends, many=True)
     return Response(response_wrapper(data=serialiser.data, success=True))
Esempio n. 10
0
 def retrieve(self, request, pk) -> Response:
     user = self.get_object()
     serialiser = UserModelSerializer(user)
     return Response(response_wrapper(data=serialiser.data, success=True))
Esempio n. 11
0
 def update(self, request, *args, **kwargs):
     response = super(UserViewSet, self).update(request, *args, **kwargs)
     data = UserModelSerializer(response.data).data
     return Response(response_wrapper(data=data, success=True))
Esempio n. 12
0
    def login(self, request):
        token_login = UserLoginSerializer(data=request.data)
        token_login.is_valid(raise_exception=True)
        token = token_login.save()

        return Response(response_wrapper(data={"token": token}, success=True))