Skip to content

Commit

Permalink
misc: Revert removal of description used in Index (#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim-Encord authored Dec 19, 2024
1 parent 3897f76 commit 4751e20
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 27 deletions.
50 changes: 30 additions & 20 deletions encord/filter_preset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
)
from encord.http.v2.api_client import ApiClient
from encord.orm.filter_preset import (
ActiveCreatePresetPayload,
ActiveFilterPresetDefinition,
ActiveUpdatePresetPayload,
CreatePresetParams,
CreatePresetPayload,
FilterPresetDefinition,
GetPresetParams,
GetPresetsResponse,
GetProjectFilterPresetParams,
UpdatePresetPayload,
IndexCreatePresetPayload,
IndexFilterPresetDefinition,
IndexUpdatePresetPayload,
)
from encord.orm.filter_preset import FilterPreset as OrmFilterPreset
from encord.orm.filter_preset import ProjectFilterPreset as OrmProjectFilterPreset
Expand Down Expand Up @@ -91,7 +94,7 @@ def _get_preset(api_client: ApiClient, preset_uuid: UUID) -> "FilterPreset":
)
if len(orm_item.results) > 0:
return FilterPreset(api_client, orm_item.results[0])
raise AuthorisationError("Collection not found")
raise AuthorisationError("Preset not found")

@staticmethod
def _get_presets(
Expand Down Expand Up @@ -130,13 +133,14 @@ def _delete_preset(api_client: ApiClient, preset_uuid: UUID) -> None:
)

@staticmethod
def _create_preset(api_client: ApiClient, name: str, *, filter_preset_json: dict) -> UUID:
filter_preset = FilterPresetDefinition.from_dict(filter_preset_json)
def _create_preset(api_client: ApiClient, name: str, description: str = "", *, filter_preset_json: dict) -> UUID:
filter_preset = IndexFilterPresetDefinition.from_dict(filter_preset_json)
if not filter_preset.local_filters and not filter_preset.global_filters:
raise EncordException("We require there to be a non-zero number of filters in a preset")
payload = CreatePresetPayload(
payload = IndexCreatePresetPayload(
name=name,
filter_preset_json=filter_preset.to_dict(),
description=description,
)
return api_client.post(
"index/presets",
Expand All @@ -145,14 +149,16 @@ def _create_preset(api_client: ApiClient, name: str, *, filter_preset_json: dict
result_type=UUID,
)

def get_filter_preset_json(self) -> FilterPresetDefinition:
def get_filter_preset_json(self) -> IndexFilterPresetDefinition:
return self._client.get(
f"index/presets/{self._preset_instance.uuid}",
params=None,
result_type=FilterPresetDefinition,
result_type=IndexFilterPresetDefinition,
)

def update_preset(self, name: Optional[str] = None, filter_preset_json: Optional[dict] = None) -> None:
def update_preset(
self, name: Optional[str] = None, description: Optional[str] = None, filter_preset_json: Optional[dict] = None
) -> None:
"""
Update the preset's definition.
Args:
Expand All @@ -162,19 +168,21 @@ def update_preset(self, name: Optional[str] = None, filter_preset_json: Optional
"""
filters_definition = None
if isinstance(filter_preset_json, dict):
filters_definition = FilterPresetDefinition.from_dict(filter_preset_json)
elif isinstance(filter_preset_json, FilterPresetDefinition):
filters_definition = IndexFilterPresetDefinition.from_dict(filter_preset_json)
elif isinstance(filter_preset_json, IndexFilterPresetDefinition):
filters_definition = filter_preset_json
if filters_definition:
if not filters_definition.local_filters and not filters_definition.global_filters:
raise EncordException("We require there to be a non-zero number of filters in a preset")
payload = UpdatePresetPayload(name=name, filter_preset=filters_definition)
payload = IndexUpdatePresetPayload(name=name, description=description, filter_preset=filters_definition)
self._client.patch(
f"index/presets/{self.uuid}",
params=None,
payload=payload,
result_type=None,
)
self._preset_instance.name = name or self.name
self._preset_instance.description = description or self.description


class ProjectFilterPreset:
Expand Down Expand Up @@ -261,7 +269,7 @@ def _get_filter_preset(
client=client,
orm_filter_preset=orm_items[0],
)
raise AuthorisationError("No collection found")
raise AuthorisationError("No Project preset found")

@staticmethod
def _list_filter_presets(
Expand Down Expand Up @@ -291,27 +299,29 @@ def _delete_filter_preset(client: ApiClient, project_uuid: UUID, filter_preset_u
result_type=None,
)

def get_filter_preset_json(self) -> FilterPresetDefinition:
def get_filter_preset_json(self) -> ActiveFilterPresetDefinition:
return self._client.get(
f"active/{self._project_uuid}/presets/{self._filter_preset_instance.preset_uuid}/raw",
params=None,
result_type=FilterPresetDefinition,
result_type=ActiveFilterPresetDefinition,
)

def update_preset(self, name: Optional[str] = None, filter_preset: Optional[FilterPresetDefinition] = None) -> None:
def update_preset(
self, name: Optional[str] = None, filter_preset: Optional[ActiveFilterPresetDefinition] = None
) -> None:
if name is None and filter_preset is None:
return
payload = UpdatePresetPayload(name=name, filter_preset=filter_preset)
payload = ActiveUpdatePresetPayload(name=name, filter_preset=filter_preset)
self._client.patch(
f"active/{self.project_hash}/presets/{self.uuid}", params=None, payload=payload, result_type=None
)

@staticmethod
def _create_filter_preset(
client: ApiClient, project_uuid: UUID, name: str, filter_preset: FilterPresetDefinition
client: ApiClient, project_uuid: UUID, name: str, filter_preset: ActiveFilterPresetDefinition
) -> UUID:
if not filter_preset.local_filters and not filter_preset.global_filters:
raise EncordException("We require there to be a non-zero number of filters in a preset for creation")
payload = CreatePresetPayload(name=name, filter_preset_json=filter_preset.to_dict())
payload = ActiveCreatePresetPayload(name=name, filter_preset_json=filter_preset.to_dict())
orm_resp = client.post(f"active/{project_uuid}/presets", params=None, payload=payload, result_type=UUID)
return orm_resp
30 changes: 26 additions & 4 deletions encord/orm/filter_preset.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ class FilterDefinition(BaseDTO):
filters: List[Dict] = Field(default_factory=list)


class FilterPresetDefinition(BaseDTO):
# Note alias is strictly required as these are stored in the Annotate DB as unstructured objects
# Stored not in camelCase like most Models
class IndexFilterPresetDefinition(BaseDTO):
local_filters: Dict[str, FilterDefinition] = Field(
default_factory=lambda: {str(uuid.UUID(int=0)): FilterDefinition()},
alias="local_filters",
)
global_filters: FilterDefinition = Field(default_factory=FilterDefinition, alias="global_filters")


class ActiveFilterPresetDefinition(BaseDTO):
local_filters: Dict[str, FilterDefinition] = Field(
default_factory=lambda: {str(uuid.UUID(int=0)): FilterDefinition()},
)
Expand All @@ -53,11 +63,23 @@ class CreatePresetParams(BaseDTO):
top_level_folder_uuid: UUID = Field(default=UUID(int=0), alias="topLevelFolderUuid")


class CreatePresetPayload(BaseDTO):
class IndexCreatePresetPayload(BaseDTO):
name: str
filter_preset_json: Dict
description: Optional[str] = ""


class ActiveCreatePresetPayload(BaseDTO):
name: str
filter_preset_json: Dict


class UpdatePresetPayload(BaseDTO):
class IndexUpdatePresetPayload(BaseDTO):
name: Optional[str] = None
description: Optional[str] = ""
filter_preset: Optional[IndexFilterPresetDefinition] = None


class ActiveUpdatePresetPayload(BaseDTO):
name: Optional[str] = None
filter_preset: Optional[FilterPresetDefinition] = None
filter_preset: Optional[ActiveFilterPresetDefinition] = None
4 changes: 2 additions & 2 deletions encord/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from encord.orm.cloud_integration import CloudIntegration
from encord.orm.collection import ProjectCollectionType
from encord.orm.dataset import Image, Video
from encord.orm.filter_preset import FilterPresetDefinition
from encord.orm.filter_preset import ActiveFilterPresetDefinition
from encord.orm.group import ProjectGroup
from encord.orm.label_log import LabelLog
from encord.orm.label_row import (
Expand Down Expand Up @@ -1316,7 +1316,7 @@ def delete_filter_preset(self, filter_preset_uuid: Union[str, UUID]) -> None:
filter_preset_uuid=UUID(filter_preset_uuid) if isinstance(filter_preset_uuid, str) else filter_preset_uuid,
)

def create_filter_preset(self, name: str, filter_preset: FilterPresetDefinition) -> ProjectFilterPreset:
def create_filter_preset(self, name: str, filter_preset: ActiveFilterPresetDefinition) -> ProjectFilterPreset:
uuid = ProjectFilterPreset._create_filter_preset(
client=self._client._get_api_client(),
project_uuid=self._project_instance.project_hash,
Expand Down
4 changes: 3 additions & 1 deletion encord/user_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,9 @@ def create_preset(self, name: str, filter_preset_json: dict, description: str =
Returns:
FilterPreset: Newly created collection.
"""
new_uuid = FilterPreset._create_preset(self._api_client, name, filter_preset_json=filter_preset_json)
new_uuid = FilterPreset._create_preset(
self._api_client, name, description=description, filter_preset_json=filter_preset_json
)
return self.get_filter_preset(new_uuid)

def delete_preset(self, preset_uuid: Union[str, UUID]) -> None:
Expand Down

0 comments on commit 4751e20

Please sign in to comment.