Permalink
Browse files

Some fixes in translation objects API.

  • Loading branch information...
1 parent e0c5fc5 commit 26a07ce57590e77ec20451222fb3225c52263280 rtnpro committed with Apostolos Bessas May 3, 2012
Showing with 44 additions and 25 deletions.
  1. +17 −11 transifex/resources/api.py
  2. +27 −14 transifex/resources/tests/api/__init__.py
View
28 transifex/resources/api.py
@@ -613,7 +613,7 @@ def _check_json_data(self, translations):
if not translations:
raise NoContentError("Translations not found!")
if not isinstance(translations, list):
- raise BadRequestError("Traslations are not in a list!")
+ raise BadRequestError("Translations are not in a list!")
return True
def _translations_as_dict(self, translations, language):
@@ -648,7 +648,7 @@ def _translations_as_dict(self, translations, language):
def _check_user_perms(self, can_submit_translations=False,
accept_translations=False, is_maintainer=False,
- can_review=False, translation_objs=[]):
+ can_review=False, translation_objs=[], translation={}):
"""
Check if user has necessary permissions.
Args:
@@ -657,6 +657,7 @@ def _check_user_perms(self, can_submit_translations=False,
is_maintainer: A boolean
can_review: A boolean
translation_objs: A list
+ translation: A translation dictionary
Returns:
A boolean
"""
@@ -667,7 +668,8 @@ def _check_user_perms(self, can_submit_translations=False,
if not translation_objs:
return False
reviewed = translation_objs[0].reviewed
- if reviewed and not can_review:
+ if (reviewed or translation.get('reviewed') != reviewed) and\
+ not can_review:
return False
return True
@@ -815,12 +817,13 @@ def _check_if_user_can_update_translation(self, project, resource,
resource.slug, language.code))
reviewed = translations[0].reviewed
- if check.proofread(project, language) and \
- translation_data.get('reviewed') != None:
- update_fields['reviewed'] = translation_data.get('reviewed')
- elif reviewed:
+ can_review = check.proofread(project, language)
+ if (reviewed or translation_data.get('reviewed') != reviewed)\
+ and not can_review:
raise ForbiddenError("User '%s' cannot update this reviewed "\
"translation." % user.username)
+ if isinstance(translation_data.get('reviewed'), bool):
+ update_fields['reviewed'] = translation_data.get('reviewed')
update_fields['user'] = user
def _update_translations(self, source_entity, language, translations,
@@ -856,7 +859,7 @@ def _update_translations(self, source_entity, language, translations,
plural_forms = [int(r) for r in plural_forms]
plural_forms.sort()
if nplurals != plural_forms:
- return rc.BAD_REQUEST
+ raise BadRequestError("Invalid plural forms in translation.")
for rule in translation_strings.keys():
update_fields['string'] = translation_strings[rule]
translations.filter(rule=rule).update(**update_fields)
@@ -1010,7 +1013,9 @@ def update(self, request, project_slug, resource_slug,
return rc.FORBIDDEN
nplurals = language.get_pluralrules_numbers()
- keys = translations[0].keys()
+ keys = ['source_entity_id', 'key', 'context', 'translation',
+ 'reviewed', 'wordcount', 'last_update', 'user',
+ 'position', 'occurrences', 'pluralized']
trans_obj_dict = self._translations_as_dict(translations, language)
@@ -1021,7 +1026,7 @@ def update(self, request, project_slug, resource_slug,
try:
se_id = translation.get('source_entity_id')
user = translation.get('user') and \
- User.objects.get(trasnlation.get('user')) or\
+ User.objects.get(username=translation.get('user')) or\
request.user
check = ProjectPermission(user)
@@ -1038,7 +1043,8 @@ def update(self, request, project_slug, resource_slug,
'can_submit_translations': can_submit_translations,
'accept_translations': accept_translations,
'is_maintainer': is_maintainer,
- 'translation_objs': translation_objs
+ 'translation_objs': translation_objs,
+ 'translation': translation
}
if not self._check_user_perms(**kwargs):
View
41 transifex/resources/tests/api/__init__.py
@@ -1088,26 +1088,37 @@ def test_check_user_perms(self):
self.assertTrue(self.obj._check_user_perms(
can_submit_translations=True,
accept_translations=True,
- translation_objs=translation_objs
+ translation_objs=translation_objs,
+ translation={'reviewed': False}
+ ))
+
+ self.assertFalse(self.obj._check_user_perms(
+ can_submit_translations=True,
+ accept_translations=True,
+ translation_objs=translation_objs,
+ translation={'reviewed': True}
))
self.assertTrue(self.obj._check_user_perms(
is_maintainer=True,
- translation_objs=translation_objs
+ translation_objs=translation_objs,
+ translation={'reviewed': False}
))
translation_objs[0].reviewed = True
self.assertFalse(self.obj._check_user_perms(
can_submit_translations=True,
accept_translations=True,
- translation_objs=translation_objs
+ translation_objs=translation_objs,
+ translation={'reviewed': False}
)
)
self.assertTrue(self.obj._check_user_perms(
can_submit_translations=True,
accept_translations=True,
can_review=True,
- translation_objs=translation_objs
+ translation_objs=translation_objs,
+ translation={'reviewed': True}
)
)
@@ -1227,7 +1238,8 @@ def test_put_translations(self):
self._setUp_test_put_translations()
response = self.client['team_member'].get(reverse(
'translation_strings',
- args=['project1', 'resource1', self.language_ar.code]))
+ args=['project1', 'resource1', self.language_ar.code]),
+ data={'details':''})
self.assertEqual(response.status_code, 200)
json = simplejson.loads(response.content)
for index, item in enumerate(json):
@@ -1236,7 +1248,9 @@ def test_put_translations(self):
'4': '4', '5': '5'}
json[index] = item
if item['source_entity_id'] == 5:
- item['translation'] = ' '
+ item['translation'] = 'fooo'
+ item['user'] = 'team_member'
+ item['reviewed'] = True
json[index] = item
response = self.client['maintainer'].put(reverse(
@@ -1247,18 +1261,21 @@ def test_put_translations(self):
expected_json = []
for item in json:
- if item['source_entity_id'] in [3, 1]:
+ if item['source_entity_id'] == 3:
expected_json.append(item)
+ item.pop('last_update')
self.assertEqual(response.status_code, 200)
- self.assertEqual(simplejson.loads(response.content), expected_json)
+ response_json = simplejson.loads(response.content)
+ for item in response_json:
+ item.pop('last_update')
+ self.assertEqual(response_json, expected_json)
self.assertTrue(self.source_entity1.translations.get(
string="ArabicString2"))
def create_sample_translations(cls):
self = cls
- self.entity = self.resource.entities[0]
self.source_entity1 = SourceEntity.objects.create(string='String2',
context='Context2', occurrences='Occurrences2',
@@ -1417,11 +1434,7 @@ def test_single_translation_handler(self):
data=simplejson.dumps(json),
content_type="application/json"
)
- self.assertEqual(response.status_code, 200)
- self.assertEqual(simplejson.loads(response.content)['translation'],
- 'Hello world')
- self.assertEqual(simplejson.loads(response.content)['reviewed'],
- False)
+ self.assertEqual(response.status_code, 500)
json['user'] = 'team_coordinator'
response = self.client['team_coordinator'].put(reverse(
'translation_string', args=[self.project.slug,

0 comments on commit 26a07ce

Please sign in to comment.