Пример #1
0
    def test_current_user_display(self):
        """
            Tests the current user display view, including profile and user info
            !!!Can be challending, since overwrites the rest-auth user url
        """
        resp = self.client.get(URL_CURRENT_USER_DISPLAY)
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        # Only get request is accepted
        allow_methods = resp._headers.get('allow')[1]
        self.assertEqual(allow_methods, 'GET, HEAD, OPTIONS')

        serializer = UserDisplaySerializer(self.user, data=resp.data)
        self.assertTrue(serializer.is_valid())
Пример #2
0
class TweetModelSerializer(serializers.ModelSerializer):
    user = UserDisplaySerializer(read_only=True)
    time_since = serializers.SerializerMethodField()
    url = serializers.SerializerMethodField()

    class Meta:
        model = Tweet
        fields = ['user', 'content', 'time_since', 'url']

    def get_time_since(self, tweet):
        return timesince(tweet.created_at)

    def get_url(self, tweet):
        return tweet.get_absolute_url()
Пример #3
0
class TweetModelSerializer(serializers.ModelSerializer):
    user = UserDisplaySerializer(
        read_only=True
    )  # read_only=True means that the request doesn't have to
    # provide this field. So the create procedure can go all the way to CreateModelMixin.perform_create()

    time_since = serializers.SerializerMethodField()
    url = serializers.SerializerMethodField()

    class Meta:
        model = Tweet
        fields = ['user', 'content', 'time_since', 'url']

    def get_time_since(self, tweet):
        return timesince(tweet.created_at)

    def get_url(self, tweet):
        return tweet.get_absolute_url()
Пример #4
0
 def get(self, request):
     serializer = UserDisplaySerializer(request.user)
     return Response(serializer.data)
Пример #5
0
 def get(self, request, pk):
     instance = get_object_or_404(CustomUser, id=pk)
     serializer = UserDisplaySerializer(instance)
     return Response(serializer.data)
Пример #6
0
 def get(self, request):
     # handler method
     serializer = UserDisplaySerializer(request.user)
     return Response(serializer.data)