Skip to content

Commit

Permalink
Merge pull request #4663 from diox/api-localized-error-messages
Browse files Browse the repository at this point in the history
Localize review API error messages
  • Loading branch information
diox committed Feb 13, 2017
2 parents 7518374 + 308a78f commit d08d826
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
17 changes: 10 additions & 7 deletions src/olympia/reviews/serializers.py
@@ -1,6 +1,8 @@
import re
from urllib2 import unquote

from django.utils.translation import ugettext as _

from rest_framework import serializers
from rest_framework.relations import PrimaryKeyRelatedField

Expand Down Expand Up @@ -96,7 +98,7 @@ def validate(self, data):
# Only one level of replying is allowed, so if it's already a
# reply, we shouldn't allow that.
raise serializers.ValidationError(
'Can not reply to a review that is already a reply.')
_(u"You can't reply to a review that is already a reply."))

data = super(ReviewSerializerReply, self).validate(data)
return data
Expand Down Expand Up @@ -130,27 +132,28 @@ class Meta:
def validate_version(self, version):
if self.partial:
raise serializers.ValidationError(
'Can not change version once the review has been created.')
_(u"You can't change the version of the add-on reviewed once "
u"the review has been created."))

addon = self.context['view'].get_addon_object()
if version.addon_id != addon.pk or not version.is_public():
raise serializers.ValidationError(
'Version does not exist on this add-on or is not public.')
_(u"This version of the add-on doesn't exist or isn't "
u"public."))
return version

def validate(self, data):
data = super(ReviewSerializer, self).validate(data)
if not self.partial:
if data['addon'].authors.filter(pk=data['user'].pk).exists():
raise serializers.ValidationError(
'An add-on author can not leave a review on its own '
'add-on.')
_(u"You can't leave a review on your own add-on."))

review_exists_on_this_version = Review.objects.filter(
addon=data['addon'], user=data['user'],
version=data['version']).exists()
if review_exists_on_this_version:
raise serializers.ValidationError(
'The same user can not leave a review on the same version'
' more than once.')
_(u"You can't leave more than one review for the same "
u"version of an add-on."))
return data
17 changes: 9 additions & 8 deletions src/olympia/reviews/tests/test_views.py
Expand Up @@ -1265,7 +1265,8 @@ def test_edit_dont_allow_version_to_be_edited(self):
response = self.client.patch(self.url, {'version': new_version.pk})
assert response.status_code == 400
assert response.data['version'] == [
'Can not change version once the review has been created.']
u"You can't change the version of the add-on reviewed once "
u"the review has been created."]

def test_edit_admin(self):
original_review_user = self.review.user
Expand Down Expand Up @@ -1549,7 +1550,7 @@ def test_post_version_not_linked_to_the_right_addon(self):
'version': addon2.current_version.pk})
assert response.status_code == 400
assert response.data['version'] == [
'Version does not exist on this add-on or is not public.']
u"This version of the add-on doesn't exist or isn't public."]

def test_post_deleted_addon(self):
version_pk = self.addon.current_version.pk
Expand Down Expand Up @@ -1580,7 +1581,7 @@ def test_post_deleted_version(self):
'version': old_version_pk})
assert response.status_code == 400
assert response.data['version'] == [
'Version does not exist on this add-on or is not public.']
u"This version of the add-on doesn't exist or isn't public."]

def test_post_disabled_version(self):
self.addon.current_version.update(created=self.days_ago(1))
Expand All @@ -1598,7 +1599,7 @@ def test_post_disabled_version(self):
'version': old_version.pk})
assert response.status_code == 400
assert response.data['version'] == [
'Version does not exist on this add-on or is not public.']
u"This version of the add-on doesn't exist or isn't public."]

def test_post_not_public_addon(self):
version_pk = self.addon.current_version.pk
Expand All @@ -1621,7 +1622,7 @@ def test_post_logged_in_but_is_addon_author(self):
'version': self.addon.current_version.pk})
assert response.status_code == 400
assert response.data['non_field_errors'] == [
'An add-on author can not leave a review on its own add-on.']
"You can't leave a review on your own add-on."]

def test_post_twice_different_version(self):
self.user = user_factory()
Expand All @@ -1648,8 +1649,8 @@ def test_post_twice_same_version(self):
'version': self.addon.current_version.pk})
assert response.status_code == 400
assert response.data['non_field_errors'] == [
'The same user can not leave a review on the same version more'
' than once.']
u"You can't leave more than one review for the same version of "
u"an add-on."]


class TestReviewViewSetFlag(TestCase):
Expand Down Expand Up @@ -1942,4 +1943,4 @@ def test_replying_to_a_reply_is_not_possible(self):
})
assert response.status_code == 400
assert response.data['non_field_errors'] == [
'Can not reply to a review that is already a reply.']
u"You can't reply to a review that is already a reply."]

0 comments on commit d08d826

Please sign in to comment.