-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LITE-29268 User can delete or update a feed
- Loading branch information
Showing
9 changed files
with
361 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,41 @@ | ||
from datetime import datetime | ||
from typing import Dict, Optional, Union | ||
from typing import Any, Dict | ||
|
||
from pydantic import BaseModel, root_validator | ||
|
||
_By = Optional[Union[str, Dict[str, str]]] | ||
Events = Dict[str, Dict[str, Union[datetime, _By]]] | ||
|
||
|
||
def clean_empties_from_dict(data): | ||
def flatten(d: Dict[Any, Any]): | ||
""" | ||
Removes inplace all the fields that are None or empty dicts in data. | ||
Returns param data, that was modified inplace. | ||
If the param is not a dict, will return the param unmodified. | ||
:param data: dict | ||
:rtype: dict | ||
Transform nested dict to flat. | ||
:param data: dict | ||
:rtype: dict | ||
``` | ||
>>> d = { | ||
'greatgrandparent': { | ||
'grandparent': { | ||
'parent': { | ||
'child1': 'test', | ||
'child2': 'other_test', | ||
}, | ||
}, | ||
}, | ||
} | ||
>>> flatten(d) | ||
{ | ||
'greatgrandparent_grandparent_parent_child1': 'test', | ||
'greatgrandparent_grandparent_parent_child2': 'other_test', | ||
} | ||
``` | ||
""" | ||
if not isinstance(data, dict): | ||
return data | ||
|
||
for key in list(data.keys()): | ||
value = data[key] | ||
if isinstance(value, dict): | ||
clean_empties_from_dict(value) | ||
value = data[key] | ||
if not value: | ||
del data[key] | ||
return data | ||
|
||
|
||
class NonNullSchema(BaseModel): | ||
def dict(self, *args, **kwargs): | ||
kwargs['exclude_none'] = True | ||
return super().dict(*args, **kwargs) | ||
|
||
@root_validator(pre=True) | ||
def validate_events(cls, values): | ||
events = values.get('events') | ||
if events: | ||
values['events'] = clean_empties_from_dict(events) | ||
return values | ||
|
||
|
||
class ReferenceSchema(NonNullSchema): | ||
id: str | ||
name: Optional[str] | ||
out = {} | ||
for key, val in d.items(): | ||
if isinstance(val, dict): | ||
val = [val] | ||
if isinstance(val, list): | ||
for subdict in val: | ||
deeper = flatten(subdict).items() | ||
out.update( | ||
{key + '_' + key2: val2 for key2, val2 in deeper}, | ||
) | ||
else: | ||
out[key] = val | ||
return out |
Oops, something went wrong.