class CoverMutation(mutations.UpdateMutationSerializer): cover = common_serializers.RelatedField( "uuid", queryset=common_models.Attachment.objects.all().local(), serializer=None, ) def get_serialized_relations(self): serialized_relations = super().get_serialized_relations() serialized_relations["cover"] = "uuid" return serialized_relations def get_previous_state_handlers(self): handlers = super().get_previous_state_handlers() handlers["cover"] = (lambda obj: str(obj.attachment_cover.uuid) if obj.attachment_cover else None) return handlers def update(self, instance, validated_data): if "cover" in validated_data: validated_data["attachment_cover"] = validated_data.pop("cover") return super().update(instance, validated_data) def mutation_post_init(self, mutation): # link cover_attachment (if any) to mutation if "cover" not in mutation.payload: return try: attachment = common_models.Attachment.objects.get( uuid=mutation.payload["cover"]) except common_models.Attachment.DoesNotExist: return common_models.MutationAttachment.objects.create(attachment=attachment, mutation=mutation)
class UserWriteSerializer(serializers.ModelSerializer): summary = common_serializers.ContentSerializer(required=False, allow_null=True) avatar = common_serializers.RelatedField( "uuid", queryset=common_models.Attachment.objects.all().local().attached( False), serializer=None, queryset_filter=lambda qs, context: qs.filter(actor=context["request"]. user.actor), write_only=True, ) class Meta: model = models.User fields = [ "name", "privacy_level", "avatar", "instance_support_message_display_date", "funkwhale_support_message_display_date", "summary", ] def update(self, obj, validated_data): if not obj.actor: obj.create_actor() summary = validated_data.pop("summary", NOOP) avatar = validated_data.pop("avatar", NOOP) obj = super().update(obj, validated_data) if summary != NOOP: common_utils.attach_content(obj.actor, "summary_obj", summary) if avatar != NOOP: obj.actor.attachment_icon = avatar obj.actor.save(update_fields=["attachment_icon"]) return obj def to_representation(self, instance): r = super().to_representation(instance) r["avatar"] = common_serializers.AttachmentSerializer( instance.get_avatar()).data return r