Coverage for apis_core/apis_entities/abc.py: 83%

71 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-10-10 13:36 +0000

1from pathlib import Path 

2 

3from django.db import models 

4from django.utils.encoding import force_str 

5from django.utils.translation import gettext_lazy as _ 

6 

7from apis_core.generic.utils.rdf_namespace import CRM 

8from apis_core.utils.rdf import load_uri_using_path 

9 

10######################### 

11# Abstract base classes # 

12######################### 

13 

14 

15# These abstract base classes are named after 

16# CIDOC CRM entities, but we are *NOT*(!) 

17# trying to implement CIDOC CRM in Django. 

18 

19RDF_CONFIG_FOLDER = Path(__file__).parent / "triple_configs" 

20 

21 

22class E21_Person(models.Model): 

23 forename = models.CharField( 

24 blank=True, default="", max_length=4096, verbose_name=_("forename") 

25 ) 

26 surname = models.CharField( 

27 blank=True, default="", max_length=4096, verbose_name=_("surname") 

28 ) 

29 gender = models.CharField( 

30 blank=True, default="", max_length=4096, verbose_name=_("gender") 

31 ) 

32 date_of_birth = models.DateField( 

33 blank=True, null=True, verbose_name=_("date of birth") 

34 ) 

35 date_of_death = models.DateField( 

36 blank=True, null=True, verbose_name=_("date of death") 

37 ) 

38 

39 class Meta: 

40 abstract = True 

41 verbose_name = _("person") 

42 verbose_name_plural = _("persons") 

43 ordering = ["surname", "forename"] 

44 

45 def __str__(self): 

46 if self.forename and self.surname: 

47 return f"{self.forename} {self.surname}" 

48 return self.forename or self.surname or force_str(_("No name")) 

49 

50 import_definitions = { 

51 "https://d-nb.info/*|/.*.rdf": lambda x: load_uri_using_path( 

52 x, RDF_CONFIG_FOLDER / "E21_PersonFromDNB.toml" 

53 ), 

54 "http://www.wikidata.org/*|/.*.rdf": lambda x: load_uri_using_path( 

55 x, RDF_CONFIG_FOLDER / "E21_PersonFromWikidata.toml" 

56 ), 

57 } 

58 

59 @classmethod 

60 def get_rdf_types(cls): 

61 return [CRM.E21_Person] 

62 

63 

64class E53_Place(models.Model): 

65 """ 

66 The feature_code field refers to the geonames feature codes, as 

67 listed on https://www.geonames.org/export/codes.html 

68 """ 

69 

70 label = models.CharField( 

71 blank=True, default="", max_length=4096, verbose_name=_("label") 

72 ) 

73 latitude = models.FloatField(blank=True, null=True, verbose_name=_("latitude")) 

74 longitude = models.FloatField(blank=True, null=True, verbose_name=_("longitude")) 

75 feature_code = models.CharField( 

76 blank=True, 

77 default="", 

78 max_length=16, 

79 verbose_name=_("feature code"), 

80 help_text='<a href="https://www.geonames.org/export/codes.html" target="_blank">Geonames Feature Code List</a>', 

81 ) 

82 

83 class Meta: 

84 abstract = True 

85 verbose_name = _("place") 

86 verbose_name_plural = _("places") 

87 ordering = ["label"] 

88 

89 def __str__(self): 

90 return self.label or force_str(_("No label")) 

91 

92 import_definitions = { 

93 "https://d-nb.info/*|/.*.rdf": lambda x: load_uri_using_path( 

94 x, RDF_CONFIG_FOLDER / "E53_PlaceFromDNB.toml" 

95 ), 

96 "https://sws.geonames.org/*|/.*.rdf*": lambda x: load_uri_using_path( 

97 x, RDF_CONFIG_FOLDER / "E53_PlaceFromGeonames.toml" 

98 ), 

99 "http://www.wikidata.org/*|/.*.rdf": lambda x: load_uri_using_path( 

100 x, RDF_CONFIG_FOLDER / "E53_PlaceFromWikidata.toml" 

101 ), 

102 } 

103 

104 @classmethod 

105 def get_rdf_types(cls): 

106 return [CRM.E53_Place] 

107 

108 @classmethod 

109 def create_from_string(cls, string): 

110 return cls.objects.create(label=string) 

111 

112 

113class E74_Group(models.Model): 

114 label = models.CharField( 

115 blank=True, default="", max_length=4096, verbose_name=_("label") 

116 ) 

117 

118 class Meta: 

119 abstract = True 

120 verbose_name = _("group") 

121 verbose_name_plural = _("groups") 

122 ordering = ["label"] 

123 

124 def __str__(self): 

125 return self.label or force_str(_("No label")) 

126 

127 import_definitions = { 

128 "https://d-nb.info/*|/.*.rdf": lambda x: load_uri_using_path( 

129 x, RDF_CONFIG_FOLDER / "E74_GroupFromDNB.toml" 

130 ), 

131 "http://www.wikidata.org/*|/.*.rdf": lambda x: load_uri_using_path( 

132 x, RDF_CONFIG_FOLDER / "E74_GroupFromWikidata.toml" 

133 ), 

134 } 

135 

136 @classmethod 

137 def get_rdf_types(cls): 

138 return [CRM.E74_Group] 

139 

140 @classmethod 

141 def create_from_string(cls, string): 

142 return cls.objects.create(label=string) 

143 

144 

145class SimpleLabelModel(models.Model): 

146 label = models.CharField( 

147 blank=True, default="", max_length=4096, verbose_name=_("label") 

148 ) 

149 

150 class Meta: 

151 abstract = True 

152 ordering = ["label"] 

153 

154 def __str__(self): 

155 return self.label or force_str(_("No label")) 

156 

157 @classmethod 

158 def create_from_string(cls, string): 

159 return cls.objects.create(label=string)