Fixed mutation erasing tags and description when other fields are modified

This commit is contained in:
Eliot Berriot 2020-03-26 16:36:03 +01:00
parent 3e902bd89e
commit f361791c1b
No known key found for this signature in database
GPG Key ID: 6B501DFD73514E14
2 changed files with 45 additions and 4 deletions

View File

@ -9,6 +9,8 @@ from funkwhale_api.tags import serializers as tags_serializers
from . import models
NOOP = object()
def can_suggest(obj, actor):
return obj.is_local
@ -34,9 +36,10 @@ class TagMutation(mutations.UpdateMutationSerializer):
return handlers
def update(self, instance, validated_data):
tags = validated_data.pop("tags", [])
tags = validated_data.pop("tags", NOOP)
r = super().update(instance, validated_data)
tags_models.set_tags(instance, *tags)
if tags != NOOP:
tags_models.set_tags(instance, *tags)
return r
@ -53,9 +56,10 @@ class DescriptionMutation(mutations.UpdateMutationSerializer):
return handlers
def update(self, instance, validated_data):
description = validated_data.pop("description", None)
description = validated_data.pop("description", NOOP)
r = super().update(instance, validated_data)
common_utils.attach_content(instance, "description", description)
if description != NOOP:
common_utils.attach_content(instance, "description", description)
return r

View File

@ -220,3 +220,40 @@ def test_album_mutation_description(factory_name, factories, mocker):
mutation.previous_state["description"]
== common_serializers.ContentSerializer(content).data
)
@pytest.mark.parametrize(
"factory_name", ["music.Track", "music.Album", "music.Artist"],
)
def test_mutation_description_keep_tags(factory_name, factories, mocker):
mocker.patch("funkwhale_api.federation.routes.outbox.dispatch")
content = factories["common.Content"]()
obj = factories[factory_name](description=content, set_tags=["punk", "rock"])
mutation = factories["common.Mutation"](
type="update",
target=obj,
payload={"description": {"content_type": "text/plain", "text": "hello there"}},
)
mutation.apply()
obj.refresh_from_db()
assert obj.description.content_type == "text/plain"
assert obj.description.text == "hello there"
assert obj.get_tags() == ["punk", "rock"]
@pytest.mark.parametrize(
"factory_name", ["music.Track", "music.Album", "music.Artist"],
)
def test_mutation_tags_keep_descriptions(factory_name, factories, mocker):
mocker.patch("funkwhale_api.federation.routes.outbox.dispatch")
content = factories["common.Content"]()
obj = factories[factory_name](description=content)
mutation = factories["common.Mutation"](
type="update", target=obj, payload={"tags": ["punk", "rock"]},
)
mutation.apply()
obj.refresh_from_db()
assert obj.description == content
assert obj.get_tags() == ["punk", "rock"]