Skip to content

Commit 5bdeb37

Browse files
WIP
1 parent f932ed5 commit 5bdeb37

4 files changed

Lines changed: 48 additions & 27 deletions

File tree

ted_sws/core/model/lazy_object.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,30 @@ def load_lazy_field(self, source_object: Any, property_field: property) -> Any:
1414
"""
1515

1616

17-
class LazyObject:
18-
_lazy_object_fields_loader: LazyObjectFieldsLoaderABC = None
17+
class LazyObjectABC(abc.ABC):
1918

19+
@abc.abstractmethod
2020
def set_lazy_object_fields_loader(self, lazy_object_fields_loader: LazyObjectFieldsLoaderABC):
2121
"""
2222
2323
:param lazy_object_fields_loader:
2424
:return:
2525
"""
26-
self._lazy_object_fields_loader = lazy_object_fields_loader
2726

27+
@abc.abstractmethod
2828
def get_lazy_object_fields_loader(self) -> Optional[LazyObjectFieldsLoaderABC]:
2929
"""
3030
3131
:return:
3232
"""
33-
return self._lazy_object_fields_loader
3433

3534
def load_lazy_field(self, property_field: property):
3635
"""
3736
3837
:param property_field:
3938
:return:
4039
"""
41-
if self._lazy_object_fields_loader:
42-
return self._lazy_object_fields_loader.load_lazy_field(source_object=self, property_field=property_field)
40+
if self.get_lazy_object_fields_loader():
41+
return self.get_lazy_object_fields_loader().load_lazy_field(source_object=self,
42+
property_field=property_field)
4343
return None

ted_sws/core/model/notice.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from __future__ import annotations
1414

1515
import abc
16+
from abc import ABC
1617
from datetime import datetime
1718
from enum import IntEnum
1819
from functools import total_ordering
@@ -21,7 +22,7 @@
2122
from pydantic import Field
2223

2324
from ted_sws.core.model import PropertyBaseModel
24-
from ted_sws.core.model.lazy_object import LazyObject
25+
from ted_sws.core.model.lazy_object import LazyObjectABC, LazyObjectFieldsLoaderABC
2526
from ted_sws.core.model.manifestation import METSManifestation, RDFManifestation, XMLManifestation, \
2627
RDFValidationManifestation, SPARQLTestSuiteValidationReport, SHACLTestSuiteValidationReport, \
2728
XPATHCoverageValidationReport, XMLValidationManifestation, ValidationSummaryReport
@@ -143,7 +144,26 @@ def update_status_to(self, new_status):
143144
"""
144145

145146

146-
class Notice(WorkExpression, LazyObject):
147+
class LazyWorkExpression(WorkExpression, LazyObjectABC, ABC):
148+
_lazy_object_fields_loader: LazyObjectFieldsLoaderABC = None
149+
150+
def set_lazy_object_fields_loader(self, lazy_object_fields_loader: LazyObjectFieldsLoaderABC):
151+
"""
152+
153+
:param lazy_object_fields_loader:
154+
:return:
155+
"""
156+
self._lazy_object_fields_loader = lazy_object_fields_loader
157+
158+
def get_lazy_object_fields_loader(self) -> Optional[LazyObjectFieldsLoaderABC]:
159+
"""
160+
161+
:return:
162+
"""
163+
return self._lazy_object_fields_loader
164+
165+
166+
class Notice(LazyWorkExpression):
147167
"""
148168
A TED notice in any of its forms across the TED-SWS pipeline. This class is conceptualised as a merger of Work
149169
and Expression in the FRBR class hierarchy and is connected to some of its Manifestations.

ted_sws/data_manager/adapters/notice_repository.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141

4242
METADATA_PUBLICATION_DATE = "publication_date"
4343
METADATA_DOCUMENT_SENT_DATE = "document_sent_date"
44-
FILE_STORAGE_COLLECTION_NAME = "fs.files"
4544

4645

4746
class NoticeRepositoryInFileSystem(NoticeRepositoryABC):
@@ -200,20 +199,20 @@ def _write_lazy_fields(self, notice: Notice):
200199
if notice_field is not None:
201200
repository.add(notice.ted_id, notice_field)
202201

203-
@staticmethod
204-
def _create_notice_from_repository_result(notice_dict: dict) -> Union[Notice, None]:
202+
def _create_notice_from_repository_result(self, notice_dict: dict) -> Union[Notice, None]:
205203
"""
206204
This method allows you to create a Notice from the dictionary extracted from the repository.
207205
:param notice_dict:
208206
:return:
209207
"""
210208
if notice_dict:
211209
del notice_dict[MONGODB_COLLECTION_ID]
212-
notice_dict.pop(NOTICE_NORMALISED_METADATA)
210+
notice_dict.pop(NOTICE_NORMALISED_METADATA,None)
213211
remove_date_string_fields(data=notice_dict, date_field_name=NOTICE_CREATED_AT)
214212
notice_dict[NOTICE_CREATED_AT] = notice_dict[NOTICE_CREATED_AT].isoformat()
215213
notice = Notice(**notice_dict)
216214
notice._status = NoticeStatus[notice_dict[NOTICE_STATUS]]
215+
notice.set_lazy_object_fields_loader(lazy_object_fields_loader=self)
217216
return notice
218217
return None
219218

@@ -225,7 +224,7 @@ def _create_dict_from_notice(notice: Notice) -> dict:
225224
:return:
226225
"""
227226

228-
notice_dict = notice.dict(include=[NOTICE_TED_ID, NOTICE_STATUS, NOTICE_CREATED_AT])
227+
notice_dict = notice.dict(include={NOTICE_TED_ID: True, NOTICE_STATUS: True, NOTICE_CREATED_AT: True})
229228
notice_dict[MONGODB_COLLECTION_ID] = notice_dict[NOTICE_TED_ID]
230229
notice_dict[NOTICE_STATUS] = str(notice_dict[NOTICE_STATUS])
231230
notice_dict[NOTICE_CREATED_AT] = datetime.fromisoformat(notice_dict[NOTICE_CREATED_AT])
@@ -278,7 +277,7 @@ def get(self, reference) -> Optional[Notice]:
278277
"""
279278
result_dict = self.collection.find_one({MONGODB_COLLECTION_ID: reference})
280279
if result_dict is not None:
281-
notice = NoticeRepository._create_notice_from_repository_result(result_dict)
280+
notice = self._create_notice_from_repository_result(result_dict)
282281
return notice
283282
return None
284283

@@ -289,7 +288,7 @@ def get_notices_by_status(self, notice_status: NoticeStatus) -> Iterator[Notice]
289288
:return:
290289
"""
291290
for result_dict in self.collection.find({NOTICE_STATUS: str(notice_status)}):
292-
notice = NoticeRepository._create_notice_from_repository_result(result_dict)
291+
notice = self._create_notice_from_repository_result(result_dict)
293292
yield notice
294293

295294
def get_notice_ids_by_status(self, notice_status: NoticeStatus) -> Iterator[str]:
@@ -307,5 +306,5 @@ def list(self) -> Iterator[Notice]:
307306
:return: list of notices
308307
"""
309308
for result_dict in self.collection.find():
310-
notice = NoticeRepository._create_notice_from_repository_result(result_dict)
309+
notice = self._create_notice_from_repository_result(result_dict)
311310
yield notice

tests/unit/core/model/test_new_notice_model.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,22 @@ class BigString(str):
8383
gridfs_id: str = None
8484

8585

86-
class NoticeModel(WorkExpression):
86+
class LazyWorkExpression(WorkExpression):
87+
88+
_test_field :str = None
89+
90+
class NoticeModel(LazyWorkExpression):
8791
"""
8892
8993
"""
9094

91-
_x: Optional[int] = Field(alias="x", default=None)
95+
_x: Optional[int] = 10
9296

9397
object_data : BigString = None
9498

99+
def __init__(self, *args, **kvargs):
100+
super().__init__(**kvargs)
101+
95102
@property
96103
def x(self):
97104
print("HELLO")
@@ -110,12 +117,7 @@ def print_property_name(property_field: property):
110117

111118

112119
def test_new_notice_model():
113-
# notice = NoticeModel(x=10)
114-
# loader_map = {NoticeModel.x : 10,
115-
# NoticeModel.y : 6
116-
# }
117-
# print(loader_map[NoticeModel.x])
118-
# print(loader_map[NoticeModel.y])
119-
120-
a = None
121-
print([x for x in a])
120+
notice = NoticeModel(x=10)
121+
notice._test_field = "ha"
122+
print(notice)
123+

0 commit comments

Comments
 (0)