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

69 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-12-04 11:32 +0000

1from django.db import models 

2from django.utils.encoding import force_str 

3from django.utils.translation import gettext_lazy as _ 

4 

5from apis_core.generic.utils.rdf_namespace import CRM 

6from apis_core.utils.rdf import load_uri_using_path 

7 

8######################### 

9# Abstract base classes # 

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

11 

12 

13# These abstract base classes are named after 

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

15# trying to implement CIDOC CRM in Django. 

16 

17 

18class E21_Person(models.Model): 

19 forename = models.CharField( 

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

21 ) 

22 surname = models.CharField( 

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

24 ) 

25 gender = models.CharField( 

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

27 ) 

28 date_of_birth = models.DateField( 

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

30 ) 

31 date_of_death = models.DateField( 

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

33 ) 

34 

35 class Meta: 

36 abstract = True 

37 verbose_name = _("person") 

38 verbose_name_plural = _("persons") 

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

40 

41 def __str__(self): 

42 if self.forename and self.surname: 

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

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

45 

46 import_definitions = { 

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

48 x, "triple_configs/E21_PersonFromDNB.toml" 

49 ), 

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

51 x, "triple_configs/E21_PersonFromWikidata.toml" 

52 ), 

53 } 

54 

55 @classmethod 

56 def get_rdf_types(cls): 

57 return [CRM.E21_Person] 

58 

59 

60class E53_Place(models.Model): 

61 """ 

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

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

64 """ 

65 

66 label = models.CharField( 

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

68 ) 

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

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

71 feature_code = models.CharField( 

72 blank=True, 

73 default="", 

74 max_length=16, 

75 verbose_name=_("feature code"), 

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

77 ) 

78 

79 class Meta: 

80 abstract = True 

81 verbose_name = _("place") 

82 verbose_name_plural = _("places") 

83 ordering = ["label"] 

84 

85 def __str__(self): 

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

87 

88 import_definitions = { 

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

90 x, "triple_configs/E53_PlaceFromDNB.toml" 

91 ), 

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

93 x, "triple_configs/E53_PlaceFromGeonames.toml" 

94 ), 

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

96 x, "triple_configs/E53_PlaceFromWikidata.toml" 

97 ), 

98 } 

99 

100 @classmethod 

101 def get_rdf_types(cls): 

102 return [CRM.E53_Place] 

103 

104 @classmethod 

105 def create_from_string(cls, string): 

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

107 

108 

109class E74_Group(models.Model): 

110 label = models.CharField( 

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

112 ) 

113 

114 class Meta: 

115 abstract = True 

116 verbose_name = _("group") 

117 verbose_name_plural = _("groups") 

118 ordering = ["label"] 

119 

120 def __str__(self): 

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

122 

123 import_definitions = { 

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

125 x, "triple_configs/E74_GroupFromDNB.toml" 

126 ), 

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

128 x, "triple_configs/E74_GroupFromWikidata.toml" 

129 ), 

130 } 

131 

132 @classmethod 

133 def get_rdf_types(cls): 

134 return [CRM.E74_Group] 

135 

136 @classmethod 

137 def create_from_string(cls, string): 

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

139 

140 

141class SimpleLabelModel(models.Model): 

142 label = models.CharField( 

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

144 ) 

145 

146 class Meta: 

147 abstract = True 

148 ordering = ["label"] 

149 

150 def __str__(self): 

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

152 

153 @classmethod 

154 def create_from_string(cls, string): 

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