Skip to content

Commit aee4255

Browse files
committed
configured settings for Image bucket
1 parent 39ab0ad commit aee4255

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

csm_web/csm_web/settings.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
"""
1212

1313
import os
14+
from datetime import datetime
1415

1516
import sentry_sdk
1617
from factory.django import DjangoModelFactory
1718
from rest_framework.serializers import ModelSerializer, Serializer
1819
from sentry_sdk.integrations.django import DjangoIntegration
20+
from storages.backends.s3boto3 import S3Boto3Storage
1921

2022
# Analogous to RAILS_ENV, is one of {prod, staging, dev}. Defaults to dev. This default can
2123
# be dangerous, but is worth it to avoid the hassle for developers setting the local ENV var
@@ -67,6 +69,7 @@
6769
"frontend",
6870
"django_extensions",
6971
"django.contrib.postgres",
72+
"storages",
7073
]
7174

7275
SHELL_PLUS_SUBCLASSES_IMPORT = [ModelSerializer, Serializer, DjangoModelFactory]
@@ -174,11 +177,33 @@
174177
AWS_S3_VERIFY = True
175178
AWS_QUERYSTRING_AUTH = False # public bucket
176179
DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
180+
PROFILE_IMAGE_STORAGE = "csm_web.settings.ProfileImageStorage"
177181

178182
# Static files (CSS, JavaScript, Images)
179183
# https://docs.djangoproject.com/en/2.1/howto/static-files/
180184

181185
STATIC_URL = "/static/"
186+
# Do I need this?
187+
# MEDIA_URL = "/media/"
188+
189+
190+
# xTODO: make sure this actually works
191+
class ProfileImageStorage(S3Boto3Storage):
192+
bucket_name = "csm-web-profile-pictures"
193+
file_overwrite = False
194+
195+
def get_accessed_time(self, name):
196+
# Implement logic to get the last accessed time
197+
return datetime.now()
198+
199+
def get_created_time(self, name):
200+
# Implement logic to get the creation time
201+
return datetime.now()
202+
203+
def path(self, name):
204+
# S3 does not support file paths
205+
raise NotImplementedError("This backend does not support absolute paths.")
206+
182207

183208
if DJANGO_ENV in (PRODUCTION, STAGING):
184209
# Enables compression and caching

csm_web/scheduler/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from django.utils import functional, timezone
1212
from rest_framework.serializers import ValidationError
1313

14+
from csm_web.settings import ProfileImageStorage
15+
1416
logger = logging.getLogger(__name__)
1517

1618
logger.info = logger.warning
@@ -53,9 +55,8 @@ class User(AbstractUser):
5355

5456
pronouns = models.CharField(max_length=20, default="", blank=True)
5557
pronunciation = models.CharField(max_length=50, default="", blank=True)
56-
profile_image = models.ImageField(
57-
upload_to="csm-profile-images/", null=True, blank=True
58-
)
58+
# the upload_to field specifies subdivisions within S3 bucket
59+
profile_image = models.ImageField(storage=ProfileImageStorage())
5960
bio = models.CharField(max_length=500, default="", blank=True)
6061

6162
def can_enroll_in_course(self, course, bypass_enrollment_time=False):

0 commit comments

Comments
 (0)