Skip to content

Commit 28d3c60

Browse files
committed
More robust email and urls #4 #10
1 parent 21c45e9 commit 28d3c60

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

src/notion_df/configs.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from notion_df.utils import (
2424
flatten_dict,
2525
IDENTITY_TRANSFORM,
26+
REMOVE_EMPTY_STR_TRANSFORM,
2627
SECURE_STR_TRANSFORM,
2728
SECURE_BOOL_TRANSFORM,
2829
SECURE_TIME_TRANSFORM,
@@ -233,10 +234,10 @@ def parse_single_config(data: Dict) -> BasePropertyConfig:
233234
# We assume the users will use the correct type and we don't need to perform any transformation
234235
"people": IDENTITY_TRANSFORM,
235236
"relation": IDENTITY_TRANSFORM,
237+
"url": REMOVE_EMPTY_STR_TRANSFORM,
238+
"email": REMOVE_EMPTY_STR_TRANSFORM,
236239
### TODO: check the following ###
237240
"files": SECURE_STR_TRANSFORM,
238-
"url": SECURE_STR_TRANSFORM,
239-
"email": SECURE_STR_TRANSFORM,
240241
"phone_number": SECURE_STR_TRANSFORM,
241242
"formula": SECURE_STR_TRANSFORM,
242243
"rollup": SECURE_STR_TRANSFORM,

src/notion_df/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ def transform_time(s:Any) -> str:
5757

5858
IDENTITY_TRANSFORM = lambda ele: ele
5959
SECURE_STR_TRANSFORM = lambda ele: str(ele) if not is_item_empty(ele) else ""
60+
REMOVE_EMPTY_STR_TRANSFORM = lambda ele: None if ele=="" or ele is None else SECURE_STR_TRANSFORM(ele)
6061
SECURE_BOOL_TRANSFORM = lambda ele: bool(ele) if not is_item_empty(ele) else None
6162
SECURE_TIME_TRANSFORM = transform_time

src/notion_df/values.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ def value(self) -> List[str]:
131131
@classmethod
132132
def from_value(cls, values: Union[List[str], str]):
133133
if isinstance(values, list):
134-
return cls(
135-
relation=[RelationObject.from_value(value) for value in values]
136-
)
134+
return cls(relation=[RelationObject.from_value(value) for value in values])
137135
else:
138136
return cls(relation=[RelationObject.from_value(values)])
139137

138+
140139
class RollUpValues(BasePropertyValues):
141140
pass
142141

142+
143143
class PeopleValues(BasePropertyValues):
144144
people: List[UserObject]
145145

@@ -150,12 +150,11 @@ def value(self) -> List[str]:
150150
@classmethod
151151
def from_value(cls, values: Union[List[str], str]):
152152
if isinstance(values, list):
153-
return cls(
154-
people=[UserObject.from_value(value) for value in values]
155-
)
153+
return cls(people=[UserObject.from_value(value) for value in values])
156154
else:
157155
return cls(people=[UserObject.from_value(values)])
158156

157+
159158
class FileValues(BasePropertyValues):
160159
pass
161160

@@ -180,9 +179,16 @@ def value(self) -> Optional[str]:
180179
return self.url
181180

182181
@classmethod
183-
def from_value(cls, value: str):
182+
def from_value(cls, value: Optional[str]):
184183
return cls(url=value)
185184

185+
def query_dict(self):
186+
res = flatten_dict(self.dict())
187+
if "url" not in res:
188+
res["url"] = None
189+
# The url value is required by the notion API
190+
return res
191+
186192

187193
class EmailValues(BasePropertyValues):
188194
email: Optional[str]
@@ -277,6 +283,13 @@ def _is_item_empty(item):
277283
return isna
278284

279285

286+
RESERVED_VALUES = ["url"]
287+
288+
289+
def _is_reserved_value(key, schema):
290+
return schema[key].type in RESERVED_VALUES
291+
292+
280293
def parse_value_with_schema(
281294
idx: int, key: str, value: Any, schema: "DatabaseSchema"
282295
) -> BasePropertyValues:
@@ -333,7 +346,7 @@ def from_series(
333346
{
334347
key: parse_value_with_schema(idx, key, val, schema)
335348
for idx, (key, val) in enumerate(series.items())
336-
if not _is_item_empty(val)
349+
if not _is_item_empty(val) or _is_reserved_value(key, schema)
337350
}
338351
)
339352

0 commit comments

Comments
 (0)