From 5416dd789476e5d954558cfcc79716dd067752a9 Mon Sep 17 00:00:00 2001 From: CaptainOfHacks <39195263+CaptainOfHacks@users.noreply.github.com> Date: Wed, 27 Sep 2023 14:49:42 +0300 Subject: [PATCH] Fix when fetch already fetched notices don't remove old lazy_fields --- .../data_manager/adapters/repository_abc.py | 2 ++ .../notice_fetcher/services/notice_fetcher.py | 32 ++++++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/ted_sws/data_manager/adapters/repository_abc.py b/ted_sws/data_manager/adapters/repository_abc.py index 026c18102..939889d0e 100644 --- a/ted_sws/data_manager/adapters/repository_abc.py +++ b/ted_sws/data_manager/adapters/repository_abc.py @@ -53,6 +53,7 @@ def remove(self, reference: str): :return: """ + class ManifestationRepositoryABC(RepositoryABC): """ This repository is intended for storing Manifestation objects. @@ -92,6 +93,7 @@ def remove(self, reference: str): :return: """ + class NoticeRepositoryABC(RepositoryABC): """ This repository is intended for storing Notice objects. diff --git a/ted_sws/notice_fetcher/services/notice_fetcher.py b/ted_sws/notice_fetcher/services/notice_fetcher.py index 9aae0ce0b..9658f59c0 100644 --- a/ted_sws/notice_fetcher/services/notice_fetcher.py +++ b/ted_sws/notice_fetcher/services/notice_fetcher.py @@ -4,7 +4,7 @@ from ted_sws.core.model.manifestation import XMLManifestation from ted_sws.core.model.metadata import TEDMetadata -from ted_sws.core.model.notice import Notice +from ted_sws.core.model.notice import Notice, NoticeStatus from ted_sws.data_manager.adapters.repository_abc import NoticeRepositoryABC from ted_sws.notice_fetcher.adapters.ted_api_abc import TedAPIAdapterABC @@ -77,18 +77,26 @@ def _create_notice(self, notice_data: dict) -> Notice: notice.set_original_metadata(original_metadata) return notice - def _store_to_notice_repository(self, documents: List[dict]) -> List[str]: + def _store_document_to_notice_repository(self, document: dict) -> str: + """ + This method stores a document in NoticeRepository and returns the notice_id. + :param document: + :return: + """ + notice_id = document["ND"] + already_fetched_notice = self.notice_repository.get(reference=notice_id) + if already_fetched_notice: + already_fetched_notice.update_status_to(NoticeStatus.RAW) + self.notice_repository.add(notice=self._create_notice(notice_data=document)) + return notice_id + + def _store_documents_to_notice_repository(self, documents: List[dict]) -> List[str]: """ This method stores documents in NoticeRepository and returns the list of stored notice_id. :param documents: :return: """ - notice_ids = set() - for document in documents: - notice_ids.add(document["ND"]) - self.notice_repository.add(notice=self._create_notice(notice_data=document)) - - return list(notice_ids) + return list({self._store_document_to_notice_repository(document=document) for document in documents}) def fetch_notice_by_id(self, document_id): """ @@ -97,7 +105,7 @@ def fetch_notice_by_id(self, document_id): :return: """ document_result = self.ted_api_adapter.get_by_id(document_id=document_id) - self.notice_repository.add(notice=self._create_notice(notice_data=document_result)) + self._store_document_to_notice_repository(document=document_result) def fetch_notices_by_query(self, query: dict) -> List[str]: """ @@ -106,7 +114,7 @@ def fetch_notices_by_query(self, query: dict) -> List[str]: :return: """ documents = self.ted_api_adapter.get_by_query(query=query) - return self._store_to_notice_repository(documents=documents) + return self._store_documents_to_notice_repository(documents=documents) def fetch_notices_by_date_range(self, start_date: date, end_date: date) -> List[str]: """ @@ -116,7 +124,7 @@ def fetch_notices_by_date_range(self, start_date: date, end_date: date) -> List[ :return: """ documents = self.ted_api_adapter.get_by_range_date(start_date=start_date, end_date=end_date) - return self._store_to_notice_repository(documents=documents) + return self._store_documents_to_notice_repository(documents=documents) def fetch_notices_by_date_wild_card(self, wildcard_date: str) -> List[str]: """ @@ -125,4 +133,4 @@ def fetch_notices_by_date_wild_card(self, wildcard_date: str) -> List[str]: :return: """ documents = self.ted_api_adapter.get_by_wildcard_date(wildcard_date=wildcard_date) - return self._store_to_notice_repository(documents=documents) + return self._store_documents_to_notice_repository(documents=documents)