Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing groups API validation #864

Merged
merged 1 commit into from Aug 31, 2020
Merged

Fixing groups API validation #864

merged 1 commit into from Aug 31, 2020

Conversation

fao89
Copy link
Member

@fao89 fao89 commented Aug 25, 2020

https://pulp.plan.io/issues/7329
closes #7329

Please be sure you have read our documentation on creating PRs:
https://docs.pulpproject.org/contributing/pull-request-walkthrough.html

@pulpbot
Copy link
Member

pulpbot commented Aug 25, 2020

Attached issue: https://pulp.plan.io/issues/7329

try:
permission = Permission.objects.get(**data)
except (Permission.MultipleObjectsReturned, Permission.DoesNotExist, FieldError) as exc:
raise ValidationError(_(str(exc)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not believe, gettext works in this situation. How is it supposed to extract the translatable string here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know how gettext works, str(exc) returns a string I imagined it would work

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I thought it must be able to extract the strings without calling the code. So it probably looks for calls to ’_("...")’.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception text should already be translated if a translation is available. Please remove the _() around str(exc).

@fao89 fao89 force-pushed the 7329 branch 3 times, most recently from 14af945 to 2dc22f4 Compare August 27, 2020 15:02
@fao89 fao89 requested review from dkliban and mdellweg August 28, 2020 15:10
Copy link
Member

@mdellweg mdellweg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if i'm the right one to review.
But at least with the gettext stuff i am pretty confident now.

obj_pk = request.data["obj"].strip("/").split("/")[-1]
uuid.UUID(obj_pk)
except (AttributeError, ValueError):
raise ValidationError(_(f"Invalid value for 'obj': {request.data['obj']}"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stackoverflow says nope:
https://stackoverflow.com/questions/49797658/how-to-use-gettext-with-python-3-6-f-strings

Suggested change
raise ValidationError(_(f"Invalid value for 'obj': {request.data['obj']}"))
raise ValidationError(_("Invalid value for 'obj': " + str(request.data['obj'])))

Or the good old "format" of course (Usually this helps people translating if they see the whole string template).

Suggested change
raise ValidationError(_(f"Invalid value for 'obj': {request.data['obj']}"))
raise ValidationError(_("Invalid value for 'obj': {obj}").format(request.data['obj']))

try:
permission = Permission.objects.get(codename=codename)
except (Permission.MultipleObjectsReturned, Permission.DoesNotExist, FieldError) as exc:
raise ValidationError(_(str(exc)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ValidationError(_(str(exc)))
raise ValidationError(str(exc))

Won't work at all, but the exception should only use translatable strings anyway.

try:
user = User.objects.get(**request.data)
except (User.DoesNotExist, FieldError) as exc:
raise ValidationError(_(str(exc)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Suggested change
raise ValidationError(_(str(exc)))
raise ValidationError(str(exc))

@@ -274,7 +309,7 @@ def destroy(self, request, group_pk, pk):
Remove a user from a group.
"""
group = Group.objects.get(pk=group_pk)
user = get_object_or_404(get_user_model(), pk=pk)
user = get_object_or_404(User, pk=pk)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this going to be a problem for galaxy_ng?

Copy link
Member Author

@fao89 fao89 Aug 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, at the beginning of this file (line 23) I declared:

User = get_user_model()

@dkliban dkliban merged commit 0336d94 into pulp:master Aug 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants