From d6ff3406ef2eedfeaa18d4a919bf17869270a92e Mon Sep 17 00:00:00 2001 From: Patrick Smith Date: Tue, 18 Dec 2018 11:00:59 -0500 Subject: [PATCH] Initial attempt at modularizing CSP integration --- atst/app.py | 14 ++------------ atst/domain/csp/__init__.py | 11 +++++++++++ atst/domain/csp/files.py | 30 ++++++++++++++++++++++++++++++ atst/models/attachment.py | 2 +- atst/routes/requests/approval.py | 2 +- 5 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 atst/domain/csp/__init__.py create mode 100644 atst/domain/csp/files.py diff --git a/atst/app.py b/atst/app.py index 44c904ea..444f7032 100644 --- a/atst/app.py +++ b/atst/app.py @@ -21,9 +21,9 @@ from atst.routes.errors import make_error_pages from atst.domain.authnid.crl import CRLCache from atst.domain.auth import apply_authentication from atst.domain.authz import Authorization +from atst.domain.csp import make_csp_provider from atst.models.permissions import Permissions from atst.eda_client import MockEDAClient -from atst.uploader import Uploader from atst.utils import mailer from atst.utils.form_cache import FormCache from atst.queue import queue @@ -52,7 +52,7 @@ def make_app(config): make_crl_validator(app) register_filters(app) make_eda_client(app) - make_upload_storage(app) + make_csp_provider(app) make_mailer(app) queue.init_app(app) @@ -191,16 +191,6 @@ def make_eda_client(app): app.eda_client = MockEDAClient() -def make_upload_storage(app): - uploader = Uploader( - provider=app.config.get("STORAGE_PROVIDER"), - container=app.config.get("STORAGE_CONTAINER"), - key=app.config.get("STORAGE_KEY"), - secret=app.config.get("STORAGE_SECRET"), - ) - app.uploader = uploader - - def make_mailer(app): if app.config["DEBUG"]: mailer_connection = mailer.RedisConnection(app.redis) diff --git a/atst/domain/csp/__init__.py b/atst/domain/csp/__init__.py new file mode 100644 index 00000000..1e296d1a --- /dev/null +++ b/atst/domain/csp/__init__.py @@ -0,0 +1,11 @@ +from .files import RackspaceFiles + + +class MockCSP: + + def __init__(self, file_provider): + self.files = file_provider + + +def make_csp_provider(app): + app.csp = MockCSP(RackspaceFiles(app)) diff --git a/atst/domain/csp/files.py b/atst/domain/csp/files.py new file mode 100644 index 00000000..9f0c06d6 --- /dev/null +++ b/atst/domain/csp/files.py @@ -0,0 +1,30 @@ +from atst.uploader import Uploader + + +class Files: + def upload(self, fyle): # pragma: no cover + """Store the file object `fyle` in the CSP. This method returns the + object name that can be used to later look up the file.""" + raise NotImplementedError() + + def download(self, object_name): # pragma: no cover + """Retrieve the stored file represented by `object_name`. Returns a + file object. + """ + raise NotImplementedError() + + +class RackspaceFiles(Files): + def __init__(self, app): + self.uploader = Uploader( + provider=app.config.get("STORAGE_PROVIDER"), + container=app.config.get("STORAGE_CONTAINER"), + key=app.config.get("STORAGE_KEY"), + secret=app.config.get("STORAGE_SECRET"), + ) + + def upload(self, fyle): + return self.uploader.upload(fyle) + + def download(self, object_name): + return self.uploader.download_stream(object_name) diff --git a/atst/models/attachment.py b/atst/models/attachment.py index 30dee524..2dee056f 100644 --- a/atst/models/attachment.py +++ b/atst/models/attachment.py @@ -25,7 +25,7 @@ class Attachment(Base, mixins.TimestampsMixin): @classmethod def attach(cls, fyle, resource=None, resource_id=None): try: - object_name = app.uploader.upload(fyle) + object_name = app.csp.files.upload(fyle) except UploadError as e: raise AttachmentError("Could not add attachment. " + str(e)) diff --git a/atst/routes/requests/approval.py b/atst/routes/requests/approval.py index 338fa1c5..9ec31bc9 100644 --- a/atst/routes/requests/approval.py +++ b/atst/routes/requests/approval.py @@ -71,7 +71,7 @@ def task_order_pdf_download(request_id): request = Requests.get(g.current_user, request_id) if request.legacy_task_order and request.legacy_task_order.pdf: pdf = request.legacy_task_order.pdf - generator = app.uploader.download_stream(pdf.object_name) + generator = app.csp.files.download(pdf.object_name) return Response( generator, headers={