Skip to content

Commit ddffc6e

Browse files
update kitem linking model
1 parent 0e4d177 commit ddffc6e

4 files changed

Lines changed: 146 additions & 285 deletions

File tree

dsms/knowledge/compacted.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
"""Compacted Knowledge Item implementation of the DSMS"""
2+
3+
from enum import Enum
4+
from typing import Optional, Union
5+
from uuid import UUID, uuid4
6+
7+
from pydantic import ( # isort: skip
8+
BaseModel,
9+
ConfigDict,
10+
Field,
11+
ValidationInfo,
12+
field_validator,
13+
)
14+
15+
from dsms.core.session import Session # isort: skip
16+
from dsms.knowledge.ktype import KType # isort: skip
17+
from dsms.knowledge.utils import _slugify, print_model # isort: skip
18+
19+
20+
class KItemBaseModel(BaseModel):
21+
"""Basic data model for a KItem"""
22+
23+
id: Optional[UUID] = Field(
24+
default_factory=uuid4,
25+
description="ID of the KItem",
26+
)
27+
28+
29+
class KItemCompactedModel(KItemBaseModel):
30+
"""
31+
KItem compacted model for the search-endpoint."""
32+
33+
name: str = Field(
34+
..., description="Human readable name of the KItem", max_length=300
35+
)
36+
ktype_id: Union[Enum, str] = Field(..., description="Type ID of the KItem")
37+
ktype: Optional[Union[Enum, KType]] = Field(
38+
None, description="KType of the KItem", exclude=True
39+
)
40+
slug: Optional[str] = Field(
41+
None,
42+
description="Slug of the KItem",
43+
min_length=4,
44+
max_length=1000,
45+
)
46+
47+
def __str__(self) -> str:
48+
"""Pretty print the kitem fields"""
49+
return print_model(self, "kitem")
50+
51+
def __repr__(self) -> str:
52+
"""Pretty print the kitem Fields"""
53+
return str(self)
54+
55+
@field_validator("slug")
56+
@classmethod
57+
def validate_slug(cls, value: str, info: ValidationInfo) -> str:
58+
"""Validate slug"""
59+
60+
kitem_id = info.data["id"]
61+
name = info.data["name"]
62+
63+
if not value:
64+
value = _slugify(name)
65+
if len(value) < 4:
66+
raise ValueError(
67+
"Slug length must have a minimum length of 4."
68+
)
69+
if Session.dsms.config.individual_slugs:
70+
value += f"-{str(kitem_id).split('-', maxsplit=1)[0]}"
71+
return value
72+
73+
@field_validator("ktype_id")
74+
@classmethod
75+
def validate_ktype_id(cls, value: Union[str, Enum]) -> KType:
76+
"""Validate the ktype id of the KItem"""
77+
78+
if isinstance(value, str):
79+
ktype = Session.ktypes.get(value)
80+
if not ktype:
81+
raise TypeError(
82+
f"KType for `ktype_id={value}` does not exist."
83+
)
84+
value = ktype
85+
if not hasattr(value, "id"):
86+
raise TypeError(
87+
"Not a valid KType. Provided Enum does not have an `id`."
88+
)
89+
90+
return value.id
91+
92+
@field_validator("ktype")
93+
@classmethod
94+
def validate_ktype(
95+
cls, value: Optional[Union[KType, Enum]], info: ValidationInfo
96+
) -> KType:
97+
"""Validate the ktype of the KItem"""
98+
99+
ktype_id = info.data.get("ktype_id")
100+
101+
if not value:
102+
value = Session.ktypes.get(ktype_id)
103+
if not value:
104+
raise TypeError(
105+
f"KType for `ktype_id={ktype_id}` does not exist."
106+
)
107+
if not hasattr(value, "id"):
108+
raise TypeError(
109+
"Not a valid KType. Provided Enum does not have an `id`."
110+
)
111+
112+
if value.id != ktype_id:
113+
raise TypeError(
114+
f"KType for `ktype_id={ktype_id}` does not match "
115+
f"the provided `ktype`."
116+
)
117+
118+
return value
119+
120+
model_config = ConfigDict(
121+
validate_assignment=True,
122+
validate_default=True,
123+
exclude={"ktype", "avatar"},
124+
arbitrary_types_allowed=True,
125+
)

dsms/knowledge/kitem.py

Lines changed: 7 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
"""Knowledge Item implementation of the DSMS"""
1+
"""Full Knowledge Item implementation of the DSMS"""
22

33
import logging
44
import warnings
55
from datetime import datetime
6-
from enum import Enum
76
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union
87
from urllib.parse import urljoin
9-
from uuid import UUID, uuid4
108

119
import pandas as pd
1210
from rdflib import Graph
1311

1412
from pydantic import ( # isort:skip
1513
BaseModel,
1614
AliasChoices,
17-
ConfigDict,
1815
Field,
1916
ValidationInfo,
2017
field_validator,
@@ -25,6 +22,11 @@
2522

2623
from dsms.core.session import Session # isort:skip
2724

25+
from dsms.knowledge.compacted import ( # isort:skip
26+
KItemBaseModel,
27+
KItemCompactedModel,
28+
)
29+
2830
from dsms.knowledge.properties import ( # isort:skip
2931
Affiliation,
3032
Annotation,
@@ -45,10 +47,10 @@
4547
UserGroup,
4648
)
4749

50+
4851
from dsms.knowledge.ktype import KType # isort:skip
4952

5053
from dsms.knowledge.utils import ( # isort:skip
51-
_slugify,
5254
_inspect_dataframe,
5355
_get_kitem,
5456
_make_annotation_schema,
@@ -80,114 +82,6 @@
8082
DATETIME_FRMT = "%Y-%m-%dT%H:%M:%S.%f"
8183

8284

83-
class KItemBaseModel(BaseModel):
84-
"""Basic data model for a KItem"""
85-
86-
id: Optional[UUID] = Field(
87-
default_factory=uuid4,
88-
description="ID of the KItem",
89-
)
90-
91-
92-
class KItemCompactedModel(KItemBaseModel):
93-
"""
94-
KItem compacted model for the search-endpoint."""
95-
96-
name: str = Field(
97-
..., description="Human readable name of the KItem", max_length=300
98-
)
99-
ktype_id: Union[Enum, str] = Field(..., description="Type ID of the KItem")
100-
ktype: Optional[Union[Enum, KType]] = Field(
101-
None, description="KType of the KItem", exclude=True
102-
)
103-
slug: Optional[str] = Field(
104-
None,
105-
description="Slug of the KItem",
106-
min_length=4,
107-
max_length=1000,
108-
)
109-
110-
def __str__(self) -> str:
111-
"""Pretty print the kitem fields"""
112-
return print_model(self, "kitem")
113-
114-
def __repr__(self) -> str:
115-
"""Pretty print the kitem Fields"""
116-
return str(self)
117-
118-
@field_validator("slug")
119-
@classmethod
120-
def validate_slug(cls, value: str, info: ValidationInfo) -> str:
121-
"""Validate slug"""
122-
123-
kitem_id = info.data["id"]
124-
name = info.data["name"]
125-
126-
if not value:
127-
value = _slugify(name)
128-
if len(value) < 4:
129-
raise ValueError(
130-
"Slug length must have a minimum length of 4."
131-
)
132-
if Session.dsms.config.individual_slugs:
133-
value += f"-{str(kitem_id).split('-', maxsplit=1)[0]}"
134-
return value
135-
136-
@field_validator("ktype_id")
137-
@classmethod
138-
def validate_ktype_id(cls, value: Union[str, Enum]) -> KType:
139-
"""Validate the ktype id of the KItem"""
140-
141-
if isinstance(value, str):
142-
ktype = Session.ktypes.get(value)
143-
if not ktype:
144-
raise TypeError(
145-
f"KType for `ktype_id={value}` does not exist."
146-
)
147-
value = ktype
148-
if not hasattr(value, "id"):
149-
raise TypeError(
150-
"Not a valid KType. Provided Enum does not have an `id`."
151-
)
152-
153-
return value.id
154-
155-
@field_validator("ktype")
156-
@classmethod
157-
def validate_ktype(
158-
cls, value: Optional[Union[KType, Enum]], info: ValidationInfo
159-
) -> KType:
160-
"""Validate the ktype of the KItem"""
161-
162-
ktype_id = info.data.get("ktype_id")
163-
164-
if not value:
165-
value = Session.ktypes.get(ktype_id)
166-
if not value:
167-
raise TypeError(
168-
f"KType for `ktype_id={ktype_id}` does not exist."
169-
)
170-
if not hasattr(value, "id"):
171-
raise TypeError(
172-
"Not a valid KType. Provided Enum does not have an `id`."
173-
)
174-
175-
if value.id != ktype_id:
176-
raise TypeError(
177-
f"KType for `ktype_id={ktype_id}` does not match "
178-
f"the provided `ktype`."
179-
)
180-
181-
return value
182-
183-
model_config = ConfigDict(
184-
validate_assignment=True,
185-
validate_default=True,
186-
exclude={"ktype", "avatar"},
187-
arbitrary_types_allowed=True,
188-
)
189-
190-
19185
class KItem(KItemCompactedModel):
19286
"""
19387
Knowledge Item of the DSMS.

dsms/knowledge/properties/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222

2323
from dsms.knowledge.properties.linked_kitems import ( # isort:skip
24-
LinkedKItem,
2524
LinkedKItemsList,
2625
KItemRelationshipModel,
2726
)
@@ -42,7 +41,6 @@
4241
"LinkedKItemsList",
4342
"Author",
4443
"Avatar",
45-
"LinkedKItem",
4644
"ContactInfo",
4745
"ExternalLink",
4846
"Affiliation",

0 commit comments

Comments
 (0)