Coverage for apps/ptf/serializers/serializers.py: 53%

97 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-02-28 09:09 +0000

1from rest_framework import serializers 

2 

3from ptf.cmds.xml.xml_utils import replace_html_entities 

4from ptf.models import Abstract 

5from ptf.models import Collection 

6from ptf.models import Container 

7from ptf.models import ExtLink 

8from ptf.models import Provider 

9from ptf.models import PtfSite 

10from ptf.models import Publisher 

11from ptf.models import Resource 

12from ptf.models import ResourceId 

13 

14 

15class ResourceSerializer(serializers.ModelSerializer): 

16 class Meta: 

17 model = ResourceId 

18 fields = ["id_type", "id_value"] 

19 

20 

21class PublisherLinkSerializer(serializers.ModelSerializer): 

22 """ 

23 Serializer for nested Publisher object. 

24 """ 

25 

26 class Meta: 

27 model = Publisher 

28 fields = ["id", "pub_name"] 

29 

30 

31class CollectionLinkSerializer(serializers.ModelSerializer): 

32 """ 

33 Serializer for nested Collection objects. 

34 """ 

35 

36 class Meta: 

37 model = Collection 

38 fields = ["pid"] 

39 

40 

41class AbstractSerializer(serializers.ModelSerializer): 

42 id = serializers.ModelField(model_field=Abstract()._meta.get_field("id")) 

43 

44 class Meta: 

45 model = Abstract 

46 fields = ["id", "lang", "seq", "value_html"] 

47 

48 

49class SitesSerializer(serializers.ModelSerializer): 

50 class Meta: 

51 model = PtfSite 

52 fields = ["domain", "id", "name"] 

53 

54 

55class SitesLinkedSerializer(serializers.ModelSerializer): 

56 class Meta: 

57 model = PtfSite 

58 fields = ["id"] 

59 

60 

61class ProviderSerializer(serializers.ModelSerializer): 

62 class Meta: 

63 model = Provider 

64 fields = ["id", "name"] 

65 

66 

67class ExtLinkSerializer(serializers.ModelSerializer): 

68 class Meta: 

69 model = ExtLink 

70 fields = ["id", "rel", "location", "mimetype"] 

71 

72 

73class CollectionSerializer(serializers.ModelSerializer): 

74 """ 

75 Serializer for Collection objects. 

76 """ 

77 

78 resourceid_set = ResourceSerializer(many=True, read_only=False, required=False) 

79 sites = SitesSerializer(many=True, read_only=True) 

80 abstract_set = AbstractSerializer(many=True, read_only=False, required=False) 

81 extlink_set = ExtLinkSerializer(many=True, read_only=True) 

82 

83 class Meta: 

84 model = Collection 

85 fields = [ 

86 "id", 

87 "pid", 

88 "doi", 

89 "title_html", 

90 "lang", 

91 "coltype", 

92 "abbrev", 

93 "abstract_set", 

94 "alive", 

95 "doi", 

96 "sites", 

97 "wall", 

98 "fyear", 

99 "lyear", 

100 "resourceid_set", 

101 "extlink_set", 

102 ] 

103 depth = 1 

104 

105 def get_unique_together_validators(self): 

106 """Overriding method to disable unique together checks""" 

107 return [] 

108 

109 def update(self, instance, validated_data): 

110 identifiers = validated_data.get("resourceid_set") 

111 sites = validated_data.get("sites") 

112 sites_to = [] 

113 sites_instance = instance.sites.all() 

114 descriptions = [] 

115 # Site Membership.objects.create(....) 

116 

117 if identifiers: 

118 resourceids = [] 

119 for identifier in identifiers: 

120 id_val = identifier["id_value"] 

121 id_type = identifier["id_type"] 

122 r = ResourceId.objects.filter(id_type=id_type, id_value=id_val) 

123 if len(r) > 0: 

124 resourceids.append(r[0]) 

125 else: 

126 resource = Resource.objects.create() 

127 resource.save() 

128 resourceid = ResourceId(**identifier, resource=resource) 

129 resourceid.save() 

130 resourceids.append(resourceid) 

131 

132 instance.resourceid_set.set(resourceids) 

133 

134 for attr, value in validated_data.items(): 

135 if attr != "resourceid_set" and attr != "sites" and attr != "abstract_set": 

136 setattr(instance, attr, value) 

137 if attr == "abstract_set": 

138 # instance.abstract_set.all().delete() 

139 abstracts = validated_data.get("abstract_set") 

140 for abstract in abstracts: 

141 if "id" in abstract: 

142 description = Abstract.objects.get(pk=abstract["id"]) 

143 description.value_html = abstract["value_html"] 

144 description.save() 

145 else: 

146 description = abstract["value_html"] 

147 abs = Abstract( 

148 resource=instance, 

149 tag="description", 

150 lang=abstract["lang"], 

151 seq=abstract["seq"], 

152 value_html=description, 

153 value_tex=description, 

154 value_xml=f'<description xml:lang="{abstract["lang"]}">{replace_html_entities(description)}</description>', 

155 ) 

156 abs.save() 

157 descriptions.append(abs) 

158 

159 instance.abstract_set.set(descriptions) 

160 instance.save() 

161 return instance 

162 

163 

164class ContainerSerializer(serializers.ModelSerializer): 

165 class Meta: 

166 model = Container 

167 fields = [ 

168 "id", 

169 "pid", 

170 "ctype", 

171 "year", 

172 "last_modified", 

173 "number", 

174 "my_collection", 

175 "vseries", 

176 "volume", 

177 "seq", 

178 ] 

179 

180 depth = 1