Initial attempt at modularizing CSP integration

This commit is contained in:
Patrick Smith 2018-12-18 11:00:59 -05:00
parent 442e136a4b
commit d6ff3406ef
5 changed files with 45 additions and 14 deletions

View File

@ -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)

View File

@ -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))

30
atst/domain/csp/files.py Normal file
View File

@ -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)

View File

@ -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))

View File

@ -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={