Coverage for toardb/contacts/schemas.py: 96%

77 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-03 20:32 +0000

1# SPDX-FileCopyrightText: 2021 Forschungszentrum Jülich GmbH 

2# SPDX-License-Identifier: MIT 

3 

4""" 

5Pydantic schemas for TOAR database 

6 

7""" 

8 

9from typing import List, Dict, Any 

10from pydantic import BaseModel, validator, Field 

11#from toardb.toardb import OK_vocabulary, CN_vocabulary 

12import toardb 

13 

14class OrganisationBase(BaseModel): 

15 id: int = Field(None, description="for internal use only") 

16 name: str = Field(..., description="Short name (abbreviation) of organisation") 

17 longname: str = Field(..., description="Long name of organisation") 

18 kind: str = Field(..., description="Kind of organisation (see controlled vocabulary: Kind Of Organization)") 

19 city: str = Field(..., description="City where organisation resides") 

20 postcode: str = Field(..., description="Postcode of organisation city") 

21 street_address: str = Field(..., description="Street address of organisation city") 

22 country: str = Field(..., description="Country to which organisation belongs (see controlled vocabulary: Country Code)") 

23 homepage: str = Field(..., description="Homepage of organisation") 

24 contact_url: str = Field(..., description="web form or general e-mail contact of organisation") 

25 

26 @validator('kind') 

27 def check_kind(cls, v): 

28 return tuple(filter(lambda x: x.value == int(v), toardb.toardb.OK_vocabulary))[0].display_str 

29 

30 @validator('country') 

31 def check_country(cls, v): 

32 return tuple(filter(lambda x: x.value == int(v), toardb.toardb.CN_vocabulary))[0].display_str 

33 

34 

35class OrganisationCreate(OrganisationBase): 

36 kind: str 

37 country: str 

38 attribution: str = Field("", description="attribution when using data from this organisation") 

39 pass 

40 

41 @validator('kind') 

42 def check_kind(cls, v): 

43 if tuple(filter(lambda x: x.string == v, toardb.toardb.OK_vocabulary)): 

44 return v 

45 else: 

46 raise ValueError(f"kind of organisation not known: {v}") 

47 

48 @validator('country') 

49 def check_country(cls, v): 

50 if tuple(filter(lambda x: x.string == v, toardb.toardb.CN_vocabulary)): 

51 return v 

52 else: 

53 raise ValueError(f"country not known: {v}") 

54 

55 

56class Organisation(OrganisationBase): 

57 id: int = Field(..., description="for internal use only") 

58 

59 class Config: 

60 orm_mode = True 

61 

62 

63class PersonBase(BaseModel): 

64 id: int = Field(None, description="for internal use only") 

65 name: str = Field(None, description="Name of person") 

66 email: str = Field(None, description="Email address of person") 

67 phone: str = Field(None, description="Phone number of person") 

68 orcid: str = Field(None, description="ORCID-iD of person") 

69 isprivate: bool = Field(None, description="Set this flag to true if the contact details shall not be exposed publicly") 

70 

71 def __str__(self): 

72 return f"{self.name} <{self.email}>" 

73 

74 class Meta: 

75 db_table = 'persons' 

76# app_label = 'Person' 

77 

78class PersonCreate(PersonBase): 

79 pass 

80 

81 

82class Person(PersonBase): 

83 id: int = Field(None, description="for internal use only") 

84 

85 class Config: 

86 orm_mode = True 

87 

88# ======== for nested view ========= 

89 

90class ContactBase(BaseModel): 

91 id: int = Field(None, description="for internal use only") 

92 person: Person = Field(None, description="data for person of the contact information") 

93 organisation: Organisation = Field(None, description="data for organisation of the contact information") 

94 

95 class Config: 

96 orm_mode = True 

97 

98class Contact(ContactBase): 

99 id: int = Field(..., description="for internal use only") 

100 

101 @validator('person') 

102 def check_contact_is_private(cls, v): 

103 if (v.id != 0): 

104 if (v.isprivate): 

105 return None 

106 else: 

107 return v 

108 else: 

109 return None 

110 

111 @validator('organisation') 

112 def check_contact_is_organisation(cls, v): 

113 if (v.id != 0): 

114 return v 

115 

116 class Config: 

117 orm_mode = True 

118