Coverage for api/docs/image_docs.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 06:14 +0000

1from rest_framework.exceptions import ( 

2 AuthenticationFailed, 

3 NotAuthenticated, 

4 NotFound, 

5 ValidationError, 

6) 

7 

8from drf_spectacular.utils import OpenApiResponse, extend_schema 

9 

10from api.constants.parameters import COLLECTION, TAG 

11from api.docs.base_docs import ( 

12 NON_FILTER_FIELDS, 

13 SEARCH_DESCRIPTION, 

14 custom_extend_schema, 

15 fields_to_md, 

16) 

17from api.examples import ( 

18 image_complain_201_example, 

19 image_complain_curl, 

20 image_detail_200_example, 

21 image_detail_404_example, 

22 image_detail_curl, 

23 image_oembed_200_example, 

24 image_oembed_curl, 

25 image_related_200_example, 

26 image_related_curl, 

27 image_search_200_example, 

28 image_search_400_example, 

29 image_search_list_curl, 

30 image_stats_200_example, 

31 image_stats_curl, 

32) 

33from api.examples.image_responses import image_oembed_400_example 

34from api.serializers.image_serializers import ( 

35 ImageReportRequestSerializer, 

36 ImageSearchRequestSerializer, 

37 ImageSerializer, 

38 OembedRequestSerializer, 

39 OembedSerializer, 

40) 

41from api.serializers.media_serializers import MediaThumbnailRequestSerializer 

42from api.serializers.source_serializers import SourceSerializer 

43 

44 

45serializer = ImageSearchRequestSerializer(context={"media_type": "image"}) 

46image_filter_fields = fields_to_md( 

47 [f for f in serializer.field_names if f not in NON_FILTER_FIELDS] 

48) 

49 

50 

51image_search_description = SEARCH_DESCRIPTION.format( 

52 filter_fields=image_filter_fields, 

53 media_type="images", 

54 collection_param=COLLECTION, 

55 tag_param=TAG, 

56) 

57 

58search = custom_extend_schema( 

59 desc=image_search_description, 

60 params=serializer, 

61 res={ 

62 200: (ImageSerializer, image_search_200_example["results"][0]), 

63 400: (ValidationError, image_search_400_example), 

64 401: (NotAuthenticated, None), 

65 }, 

66 eg=[image_search_list_curl], 

67 external_docs={ 

68 "description": "Openverse Syntax Guide", 

69 "url": "https://openverse.org/search-help", 

70 }, 

71) 

72 

73stats = custom_extend_schema( 

74 desc=f""" 

75 Get a list of all content sources and their respective number of 

76 images in the Openverse catalog. 

77 

78 By using this endpoint, you can obtain info about content sources such 

79 as {fields_to_md(SourceSerializer.Meta.fields)}.""", 

80 res={ 

81 200: (SourceSerializer(many=True), image_stats_200_example[0]), 

82 401: (AuthenticationFailed, None), 

83 }, 

84 eg=[image_stats_curl], 

85) 

86 

87detail = custom_extend_schema( 

88 desc=f""" 

89 Get the details of a specified image. 

90 

91 By using this endpoint, you can obtain info about images such as 

92 {fields_to_md(ImageSerializer.Meta.fields)}""", 

93 res={ 

94 200: (ImageSerializer, image_detail_200_example), 

95 401: (AuthenticationFailed, None), 

96 404: (NotFound, image_detail_404_example), 

97 }, 

98 eg=[image_detail_curl], 

99) 

100 

101related = custom_extend_schema( 

102 desc=f""" 

103 Get related images for a specified image. 

104 

105 By using this endpoint, you can get the details of related images such as 

106 {fields_to_md(ImageSerializer.Meta.fields)}.""", 

107 res={ 

108 200: (ImageSerializer(many=True), image_related_200_example["results"][0]), 

109 401: (AuthenticationFailed, None), 

110 404: (NotFound, None), 

111 }, 

112 eg=[image_related_curl], 

113) 

114 

115report = custom_extend_schema( 

116 res={ 

117 201: (ImageReportRequestSerializer, image_complain_201_example), 

118 401: (AuthenticationFailed, None), 

119 400: (ValidationError, None), 

120 404: (NotFound, None), 

121 }, 

122 eg=[image_complain_curl], 

123) 

124 

125thumbnail = extend_schema( 

126 parameters=[MediaThumbnailRequestSerializer], 

127 responses={ 

128 200: OpenApiResponse(description="Thumbnail image"), 

129 404: NotFound, 

130 401: AuthenticationFailed, 

131 }, 

132) 

133 

134oembed = custom_extend_schema( 

135 params=OembedRequestSerializer, 

136 res={ 

137 200: (OembedSerializer, image_oembed_200_example), 

138 400: (ValidationError, image_oembed_400_example), 

139 401: (AuthenticationFailed, None), 

140 404: (NotFound, None), 

141 }, 

142 eg=[image_oembed_curl], 

143)