Skip to content

Commit 7ff82fb

Browse files
Update notice_publisher.py
1 parent 9f72ec2 commit 7ff82fb

1 file changed

Lines changed: 43 additions & 75 deletions

File tree

Lines changed: 43 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,48 @@
11
import base64
2+
import pathlib
23
import tempfile
3-
from pathlib import Path
4-
from typing import Union
54

65
from ted_sws import config
7-
from ted_sws.core.model.notice import Notice, NoticeStatus, UnsupportedStatusTransition
6+
from ted_sws.core.model.notice import Notice, NoticeStatus
87
from ted_sws.data_manager.adapters.notice_repository import NoticeRepositoryABC
9-
from ted_sws.notice_packager.adapters.archiver import ARCHIVE_DEFAULT_FORMAT
10-
from ted_sws.notice_publisher.adapters.notice_publisher import NoticePublisherFactory
11-
from ted_sws.notice_publisher.adapters.notice_publisher_abc import NoticePublisherABC
12-
13-
14-
class NoticePublishBuilder:
15-
def __init__(self, notice: Union[Notice, str], notice_repository: NoticeRepositoryABC,
16-
notice_publisher: NoticePublisherABC, remote_path: str = None):
17-
self.notice_repository = notice_repository
18-
if isinstance(notice, str):
19-
notice_id = notice
20-
notice: Notice = self.notice_repository.get(reference=notice_id)
21-
if notice is None:
22-
raise Exception(f"Notice {notice_id} could not be found.")
23-
24-
self.notice = notice
25-
self.check_publish_eligibility()
26-
27-
self.notice_publisher = notice_publisher
28-
self.remote_path = remote_path
29-
30-
def check_publish_eligibility(self):
31-
if self.notice.status != NoticeStatus.ELIGIBLE_FOR_PUBLISHING:
32-
raise UnsupportedStatusTransition(
33-
f"Notice {self.notice.ted_id} is not Eligible for Publishing. Status: {self.notice.status}")
34-
35-
def package_content(self) -> bytes:
36-
mets_manifestation = self.notice.mets_manifestation
37-
if not mets_manifestation or not mets_manifestation.object_data:
38-
raise ValueError("Notice does not have a METS manifestation to be published.")
39-
40-
package_content = base64.b64decode(bytes(mets_manifestation.object_data, encoding='utf-8'), validate=True)
41-
return package_content
42-
43-
def build_remote_path(self) -> str:
44-
remote_path = self.remote_path
45-
if remote_path is None:
46-
remote_path = f"{config.SFTP_PATH}/{self.notice.ted_id}"
47-
if ARCHIVE_DEFAULT_FORMAT == "zip":
48-
remote_path += '.zip'
49-
return remote_path
50-
51-
def publish(self):
52-
source_file = tempfile.NamedTemporaryFile()
53-
source_file.write(self.package_content())
54-
55-
try:
56-
if self.notice_publisher.publish(source_path=Path(source_file.name), remote_path=self.build_remote_path()):
57-
self.notice.update_status_to(NoticeStatus.PUBLISHED)
58-
self.notice_repository.update(self.notice)
59-
except Exception as e:
60-
raise Exception(f"Notice {self.notice.ted_id} could not be published: " + str(e))
61-
62-
return self.notice.status == NoticeStatus.PUBLISHED
63-
64-
65-
def publish_notice(notice: Union[Notice, str], notice_publisher: NoticePublisherABC,
66-
notice_repository: NoticeRepositoryABC, remote_path=None) -> bool:
67-
publish_builder = NoticePublishBuilder(notice, notice_repository, notice_publisher, remote_path)
68-
return publish_builder.publish()
69-
70-
71-
def publish_single_notice(notice: Union[Notice, str], notice_repository: NoticeRepositoryABC, hostname, username,
72-
password, port=None, remote_path=None) -> bool:
73-
notice_publisher: NoticePublisherABC = NoticePublisherFactory.get_publisher(hostname=hostname, username=username,
74-
password=password, port=port,
75-
remote_path=remote_path)
76-
notice_publisher.connect()
77-
notice_published: bool = publish_notice(notice, notice_publisher, notice_repository, remote_path)
78-
notice_publisher.disconnect()
79-
80-
return notice_published
8+
from ted_sws.notice_publisher.adapters.sftp_notice_publisher import SFTPPublisher
9+
from ted_sws.notice_publisher.adapters.sftp_publisher_abc import SFTPPublisherABC
10+
11+
12+
def publish_notice(notice: Notice, publisher: SFTPPublisherABC = SFTPPublisher(),
13+
remote_folder_path=config.SFTP_PATH) -> bool:
14+
"""
15+
This function publishes the METS manifestation for a Notice in Cellar.
16+
"""
17+
18+
mets_manifestation = notice.mets_manifestation
19+
if not mets_manifestation or not mets_manifestation.object_data:
20+
raise ValueError("Notice does not have a METS manifestation to be published.")
21+
22+
package_content = base64.b64decode(bytes(mets_manifestation.object_data, encoding='utf-8'), validate=True)
23+
remote_notice_path = f"{remote_folder_path}/{notice.ted_id}.zip"
24+
source_file = tempfile.NamedTemporaryFile()
25+
source_file.write(package_content)
26+
27+
try:
28+
publisher.connect()
29+
if publisher.publish(source_path=pathlib.Path(source_file.name),
30+
remote_path=remote_notice_path):
31+
notice.update_status_to(NoticeStatus.PUBLISHED)
32+
publisher.disconnect()
33+
except Exception as e:
34+
raise Exception(f"Notice {notice.ted_id} could not be published: " + str(e))
35+
36+
return notice.status == NoticeStatus.PUBLISHED
37+
38+
39+
def publish_notice_by_id(notice_id: str, notice_repository: NoticeRepositoryABC,
40+
publisher: SFTPPublisherABC, remote_folder_path=None) -> bool:
41+
"""
42+
This function publishes the METS manifestation of a Notice, based on notice_id, in Cellar.
43+
"""
44+
notice = notice_repository.get(reference=notice_id)
45+
result = publish_notice(notice=notice, publisher=publisher, remote_folder_path=remote_folder_path)
46+
if result:
47+
notice_repository.update(notice=notice)
48+
return result

0 commit comments

Comments
 (0)