Coverage for apis_core/collections/models.py: 69%

62 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-23 06:59 +0000

1from django.contrib.contenttypes.fields import GenericForeignKey 

2from django.contrib.contenttypes.models import ContentType 

3from django.db import models 

4from django.utils.translation import gettext_lazy as _ 

5 

6from apis_core.generic.abc import GenericModel 

7 

8 

9class SkosCollectionManager(models.Manager): 

10 def by_instance(self, instance): 

11 content_type = ContentType.objects.get_for_model(instance) 

12 scco = SkosCollectionContentObject.objects.filter( 

13 content_type=content_type, object_id=instance.id 

14 ) 

15 return self.get_queryset().filter( 

16 pk__in=scco.values_list("collection", flat=True) 

17 ) 

18 

19 

20class SkosCollection(GenericModel, models.Model): 

21 """ 

22 SKOS collections are labeled and/or ordered groups of SKOS concepts. 

23 Collections are useful where a group of concepts shares something in common, 

24 and it is convenient to group them under a common label, or 

25 where some concepts can be placed in a meaningful order. 

26 

27 Miles, Alistair, and Sean Bechhofer. "SKOS simple knowledge 

28 organization system reference. W3C recommendation (2009)." 

29 

30 """ 

31 

32 class Meta: 

33 ordering = ["name"] 

34 verbose_name = _("collection") 

35 verbose_name_plural = _("collections") 

36 

37 class Config(GenericModel.Config): 

38 overview_section = _("Collections") 

39 search_initial = False 

40 

41 name = models.CharField( 

42 max_length=300, 

43 verbose_name="skos:prefLabel", 

44 help_text="Collection label or name", 

45 ) 

46 label_lang = models.CharField( 

47 max_length=3, 

48 blank=True, 

49 default="en", 

50 verbose_name="skos:prefLabel language", 

51 help_text="Language of preferred label given above", 

52 ) 

53 creator = models.TextField( 

54 blank=True, 

55 verbose_name="dc:creator", 

56 help_text="Person or organisation that created this collection" 

57 "If more than one list all using a semicolon ;", 

58 ) 

59 contributor = models.TextField( 

60 blank=True, 

61 verbose_name="dc:contributor", 

62 help_text="Person or organisation that made contributions to the collection" 

63 "If more than one list all using a semicolon ;", 

64 ) 

65 objects = SkosCollectionManager() 

66 

67 def __str__(self): 

68 return self.name 

69 

70 def add(self, instance: object): 

71 content_type = ContentType.objects.get_for_model(instance) 

72 SkosCollectionContentObject.objects.get_or_create( 

73 collection=self, content_type=content_type, object_id=instance.pk 

74 ) 

75 

76 def remove(self, instance: object): 

77 content_type = ContentType.objects.get_for_model(instance) 

78 SkosCollectionContentObject.objects.filter( 

79 collection=self, content_type=content_type, object_id=instance.pk 

80 ).delete() 

81 

82 @property 

83 def parent_collection(self): 

84 content_type = ContentType.objects.get_for_model(self) 

85 sccos = SkosCollectionContentObject.objects.filter( 

86 content_type=content_type, object_id=self.id 

87 ) 

88 if len(sccos) == 1: 

89 return sccos.first().collection 

90 raise SkosCollection.MultipleObjectsReturned( 

91 f'"{self}" is part of multiple collections' 

92 ) 

93 

94 def list(self, content_type=None): 

95 qs = SkosCollectionContentObject.objects.filter(collection=self) 

96 if content_type is not None: 

97 return qs.filter(content_type=content_type) 

98 return qs 

99 

100 def contains(self, instance: object): 

101 content_type = ContentType.objects.get_for_model(instance) 

102 return SkosCollectionContentObject.objects.filter( 

103 collection=self, content_type=content_type, object_id=instance.pk 

104 ).exists() 

105 

106 

107class SkosCollectionContentObject(GenericModel, models.Model): 

108 """ 

109 *Throughtable* datamodel to connect collections to arbitrary content 

110 """ 

111 

112 class Meta: 

113 verbose_name = _("collection content object") 

114 verbose_name_plural = _("collection content objects") 

115 indexes = [models.Index(fields=["content_type", "object_id", "collection"])] 

116 

117 class Config(GenericModel.Config): 

118 overview_section = _("Collections") 

119 search_initial = False 

120 

121 collection = models.ForeignKey(SkosCollection, on_delete=models.CASCADE) 

122 

123 content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) 

124 object_id = models.PositiveIntegerField() 

125 content_object = GenericForeignKey("content_type", "object_id") 

126 

127 def __str__(self): 

128 if self.content_object: 

129 return f"{self.content_object} -> {self.collection}" 

130 return super().__str__()