Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Move media type categories to constants module (#589)
Madison Swain-Bowdenfbca822683f4
diff --git a/api/catalog/api/constants/field_values.py b/api/catalog/api/constants/field_values.pynew file mode 100644index 000000000..23b1bc977--- /dev/null+++ b/api/catalog/api/constants/field_values.py@@ -0,0 +1,31 @@+AUDIO_CATEGORIES = {+ "audiobook",+ "music",+ "news",+ "podcast",+ "pronunciation",+ "sound_effect",+}++IMAGE_CATEGORIES = {+ "digitized_artwork",+ "illustration",+ "photograph",+}++ASPECT_RATIOS = {+ "tall",+ "wide",+ "square",+}++IMAGE_SIZES = {+ "small",+ "medium",+ "large",+}++DURATION = {+ "short",+ "long",+}diff --git a/api/catalog/api/serializers/audio_serializers.py b/api/catalog/api/serializers/audio_serializers.pyindex fcc5b60d1..59497ca45 100644--- a/api/catalog/api/serializers/audio_serializers.py+++ b/api/catalog/api/serializers/audio_serializers.py@@ -1,3 +1,4 @@+from catalog.api.constants.field_values import AUDIO_CATEGORIES, DURATIONfrom catalog.api.docs.media_docs import fields_to_mdfrom catalog.api.models import AudioReportfrom catalog.api.models.audio import Audio@@ -9,6 +10,7 @@ from catalog.api.serializers.media_serializers import (_validate_enum,get_search_request_source_serializer,)+from catalog.api.utils.help_text import make_comma_separated_help_textfrom elasticsearch_dsl.response import Hitfrom rest_framework import serializers@@ -62,35 +64,23 @@ class AudioSearchRequestSerializer(category = serializers.CharField(label="category",- help_text="A comma separated list of categories; available categories "- "include `music`, `sound_effect`, `podcast`, `audiobook`, "- "and `news`.",+ help_text=make_comma_separated_help_text(AUDIO_CATEGORIES, "categories"),required=False,)duration = serializers.CharField(label="duration",- help_text="A comma separated list of audio lengths; available lengths "- "include `short`, and `long`.",+ help_text=make_comma_separated_help_text(DURATION, "audio lengths"),required=False,)@staticmethod- def validate_categories(value):- valid_categories = {- "music",- "sound_effect",- "podcast",- "news",- "audiobook",- "pronunciation",- }- _validate_enum("category", valid_categories, value)+ def validate_category(value):+ _validate_enum("category", AUDIO_CATEGORIES, value)return value.lower()@staticmethoddef validate_duration(value):- valid_durations = {"short", "long"} # TODO: Finalise duration filters- _validate_enum("duration", valid_durations, value)+ _validate_enum("duration", DURATION, value)return value.lower()diff --git a/api/catalog/api/serializers/image_serializers.py b/api/catalog/api/serializers/image_serializers.pyindex 17dee58db..6fbecb993 100644--- a/api/catalog/api/serializers/image_serializers.py+++ b/api/catalog/api/serializers/image_serializers.py@@ -1,3 +1,8 @@+from catalog.api.constants.field_values import (+ ASPECT_RATIOS,+ IMAGE_CATEGORIES,+ IMAGE_SIZES,+)from catalog.api.docs.media_docs import fields_to_mdfrom catalog.api.models import Image, ImageReportfrom catalog.api.serializers.base import SchemableHyperlinkedIdentityField@@ -9,6 +14,7 @@ from catalog.api.serializers.media_serializers import (_validate_enum,get_search_request_source_serializer,)+from catalog.api.utils.help_text import make_comma_separated_help_textfrom rest_framework import serializers@@ -38,34 +44,33 @@ class ImageSearchRequestSerializer(# Ref: ingestion_server/ingestion_server/categorize.py#Categorycategory = serializers.CharField(label="category",- help_text="A comma separated list of categories; available categories "- "include `illustration`, `photograph`, and "- "`digitized_artwork`.",+ help_text=make_comma_separated_help_text(IMAGE_CATEGORIES, "categories"),required=False,)aspect_ratio = serializers.CharField(label="aspect_ratio",- help_text="A comma separated list of aspect ratios; available aspect "- "ratios include `tall`, `wide`, and `square`.",+ help_text=make_comma_separated_help_text(ASPECT_RATIOS, "aspect ratios"),required=False,)size = serializers.CharField(label="size",- help_text="A comma separated list of image sizes; available sizes"- " include `small`, `medium`, or `large`.",+ help_text=make_comma_separated_help_text(IMAGE_SIZES, "image sizes"),required=False,)@staticmethod- def validate_categories(value):- valid_categories = {"illustration", "digitized_artwork", "photograph"}- _validate_enum("category", valid_categories, value)+ def validate_category(value):+ _validate_enum("category", IMAGE_CATEGORIES, value)return value.lower()@staticmethoddef validate_aspect_ratio(value):- valid_ratios = {"tall", "wide", "square"}- _validate_enum("aspect ratio", valid_ratios, value)+ _validate_enum("aspect ratio", ASPECT_RATIOS, value)+ return value.lower()++ @staticmethod+ def validate_size(value):+ _validate_enum("size", IMAGE_SIZES, value)return value.lower()diff --git a/api/catalog/api/serializers/media_serializers.py b/api/catalog/api/serializers/media_serializers.pyindex 24689b9f5..82b65872e 100644--- a/api/catalog/api/serializers/media_serializers.py+++ b/api/catalog/api/serializers/media_serializers.py@@ -3,6 +3,7 @@ from urllib.parse import urlparseimport catalog.api.licenses as license_helpersfrom catalog.api.controllers.search_controller import get_sources+from catalog.api.utils.help_text import make_comma_separated_help_textfrom drf_yasg.utils import swagger_serializer_methodfrom rest_framework import serializers@@ -103,9 +104,8 @@ def get_search_request_source_serializer(media_type):"""_field_attrs = {- "help_text": (- "A comma separated list of data sources to search. "- f"Valid inputs: {format_enums(get_sources(media_type).keys())}"+ "help_text": make_comma_separated_help_text(+ get_sources(media_type).keys(), "data sources"),"required": False,}@@ -172,21 +172,21 @@ class MediaSearchRequestSerializer(serializers.Serializer):q = serializers.CharField(label="query",- help_text="A query string that should not exceed 200 characters in " "length",+ help_text="A query string that should not exceed 200 characters in length",required=False,)license = serializers.CharField(label="licenses",- help_text="A comma-separated list of licenses. Example: `by,cc0`. "- "Valid inputs: "- f"`{list(license_helpers.LICENSE_GROUPS['all'])}`",+ help_text=make_comma_separated_help_text(+ license_helpers.LICENSE_GROUPS["all"], "licenses"+ ),required=False,)license_type = serializers.CharField(label="license type",- help_text="A list of license types. "- "Valid inputs: "- f"`{list(license_helpers.LICENSE_GROUPS.keys())}`",+ help_text=make_comma_separated_help_text(+ license_helpers.LICENSE_GROUPS.keys(), "license types"+ ),required=False,)creator = serializers.CharField(diff --git a/api/catalog/api/utils/help_text.py b/api/catalog/api/utils/help_text.pynew file mode 100644index 000000000..a17e5c834--- /dev/null+++ b/api/catalog/api/utils/help_text.py@@ -0,0 +1,22 @@+from typing import Iterable+++def make_comma_separated_help_text(items: Iterable[str], name: str) -> str:+ """+ Generate help text that describes a comma separated list of items with the name+ provided. Items are wrapped in backticks, and lists with more than one item will+ have an "and" added before the final item.++ :param items: iterable of available options for this field+ :param name: plural name of the list of items (e.g. "categories", "aspect ratios")+ :return: generated help text+ """+ formatted = [f"`{item}`" for item in sorted(items)]+ # Add an "and" at the end of the list+ if len(formatted) > 1:+ formatted[-1] = f"and {formatted[-1]}"+ help_text = (+ f"A comma separated list of {name}; available {name} include: "+ f"{', '.join(formatted)}."+ )+ return help_textdiff --git a/api/test/audio_integration_test.py b/api/test/audio_integration_test.pyindex 1c6d6f78b..d9bcd4b89 100644--- a/api/test/audio_integration_test.py+++ b/api/test/audio_integration_test.py@@ -43,6 +43,11 @@ def test_search_category_filtering(audio_fixture):search_by_category("audio", "pronunciation", audio_fixture)+def test_search_category_filtering_fails(audio_fixture):+ with pytest.raises(AssertionError):+ search_by_category("audio", "not_valid", audio_fixture)++def test_search_all_excluded():search_all_excluded("audio", ["jamendo", "wikimedia_audio"])diff --git a/api/test/unit/utils/test_help_text.py b/api/test/unit/utils/test_help_text.pynew file mode 100644index 000000000..5463079bd--- /dev/null+++ b/api/test/unit/utils/test_help_text.py@@ -0,0 +1,27 @@+import pytest+from catalog.api.utils.help_text import make_comma_separated_help_text+++ "items, expected",+ [+ ([], "A comma separated list of items; available items include: ."),+ (+ ["a"],+ "A comma separated list of items; available items include: `a`.",+ ),+ (+ ["a", "b"],+ "A comma separated list of items; "+ "available items include: `a`, and `b`.",+ ),+ (+ ["a", "b", "c"],+ "A comma separated list of items; "+ "available items include: `a`, `b`, and `c`.",+ ),+ ],+)+def test_make_help_text(items, expected):+ actual = make_comma_separated_help_text(items, "items")+ assert actual == expected