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

28 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-16 07:42 +0000

1from django.contrib.contenttypes.fields import GenericForeignKey 

2from django.contrib.contenttypes.models import ContentType 

3from django.db import models 

4 

5from apis_core.generic.abc import GenericModel 

6 

7 

8class SkosCollection(GenericModel, models.Model): 

9 """ 

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

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

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

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

14 

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

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

17 

18 """ 

19 

20 class Meta: 

21 ordering = ["name"] 

22 

23 parent = models.ForeignKey("self", null=True, on_delete=models.CASCADE, blank=True) 

24 name = models.CharField( 

25 max_length=300, 

26 verbose_name="skos:prefLabel", 

27 help_text="Collection label or name", 

28 ) 

29 label_lang = models.CharField( 

30 max_length=3, 

31 blank=True, 

32 default="en", 

33 verbose_name="skos:prefLabel language", 

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

35 ) 

36 creator = models.TextField( 

37 blank=True, 

38 verbose_name="dc:creator", 

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

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

41 ) 

42 contributor = models.TextField( 

43 blank=True, 

44 verbose_name="dc:contributor", 

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

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

47 ) 

48 

49 def __str__(self): 

50 return self.name 

51 

52 def children(self): 

53 return SkosCollection.objects.filter(parent=self) 

54 

55 def children_tree_as_list(self): 

56 childtrees = [self] 

57 for child in self.children(): 

58 childtrees.extend(child.children_tree_as_list()) 

59 return childtrees 

60 

61 

62class SkosCollectionContentObject(GenericModel, models.Model): 

63 """ 

64 *Throughtable* datamodel to connect collections to arbitrary content 

65 """ 

66 

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

68 

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

70 object_id = models.PositiveIntegerField() 

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

72 

73 def __str__(self): 

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