|
50 | 50 | from lib.core.jsx_conditions import Query |
51 | 51 | from lib.core.reporter import Reporter |
52 | 52 | from lib.core.response import Response |
53 | | -from lib.core.service_types import PROJECT_TYPE_RESPONSE_MAP |
54 | | -from lib.core.usecases import serialize_item_entity |
55 | 53 | from lib.infrastructure.custom_entities import generate_schema |
56 | 54 | from lib.infrastructure.helpers import timed_lru_cache |
57 | 55 | from lib.infrastructure.query_builder import FieldValidationHandler |
@@ -932,15 +930,16 @@ def process_response( |
932 | 930 | service_provider, |
933 | 931 | items: List[BaseItemEntity], |
934 | 932 | project: ProjectEntity, |
935 | | - folder: FolderEntity, |
| 933 | + folder: Optional[FolderEntity] = None, |
936 | 934 | map_fields: bool = True, |
937 | 935 | ) -> List[BaseItemEntity]: |
938 | 936 | """Process the response data and return a list of serialized items.""" |
939 | 937 | data = [] |
940 | 938 | for item in items: |
941 | 939 | if map_fields: |
942 | 940 | item = usecases.serialize_item_entity(item, project) |
943 | | - item = usecases.add_item_path(project, folder, item) |
| 941 | + if folder: |
| 942 | + item = usecases.add_item_path(project, folder, item) |
944 | 943 | else: |
945 | 944 | item = usecases.serialize_item_entity(item, project, map_fields=False) |
946 | 945 | item.annotation_status = service_provider.get_annotation_status_name( |
@@ -985,6 +984,30 @@ def list_items( |
985 | 984 | data = self.service_provider.item_service.list(project.id, folder.id, query) |
986 | 985 | return self.process_response(self.service_provider, data, project, folder) |
987 | 986 |
|
| 987 | + def get_item_by_id( |
| 988 | + self, |
| 989 | + item_id: int, |
| 990 | + project: ProjectEntity, |
| 991 | + include: List[Literal["categories", "assignments"]] = None, |
| 992 | + ) -> BaseItemEntity: |
| 993 | + query = EmptyQuery() |
| 994 | + |
| 995 | + if include: |
| 996 | + if "assignments" in include: |
| 997 | + query &= Join("assignments") |
| 998 | + if "categories" in include: |
| 999 | + # join item categories for multimodal projects |
| 1000 | + query &= Join("categories") |
| 1001 | + |
| 1002 | + response = self.service_provider.item_service.get( |
| 1003 | + project_id=project.id, item_id=item_id, query=query |
| 1004 | + ) |
| 1005 | + if response.error: |
| 1006 | + raise AppException(response.error) |
| 1007 | + |
| 1008 | + item = self.process_response(self.service_provider, [response.data], project)[0] |
| 1009 | + return item |
| 1010 | + |
988 | 1011 | def attach( |
989 | 1012 | self, |
990 | 1013 | project: ProjectEntity, |
@@ -1631,19 +1654,6 @@ def get_project_by_id(self, project_id: int): |
1631 | 1654 | raise AppException("Project not found.") |
1632 | 1655 | return response |
1633 | 1656 |
|
1634 | | - def get_item_by_id(self, item_id: int, project: ProjectEntity) -> BaseItemEntity: |
1635 | | - response = self.service_provider.item_service.get( |
1636 | | - project_id=project.id, item_id=item_id |
1637 | | - ) |
1638 | | - if response.error: |
1639 | | - raise AppException(response.error) |
1640 | | - PROJECT_TYPE_RESPONSE_MAP[project.type] = response.data |
1641 | | - item = serialize_item_entity(response.data, project) |
1642 | | - item.annotation_status = self.service_provider.get_annotation_status_name( |
1643 | | - project, item.annotation_status |
1644 | | - ) |
1645 | | - return item |
1646 | | - |
1647 | 1657 | def get_project_folder_by_path( |
1648 | 1658 | self, path: Union[str, Path] |
1649 | 1659 | ) -> Tuple[ProjectEntity, FolderEntity]: |
@@ -2044,7 +2054,7 @@ def get_item( |
2044 | 2054 | self, project: ProjectEntity, folder: FolderEntity, item: Union[int, str] |
2045 | 2055 | ) -> BaseItemEntity: |
2046 | 2056 | if isinstance(item, int): |
2047 | | - return self.get_item_by_id(item_id=item, project=project) |
| 2057 | + return self.items.get_item_by_id(item_id=item, project=project) |
2048 | 2058 | else: |
2049 | 2059 | return self.items.get_by_name(project, folder, item) |
2050 | 2060 |
|
|
0 commit comments