class FollowSerializer(serializers.ModelSerializer):
    follower = UserSerializer(read_only=True, many=True)
    following = UserSerializer(read_only=True, many=True)

    class Meta:
        model = Follow
        exclude = ('id', "user",)
Esempio n. 2
0
def get_user_for_profile(request, *args, **kwargs):
    if (request.method == "POST"):
        try:
            post_data = json.loads(request.body)
            username = post_data["username"]
            user = User.objects.filter(username=username)
            current_user = AuthToken.objects.filter(
                token=request.COOKIES.get("auth_token")).first().user

            if (len(user)):
                user = user.first()

                friend_request_from = False
                request_from = Friends.objects.filter(from_user=user,
                                                      to_user=current_user)
                friend_request_from = len(request_from) > 0

                friend_request_sent = False
                friends = Friends.objects.filter(from_user=current_user,
                                                 to_user=user)
                friend_request_sent = (len(friends) > 0)

                if (friend_request_sent):
                    status = friends.first().status
                elif (friend_request_from):
                    status = request_from.first().status

                blogs = Blog.objects.filter(user=user)
                serialized_user = UserSerializer(user, many=False)

                if (len(blogs)):
                    serialized_blogs = BlogSerializer(blogs, many=True)
                    return JsonResponse(
                        {
                            'msg': serialized_user.data,
                            'blogs': serialized_blogs.data,
                            "profile_pic":
                            settings.MEDIA_URL + user.profile_pic,
                            "currentUser": current_user.username,
                            "friendRequested": friend_request_sent,
                            'friendRequestFrom': friend_request_from,
                            'friendStatus': status
                        },
                        status=201)
                else:
                    return JsonResponse(
                        {
                            'msg': serialized_user.data,
                            "profile_pic":
                            settings.MEDIA_URL + user.profile_pic
                        },
                        status=201)
            else:
                return JsonResponse({'error': 'User not found!'}, status=400)
        except Exception as e:
            print(e)
            return JsonResponse({'msg': 'Error at Server!'}, status=500)
    else:
        return JsonResponse({'error': 'Invalid Method!'}, status=400)
Esempio n. 3
0
    def partial_update(self, request, pk=None):

        queryset = User.objects.all()
        user = get_object_or_404(queryset, pk=self.request.user.id)
        serializer = UserSerializer(user, data=request.data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 4
0
def get_user(request, *args, **kwargs):
    if (request.method == "POST"):
        try:
            auth_token = request.COOKIES.get('auth_token')
            user = AuthToken.objects.filter(token=auth_token).first().user
            serialized_user = UserSerializer(user, many=False)
            return JsonResponse(
                {
                    'msg': serialized_user.data,
                    "profile_pic": settings.MEDIA_URL + user.profile_pic
                },
                status=201)
        except Exception as e:
            print(e)
            return JsonResponse({'msg': 'Error at Server!'}, status=500)
    else:
        return JsonResponse({'error': 'Invalid Method!'}, status=400)
Esempio n. 5
0
 def get_serializer(self):
     return UserSerializer()
Esempio n. 6
0
 def list(self, request):
     serializer = UserSerializer(request.user)
     return Response(serializer.data)
Esempio n. 7
0
 def create(self, request):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Esempio n. 8
0
 def post(self, request, format=None):
     serializer = UserSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data)
class BlockSerializer(serializers.ModelSerializer):
    blocked = UserSerializer(read_only=True, many=True)
    class Meta:
        model = BlockList
        fields = ('blocked',)
class CommentSerializer(CustomModelSerializer):
    user = UserSerializer(read_only=True)

    class Meta:
        model = Comment
        fields = "__all__"
class LikePostSerializer(CustomModelSerializer):
    user = UserSerializer(read_only=True)

    class Meta:
        model = Like
        fields = ("user", )