From 2b5a2a11367fedabd32cb13ad6f858a43d21be46 Mon Sep 17 00:00:00 2001 From: Madison Swain-Bowden Date: Thu, 8 Feb 2024 10:49:42 -0800 Subject: [PATCH] Use the sensitive source field in the serializer Also correctly reference mature vs sensitive depending on what type of object is being handed into the serializer --- api/api/serializers/media_serializers.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/api/api/serializers/media_serializers.py b/api/api/serializers/media_serializers.py index 961ce5b5027..6ce15622a53 100644 --- a/api/api/serializers/media_serializers.py +++ b/api/api/serializers/media_serializers.py @@ -516,6 +516,7 @@ class Meta: mature = serializers.BooleanField( help_text="Whether the media item is marked as mature", + source="sensitive", ) # This should be promoted to a stable field alongside @@ -540,9 +541,9 @@ def get_unstable__sensitivity(self, obj: Hit | AbstractMedia) -> list[str]: ): result.append(sensitivity.TEXT) - # ``obj.mature`` will either be `mature` from the ES document - # or the ``mature`` property on the Image or Audio model. - if obj.mature: + # ``obj.sensitive`` will either be `mature` from the ES document (see below) + # or the ``sensitive`` property on the Image or Audio model. + if obj.sensitive: # We do not currently have any documents marked `mature=true` # that were not marked so as a result of a confirmed user report. # This is despite the fact that the ingestion server _does_ copy @@ -569,6 +570,16 @@ def get_unstable__sensitivity(self, obj: Hit | AbstractMedia) -> list[str]: return result def to_representation(self, *args, **kwargs): + # This serializer adapts both ES Hits *and* Media instances. Currently, + # ES has a `mature` field on it which represents if maturity was present on + # the record in the database. The attributes in the code have been renamed + # to `sensitive`, but for the time being this flag still exists on the ES index. + # In order to prevent failures in serialization (since the serializer is looking + # for the `sensitive` attribute), we rename it here. + obj = args[0] + if isinstance(obj, Hit): + obj.sensitive = obj.mature + output = super().to_representation(*args, **kwargs) # Ensure lists are ``[]`` instead of ``None``