Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix when fetch already fetched notices don't remove old lazy_fields #498

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ted_sws/data_manager/adapters/repository_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def remove(self, reference: str):
:return:
"""


class ManifestationRepositoryABC(RepositoryABC):
"""
This repository is intended for storing Manifestation objects.
Expand Down Expand Up @@ -92,6 +93,7 @@ def remove(self, reference: str):
:return:
"""


class NoticeRepositoryABC(RepositoryABC):
"""
This repository is intended for storing Notice objects.
Expand Down
32 changes: 20 additions & 12 deletions ted_sws/notice_fetcher/services/notice_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
"""
Expand All @@ -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]:
"""
Expand All @@ -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]:
"""
Expand All @@ -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]:
"""
Expand All @@ -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)
Loading