Add is_developer_reply property to ratings API responses (#9099)
Add is_developer_reply property to ratings API responses
This commit is contained in:
Родитель
33c91e38aa
Коммит
c75f9b855a
|
@ -223,3 +223,4 @@ v4 API changelog
|
|||
https://github.com/mozilla/addons-server/issues/8794
|
||||
* 2018-07-18: added ``previews`` property to discovery API ``addons`` object. This change was also backported to the `v3` API. https://github.com/mozilla/addons-server/issues/8863
|
||||
* 2018-07-20: dropped ``downloads`` property from the collection add-ons results. https://github.com/mozilla/addons-server/issues/8944
|
||||
* 2018-08-16: added ``is_developer_reply`` property to ratings. This changed was also backported to the `v3` API. https://github.com/mozilla/addons-server/issues/8993
|
||||
|
|
|
@ -19,14 +19,15 @@ from olympia.versions.models import Version
|
|||
class BaseRatingSerializer(serializers.ModelSerializer):
|
||||
addon = serializers.SerializerMethodField()
|
||||
body = serializers.CharField(allow_null=True, required=False)
|
||||
is_developer_reply = serializers.SerializerMethodField()
|
||||
is_latest = serializers.BooleanField(read_only=True)
|
||||
previous_count = serializers.IntegerField(read_only=True)
|
||||
user = BaseUserSerializer(read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = Rating
|
||||
fields = ('id', 'addon', 'body', 'created', 'is_latest',
|
||||
'previous_count', 'user')
|
||||
fields = ('id', 'addon', 'body', 'created', 'is_developer_reply',
|
||||
'is_latest', 'previous_count', 'user')
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(BaseRatingSerializer, self).__init__(*args, **kwargs)
|
||||
|
@ -44,6 +45,9 @@ class BaseRatingSerializer(serializers.ModelSerializer):
|
|||
'slug': addon.slug
|
||||
}
|
||||
|
||||
def get_is_developer_reply(self, obj):
|
||||
return obj.reply_to_id is not None
|
||||
|
||||
def validate(self, data):
|
||||
data = super(BaseRatingSerializer, self).validate(data)
|
||||
request = self.context['request']
|
||||
|
|
|
@ -43,6 +43,7 @@ class TestBaseRatingSerializer(TestCase):
|
|||
assert result['created'] == (
|
||||
self.rating.created.replace(microsecond=0).isoformat() + 'Z')
|
||||
assert result['previous_count'] == int(self.rating.previous_count)
|
||||
assert result['is_developer_reply'] is False
|
||||
assert result['is_latest'] == self.rating.is_latest
|
||||
assert result['score'] == int(self.rating.rating)
|
||||
assert result['reply'] is None
|
||||
|
@ -142,6 +143,20 @@ class TestBaseRatingSerializer(TestCase):
|
|||
assert result['is_latest'] is False
|
||||
|
||||
def test_with_reply(self):
|
||||
def _test_reply(data):
|
||||
assert data['id'] == reply.pk
|
||||
assert data['body'] == unicode(reply.body)
|
||||
assert data['created'] == (
|
||||
reply.created.replace(microsecond=0).isoformat() + 'Z')
|
||||
assert data['is_developer_reply'] is True
|
||||
assert data['user'] == {
|
||||
'id': reply_user.pk,
|
||||
'name': unicode(reply_user.name),
|
||||
# should be the profile for a developer
|
||||
'url': absolutify(reply_user.get_url_path()),
|
||||
'username': reply_user.username,
|
||||
}
|
||||
|
||||
reply_user = user_factory()
|
||||
addon = addon_factory(users=[reply_user])
|
||||
self.rating = Rating.objects.create(
|
||||
|
@ -154,19 +169,17 @@ class TestBaseRatingSerializer(TestCase):
|
|||
result = self.serialize()
|
||||
|
||||
assert result['reply']
|
||||
assert 'rating' not in result['reply']
|
||||
assert 'score' not in result['reply']
|
||||
assert 'reply' not in result['reply']
|
||||
assert result['reply']['id'] == reply.pk
|
||||
assert result['reply']['body'] == unicode(reply.body)
|
||||
assert result['reply']['created'] == (
|
||||
reply.created.replace(microsecond=0).isoformat() + 'Z')
|
||||
assert result['reply']['user'] == {
|
||||
'id': reply_user.pk,
|
||||
'name': unicode(reply_user.name),
|
||||
# should be the profile for a developer
|
||||
'url': absolutify(reply_user.get_url_path()),
|
||||
'username': reply_user.username,
|
||||
}
|
||||
_test_reply(result['reply'])
|
||||
|
||||
# If we instantiate a standard RatingSerializer class with a reply, it
|
||||
# should work like normal. `score` and `reply` should be there, blank.
|
||||
self.rating = reply
|
||||
result = self.serialize()
|
||||
_test_reply(result)
|
||||
assert result['score'] is None
|
||||
assert result['reply'] is None
|
||||
|
||||
def test_reply_profile_url_for_yourself(self):
|
||||
addon = addon_factory()
|
||||
|
|
Загрузка…
Ссылка в новой задаче