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
« 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
4"""
5Pydantic schemas for TOAR database
7"""
9from typing import List, Dict, Any
10from pydantic import BaseModel, validator, Field
11#from toardb.toardb import OK_vocabulary, CN_vocabulary
12import toardb
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")
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
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
35class OrganisationCreate(OrganisationBase):
36 kind: str
37 country: str
38 attribution: str = Field("", description="attribution when using data from this organisation")
39 pass
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}")
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}")
56class Organisation(OrganisationBase):
57 id: int = Field(..., description="for internal use only")
59 class Config:
60 orm_mode = True
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")
71 def __str__(self):
72 return f"{self.name} <{self.email}>"
74 class Meta:
75 db_table = 'persons'
76# app_label = 'Person'
78class PersonCreate(PersonBase):
79 pass
82class Person(PersonBase):
83 id: int = Field(None, description="for internal use only")
85 class Config:
86 orm_mode = True
88# ======== for nested view =========
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")
95 class Config:
96 orm_mode = True
98class Contact(ContactBase):
99 id: int = Field(..., description="for internal use only")
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
111 @validator('organisation')
112 def check_contact_is_organisation(cls, v):
113 if (v.id != 0):
114 return v
116 class Config:
117 orm_mode = True