Skip to content

Commit

Permalink
Use the sensitive source field in the serializer
Browse files Browse the repository at this point in the history
Also correctly reference mature vs sensitive depending on what type of object is being handed into the serializer
  • Loading branch information
AetherUnbound committed Feb 8, 2024
1 parent f38774a commit 2b5a2a1
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions api/api/serializers/media_serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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``
Expand Down

0 comments on commit 2b5a2a1

Please sign in to comment.