Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
symroe committed Nov 25, 2024
1 parent 83843f5 commit b78231e
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 102 deletions.
178 changes: 87 additions & 91 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion electionleaflets/apps/leaflets/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from unittest.mock import patch

import boto3
import pytest
from django.core.files.storage import default_storage


from moto import mock_aws

@pytest.fixture
def mock_get_ballot_data_from_ynr():
Expand All @@ -12,3 +14,18 @@ def _mock_ynr_value(return_value):
return_value=return_value
)
return _mock_ynr_value



@pytest.fixture
def s3_client():
"""Fixture to provide a mocked S3 client."""
# Initialize the mocked S3 client
with mock_aws():
client = boto3.client("s3")
yield client

@pytest.fixture
def s3_bucket(s3_client, settings):
bucket = s3_client.create_bucket(Bucket=settings.AWS_STORAGE_BUCKET_NAME)
return bucket
14 changes: 14 additions & 0 deletions electionleaflets/apps/leaflets/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,17 @@ def test_image_moved_from_temp_upload(db):

path = Path(leaflet_image.image.storage.base_location) / f"leaflets/{leaflet.pk}/test-leaflet.jpeg"
assert str(path) == leaflet_image.image.path

def test_image_moved_from_temp_upload_s3_backend(db, settings, s3_bucket):
settings.DEFAULT_FILE_STORAGE = "electionleaflets.storages.TempUploadS3MediaStorage"
leaflet = Leaflet()
leaflet.save()
leaflet.refresh_from_db()
leaflet_image = LeafletImage(leaflet=leaflet)
leaflet_image.set_image_from_temp_file("test_images/test_leaflet.jpeg")
leaflet_image.save()
leaflet_image.refresh_from_db()

path = Path(
leaflet_image.image.storage.base_location) / f"leaflets/{leaflet.pk}/test-leaflet.jpeg"
assert str(path) == leaflet_image.image.path
9 changes: 1 addition & 8 deletions electionleaflets/apps/leaflets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,7 @@ def get(self, *args, **kwargs):
return super(LeafletUploadWizzard, self).get(*args, **kwargs)


def copy_file(self, bucket, file_path, new_file_name):
copy_source = {
"Bucket": bucket.name,
"Key": file_path,
}
moved_file = bucket.Object(new_file_name)
moved_file.copy(copy_source)
return moved_file


@transaction.atomic
def done(self, form_list, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion electionleaflets/settings/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .base import * # noqa: F403
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"

DEFAULT_FILE_STORAGE = "django.core.files.storage.FileSystemStorage"
DEFAULT_FILE_STORAGE = "electionleaflets.storages.TempUploadLocalMediaStorage"
STATICFILES_STORAGE = "electionleaflets.storages.StaticStorage"

MEDIA_ROOT = root("test_media",) # noqa: F405
Expand Down
13 changes: 12 additions & 1 deletion electionleaflets/storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,20 @@ class TempUploadBaseMixin(abc.ABC):
def save_from_temp_upload(self, source_path, target_file_path):
raise NotImplementedError



class TempUploadS3MediaStorage(TempUploadBaseMixin, S3Boto3Storage):
def save_from_temp_upload(self, source_path, target_file_path):
...
self.copy_file(source_path, target_file_path)

def copy_file(self, source_path, target_file_path):
copy_source = {
"Bucket": self.bucket.name,
"Key": source_path,
}
moved_file = self.bucket.Object(target_file_path)
moved_file.copy(copy_source)
return moved_file

class TempUploadLocalMediaStorage(TempUploadBaseMixin, FileSystemStorage):
def save_from_temp_upload(self, source_path, target_file_path: Path):
Expand Down

0 comments on commit b78231e

Please sign in to comment.