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

'str' object has no attribute 'get' error when updating units via API #8544

Closed
2 tasks done
violarulan opened this issue Dec 29, 2022 · 3 comments
Closed
2 tasks done
Assignees
Labels
bug Something is broken.
Milestone

Comments

@violarulan
Copy link

violarulan commented Dec 29, 2022

Describe the issue

version: 4.15 (docker)

When I try to update a specific unit using the API, it throws the following error:

'str' object has no attribute 'get'
/usr/local/lib/python3.11/site-packages/weblate/api/serializers.py, line 1039, in to_internal_value

I already tried

  • I've read and searched the documentation.
  • I've searched for similar issues in this repository.

Steps to reproduce the behavior

The endpoint is PATCH /api/units/<my unit id>/ and the payload is {"state": 10, "target": ["\u52a0\u8fbe\u91cc\u5de5\u4f1a\u65e5"]}. I use Python requests to send it.

    headers = {'Authorization': f"Token {mytoken}", 'Accept': 'application/json'}
    payload = json.dumps({"state": 10, "target": [translation]})
    print(f"--> {payload}")
    update = requests.patch(f"{WEBLATE_API_BASE}units/{unit_id}/", headers=headers, json=payload).json()
    print(f"<-- {update}")

The output:

--> {"state": 10, "target": ["\u52a0\u8fbe\u91cc\u5de5\u4f1a\u65e5"]}
<-- {'error': 'Server Error (500)'}

When I checked the Local Vars part in the Django error output, I noticed the data variable is still a string rather than dictionary at the very bottom of the traceback.

Variable Value
data '{"state": 10, "target": ["\u52a0\u8fbe\u91cc\u5de5\u4f1a\u65e5"]}'
is_empty_value False
self UnitWriteSerializer(<Unit: [628506] Caldari Union Day>, context={'request': <rest_framework.request.Request: PATCH '/api/units/529344/'>, 'format': None, 'view': <weblate.api.views.UnitViewSet object>}, data='{"state": 10, "target": ["\u52a0\u8fbe\u91cc\u5de5\u4f1a\u65e5"]}', partial=True): target = PluralField() state = ChoiceField(choices=((0, 'Empty'), (10, 'Needs editing'), (20, 'Translated'), (30, 'Approved'), (100, 'Read only')), required=False, validators=[<django.core.validators.MinValueValidator object>, <django.core.validators.MaxValueValidator object>]) explanation = CharField(allow_blank=True, help_text='Additional explanation to clarify meaning or usage of the string.', required=False, style={'base_template': 'textarea.html'}) extra_flags = CharField(allow_blank=True, help_text='Additional comma-separated flags to influence Weblate behavior.', label='Translation flags', required=False, style={'base_template': 'textarea.html'}, validators=[]) labels = LabelsSerializer(many=True)

I think that should be problem.

Expected behavior

No response

Screenshots

No response

How do you run Weblate?

Docker container

Weblate versions

4.15 (docker)

Weblate deploy checks

> docker-compose exec --user weblate weblate weblate check --deploy
System check identified some issues:

INFOS:
?: (weblate.I021) Error collection is not set up, it is highly recommended for production use
        HINT: https://docs.weblate.org/en/weblate-4.15/admin/install.html#collecting-errors

System check identified 1 issue (1 silenced).

Additional context

No response

@nijel
Copy link
Member

nijel commented Dec 29, 2022

I noticed the data variable is still a string rather than dictionary at the very bottom of the traceback.

Yes, because you are double encoding the payload as JSON. The requests json parameter encodes it as JSON. Either do .patch(json={}) or .patch(data=json.dumps({}).

Still Weblate should not crash and report the issue correctly.

@nijel nijel added the bug Something is broken. label Dec 29, 2022
@nijel nijel self-assigned this Dec 29, 2022
@nijel nijel added this to the 4.15.1 milestone Dec 29, 2022
@nijel nijel closed this as completed in 5b24bd9 Dec 29, 2022
@github-actions
Copy link

The issue you have reported is now resolved. If you don’t feel it’s right, please follow its labels to get a clue for further steps.

  • In case you see a similar problem, please open a separate issue.
  • If you are happy with the outcome, don’t hesitate to support Weblate by making a donation.

@github-actions
Copy link

Thank you for your report; the issue you have reported has just been fixed.

  • In case you see a problem with the fix, please comment on this issue.
  • In case you see a similar problem, please open a separate issue.
  • If you are happy with the outcome, don’t hesitate to support Weblate by making a donation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken.
Projects
None yet
Development

No branches or pull requests

2 participants